-
Notifications
You must be signed in to change notification settings - Fork 6
fix: lookupExecutable race condition #87
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
Merged
AndrewBarba
merged 5 commits into
tuist:main
from
AndrewBarba:fix-lookup-race-condition
Aug 15, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,18 +100,15 @@ public enum CommandEvent: Sendable { | |
| public enum CommandError: Error, CustomStringConvertible, Sendable { | ||
| case terminated(Int32, stderr: String) | ||
| case signalled(Int32) | ||
| case errorObtainingExecutable(executable: String, error: String) | ||
| case executableNotFound(String) | ||
| case missingExecutableName | ||
|
|
||
| public var description: String { | ||
| switch self { | ||
| case let .signalled(code): return "The command terminated after receiving a signal with code \(code)" | ||
| case let .terminated(code, _): return "The command terminated with the code \(code)" | ||
| case let .errorObtainingExecutable( | ||
| name, | ||
| error | ||
| ): return "There was an error trying to obtain the path to the executable '\(name)': \(error)" | ||
| case let .executableNotFound(name): return "Couldn't locate the executable '\(name)' in the environment." | ||
| case .missingExecutableName: return "The executable name is missing." | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -179,9 +176,7 @@ public struct CommandRunner: CommandRunning, Sendable { | |
| let executable = try lookupExecutable(firstArgument: arguments.first) | ||
| process.executableURL = executable | ||
|
|
||
| if let executable { | ||
| logger?.debug("Running command: \(executable.absoluteString) \(processArguments.joined(separator: " "))") | ||
| } | ||
| logger?.debug("Running command: \(executable.absoluteString) \(processArguments.joined(separator: " "))") | ||
|
|
||
| let threadSafeProcess = ThreadSafe(process) | ||
|
|
||
|
|
@@ -238,8 +233,10 @@ public struct CommandRunner: CommandRunning, Sendable { | |
| } | ||
| } | ||
|
|
||
| func lookupExecutable(firstArgument: String?) throws -> URL? { | ||
| guard let firstArgument else { return nil } | ||
| func lookupExecutable(firstArgument: String?) throws -> URL { | ||
| guard let firstArgument else { | ||
| throw CommandError.missingExecutableName | ||
| } | ||
|
|
||
| // If the first argument is an absolute URL to an executable, return it. | ||
| if let executablePath = try? Path.AbsolutePath(validating: firstArgument) { | ||
|
|
@@ -250,7 +247,7 @@ public struct CommandRunner: CommandRunning, Sendable { | |
| let arguments: [String] | ||
|
|
||
| #if os(Windows) | ||
| command = "where" | ||
| command = "C:\\Windows\\System32\\where.exe" | ||
| arguments = [firstArgument] | ||
| #else | ||
| command = "/usr/bin/which" | ||
|
|
@@ -265,27 +262,23 @@ public struct CommandRunner: CommandRunning, Sendable { | |
| let process = Process() | ||
| process.executableURL = URL(fileURLWithPath: command) | ||
| process.arguments = arguments | ||
| process.environment = ProcessInfo.processInfo.environment | ||
| process.currentDirectoryURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath) | ||
|
Comment on lines
+265
to
+266
Contributor
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. I wrongly assumed those values would be inherited 😅. Good catch @AndrewBarba |
||
|
|
||
| let pipe = Pipe() | ||
| process.standardOutput = pipe | ||
| process.standardError = pipe | ||
|
|
||
| do { | ||
| try process.run() | ||
| } catch { | ||
| let data = pipe.fileHandleForReading.readDataToEndOfFile() | ||
| let output = String(data: data, encoding: .utf8) ?? "" | ||
| throw CommandError.errorObtainingExecutable(executable: firstArgument, error: output) | ||
| } | ||
|
|
||
| let data = pipe.fileHandleForReading.readDataToEndOfFile() | ||
| try process.run() | ||
| process.waitUntilExit() | ||
|
|
||
| if let output = String(data: data, encoding: .utf8) { | ||
| let trimmedOutput = output.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| return trimmedOutput.isEmpty ? nil : URL(fileURLWithPath: trimmedOutput) | ||
| let data = try pipe.fileHandleForReading.readToEnd() | ||
| let output = String(data: data ?? .init(), encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) | ||
|
|
||
| guard let output, !output.isEmpty else { | ||
| throw CommandError.executableNotFound(firstArgument) | ||
| } | ||
|
|
||
| return nil | ||
| return URL(fileURLWithPath: output) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Good catch!