Skip to content

dotnet-script fails with StateSmith because it uses Microsoft.CodeAnalysis.CSharp 4.5.0.0 or higher #703

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

Closed
adamfk opened this issue Mar 9, 2023 · 3 comments

Comments

@adamfk
Copy link

adamfk commented Mar 9, 2023

Hello,

I ran into an interesting problem with dotnet-script. It appears that it cannot use the 0.8.2 version of StateSmith because StateSmith uses a newer version of 'Microsoft.CodeAnalysis.CSharp`.

dbug: Dotnet.Script.Core.ScriptCompiler[0]
      Suppressed diagnostic CS1705: error CS1705: Assembly 'StateSmith' with identity 'StateSmith, Version=0.8.2.0, Culture=neutral, PublicKeyToken=null' uses 'Microsoft.CodeAnalysis.CSharp, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' which has a higher version than referenced assembly 'Microsoft.CodeAnalysis.CSharp' with identity 'Microsoft.CodeAnalysis.CSharp, Version=4.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

I've created a minimal reproduction repo here: https://github.com/adamfk/dotnet-script-issue1

It has a proj directory for a regular C# project (that works), and a script directory for the dotnet-script equivalent which fails.

The script file test.csx is tiny:

#!/usr/bin/env dotnet-script
#r "nuget: StateSmith, 0.8.2-alpha"
using StateSmith.Output.Gil;

WriteLine("Started running...");
GilHelper.Compile("public class Blah{}", out var a, out var b);
WriteLine("Finished running.");

When I run it with --debug --no-cache, here's the output I get:

click to expand
PS C:\Users\akruck\Downloads\dotnet-script-issue1> cd script   
PS C:\Users\akruck\Downloads\dotnet-script-issue1\script> dotnet-script test.csx
Object reference not set to an instance of an object.
PS C:\Users\akruck\Downloads\dotnet-script-issue1\script> dotnet-script test.csx --debug --no-cache
dbug: Dotnet.Script.Core.Commands.ExecuteScriptCommand[0]
      The script C:\Users\akruck\Downloads\dotnet-script-issue1\script\test.csx was executed with the '--no-cache' flag. Skipping cache.
info: Dotnet.Script.Core.ScriptCompiler[0]
      Current runtime is 'win'.
dbug: Dotnet.Script.DependencyModel.ProjectSystem.ScriptProjectProvider[0]
      Creating project file for C:\Users\akruck\Downloads\dotnet-script-issue1\script\test.csx
dbug: Dotnet.Script.DependencyModel.ProjectSystem.ScriptParser[0]
      Parsing C:\Users\akruck\Downloads\dotnet-script-issue1\script\test.csx
dbug: Dotnet.Script.DependencyModel.ProjectSystem.ScriptProjectProvider[0]
      Project file saved to C:\Users\akruck\AppData\Local\Temp\dotnet-script\C\Users\akruck\Downloads\dotnet-script-issue1\script\net6.0\script.csproj
dbug: Dotnet.Script.DependencyModel.ProjectSystem.ScriptProjectProvider[0]
      <?xml version="1.0" encoding="utf-8"?>
      <Project Sdk="Microsoft.NET.Sdk">
        <PropertyGroup>
          <OutputType>Exe</OutputType>
          <TargetFramework>net6.0</TargetFramework>
          <LangVersion>latest</LangVersion>
        </PropertyGroup>
        <ItemGroup>
          <PackageReference Include="StateSmith" Version="0.8.2-alpha" />
        </ItemGroup>
        <Target Name="RecordReferencePaths" AfterTargets="AfterResolveReferences">
          <WriteLinesToFile File="$(OutputPath)/ReferencePaths.txt" Lines="@(ReferencePath)" />
        </Target>
      </Project>
dbug: Dotnet.Script.DependencyModel.Context.DotnetRestorer[0]
dbug: Dotnet.Script.DependencyModel.Context.ProfiledRestorer[0]
      Restoring C:\Users\akruck\AppData\Local\Temp\dotnet-script\C\Users\akruck\Downloads\dotnet-script-issue1\script\net6.0\script.csproj took 892ms
