dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.95k stars 4.02k forks source link

Scripting using Basic.Reference.Assemblies does not work #55689

Open MatthieuMEZIL opened 3 years ago

MatthieuMEZIL commented 3 years ago

With the following code

var sessionSetupTask = CSharpScript.Create(
    code: string.Empty,
    options: ScriptOptions.Default.WithImports("System").WithReferences(Net472.mscorlib),
    globalsType: null,
    assemblyLoader: new InteractiveAssemblyLoader())
.RunAsync(null);
            ScriptState<Attribute[]> test2 = sessionSetupTask.Result.ContinueWithAsync<Attribute[]>(@"
[Obsolete()]
class __scope__internal
{
}
System.Attribute.GetCustomAttributes(typeof(__scope__internal))").Result;

With Net472.mscorlib coming from Basic.Reference.Assemblies, we got the following exception wityh Roslyn 3.10 and 3.11: “System.ArgumentException: 'Absolute path expected. Parameter name: path'” at Microsoft.CodeAnalysis.Scripting.Hosting.InteractiveAssemblyLoader.RegisterDependency(AssemblyIdentity dependency, String path) at Microsoft.CodeAnalysis.Scripting.ScriptBuilder.Build[T](Compilation compilation, DiagnosticBag diagnostics, Boolean emitDebugInformation, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Scripting.ScriptBuilder.CreateExecutor[T](ScriptCompiler compiler, Compilation compilation, Boolean emitDebugInformation, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Scripting.Script1.GetExecutor(CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Scripting.Script1.RunFromAsync(ScriptState previousState, Func2 catchException, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Scripting.ScriptState.ContinueWithAsync[TResult](String code, ScriptOptions options, Func2 catchException, CancellationToken cancellationToken)

With Roslyn 3.8 it was working fine.

svick commented 2 years ago

As far as I can tell, this is not a change in Roslyn, but a change in Basic.Reference.Assemblies. For example, Basic.Reference.Assemblies 1.1.1, which targets Roslyn 3.8, returns null for Net472.mscorlib.FilePath, which is acceptable:

https://github.com/dotnet/roslyn/blob/ae8beb0ee14397f6da2d8099a2964c441a86fd1f/src/Scripting/Core/ScriptBuilder.cs#L147-L153

Whereas Basic.Reference.Assemblies 1.2.4 targets Roslyn 3.9 and returns "mscorlib.dll" for Net472.mscorlib.FilePath, which throws the exception:

https://github.com/dotnet/roslyn/blob/ae8beb0ee14397f6da2d8099a2964c441a86fd1f/src/Scripting/Core/Hosting/AssemblyLoader/InteractiveAssemblyLoader.cs#L175-L178

Though I don't know if Roslyn should change here, or if Basic.Reference.Assemblies should.

cc: @jaredpar (author of Basic.Reference.Assemblies)