question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing path with forward-slashes to LoadUnmanagedDllFromPath cause load to fail

See original GitHub issue

Describe the bug When a native binary in a package is defined in the .nuspec using forward-slashes, AssemblyLoadContext.LoadUnmanagedDllFromPath() will fail loading it.

Sample <file> causing this failure:

Foo.nuspec

<files>
  <file src="nativeassets\*.dll" target="runtimes/win-x64/native" />
</files>

To Reproduce Steps to reproduce the behavior:

  1. Define a NuGet package that include a native binary using the above pattern.
  2. Load an assembly in an ALC (new ManagedLoadContext(...)) that depend on the native binary and execute some code in it, e.g. using P/Invoke.

Expected behavior The native binary is found and successfully loaded by DotNetCorePlugins.

Actual behavior The native binary is found, but fail to load.

Screenshots image

Additional context Spent quite some time figuring out what this problem was about, and what especially fooled me was that File.Exist(unmanagedPath) return true, because that API clearly deal with mixed slashes. Then when you pass the path on to LoadContext.LoadUnmanagedDllFromPath(path) here, here, here or here, it will raise the above exception.

Considered making a PR, but wasn’t sure in what spot you want to deal with this - SeachForLibrary maybe - or if this even should be reported as a flaw in AssemblyLoadContext.LoadUnmanagedDllFromPath. If the later, just let me know and I’ll file an issue there instead.

Tested only on

.NET Core SDK (reflecting any global.json):
 Version:   2.2.101
 Commit:    236713b0b7

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17134
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.2.101\

Host (useful for support):
  Version: 2.2.0
  Commit:  1249f08fed

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
natemcmastercommented, Jan 22, 2019

@per-samuelsson thanks for sharing details on your investigation. I’d be happy to add a workaround to this library which normalizes the file path before calling LoadUnmanagedDllFromPath. Feel free to send a PR and I can get an update to nuget.org this week.

1reaction
natemcmastercommented, Jan 18, 2019

even should be reported as a flaw in AssemblyLoadContext.LoadUnmanagedDllFromPath.

Yes, can you please open this on https://github.com/dotnet/coreclr, if it doesn’t exist already?

In the meantime, I’m happy to take a workaround for this that normalizes slashes before calling LoadUnmanagedDllFromPath.

Read more comments on GitHub >

github_iconTop Results From Across the Web

LoadLibraryW and POSIX path separator - winapi
When specifying a path, be sure to use backslashes (), not forward slashes (/). Is there some other pitfails that may cause LoadLibrary...
Read more >
Failing code with forward slashes as path separators ...
Found using System.Xml.Linq.XDocument.Load(), can't reproduce with just System.IO.Path.GetFullPath() so my guess is the path gets ...
Read more >
Backslash mapping in Unix file paths - .NET
The native CoreCLR runtime automatically converted backslash ( \ ) characters in file paths to forward slashes ( / ) on Unix.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found