Skip to content

[6.0.100-alpha.1.21064.42] Replychsharp get error CS0009: Metadata file 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.0-alpha.1.21063.13\System.IO.Compression.Native.dll' could not be opened #47029

Closed

Description

@jiangzeng01

Application Name: replay-csharp
OS: Windows 10 RS5
CPU: X64
.NET Build Number: 6.0.100-alpha.1.21064.42
App Source: replay-csharp.zip
DevDiv bug: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1266457

Repro steps:

  1. Download app source above and 'cd replay-csharp\Replay'
  2. dotnet run
  3. Type “Console.Write("hellow world")” in app window, and click Enter.

Expected Result:
print "hello world"

Actual Result:
error CS0009: Metadata file 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.0-alpha.1.21063.13\System.IO.Compression.Native.dll' could not be opened -- PE image doesn't contain managed metadata.

Findings:
When debugging the source code in Visual Studio 2019, below logs print in Output window.

'Replay.exe' (CoreCLR: clrhost): Loaded 'C:\.NETCoreUpgradeApps\replay-csharp\Replay\bin\Debug\netcoreapp3.1\win-x64\System.Globalization.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Replay.exe' (CoreCLR: clrhost): Loaded 'C:\.NETCoreUpgradeApps\replay-csharp\Replay\bin\Debug\netcoreapp3.1\win-x64\System.AppContext.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Exception thrown: 'Microsoft.CodeAnalysis.Scripting.CompilationErrorException' in Microsoft.CodeAnalysis.Scripting.dll

@dotnet-actwx-bot

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jan 15, 2021
  2. changed the title [-][dotnet-sdk-6.0.100-alpha.1.21064.42] Replychsharp get error CS0009: Metadata file 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.0-alpha.1.21063.13\System.IO.Compression.Native.dll' could not be opened[/-] [+][6.0.100-alpha.1.21064.42] Replychsharp get error CS0009: Metadata file 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.0-alpha.1.21063.13\System.IO.Compression.Native.dll' could not be opened[/+] on Jan 15, 2021
  3. ericstj commented on Jan 15, 2021

    @ericstj
    Member

    This app is doing the following:

            public MetadataReference[] GetDefaultAssemblies()
            {
                var installation = dotNetInstallationLocator.GetReferenceAssemblyPath() ?? throw new InvalidOperationException("Could not find dotnet installation");
    
                // get SDK DLLs
                var implementationPath = Path.Combine(installation.BasePath, "shared", "Microsoft.NETCore.App", installation.Version);
                // get xml documentation for those DLLs
                var latestRef = io
                    .GetDirectories(
                        Path.Combine(installation.BasePath, "packs", "Microsoft.NETCore.App.Ref")
                    )
                    .Last();
                var referencePath = Path.Combine(latestRef, "ref");
    
                return GroupDirectoryContentsIntoAssemblies(
                        io.GetFilesInDirectory(implementationPath, "*.dll", SearchOption.AllDirectories)
                        .Union(io.GetFilesInDirectory(referencePath, "*.xml", SearchOption.AllDirectories))
                    )
                    .Where(assembly => assembly.AssemblyName.StartsWith("System"))
                    .Select(assembly => io.CreateMetadataReferenceWithDocumentation(assembly))
                    .ToArray();
            }
    

    It "noticed" that the dll name changed from clrcompression.dll to System.IO.Compression.Native.dll which was done by @VSadov in 99f3f55 (#45713).

    What this app is doing is incorrect. It's examining the .NETCore installation directory and using implementation assemblies for compilation. It assumes everything that starts with "System.*" will be managed assembly.

    It looks like it already understands where the reference assemblies live. It should just use those for compilation.

    @waf it looks like this is your project. https://github.com/waf/replay-csharp/blob/e433ad0da9c637d44ebe79ef13b25a3a89b27c35/Replay.Services/AssemblyLoading/DotNetAssemblyLocator.cs#L20

    Is there any reason you have to use implementation assemblies here?

  4. added
    tenet-compatibilityIncompatibility with previous versions or .NET Framework
    and removed
    untriagedNew issue has not been triaged by the area owner
    on Jan 15, 2021
  5. added this to the 6.0.0 milestone on Jan 15, 2021
  6. waf commented on Jan 16, 2021

    @waf
    Contributor

    @ericstj Hi, appreciate you taking a look into this. I'm sure the app is at fault here, rather than the runtime. As background, the app is a REPL for C#, and heavily uses the Roslyn APIs.

    It ultimately needs the assemblies to pass into the following two Roslyn APIs:

    1. The Roslyn ScriptOptions - Runtime evaluation with the Roslyn Scripting APIs (e.g. CSharpScript). This drives the core of the REPL evaluation.
    2. The Roslyn ProjectInfo, for use with Roslyn Workspaces. This drives all the syntax highlighting, code completion, method documentation, and miscellaneous other services that require workspaces / projects.

    I initially tried using the reference assemblies, and while that worked for the workspace APIs (item 2, above), it did not work for the scripting APIs (item 1, above). It failed with the error: Predefined type 'System.Object' is not defined or imported even without the System prefix filtering. The only way I was able to get the scripting API to work was by using the implementation assemblies.

    I'm sure there's a better way to do this, but I was mostly figuring this out by trial and error.

  7. jiangzeng01 commented on Jan 18, 2021

    @jiangzeng01
    ContributorAuthor

    @ericstj Can we confirm this issue as app issue but not a product issue?

  8. ericstj commented on Jan 19, 2021

    @ericstj
    Member

    @ericstj Can we confirm this issue as app issue but not a product issue?

    Yes this is an app issue. The app is attempting to load all files in the framework directory and failed because we added a native binary that matched it's pattern. The app should be changed to handle this exception at the very least.

    I initially tried using the reference assemblies, and while that worked for the workspace APIs (item 2, above), it did not work for the scripting APIs (item 1, above). It failed with the error: Predefined type 'System.Object' is not defined or imported even without the System prefix filtering. The only way I was able to get the scripting API to work was by using the implementation assemblies.

    @jaredpar @tmat do you know if there is a way to use Roslyn scripting APIs against reference assemblies? I checked https://github.com/dotnet/roslyn/blob/afd10305a37c0ffb2cfb2c2d8446154c68cfa87a/docs/wiki/Scripting-API-Samples.md and couldn't find any example.

  9. tmat commented on Jan 19, 2021

    @tmat
    Member

    @ericstj No there is not. I'm not sure why the app is loading all framework assemblies instead of using #r directive to reference assemblies they want to reference by name and have Roslyn's assembly loader to resolve the name correctly.

  10. waf commented on Jan 20, 2021

    @waf
    Contributor

    The reason for using ScriptOptions.WithReferences was to ensure that both the CSharpScript instance and the Workspace instance had the exact same references; they need to be identical so services like code completion match what's evaluated in the script. I believe this is a constraint that any REPL (e.g. csi) would need to follow.

    I'll take a look at using Roslyn's assembly loader, thanks for the suggestion! Is the idea that ScriptOptions.WithReferences should only be used for third-party assemblies?

  11. tmat commented on Jan 20, 2021

    @tmat
    Member

    @waf Hosting REPL and providing IDE services for it is non-trivial. We do not have good APIs that you could use out of the box. Our plan for future is to provide LSP server that provides IDE services that you could connect to. But we do not have that yet.

    Relevant sources in Roslyn:
    http://sourceroslyn.io/#Microsoft.CodeAnalysis.Scripting/Hosting/Resolvers/RuntimeMetadataReferenceResolver.cs
    http://sourceroslyn.io/#Microsoft.CodeAnalysis.InteractiveHost/Interactive/Core/InteractiveHost.cs
    https://github.com/dotnet/roslyn/blob/ab5cee40ada204ea6975c43349ffba119905dd09/src/EditorFeatures/Core/Implementation/Interactive/InteractiveWorkspace.cs

  12. waf commented on Jan 20, 2021

    @waf
    Contributor

    Understood, and thanks for the links. This has always been more of research side-project for me. I'll update my app to account for these changes in .NET 6.

  13. jiangzeng01 commented on Jan 20, 2021

    @jiangzeng01
    ContributorAuthor

    @ericstj @waf Thanks for confirmation, as this is an app issue, we will close the ticket here.
    Instead I created a bug to the app repo: waf/replay-csharp#3

  14. ghost locked as resolved and limited conversation to collaborators on Feb 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions