-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Close #32 Searching for a file on the system path #111
base: release-1.7
Are you sure you want to change the base?
Changes from 2 commits
74f94d4
edde9b9
68a3737
30cf111
f77706a
0e29d00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,9 @@ | |
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
@@ -36,12 +38,17 @@ public Command Run(string executable, IEnumerable<object>? arguments = null, Act | |
{ | ||
Throw.If(string.IsNullOrEmpty(executable), "executable is required"); | ||
|
||
var executablePath = !executable.Contains(Path.DirectorySeparatorChar) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This changes the behavior, but my understanding is that using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The proposal wasn't to make this default behavior. This should be opt-in behavior. When this behavior is enabled, it should behave like a shell would. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @madelson What is the expected behavior if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We want to follow the actual behavior on the command line for guidance. I believe that the current directory is searched before the system path, but I could have that backwards. Certainly a fully-qualified path should work without a search. There are also relative paths like |
||
&& GetFullPathUsingSystemPathOrDefault(executable) is { } fullPath | ||
? fullPath | ||
: executable; | ||
|
||
var finalOptions = this.GetOptions(options); | ||
|
||
var processStartInfo = new ProcessStartInfo | ||
{ | ||
CreateNoWindow = true, | ||
FileName = executable, | ||
FileName = executablePath, | ||
RedirectStandardError = true, | ||
RedirectStandardInput = true, | ||
RedirectStandardOutput = true, | ||
|
@@ -76,6 +83,24 @@ public Command Run(string executable, IEnumerable<object>? arguments = null, Act | |
return command; | ||
} | ||
|
||
// internal for testing | ||
internal static string? GetFullPathUsingSystemPathOrDefault(string executable) | ||
Bartleby2718 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var paths = Environment.GetEnvironmentVariable("PATH")!.Split(Path.PathSeparator); | ||
Bartleby2718 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
var pathExtensions = Environment.GetEnvironmentVariable("PATHEXT")! | ||
Bartleby2718 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.Split(Path.PathSeparator) | ||
.Concat(new[] { string.Empty }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The user may have included the extension, in which case we shouldn't be adding more. Given that there may be an executable like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the OS precedence behavior here? E.g. if I type What if What if there is a file We should strive to match the OS behavior exactly, documenting and testing each decision point. |
||
.ToArray(); | ||
return paths.SelectMany(path => pathExtensions.Select(pathExtension => Path.Combine(path, executable + pathExtension))) | ||
.FirstOrDefault(File.Exists); | ||
Bartleby2718 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return paths.Select(path => Path.Combine(path, executable)).FirstOrDefault(File.Exists); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By checking For example, I type |
||
} | ||
|
||
private static void PopulateArguments(ProcessStartInfo processStartInfo, IEnumerable<object?>? arguments, Options options) | ||
{ | ||
if (arguments is null) { return; } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this part adds any value here and in the other test.