Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot create MetadataReference from WebCIL #75868

Open
wherewhere opened this issue Nov 12, 2024 · 2 comments
Open

Cannot create MetadataReference from WebCIL #75868

wherewhere opened this issue Nov 12, 2024 · 2 comments
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@wherewhere
Copy link

wherewhere commented Nov 12, 2024

Version Used: Roslyn 4.12.0-3.final; .NET 8.0; Visual Studio 2022

Steps to Reproduce:

  1. Create a MetadataReference from WebCIL file
    await using FileStream stream = File.OpenRead("System.Private.CoreLib.wasm");
    references = MetadataReference.CreateFromStream(stream);
  2. Use this reference to create a Compilation
    SyntaxTree syntaxTree =
        SyntaxFactory.ParseSyntaxTree(
            "int a = 2;",
            new CSharpParseOptions(
                LanguageVersion.Preview,
                DocumentationMode.Parse,
                SourceCodeKind.Regular),
            null!);
    Compilation compilation =
        CSharpCompilation.Create(
            "SharpScript",
            [syntaxTree],
            [references],
            new CSharpCompilationOptions(
                OutputKind.ConsoleApplication,
                allowUnsafe: true,
                concurrentBuild: false));
  3. Emit it
    await using MemoryStream assemblyStream = new();
    EmitResult emitResult = compilation.Emit(assemblyStream);
    if (emitResult.Success)
    {
        AssemblyLoadContext context = new("ExecutorContext", isCollectible: true);
        try
        {
            assemblyStream.Seek(0, SeekOrigin.Begin);
            Assembly assembly = context.LoadFromStream(assemblyStream);
            if (assembly.EntryPoint is MethodInfo main)
            {
                string[][]? _args = main.GetParameters().Length > 0 ? [Array.Empty<string>()] : null;
                object? @return = main.Invoke(null, _args);
                Console.WriteLine(@return);
            }
        }
        finally
        {
            context.Unload();
        }
    }
    else
    {
        foreach (Diagnostic diagnostic in emitResult.Diagnostics)
        {
            Console.WriteLine(diagnostic);
        }
    }

Expected Behavior:

Compiled succeed.

Actual Behavior:

Compiled failed.

warning CS8021: 找不到 RuntimeMetadataVersion 的值。找不到包含 System.Object 的程序集,或未通过选项为 RuntimeMetadataVersion 指定值。
error CS0009: 无法打开元数据文件“<内存中的程序集>”-- PE 映像不包含任何托管元数据。
(1,1): error CS0518: 预定义类型“System.Object”未定义或导入
error CS0518: 预定义类型“System.Void”未定义或导入
error CS0518: 预定义类型“System.String”未定义或导入
(1,1): error CS0518: 预定义类型“System.Int32”未定义或导入
(1,9): error CS0518: 预定义类型“System.Int32”未定义或导入
(1,1): error CS1729: “object”不包含采用 0 个参数的构造函数
error CS0518: 预定义类型“System.Attribute”未定义或导入
error CS0518: 预定义类型“System.Attribute”未定义或导入
error CS0518: 预定义类型“System.Int32”未定义或导入
error CS0656: 缺少编译器要求的成员“System.AttributeUsageAttribute..ctor”
error CS0656: 缺少编译器要求的成员“System.AttributeUsageAttribute.AllowMultiple”
error CS0656: 缺少编译器要求的成员“System.AttributeUsageAttribute.Inherited”

Similar issue: #74555

[jcouv update:] FWIW, here are the translated diagnostics:

warning CS8021: Value for RuntimeMetadataVersion not found. The assembly containing System.Object could not be found, or the value for RuntimeMetadataVersion was not specified via the option.
error CS0009: Unable to open metadata file '<内存中的程序集>'-- PE image does not contain any managed metadata.
(1,1): error CS0518: Predefined type 'System.Object' is not defined or imported
error CS0518: Predefined type "System.Void"' is not defined or imported
error CS0518: Predefined type &System.String"Undefined or imported
(1,1): error CS0518: Predefined type &System.Int32" not defined or imported
(1,9): error CS0518: Predefined type 'System.Int32' is not defined or imported
(1,1): error CS1729: 'object' does not contain constructor with 0 arguments
error CS0518: Predefined type "System.Attribute"' is not defined or imported
error CS0518: Predefined type "System.Attribute"' is not defined or imported
error CS0518: Predefined type "System.Int32" is not defined or imported
error CS0656: Member "System.AttributeUsageAttribute.. is missing compiler requirement ctor”
error CS0656: Member "System.AttributeUsageAttribute.AllowMultiple" is missing compiler requirement
error CS0656: Member "System.AttributeUsageAttribute.Inherited' is missing compiler requirement
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 12, 2024
@jjonescz
Copy link
Member

@wherewhere
Copy link
Author

Thanks, it works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

2 participants