Skip to content

Commit 0edd2d8

Browse files
CopilotNeo-vortexadamsitnik
authored
Throw Win32Exception(ERROR_NO_ASSOCIATION) when Unix has no shell opener (#132219)
- Cherry-picks the contributor fix to throw `Win32Exception(ERROR_NO_ASSOCIATION)` when no Unix shell opener is available. - Adds a Linux `RemoteExecutor` regression test that clears `PATH`, ensuring the failure is deterministic. fixes #96565 --------- Co-authored-by: Mohamadreza Nakhleh <[email protected]> Co-authored-by: adamsitnik <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]>
1 parent 2580b59 commit 0edd2d8

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎src/libraries/System.Diagnostics.Process/src/Microsoft/Win32/SafeHandles/SafeProcessHandle.Unix.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ private static SafeProcessHandle StartWithShellExecute(ProcessStartInfo startInf
405405
}
406406

407407
// use default program to open file/url
408-
filename = Process.GetPathToOpenFile();
408+
filename = Process.GetPathToOpenFile() ?? throw new Win32Exception(Interop.Errors.ERROR_NO_ASSOCIATION);
409409
string[] openFileArgv = ProcessUtils.ParseArgv(startInfo, filename, ignoreArguments: true);
410410

411411
SafeProcessHandle result = ForkAndExecProcess(

‎src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ public void ProcessStart_UseShellExecute_OnLinux_ThrowsIfNoProgramInstalled()
7979
}
8080
}
8181

82+
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
83+
[PlatformSpecific(TestPlatforms.Linux | TestPlatforms.FreeBSD)]
84+
public void ProcessStart_UseShellExecute_OnUnix_ThrowsWhenNoOpenerOnPath()
85+
{
86+
RemoteInvokeOptions options = new RemoteInvokeOptions();
87+
options.StartInfo.EnvironmentVariables["PATH"] = string.Empty;
88+
89+
RemoteExecutor.Invoke(() =>
90+
{
91+
Win32Exception exception = Assert.Throws<Win32Exception>(() => Process.Start(new ProcessStartInfo { UseShellExecute = true, FileName = Environment.CurrentDirectory }));
92+
Assert.Equal(Interop.Errors.ERROR_NO_ASSOCIATION, exception.NativeErrorCode);
93+
}, options).Dispose();
94+
}
95+
8296
[Fact]
8397
[OuterLoop("Opens program")]
8498
[SkipOnPlatform(TestPlatforms.MacCatalyst, "In App Sandbox mode, the process doesn't have read access to the binary.")]
@@ -1060,7 +1074,6 @@ private static unsafe HashSet<uint> GetGroups()
10601074

10611075
private const int O_RDONLY = 0;
10621076
private const int O_WRONLY = 1;
1063-
10641077
private static readonly string[] s_allowedProgramsToRun = new string[] { "xdg-open", "gnome-open", "kfmclient" };
10651078

10661079
private string WriteScriptFile(string directory, string name, int returnValue)

‎src/libraries/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
Link="Common\Interop\Unix\System.Native\Interop.PosixSignal.cs" />
101101
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Stat.cs"
102102
Link="Common\Interop\Unix\System.Native\Interop.Stat.cs" />
103+
<Compile Include="$(CommonPath)Interop\Windows\Interop.Errors.cs"
104+
Link="Common\Interop\Windows\Interop.Errors.cs" />
103105
</ItemGroup>
104106
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'android' or '$(TargetPlatformIdentifier)' == 'maccatalyst'">
105107
<Compile Include="ProcessTests.Mobile.cs" />

0 commit comments

Comments
 (0)