oleg-shilo / cs-script

C# scripting platform
http://www.cs-script.net
MIT License
1.57k stars 234 forks source link

CSScriptLib.CompilerException: error CS2021: File name '' is empty #291

Closed lihaochen910 closed 2 years ago

lihaochen910 commented 2 years ago

cs-script 4.4.3 macOS 12.3.1 .net sdk 6.0.201 arm64

if ( !Debugger.IsAttached ) {
    var script = string.Empty;
    if ( Environment.OSVersion.Platform == System.PlatformID.Unix ) {
        script = Path.Combine ( "/Users/Kanbaru/OneDrive/Documents/GameSolution/GameScriptLibrary", "main.csx" );
        CSScript.EvaluatorConfig.Access = EvaluatorAccess.Singleton;
        CSScript.EvaluatorConfig.Engine = EvaluatorEngine.CodeDom;
        CSScript.EvaluatorConfig.ReferenceDomainAssemblies = true;
        CSScript.GlobalSettings.AddSearchDir ( "/Users/Kanbaru/OneDrive/Documents/GameSolution/GameScriptLibrary" );
    }
    else {
        script = Path.Combine ( @"D:\OneDrive\Documents\GameSolution\GameScriptLibrary", "main.csx" );
        CSScript.EvaluatorConfig.Access = EvaluatorAccess.Singleton;
        CSScript.EvaluatorConfig.Engine = EvaluatorEngine.CodeDom;
        CSScript.EvaluatorConfig.ReferenceDomainAssemblies = true;
        CSScript.GlobalSettings.AddSearchDir ( @"D:\OneDrive\Documents\GameSolution\GameScriptLibrary" );
    }

    if ( File.Exists ( script ) ) {
        try {
            IGameComponent scriptEntryComponent =
                CSScript.Evaluator
                        .ReferenceAssembly ( Assembly.GetExecutingAssembly () )
                        .ReferenceAssembly ( Assembly.GetExecutingAssembly ().Location )
                        .ReferenceAssemblyOf ( this )
                        .ReferenceDomainAssemblies ()
                        .LoadCode< IGameComponent > ( File.ReadAllText ( script ), this, _services );
            Components.Add ( scriptEntryComponent );
        }
        catch ( Exception e ) {
            Console.WriteLine ( e );
        }
    }
    else {
        Console.WriteLine ( "main.csx not found." );
    }
}
else {
    Assembly asm = Assembly.UnsafeLoadFrom ( Path.Combine ( Path.GetDirectoryName ( Assembly.GetEntryAssembly ().Location ), "GameScriptLibrary.dll" ) );
    Type type = asm.GetType ( "GameScriptLibrary.CSScriptEntryComponent" );
    var scriptEntryComponent = Activator.CreateInstance ( type, this, _services ) as IGameComponent;
    Components.Add ( scriptEntryComponent );
}

Output:

/Users/Kanbaru/Library/CloudStorage/OneDrive-個人/Documents/GameSolution/build/Debug/FNAHost
Non-standard D32S8 window depth format!
FNA3D Driver: OpenGL
OpenGL Renderer: Apple M1
OpenGL Driver: 2.1 Metal - 76.3
OpenGL Vendor: Apple
ARB_debug_output/KHR_debug not supported!
GREMEDY_string_marker not supported!
MojoShader Profile: glsl120
searchdir: /Users/Kanbaru/OneDrive/Documents/GameSolution/GameScriptLibrary
searchdir: /Users/Kanbaru/Library/CloudStorage/OneDrive-個人/Documents/GameSolution/build/Debug/lib
CSScriptLib.CompilerException: /var/folders/z4/gvq427fs24l6q4dsbs1m9gb00000gn/T/CSSCRIPT/32430.8d48bc7b-8c8f-4e46-aa57-f5f2a6278e2b.tmp(0,0): error CS2021: File name '' is empty, contains invalid characters, has a drive specification without an absolute path, or is too long

   at CSScriptLib.CodeDomEvaluator.CompileAssemblyFromFileBatch_with_Csc(String[] fileNames, String[] ReferencedAssemblies, String outAssembly, Boolean IncludeDebugInformation, CompileInfo info)
   at CSScriptLib.CodeDomEvaluator.Compile(String scriptText, String scriptFile, CompileInfo info)
   at CSScriptLib.EvaluatorBase`1.CompileCode(String scriptText, String scriptFile, CompileInfo info)
   at CSScriptLib.EvaluatorBase`1.CompileCode(String scriptText)
   at CSScriptLib.EvaluatorBase`1.LoadCode[T](String scriptText, Object[] args)
   at Samples.MyGame.InitializeScriptEngine() in /Users/Kanbaru/Library/CloudStorage/OneDrive-個人/Documents/GameSolution/FNAHost/MyGame.cs:line 411
oleg-shilo commented 2 years ago

Can you please collapse your sample into something lighter? Is this the accurate equivalent:

var script = Path.Combine ( "/Users/Kanbaru/OneDrive/Documents/GameSolution/GameScriptLibrary", "main.csx" );
CSScript.EvaluatorConfig.Access = EvaluatorAccess.Singleton;
CSScript.EvaluatorConfig.Engine = EvaluatorEngine.CodeDom;
CSScript.EvaluatorConfig.ReferenceDomainAssemblies = true;
CSScript.GlobalSettings.AddSearchDir ( "/Users/Kanbaru/OneDrive/Documents/GameSolution/GameScriptLibrary" );

if ( File.Exists ( script ) ) 
    try {
        object scriptEntryComponent =
            CSScript.Evaluator
                    .ReferenceAssembly ( Assembly.GetExecutingAssembly () )
                    .ReferenceAssembly ( Assembly.GetExecutingAssembly ().Location )
                    .ReferenceAssemblyOf ( this )
                    .ReferenceDomainAssemblies ()
                    .LoadCode< IGameComponent > ( File.ReadAllText ( script ), this, _services );

    }
    catch ( Exception e ) {
        Console.WriteLine ( e );
    }

If yes I can try to debug it on Linux. Sorry, I do not have Mac handy

The error itself is about an invalid path. Being an empty string is only one possible reason. The error message lists others too.

lihaochen910 commented 2 years ago

I'm sure the load path is correct. Because before I used Mono runtime, CS-Script 3.30 version was working fine. These problems only appeared because I switched to .Net SDK 6, Just now I get the same error when running on win11.

oleg-shilo commented 2 years ago

Not a problem. Will have a look. Is the code (I extracted) accurate?

oleg-shilo commented 2 years ago

BTW this code adds the same assembly 3 times:

.ReferenceAssembly ( Assembly.GetExecutingAssembly () )
.ReferenceAssembly ( Assembly.GetExecutingAssembly ().Location )
.ReferenceAssemblyOf ( this )

And .ReferenceDomainAssemblies () does it all anyway

lihaochen910 commented 2 years ago

Thank you for your reminder :)

lihaochen910 commented 2 years ago

BTW my main.csx file starts like this:

//css_engine dotnet

//css_co /unsafe
//css_co /d:TRACE /d:DEBUG

//css_reference netstandard.dll
//css_reference System.Xml.Linq.dll
//css_reference System.Numerics.dll
//css_reference System.Numerics.Vectors.dll
//css_reference ImGui.NET.dll
//css_reference DigitalRune.ImGui.Editor.dll
//css_include AppBootstrapper
//css_include TestComponent

css_engine: dotnet or csc got the same error when running

oleg-shilo commented 2 years ago

Tested, your scenario works OK on Linux. The sample is attached.

image

Let's now go through your code. I think there is a problem there that is caused by the leftovers from the Mono version.

hosting.zip

lihaochen910 commented 2 years ago

macOS 12.3.1 .net sdk 6.0.201 arm64 I tried the sample you gave

/Users/Kanbaru/Downloads/hosting/bin/Debug/net6.0/hosting
/Users/Kanbaru/Downloads/hosting/bin/Debug/net6.0
Unhandled exception. CSScriptLib.CompilerException: BUILD: error : Unknown compiler error

   at CSScriptLib.CodeDomEvaluator.CompileAssemblyFromFileBatch_with_Csc(String[] fileNames, String[] ReferencedAssemblies, String outAssembly, Boolean IncludeDebugInformation, CompileInfo info)
   at CSScriptLib.CodeDomEvaluator.Compile(String scriptText, String scriptFile, CompileInfo info)
   at CSScriptLib.EvaluatorBase`1.CompileCode(String scriptText, String scriptFile, CompileInfo info)
   at CSScriptLib.EvaluatorBase`1.CompileCode(String scriptText)
   at CSScriptLib.EvaluatorBase`1.LoadCode[T](String scriptText, Object[] args)
   at App.Main() in /Users/Kanbaru/Downloads/hosting/Program.cs:line 37

Process finished with exit code 134.
lihaochen910 commented 2 years ago

Running this sample with .net 6 sdk x86_64 on windows platform works fine.

C:\Users\Kanbaru\Downloads\hosting\bin\Debug\net6.0
YourPrinter is printing...

C:\Users\Kanbaru\Downloads\hosting\bin\Debug\net6.0\hosting.exe (进程 17188)已退出,代码为 0。
按任意键关闭此窗口. . .

Tested, your scenario works OK on Linux. The sample is attached.

image

Let's now go through your code. I think there is a problem there that is caused by the leftovers from the Mono version.

  • //css_engine dotnet does not do anything as it is a directive for CLI but not hosted script engine
  • //css_reference netstandard.dll //css_reference System.Xml.Linq.dll //css_reference System.Numerics.dll //css_reference System.Numerics.Vectors.dll Not needed as all default assemblies will be referenced anyway.

I feel that there is something wrong with your imported scripts.

I suggest, if you still experience the problem, take the sample I gave you modify it a little to start exhibiting the problem and send me the sample back so I can debug it.

hosting.zip

lihaochen910 commented 2 years ago

@oleg-shilo I found a bug about the CSScriptLib::CSharpParser.GetRawStatements method, using my test file main.csx, when parsing the css_include information of main.csx, the source file csparser.cs line 895 calls codeToanalyze.Substring(pos, endPos - pos).Trim() returned an incorrect value:

WorldRenderGameComponent\n\nusing System

This will cause subsequent compilation operations to fail. issues Sample main.csx

oleg-shilo commented 2 years ago

Great. Thank you for such a detailed investigation report. Fixing it was just a matter of a few mins.

Done.

The fix is published as a HotFix release: https://www.nuget.org/packages/CS-Script/4.4.4-pre

lihaochen910 commented 2 years ago

Thank you for your quick fix. Good job!