Skip to content

Replace usage of libpsl-native APIs with .NET equivalents#27327

Draft
adityapatwardhan wants to merge 4 commits into
PowerShell:masterfrom
adityapatwardhan:RemovePSNative
Draft

Replace usage of libpsl-native APIs with .NET equivalents#27327
adityapatwardhan wants to merge 4 commits into
PowerShell:masterfrom
adityapatwardhan:RemovePSNative

Conversation

@adityapatwardhan
Copy link
Copy Markdown
Member

PR Summary

This pull request refactors and modernizes the implementation of symbolic and hard link creation, process management, and error handling on non-Windows platforms in PowerShell. The main changes include replacing native interop calls with .NET APIs, improving error reporting, and updating related code paths to use the new patterns.

Key improvements and changes:

Symbolic and Hard Link Creation:

  • Replaced the use of native interop (Unix.NativeMethods.CreateSymLink and CreateHardLink) with .NET's File.CreateSymbolicLink, Directory.CreateSymbolicLink, and File.CreateHardLink methods. These methods now return an error code via an out parameter and provide more robust error handling by catching relevant exceptions and extracting error information. [1] [2] [3]

Process Management:

  • Updated process termination and waiting logic to use .NET's Process class methods (Process.Kill, Process.WaitForExit) instead of native interop (Unix.NativeMethods.KillProcess, WaitPid). This includes improved exception handling to gracefully handle cases where the process may have already exited or cannot be queried. [1] [2]

Error Handling and Categorization:

  • Replaced the use of a native method for mapping error codes to PowerShell error categories with a managed C# switch statement, making error categorization clearer and reducing reliance on native code. [1] [2]

Code Cleanup and Removal of Native Interop:

  • Removed several now-unnecessary native interop method declarations from NativeMethods, including those for link creation, process killing, and error categorization. [1] [2] [3]

General Refactoring:

  • Updated method signatures and call sites throughout the codebase to support the new .NET-based implementations and error handling patterns, ensuring consistent and reliable behavior across platforms. [1] [2]

These changes collectively modernize the codebase, improve maintainability, and enhance cross-platform compatibility by leveraging .NET APIs instead of platform-specific native interop.

PR Context

PR Checklist

adityapatwardhan and others added 3 commits April 22, 2026 10:34
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Copilot AI review requested due to automatic review settings April 22, 2026 17:44
@adityapatwardhan adityapatwardhan added the CL-Engine Indicates that a PR should be marked as an engine change in the Change Log label Apr 22, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors several non-Windows code paths in System.Management.Automation to reduce reliance on libpsl-native by replacing native interop calls with .NET APIs for link creation, process termination/waiting, and errno-to-ErrorCategory mapping.

Changes:

  • Switch non-Windows symbolic/hard link creation to File/Directory.CreateSymbolicLink and File.CreateHardLink, plumbing an errorCode back to the caller.
  • Replace non-Windows waitpid usage in SSH process termination with Process-based waiting and add exception handling around process lookup/state checks.
  • Replace native GetErrorCategory with a managed switch mapping and remove several now-unused native method declarations.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/System.Management.Automation/namespaces/FileSystemProvider.cs Updates UNIX link-creation call sites to use new Platform.NonWindowsCreate*Link(..., out errorCode) signatures.
src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs Replaces NonWindowsWaitPid with Process.GetProcessById(...).WaitForExit() (with exception handling) in UNIX SSH teardown.
src/System.Management.Automation/CoreCLR/CorePsPlatform.cs Implements .NET-based link creation + process kill; replaces native error-category mapping with managed switch; removes related native imports.

Comment thread src/System.Management.Automation/CoreCLR/CorePsPlatform.cs Outdated
Comment on lines +493 to +497
errorCode = Marshal.GetLastPInvokeError();
if (errorCode == 0)
{
errorCode = ex.HResult & 0xFFFF;
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

return Unix.NativeMethods.WaitPid(pid, nohang);
try
{
Process.GetProcessById(pid).Kill();
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@adityapatwardhan adityapatwardhan changed the title Replace usage of libpsl-nnative APIs with .NET equivalents Replace usage of libpsl-native APIs with .NET equivalents Apr 22, 2026
@adityapatwardhan adityapatwardhan marked this pull request as draft April 23, 2026 17:23
@microsoft-github-policy-service microsoft-github-policy-service Bot added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Apr 26, 2026
@daxian-dbw
Copy link
Copy Markdown
Member

daxian-dbw commented May 5, 2026

@adityapatwardhan Are we going to get rid of the Microsoft.PowerShell.Native package completely, or just the Unix part of it?
Currently, we depend on this nuget package to get the ETW manifest resource DLL Microsoft.PowerShell.Instrumentation.dll, so that package needs to be available unless we get can the manifest resource DLL in a different way.

@adityapatwardhan
Copy link
Copy Markdown
Member Author

The initial plan is for Linux part only. In phase 2 I will investigate whether we can get rid of pwrshplugin as well. From what i have seen, there is no 1:1 mapping of all Linux functionality in .NET.

@microsoft-github-policy-service microsoft-github-policy-service Bot removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-Engine Indicates that a PR should be marked as an engine change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants