-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Fix NativeDllHandler
to not throw when file is not found
#11787
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
Conversation
Hello @TravisEz13! Because this pull request has the Do note that I've been instructed to only help merge pull requests of this repository that have been opened for at least 24 hours, a condition that will be fulfilled in about 23 hours 19 minutes. No worries though, I will be back when the time is right! 😉 p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
@iSazonov How would that add any benefit? There will be a file check in |
@msftclas make sure @PaulHigin has a chance to review before you merge |
The resolver is last resort resolver. In most scenarios all native dll-s are loaded before the resolver. The resolver is called only for one scenario - last attempt to load arch-dependent native dll. Normally the attempt is successful and we do not need to check for the dll file before p/invoke. Any fail in native dll loading is rare and it is a bug (we should again not worry about dll file check) - either dll is absent or corrupted - both these cases TryLoad() can catch. |
This is just the current runtime behavior. It's possible another resolving approach could be added after the This cause problem to application that hosts powershell, e.g. dotnet-interactive. Both the application code and powershell is using the default load context, so this event handler will kick in for all native library loading, even if it's triggered by the application code instead of a powershell module. With the current code, the exception thrown from this handler is super confusing, also the stack trace makes PowerShell look like a culprit to blame.
Using |
In the case we should catch and log any exception and continue resolving process. |
Actually, on a second thought, I think you are right about using I will update the code. Thanks @iSazonov! |
…l#11787) <!-- Anything that looks like this is a comment and can't be seen after the Pull Request is created. --> # PR Summary Fix `NativeDllHandler` to not throw when file is not found. Today, the `NativeDllHandler` calls `NativeLibrary.Load(fullName)` even if the file doesn't exist. This is not right, it should return `IntPtr.Zero` to let the runtime try the next resolution approach, if there is one. Also, this behavior results in the exception message to be `Unable to load DLL 'F:\win-x64\nativedll.dll' or one of its dependencies: The specified module could not be found`, which is confusing because there was never an explicit loading of a dll with that path. The real exception generated from runtime should be `Unable to load DLL 'nativedll' or one of its dependencies: The specified module could not be found.` ## PR Checklist - [x] [PR has a meaningful title](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - Use the present tense and imperative mood when describing your changes - [x] [Summarized changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - [x] [Make sure all `.h`, `.cpp`, `.cs`, `.ps1` and `.psm1` files have the correct copyright header](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - [x] This PR is ready to merge and is not [Work in Progress](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---work-in-progress). - If the PR is work in progress, please add the prefix `WIP:` or `[ WIP ]` to the beginning of the title (the `WIP` bot will keep its status check at `Pending` while the prefix is present) and remove the prefix when the PR is ready. - **[Breaking changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#making-breaking-changes)** - [x] None - **OR** - [ ] [Experimental feature(s) needed](https://github.com/MicrosoftDocs/PowerShell-Docs/blob/staging/reference/6/Microsoft.PowerShell.Core/About/about_Experimental_Features.md) - [ ] Experimental feature name(s): <!-- Experimental feature name(s) here --> - **User-facing changes** - [x] Not Applicable - **OR** - [ ] [Documentation needed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - [ ] Issue filed: <!-- Number/link of that issue here --> - **Testing - New and feature** - [x] N/A or can only be tested interactively - **OR** - [ ] [Make sure you've added a new test if existing tests do not effectively test the code changed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#before-submitting) - **Tooling** - [x] I have considered the user experience from a tooling perspective and don't believe tooling will be impacted. - **OR** - [ ] I have considered the user experience from a tooling perspective and enumerated concerns in the summary. This may include: - Impact on [PowerShell Editor Services](https://github.com/PowerShell/PowerShellEditorServices) which is used in the [PowerShell extension](https://github.com/PowerShell/vscode-powershell) for VSCode (which runs in a different PS Host). - Impact on Completions (both in the console and in editors) - one of PowerShell's most powerful features. - Impact on [PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer) (which provides linting & formatting in the editor extensions). - Impact on [EditorSyntax](https://github.com/PowerShell/EditorSyntax) (which provides syntax highlighting with in VSCode, GitHub, and many other editors).
🎉 Handy links: |
PR Summary
Fix
NativeDllHandler
to not throw when file is not found.Today, the
NativeDllHandler
callsNativeLibrary.Load(fullName)
even if the file doesn't exist. This is not right, it should returnIntPtr.Zero
to let the runtime try the next resolution approach, if there is one.Also, this behavior results in the exception message to be
Unable to load DLL 'F:\win-x64\nativedll.dll' or one of its dependencies: The specified module could not be found
, which is confusing because there was never an explicit loading of a dll with that path.The real exception generated from runtime should be
Unable to load DLL 'nativedll' or one of its dependencies: The specified module could not be found.
PR Checklist
.h
,.cpp
,.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
or[ WIP ]
to the beginning of the title (theWIP
bot will keep its status check atPending
while the prefix is present) and remove the prefix when the PR is ready.