-
Notifications
You must be signed in to change notification settings - Fork 259
Publish to single file gives an empty Assembly.Location which crashes Roslyn Evaluator #343
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
Comments
This error means that you are trying to reference (from the script) an assembly that has not been loaded to your app domain from the file but from memory. I guess this is what runtime does in the "single file" scenario. It's not a problem for the host assembly execution, but it is a problem for Roslyn, which has the limitation that it cannot reference assemblies without a file. If it is you who is calling If it is CS-Script itself when loading domain assemblies, then you should disable referencing domain assemblies and reference them individually by file name. In this case, it will be that single file you built. Though I am not sure how you can find its path if |
I think it's a Roslyn problem, this issue has gone nowhere for a few years: That page links to a workaround here: https://github.com/andersstorhaug/SingleFileScripting I'm new to this so I don't know if that's something I can do myself, or if it could be used in cs-script ? For now I'm going to switch off single file output, and add a hundred dlls to my Wix installer. |
I am not sure it is a real workaround. But... I will play a little with it today to confirm what I just described. |
Looked at the sample more... |
Thanks for looking, would be very nice.... |
I can confirm now that with that work around it is now possible to execute scripts from the host app built as a single self-contaied file. Thank you for sharing the info. It will take a little time to properly integrate it and release the update. The future syntax will look like this: var calc = CSScript.Evaluator.Execute("1 + 2"); or var calc = CSScript.Evaluator
.Execute(@"using System;
public class Script
{
public int Sum(int a, int b)
{
return a+b;
}
}
return new Script();");
int sum = calc.Sum(1, 2); |
- Issue #343: Publish to single file gives an empty Assembly.Location which crashes Roslyn Evaluator
- Issue #343: Publish to single file gives an empty Assembly.Location which crashes Roslyn Evaluator - Added support for single-file published host applications
Done. var calc = CSScript.Evaluator
.Eval(@"using System;
public class Script
{
public int Sum(int a, int b)
{
return a+b;
}
}
return new Script();");
int sum = calc.Sum(1, 2);
Console.WriteLine(sum); The complete sample can be found here. |
Ok, awesome, I have refactored from LoadMethod to now use Eval, and it works great in debug mode. I publish to a single file and now I get:
I'm pretty sure my code is similar to your example, but with lots of "using " statements for other referenced assemblies. |
I did test the code sample (https://github.com/oleg-shilo/cs-script/blob/master/src/CSScriptLib/src/Client.SingleFileBuild/Program.cs) after publishing so there is something new in your case. Can you share the solution you are testing so I can have a look? A sanitized version of it. |
Right, I added a class library with one class that runs a script.
Then I reference the project and call that method from the Client.SingleFileBuild sample.
This is the setup I use, where the script runner is in a dedicated project and I call that from my apps. |
I repeated the test. Seems to work as expected. |
It looks like the Deployment Mode must be set to Self-contained. |
Please have a look at "publish" folder content on the screenshot I provided. Just to ensure we are on the same page I have rebuilt the project with explicit CLI parameters for self-containment:
The outcome is the same. Can you please share the project sample, and the CLI command to build it? So we are working on the same things. |
Sorry for the confusion, I am using your project and yes it does work in self contained mode. The problem arises when deployed as Framework dependent, or self-contained = false. I have always used Framework dependent deployments, due to a smaller file size, but I'm thinking self-contained does have benefits, and the bandwidth is not such a big deal these days. So deploying self-contained as the solution is not a problem. |
OK, but... |
|
Great, it works. In the code I analyze if it is a single-file deployment. The analysis is done like this: public static bool IsSingleFileApplication { get; } = "".GetType().Assembly.Location.IsEmpty();
. . .
catch (Exception ex)
{
#if class_lib
if (Runtime.IsSingleFileApplication)
return null; // a single file compilation (published with PublishSingleFile option)
#endif
throw;
} But if the app compiled as in your case the The updated version with the fallback exception handler looks like this: catch (Exception ex)
{
#if class_lib
if (Runtime.IsSingleFileApplication)
return null; // a single file compilation (published with PublishSingleFile option)
else if (ex.Message.Contains("CodeBase is not supported on assemblies loaded from a single-file bundle")
|| ex.StackTrace.Contains("at System.Reflection.RuntimeAssembly.get_CodeBase()"))
return null;
#endif
throw;
} |
I do not want to do premature release so will release this change as a pre-release. You can probably appreciate now why I always ask for a VS test project 😄 |
- Issue #343: Publish to single file gives an empty Assembly.Location which crashes Roslyn Evaluator
--- ## Changes ### CLI - no changes ### CSScriptLib - Issue #343: Publish to single file gives an empty Assembly.Location which crashes Roslyn Evaluator Added support for single-file publishing with runtime dependency
Done, you can get the latest v4.8.4-pre pre-release from nuget.org.
|
It works! Amazing thank you. |
Hi Oleg I'm also trying to build single-file executable so I used your attached example which works fine but when I moved the code from the class library into Main() and removed the library from the solution the generated exe failed with
Is it supposed to be like this and I should always use the library to publish single-file executable? |
That is a very interesting scenario which, ironically, is already covered in the samples but not well reflected in the documentation. The reason is... I did not know about this behaviour. :) Your post pushed me to re-test both samples:
This is what I have found:
This means that the different deployment strategies lead to different points of failure. And while it sounds very hacky, the way you structure your single-file build is the only way to control this behaviour today. Unfortunately. Thus, I suggest you either keep that library. Or change the value of What is kinda disappointing is that Roslyn does not deliver any proper solution, and the workaround found and discussed in this thread (using Have a look at the sample. It might be exactly wat you need. |
Oleg, Thank you very much for this wonderful project and your support effort. |
I'm running a .NET 7 WPF app with a reference to CSScriptLib nuget.
My project works great and scripts run right in debug mode. When I publish I get the error:
"Current version of Roslyn-based evaluator does not support referencing assemblies which are not loaded from the file location."
I publish to a single file which merges most of the referenced dlls into a single dll. Looks like this code in Evaluator.Roslyn.cs is having a problem with it:
I checked this in my code when publishing both to many files and to a single file and in the latter case Location is empty.
The text was updated successfully, but these errors were encountered: