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

scriptcs crashes when native DLL's are present #913

Closed
sbedford opened this Issue Feb 4, 2015 · 22 comments

Comments

Projects
None yet
4 participants
@sbedford

sbedford commented Feb 4, 2015

I'm getting a BadImageFormatException when trying to load the LibGit2Sharp library in a csx file.

I'm running ScriptCS version 0.13 on Windows 8.1 x64 - see https://gist.github.com/sbedford/472d9fa63a8304ce0b2f for a reproduction.

Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'git2-e0902fb.dll' or one of its dependencies. The module was expected to contain an assembly manifest. ---> System.BadImageFormatException: Could not load file or assembly 'git2-e0902fb.dll' or one of its dependencies. The module was
expected to contain an assembly manifest.
--- End of inner exception stack trace ---
at System.Reflection.AssemblyName.nGetFileInformation(String s)
at System.Reflection.AssemblyName.GetAssemblyName(String assemblyFile)
at ScriptCs.AppDomainAssemblyResolver.AddAssemblyPaths(IEnumerable1 assemblyPaths) at ScriptCs.ScriptServicesBuilderFactory.Create(ScriptCsArgs commandArgs, String[] scriptArgs) at ScriptCs.Command.CommandFactory.<>c__DisplayClass2.<CreateCommand>b__0() at ScriptCs.Command.DeferredCreationCommand1.Execute()
at ScriptCs.Command.CompositeCommand.Execute()
at ScriptCs.Program.Main(String[] args)

@glennblock glennblock self-assigned this Feb 4, 2015

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 4, 2015

Member

I got this....

Member

glennblock commented Feb 4, 2015

I got this....

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 4, 2015

Member

Good news @sbedford. I see exactly where this is happening and it should be an easy fix. Also I am happy that it is NOT a MEF issue :p

Member

glennblock commented Feb 4, 2015

Good news @sbedford. I see exactly where this is happening and it should be an easy fix. Also I am happy that it is NOT a MEF issue :p

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 4, 2015

Member

The issue occurs as the assemblies which will be added to the MEF catalog are loaded. I am adding a check now that will only load it IF it is a managed assembly.

Member

glennblock commented Feb 4, 2015

The issue occurs as the assemblies which will be added to the MEF catalog are loaded. I am adding a check now that will only load it IF it is a managed assembly.

@nulltoken

This comment has been minimized.

Show comment
Hide comment
@nulltoken

nulltoken Feb 4, 2015

Indeed, git2-e0902fb.dll is a native dll.

@glennblock Thanks a lot for taking care about this 😍

nulltoken commented Feb 4, 2015

Indeed, git2-e0902fb.dll is a native dll.

@glennblock Thanks a lot for taking care about this 😍

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 4, 2015

Member

My fix works :-). I'm going to commit to my branch so you can try it out.

Member

glennblock commented Feb 4, 2015

My fix works :-). I'm going to commit to my branch so you can try it out.

glennblock added a commit to glennblock/scriptcs that referenced this issue Feb 4, 2015

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 4, 2015

Member

@sbedford @nulltoken I have a fix here: https://github.com/glennblock/scriptcs/tree/913. I have not sent a PR yet as tests are breaking, but if you grab it and build you should be able to run your sample fine.

Member

glennblock commented Feb 4, 2015

@sbedford @nulltoken I have a fix here: https://github.com/glennblock/scriptcs/tree/913. I have not sent a PR yet as tests are breaking, but if you grab it and build you should be able to run your sample fine.

@sbedford

This comment has been minimized.

Show comment
Hide comment
@sbedford

sbedford Feb 4, 2015

Weird, I'm getting a different error this time? Possibly environmental?

WARN: Some assemblies failed to load. Launch with '-loglevel debug' to see the details
ERROR: error CS0009: Metadata file 'c:\bin\LibGit2Sharp\packages\LibGit2Sharp.0.21.0.176\lib\net40\NativeBinaries\x86\git2-e0902fb.dll' could not be opened -- 'MetadataReaderException: COR20HeaderTooSmall'

//DEBUG dump

ERROR: error CS0009: Metadata file 'c:\bin\LibGit2Sharp\packages\LibGit2Sharp.0.21.0.176\lib\net40\NativeBinaries\x86\git2-e0902fb.dll' could not be opened -- 'MetadataReaderException: COR20HeaderTooSmall'
DEBUG: Roslyn.Compilers.CompilationErrorException: error CS0009: Metadata file 'c:\bin\LibGit2Sharp\packages\LibGit2Sharp.0.21.0.176\lib\net40\NativeBinaries\x86\git2-e0902fb.dll' could not be opened -- 'MetadataReaderException: COR20HeaderTooSmall'
at Roslyn.Scripting.CommonScriptEngine.CompilationError(DiagnosticBag localDiagnostics, DiagnosticBag diagnostics)
at Roslyn.Scripting.CommonScriptEngine.Compile(String code, String path, DiagnosticBag diagnostics, Session session, Type delegateType, Type returnType, CancellationToken cancellationToken, Boolean isInteractive, Boolean isExecute, CommonCompilation& compilation, Delegate& factory)
at Roslyn.Scripting.CommonScriptEngine.CompileSubmission[T](String code, Session session, String path, Boolean isInteractive)
at ScriptCs.Engine.Roslyn.RoslynScriptEngine.Execute(String code, Session session) in c:\development\git\scriptcs\src\ScriptCs.Engine.Roslyn\RoslynScriptEngine.cs:line 119

sbedford commented Feb 4, 2015

Weird, I'm getting a different error this time? Possibly environmental?

WARN: Some assemblies failed to load. Launch with '-loglevel debug' to see the details
ERROR: error CS0009: Metadata file 'c:\bin\LibGit2Sharp\packages\LibGit2Sharp.0.21.0.176\lib\net40\NativeBinaries\x86\git2-e0902fb.dll' could not be opened -- 'MetadataReaderException: COR20HeaderTooSmall'

//DEBUG dump

ERROR: error CS0009: Metadata file 'c:\bin\LibGit2Sharp\packages\LibGit2Sharp.0.21.0.176\lib\net40\NativeBinaries\x86\git2-e0902fb.dll' could not be opened -- 'MetadataReaderException: COR20HeaderTooSmall'
DEBUG: Roslyn.Compilers.CompilationErrorException: error CS0009: Metadata file 'c:\bin\LibGit2Sharp\packages\LibGit2Sharp.0.21.0.176\lib\net40\NativeBinaries\x86\git2-e0902fb.dll' could not be opened -- 'MetadataReaderException: COR20HeaderTooSmall'
at Roslyn.Scripting.CommonScriptEngine.CompilationError(DiagnosticBag localDiagnostics, DiagnosticBag diagnostics)
at Roslyn.Scripting.CommonScriptEngine.Compile(String code, String path, DiagnosticBag diagnostics, Session session, Type delegateType, Type returnType, CancellationToken cancellationToken, Boolean isInteractive, Boolean isExecute, CommonCompilation& compilation, Delegate& factory)
at Roslyn.Scripting.CommonScriptEngine.CompileSubmission[T](String code, Session session, String path, Boolean isInteractive)
at ScriptCs.Engine.Roslyn.RoslynScriptEngine.Execute(String code, Session session) in c:\development\git\scriptcs\src\ScriptCs.Engine.Roslyn\RoslynScriptEngine.cs:line 119

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 4, 2015

Member

Are you on Linux or Windows? I am not seeing this on Widows. It looks like the error is as it inspects the metadata. I may be able to fix with a try/catch.

Member

glennblock commented Feb 4, 2015

Are you on Linux or Windows? I am not seeing this on Widows. It looks like the error is as it inspects the metadata. I may be able to fix with a try/catch.

@sbedford

This comment has been minimized.

Show comment
Hide comment
@sbedford

sbedford Feb 4, 2015

Win 8.1 x64

sbedford commented Feb 4, 2015

Win 8.1 x64

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 4, 2015

Member

Weird. Is this using just the sample? How are you running it?

Member

glennblock commented Feb 4, 2015

Weird. Is this using just the sample? How are you running it?

@sbedford

This comment has been minimized.

Show comment
Hide comment
@sbedford

sbedford Feb 5, 2015

I have the gist in a csx file (along with packages.config) which I've copied into the artefacts/Release/bin directory and running it like

scriptcs --install
scriptcs app.csx

sbedford commented Feb 5, 2015

I have the gist in a csx file (along with packages.config) which I've copied into the artefacts/Release/bin directory and running it like

scriptcs --install
scriptcs app.csx

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 5, 2015

Member

I wouldn't run that way.

Member

glennblock commented Feb 5, 2015

I wouldn't run that way.

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 5, 2015

Member

Have scriptcs in a separate folder from your scripts. It should migrate your packages.config to scriptcs_packages.config the first time you run. Then you'll need to run again.

Member

glennblock commented Feb 5, 2015

Have scriptcs in a separate folder from your scripts. It should migrate your packages.config to scriptcs_packages.config the first time you run. Then you'll need to run again.

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 5, 2015

Member

We don't test / having scriptcs in the same folder as your script.

Member

glennblock commented Feb 5, 2015

We don't test / having scriptcs in the same folder as your script.

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 5, 2015

Member

Can you try testing with separate folders?

Member

glennblock commented Feb 5, 2015

Can you try testing with separate folders?

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 5, 2015

Member

I think you may have found another area where we are not checking for non-native dlls. We automatically load all binaries in the scriptcs folder as we have modules that we include in the box.

We don't really test / support this scenario, so it might be good for us to add validation that throws an exception if you try to run a script in that folder.

Member

glennblock commented Feb 5, 2015

I think you may have found another area where we are not checking for non-native dlls. We automatically load all binaries in the scriptcs folder as we have modules that we include in the box.

We don't really test / support this scenario, so it might be good for us to add validation that throws an exception if you try to run a script in that folder.

@sbedford

This comment has been minimized.

Show comment
Hide comment
@sbedford

sbedford Feb 5, 2015

Ok, this is really dumb but im going to spell it out anyway..

I pulled your repo down (https://github.com/glennblock/scriptcs.git), then pulled down branch 913, ran build.cmd.

Change directory to where my test script is: c:\development\gists\libgittest

Then I ran: C:\development\git\glennblock_scriptcs\artifacts\Release\bin\scriptcs.exe app.csx

But I'm back where we started and I'm thoroughly confused :)

Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'git2-e0902fb.dll' or one of its dependencies. The module was expected to contain an assembly manifest. ---> System.BadImageFormatException: Could not load file or assembly 'git2-e0902fb.dll' or one of its dependencies. The module was
expected to contain an assembly manifest.
--- End of inner exception stack trace ---
at System.Reflection.AssemblyName.nGetFileInformation(String s)
at System.Reflection.AssemblyName.GetAssemblyName(String assemblyFile)
at ScriptCs.AppDomainAssemblyResolver.AddAssemblyPaths(IEnumerable`1 assemblyPaths) in c:\development\git\glennblock_scriptcs\src\ScriptCs.Core\AppDomainAssemblyResolver.cs:line 79
at ScriptCs.ScriptServicesBuilderFactory.Create(ScriptCsArgs commandArgs, String[] scriptArgs) in c:\development\git\glennblock_scriptcs\src\ScriptCs\ScriptServicesBuilderFactory.cs:line 27
at ScriptCs.Program.Main(String[] args) in c:\development\git\glennblock_scriptcs\src\ScriptCs\Program.cs:line 17

sbedford commented Feb 5, 2015

Ok, this is really dumb but im going to spell it out anyway..

I pulled your repo down (https://github.com/glennblock/scriptcs.git), then pulled down branch 913, ran build.cmd.

Change directory to where my test script is: c:\development\gists\libgittest

Then I ran: C:\development\git\glennblock_scriptcs\artifacts\Release\bin\scriptcs.exe app.csx

But I'm back where we started and I'm thoroughly confused :)

Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'git2-e0902fb.dll' or one of its dependencies. The module was expected to contain an assembly manifest. ---> System.BadImageFormatException: Could not load file or assembly 'git2-e0902fb.dll' or one of its dependencies. The module was
expected to contain an assembly manifest.
--- End of inner exception stack trace ---
at System.Reflection.AssemblyName.nGetFileInformation(String s)
at System.Reflection.AssemblyName.GetAssemblyName(String assemblyFile)
at ScriptCs.AppDomainAssemblyResolver.AddAssemblyPaths(IEnumerable`1 assemblyPaths) in c:\development\git\glennblock_scriptcs\src\ScriptCs.Core\AppDomainAssemblyResolver.cs:line 79
at ScriptCs.ScriptServicesBuilderFactory.Create(ScriptCsArgs commandArgs, String[] scriptArgs) in c:\development\git\glennblock_scriptcs\src\ScriptCs\ScriptServicesBuilderFactory.cs:line 27
at ScriptCs.Program.Main(String[] args) in c:\development\git\glennblock_scriptcs\src\ScriptCs\Program.cs:line 17

