Skip to content

Usage with Microsoft.CodeAnalysis.CSharp.Scripting produces an exception #74

Description

@DarkDaskin

I'm trying to use this library to compile a C# script against .NET Standard 2.1. Here is the code:

var code = "2+2";
var options = ScriptOptions.Default
    .WithReferences(NetStandard21.References.All)
    .AddImports("System");
var script = CSharpScript.Create(code, options);
var compilation = script.GetCompilation();
var assemblyStream = new MemoryStream();
var emitResult = compilation.Emit(assemblyStream);

However, the last line throws an exception:

System.IndexOutOfRangeException
  HResult=0x80131508
  Message=Index was outside the bounds of the array.
  Source=System.Private.CoreLib
  StackTrace:
   at Internal.Runtime.CompilerHelpers.ThrowHelpers.ThrowIndexOutOfRangeException()
   at Roslyn.Utilities.PathUtilities.CombinePathsUnchecked(String root, String relativePath)
   at Microsoft.CodeAnalysis.Scripting.Hosting.RuntimeMetadataReferenceResolver.ResolveMissingAssembly(MetadataReference definition, AssemblyIdentity referenceIdentity)
   at Microsoft.CodeAnalysis.Scripting.ScriptMetadataResolver.ResolveMissingAssembly(MetadataReference definition, AssemblyIdentity referenceIdentity)
   at Microsoft.CodeAnalysis.CommonReferenceManager`2.TryResolveMissingReference(MetadataReference requestingReference, AssemblyIdentity referenceIdentity, ImmutableDictionary`2& implicitReferenceResolutions, MetadataReferenceResolver resolver, DiagnosticBag resolutionDiagnostics, AssemblyIdentity& resolvedAssemblyIdentity, AssemblyMetadata& resolvedAssemblyMetadata, PortableExecutableReference& resolvedReference)
   at Microsoft.CodeAnalysis.CommonReferenceManager`2.ResolveAndBindMissingAssemblies(ImmutableArray`1 explicitAssemblies, MultiDictionary`2 explicitAssembliesMap, ImmutableArray`1 explicitModules, ImmutableArray`1 explicitReferences, ImmutableArray`1 explicitReferenceMap, MetadataReferenceResolver resolver, MetadataImportOptions importOptions, Boolean supersedeLowerVersions, ArrayBuilder`1 referenceBindings, Dictionary`2 assemblyReferencesBySimpleName, ImmutableArray`1& allAssemblies, ImmutableArray`1& metadataReferences, ImmutableArray`1& resolvedReferences, ImmutableDictionary`2& implicitReferenceResolutions, DiagnosticBag resolutionDiagnostics)
   at Microsoft.CodeAnalysis.CommonReferenceManager`2.Bind(ImmutableArray`1 explicitAssemblies, ImmutableArray`1 explicitModules, ImmutableArray`1 explicitReferences, ImmutableArray`1 explicitReferenceMap, MetadataReferenceResolver resolverOpt, MetadataImportOptions importOptions, Boolean supersedeLowerVersions, Dictionary`2 assemblyReferencesBySimpleName, ImmutableArray`1& allAssemblies, ImmutableArray`1& implicitlyResolvedReferences, ImmutableArray`1& implicitlyResolvedReferenceMap, ImmutableDictionary`2& implicitReferenceResolutions, DiagnosticBag resolutionDiagnostics, Boolean& hasCircularReference, Int32& corLibraryIndex)
   at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.ReferenceManager.CreateAndSetSourceAssemblyFullBind(CSharpCompilation compilation)
   at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.ReferenceManager.CreateSourceAssemblyForCompilation(CSharpCompilation compilation)
   at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetBoundReferenceManager()
   at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.get_SourceAssembly()
   at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.get_Assembly()
   at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetRuntimeMetadataVersion(EmitOptions emitOptions)
   at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetRuntimeMetadataVersion(EmitOptions emitOptions, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.CreateModuleBuilder(EmitOptions emitOptions, IMethodSymbol debugEntryPoint, Stream sourceLinkStream, IEnumerable`1 embeddedTexts, IEnumerable`1 manifestResources, CompilationTestData testData, DiagnosticBag diagnostics, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Compilation.CheckOptionsAndCreateModuleBuilder(DiagnosticBag diagnostics, IEnumerable`1 manifestResources, EmitOptions options, IMethodSymbol debugEntryPoint, Stream sourceLinkStream, IEnumerable`1 embeddedTexts, CompilationTestData testData, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Compilation.Emit(Stream peStream, Stream metadataPEStream, Stream pdbStream, Stream xmlDocumentationStream, Stream win32Resources, IEnumerable`1 manifestResources, EmitOptions options, IMethodSymbol debugEntryPoint, Stream sourceLinkStream, IEnumerable`1 embeddedTexts, RebuildData rebuildData, CompilationTestData testData, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Compilation.Emit(Stream peStream, Stream pdbStream, Stream xmlDocumentationStream, Stream win32Resources, IEnumerable`1 manifestResources, EmitOptions options, IMethodSymbol debugEntryPoint, Stream sourceLinkStream, IEnumerable`1 embeddedTexts, Stream metadataPEStream, RebuildData rebuildData, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Compilation.Emit(Stream peStream, Stream pdbStream, Stream xmlDocumentationStream, Stream win32Resources, IEnumerable`1 manifestResources, EmitOptions options, IMethodSymbol debugEntryPoint, Stream sourceLinkStream, IEnumerable`1 embeddedTexts, Stream metadataPEStream, CancellationToken cancellationToken)
...

Am I using the library the wrong way, or there is a bug?

If I omit the WithReferences line, the script compiles just fine, but against .NET 9, which is not what I want.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions