feat: add windows path ext resolution
All checks were successful
default / dotnet-default-workflow (push) Successful in 2m21s

This resolves a problem where the file name of a script file was not correctly executed when passed without extension.
This commit is contained in:
Louis Seubert 2026-02-12 17:44:37 +01:00
commit 38b6958412
Signed by: louis9902
GPG key ID: 4B9DB28F826553BD

View file

@ -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<string> 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";
}
}
}
}