@sbedford

This comment has been minimized.

Show comment
Hide comment
@sbedford

sbedford Feb 5, 2015

ERROR: error CS0009: Metadata file 'c:\development\gist\libgittest\packages\LibGit2Sharp.0.21.0.176\lib\net40\NativeBinaries\x86\git2-e0902fb.dll' could not be
opened -- 'MetadataReaderException: COR20HeaderTooSmall'
DEBUG: Roslyn.Compilers.CompilationErrorException: error CS0009: Metadata file 'c:\development\gist\libgittest\packages\LibGit2Sharp.0.21.0.176\lib\net40\NativeBinaries\x86\git2-e0902fb.dll' could not be opened -- 'MetadataReaderException:
COR20HeaderTooSmall'

sbedford commented Feb 5, 2015

ERROR: error CS0009: Metadata file 'c:\development\gist\libgittest\packages\LibGit2Sharp.0.21.0.176\lib\net40\NativeBinaries\x86\git2-e0902fb.dll' could not be
opened -- 'MetadataReaderException: COR20HeaderTooSmall'
DEBUG: Roslyn.Compilers.CompilationErrorException: error CS0009: Metadata file 'c:\development\gist\libgittest\packages\LibGit2Sharp.0.21.0.176\lib\net40\NativeBinaries\x86\git2-e0902fb.dll' could not be opened -- 'MetadataReaderException:
COR20HeaderTooSmall'

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 5, 2015

Member

I am seeing this error now as well. It looks like it may be a Roslyn CTP issue from what I am seeing.

Member

glennblock commented Feb 5, 2015

I am seeing this error now as well. It looks like it may be a Roslyn CTP issue from what I am seeing.

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 5, 2015

Member

@sbedford making progress on this. It doesn't appear to be a Roslyn issue, it appears that we've passed the assembly to Roslyn as a reference. Its metadata reader fails (the error) as it should. I am going to add further filtering logic to prevent that!

Member

glennblock commented Feb 5, 2015

@sbedford making progress on this. It doesn't appear to be a Roslyn issue, it appears that we've passed the assembly to Roslyn as a reference. Its metadata reader fails (the error) as it should. I am going to add further filtering logic to prevent that!

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 5, 2015

Member

I think I've nailed it.....

libgit

Member

glennblock commented Feb 5, 2015

I think I've nailed it.....

libgit

glennblock added a commit to glennblock/scriptcs that referenced this issue Feb 5, 2015

glennblock added a commit to glennblock/scriptcs that referenced this issue Feb 5, 2015

@adamralph adamralph changed the title from BadImageException loading LibGit2Sharp to scriptcs crashes when native DLL's are present Feb 5, 2015

@adamralph adamralph added the bug label Feb 5, 2015

@adamralph adamralph added this to the v0.13.1 milestone Feb 5, 2015

adamralph added a commit that referenced this issue Feb 5, 2015

Merge pull request #914 from glennblock/913
Changes ModuleLoader to ignore native assemblies. Fix for #913

@adamralph adamralph closed this Feb 5, 2015

@glennblock

This comment has been minimized.

Show comment
Hide comment
@glennblock

glennblock Feb 5, 2015

Member

w00t!

Member

glennblock commented Feb 5, 2015

w00t!

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