Skip to content

Commit 9dae756

Browse files
authored
Merge pull request #26 from hartbit/lookup-executable-path-refactor
Refactor lookupExecutablePath to find executables in PATH even if cwd is not defined
2 parents 8f69898 + 9cd33e2 commit 9dae756

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

Sources/TSCBasic/misc.swift

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,33 +85,24 @@ public func lookupExecutablePath(
8585
return nil
8686
}
8787

88-
let path: AbsolutePath
88+
var paths: [AbsolutePath] = []
89+
8990
if let cwd = currentWorkingDirectory {
9091
// We have a value, but it could be an absolute or a relative path.
91-
path = AbsolutePath(value, relativeTo: cwd)
92+
paths.append(AbsolutePath(value, relativeTo: cwd))
9293
} else if let absPath = try? AbsolutePath(validating: value) {
9394
// Current directory not being available is not a problem
9495
// for the absolute-specified paths.
95-
path = absPath
96-
} else {
97-
return nil
96+
paths.append(absPath)
9897
}
9998

100-
if localFileSystem.isExecutableFile(path) {
101-
return path
102-
}
10399
// Ensure the value is not a path.
104-
guard !value.contains("/") else {
105-
return nil
100+
if !value.contains("/") {
101+
// Try to locate in search paths.
102+
paths.append(contentsOf: searchPaths.map({ $0.appending(component: value) }))
106103
}
107-
// Try to locate in search paths.
108-
for path in searchPaths {
109-
let exec = path.appending(component: value)
110-
if localFileSystem.isExecutableFile(exec) {
111-
return exec
112-
}
113-
}
114-
return nil
104+
105+
return paths.first(where: { localFileSystem.isExecutableFile($0) })
115106
}
116107

117108
/// A wrapper for Range to make it Codable.

0 commit comments

Comments
 (0)