From 38b6958412ef6204c245c65df994f9e83bf5e068 Mon Sep 17 00:00:00 2001 From: Louis Seubert Date: Thu, 12 Feb 2026 17:44:37 +0100 Subject: [PATCH] feat: add windows path ext resolution This resolves a problem where the file name of a script file was not correctly executed when passed without extension. --- src/process/Command.Execute.cs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/process/Command.Execute.cs b/src/process/Command.Execute.cs index 02c9fc4..826b210 100644 --- a/src/process/Command.Execute.cs +++ b/src/process/Command.Execute.cs @@ -11,7 +11,6 @@ public sealed partial class Command return process.MainModule?.FileName; }); - private static readonly string[] WindowsExecutableExtensions = ["exe", "cmd", "bat"]; private static readonly TimeSpan CancelWaitTimeout = TimeSpan.FromSeconds(5); private static string? ProcessPath => ProcessPathLazy.Value; @@ -71,8 +70,7 @@ public sealed partial class Command // Don't do anything for fully qualified paths or paths that already have an extension specified. // System.Diagnostics.Process knows how to handle those without our help. - if (Path.IsPathFullyQualified(TargetFilePath) || - !string.IsNullOrWhiteSpace(Path.GetExtension(TargetFilePath))) + if (Path.IsPathFullyQualified(TargetFilePath) || !string.IsNullOrWhiteSpace(Path.GetExtension(TargetFilePath))) { return TargetFilePath; } @@ -82,7 +80,7 @@ public sealed partial class Command where Directory.Exists(probeDirPath) select Path.Combine(probeDirPath, TargetFilePath) into baseFilePath - from extension in WindowsExecutableExtensions + from extension in GetPathExtensions() select Path.ChangeExtension(baseFilePath, extension) ).FirstOrDefault(File.Exists) ?? TargetFilePath; @@ -112,6 +110,24 @@ public sealed partial class Command } } } + + static IEnumerable GetPathExtensions() + { + if (System.Environment.GetEnvironmentVariable("PATHEXT")?.Split(Path.PathSeparator) is { } extensions) + { + foreach (var extension in extensions) + { + yield return extension; + } + } + else + { + yield return ".COM"; + yield return ".EXE"; + yield return ".BAT"; + yield return ".CMD"; + } + } } }