Skip to content

Commit

Permalink
Solve all issues in cs loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Feb 1, 2023
1 parent 9c063c8 commit 85ecba3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
4 changes: 4 additions & 0 deletions source/loaders/cs_loader/netcore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,28 @@ if(OPTION_BUILD_GUIX)

# Build without internet access
add_custom_target(${target} ALL
COMMAND ${CMAKE_COMMAND} -E $<IF:$<VERSION_LESS:CMAKE_VERSION,3.17>,remove,rm> ${PROJECT_OUTPUT_DIR}/CSLoader.dll
COMMAND ${DOTNET_COMMAND} restore ${DOTNET_CORE_PATH_SOURCE} ${DOTNET_ADDITIONAL_PACKAGES_SOURCE} ${DOTNET_SOURCE} ${CMAKE_CURRENT_SOURCE_DIR}/source/project.csproj
COMMAND ${DOTNET_COMMAND} publish ${DOTNET_CORE_PATH_SOURCE} ${DOTNET_ADDITIONAL_PACKAGES_SOURCE} ${DOTNET_SOURCE} ${CMAKE_CURRENT_SOURCE_DIR}/source/project.csproj -o ${CMAKE_BINARY_DIR}
)
else()
if(DOTNET_VERSION VERSION_EQUAL "2.0" OR DOTNET_VERSION VERSION_GREATER "2.0")
add_custom_target(${target} ALL
COMMAND ${CMAKE_COMMAND} -E $<IF:$<VERSION_LESS:CMAKE_VERSION,3.17>,remove,rm> ${PROJECT_OUTPUT_DIR}/CSLoader.dll
COMMAND ${DOTNET_ENV_VAR} ${DOTNET_COMMAND} restore ${CMAKE_CURRENT_SOURCE_DIR}/source/project.csproj
COMMAND ${DOTNET_ENV_VAR} ${DOTNET_COMMAND} publish ${CMAKE_CURRENT_SOURCE_DIR}/source/project.csproj -o ${PROJECT_OUTPUT_DIR}
)
else()
if(DOTNET_MIGRATE)
add_custom_target(${target} ALL
COMMAND ${CMAKE_COMMAND} -E $<IF:$<VERSION_LESS:CMAKE_VERSION,3.17>,remove,rm> ${PROJECT_OUTPUT_DIR}/CSLoader.dll
COMMAND ${DOTNET_ENV_VAR} ${DOTNET_COMMAND} migrate ${CMAKE_CURRENT_SOURCE_DIR}/source/project.json
COMMAND ${DOTNET_ENV_VAR} ${DOTNET_COMMAND} restore ${CMAKE_CURRENT_SOURCE_DIR}/source/project.json
COMMAND ${DOTNET_ENV_VAR} ${DOTNET_COMMAND} publish ${CMAKE_CURRENT_SOURCE_DIR}/source/project.json -o ${PROJECT_OUTPUT_DIR}
)
else()
add_custom_target(${target} ALL
COMMAND ${CMAKE_COMMAND} -E $<IF:$<VERSION_LESS:CMAKE_VERSION,3.17>,remove,rm> ${PROJECT_OUTPUT_DIR}/CSLoader.dll
COMMAND ${DOTNET_ENV_VAR} ${DOTNET_COMMAND} restore ${CMAKE_CURRENT_SOURCE_DIR}/source/project.json
COMMAND ${DOTNET_ENV_VAR} ${DOTNET_COMMAND} publish ${CMAKE_CURRENT_SOURCE_DIR}/source/project.json -o ${PROJECT_OUTPUT_DIR}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.CodeAnalysis.CSharp;
using System.IO;
using Microsoft.CodeAnalysis.Emit;
using System.Runtime.Loader;
using System.Runtime.InteropServices;
using static CSLoader.MetacallDef;
using System.Collections.Immutable;
Expand Down
32 changes: 16 additions & 16 deletions source/loaders/cs_loader/netcore/source/Providers/LoaderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,24 @@ public bool LoadFromFileFunctions(string[] files)
return LoadFromSourceFunctions(sources.ToArray());
}

private void PrintDiagnostics(ImmutableArray<Diagnostic> diagnostics)
private int PrintDiagnostics(ImmutableArray<Diagnostic> diagnostics)
{
IEnumerable<Diagnostic> failures = diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);
int errorCount = 0;

foreach (Diagnostic diagnostic in failures)
foreach (Diagnostic diagnostic in diagnostics)
{
this.log.Error("CSLoader compilation error: " + diagnostic.GetMessage());
if (diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error)
{
this.log.Error("CSLoader compilation error: " + diagnostic.ToString());
++errorCount;
}
else
{
this.log.Error("CSLoader compilation warning: " + diagnostic.ToString());
}
}

return errorCount;
}

public bool LoadFromSourceFunctions(string[] source)
Expand Down Expand Up @@ -185,21 +193,13 @@ public bool LoadFromSourceFunctions(string[] source)
)
);

ImmutableArray<Diagnostic> compilationErrors = compilation.GetDiagnostics();

if (compilationErrors.Count() > 0)
if (PrintDiagnostics(compilation.GetDiagnostics()) > 0)
{
PrintDiagnostics(compilationErrors);

return false;
}

ImmutableArray<Diagnostic> declarationErrors = compilation.GetDeclarationDiagnostics();

if (compilationErrors.Count() > 0)
if (PrintDiagnostics(compilation.GetDeclarationDiagnostics()) > 0)
{
PrintDiagnostics(declarationErrors);

return false;
}

Expand Down
2 changes: 0 additions & 2 deletions source/tests/metacall_cs_test/source/metacall_cs_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ TEST_F(metacall_cs_test, Concat)
metacall_value_destroy(ret);
}

#if !defined(__ADDRESS_SANITIZER__) /* TODO: C# Loader leaks when fails to load a script */
TEST_F(metacall_cs_test, Fail)
{
/* This is a Python script on purpose, in order to test C# when it fails */
Expand All @@ -99,7 +98,6 @@ TEST_F(metacall_cs_test, Fail)

EXPECT_EQ((int)1, (int)metacall_load_from_memory("cs", buffer, sizeof(buffer), NULL));
}
#endif

TEST_F(metacall_cs_test, FailRelativePath)
{
Expand Down
2 changes: 2 additions & 0 deletions source/tests/sanitizer/lsan.supp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ leak:libruby*
#
leak:libcoreclr*
leak:libicuuc*
# TODO: Implement assembly unloading with loader context
leak:System.Private.CoreLib.dll
#
# Rust
#
Expand Down

0 comments on commit 85ecba3

Please sign in to comment.