Fix unhelpful error when P/Invoke references a nonexistent DLL#27352
Open
gvsrgh wants to merge 4 commits intoPowerShell:masterfrom
Open
Fix unhelpful error when P/Invoke references a nonexistent DLL#27352gvsrgh wants to merge 4 commits intoPowerShell:masterfrom
gvsrgh wants to merge 4 commits intoPowerShell:masterfrom
Conversation
Author
|
@microsoft-github-policy-service agree |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an unhelpful ArgumentNullException (path1) surfaced when invoking a P/Invoke method compiled via Add-Type that references a missing native DLL, allowing the CLR to report the correct DllNotFoundException instead.
Changes:
- Prevent
NativeDllHandlerfrom callingPath.Combine()with a null/empty folder when the invoking assembly has an emptyLocation. - Add a Pester test validating that a missing P/Invoke DLL produces a
DllNotFoundExceptionmentioning the requested library name.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs |
Early-return from NativeDllHandler when the assembly location directory is null/empty to avoid ArgumentNullException and allow normal unmanaged DLL resolution/failure. |
test/powershell/Language/Interop/DotNet/DotNetInterop.Tests.ps1 |
Adds a regression test that exercises Add-Type + missing DllImport and asserts DllNotFoundException includes the missing DLL name. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #26941
PR Summary
When calling a P/Invoke method via
Add-Typethat references a missing DLL,PowerShell 7 threw an obscure
ArgumentNullException:This was caused by
NativeDllHandlerinCorePsAssemblyLoadContext.cscallingPath.Combine(folder, ...)wherefolderwasnull— because in-memoryassemblies compiled by
Add-Typehave an emptyLocation, causingPath.GetDirectoryName("")to returnnull.The fix returns
IntPtr.Zeroearly whenfolderis null/empty, letting the CLRfall through to its own resolution which correctly surfaces a
DllNotFoundException:PR Checklist
Breaking Changes
User-facing Changes
Testing - New and feature
test/powershell/Language/Interop/DotNet/DotNetInterop.Tests.ps1