dbug: Dotnet.Script.Core.ScriptCompiler[0]
      Configuration/Optimization mode: Debug
dbug: Dotnet.Script.Core.ScriptCompiler[0]
      Suppressed diagnostic CS1705: error CS1705: Assembly 'StateSmith' with identity 'StateSmith, Version=0.8.2.0, Culture=neutral, PublicKeyToken=null' uses 'Microsoft.CodeAnalysis.CSharp, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' which has a higher version than referenced assembly 'Microsoft.CodeAnalysis.CSharp' with identity 'Microsoft.CodeAnalysis.CSharp, Version=4.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
System.NullReferenceException: Object reference not set to an instance of an object.
   at Dotnet.Script.Core.ScriptPublisher.CreateScriptAssembly[TReturn,THost](ScriptContext context, String outputDirectory, String assemblyFileName) in C:\Users\runneradmin\AppData\Local\Temp\tmpFE4C\Dotnet.Script.Core\ScriptPublisher.cs:line 106
   at Dotnet.Script.Core.ScriptPublisher.CreateAssembly[TReturn,THost](ScriptContext context, LogFactory logFactory, String assemblyFileName) in C:\Users\runneradmin\AppData\Local\Temp\tmpFE4C\Dotnet.Script.Core\ScriptPublisher.cs:line 45
   at Dotnet.Script.Core.Commands.PublishCommand.Execute(PublishCommandOptions options) in C:\Users\runneradmin\AppData\Local\Temp\tmpFE4C\Dotnet.Script.Core\Commands\PublishCommand.cs:line 44
   at Dotnet.Script.Core.Commands.ExecuteScriptCommand.GetLibrary(ExecuteScriptCommandOptions executeOptions) in C:\Users\runneradmin\AppData\Local\Temp\tmpFE4C\Dotnet.Script.Core\Commands\ExecuteScriptCommand.cs:line 73
   at Dotnet.Script.Core.Commands.ExecuteScriptCommand.Run[TReturn,THost](ExecuteScriptCommandOptions options) in C:\Users\runneradmin\AppData\Local\Temp\tmpFE4C\Dotnet.Script.Core\Commands\ExecuteScriptCommand.cs:line 34
   at Dotnet.Script.Program.<>c__DisplayClass4_0.<<Wain>b__9>d.MoveNext() in C:\Users\runneradmin\AppData\Local\Temp\tmpFE4C\Dotnet.Script\Program.cs:line 261
--- End of stack trace from previous location ---
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.ExecuteAsync(String[] args, CancellationToken cancellationToken)
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Dotnet.Script.Program.Wain(String[] args) in C:\Users\runneradmin\AppData\Local\Temp\tmpFE4C\Dotnet.Script\Program.cs:line 270
   at Dotnet.Script.Program.Main(String[] args) in C:\Users\runneradmin\AppData\Local\Temp\tmpFE4C\Dotnet.Script\Program.cs:line 28
PS C:\Users\akruck\Downloads\dotnet-script-issue1\script>
PS C:\Users\akruck\Downloads\dotnet-script-issue1\script> dotnet-script --version
1.4.0
PS C:\Users\akruck\Downloads\dotnet-script-issue1\script> 

I'm hopeful that I can simply downgrade the StateSmith Roslyn dependencies to 4.4.0. Any other ideas?

Sidebar: StateSmith generates state machine code written in C#, then I use Roslyn to transpile that C# to C99, and also a more efficient C# implementation. I'll be transpiling to javascript/typescript in a few weeks. I'm hoping to have StateSmith create state machines for most programming languages :)

Thanks for the awesome project!

@seesharper
Copy link
Collaborator

Try this dotnet script main.csx --isolated-load-context

It ensures that the script does not share assemblies with the host (dotnet-script) 👍

@adamfk
Copy link
Author

adamfk commented Mar 9, 2023

--isolated-load-context worked great! Any chance that might become a default in the future?

@seesharper
Copy link
Collaborator

We considered this to be the default, but there are some rare cases where it breaks down. For instance, Assembly.LoadFrom does not work since we are unable to intercept it and add it to our custom AssemblyLoadContext.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants