Skip to content

Commit f6e6fd1

Browse files
Fix which not to return directories
emsdk includes `node` directory but the `which` function returned it as a valid executable.
1 parent c8c17b2 commit f6e6fd1

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

Plugins/BridgeJS/Sources/TS2Skeleton/TS2Skeleton.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ import protocol Dispatch.DispatchSourceSignal
1212
import class Dispatch.DispatchSource
1313

1414
internal func which(_ executable: String) throws -> URL {
15+
func checkCandidate(_ candidate: URL) -> Bool {
16+
var isDirectory: ObjCBool = false
17+
let fileExists = FileManager.default.fileExists(atPath: candidate.path, isDirectory: &isDirectory)
18+
return fileExists && !isDirectory.boolValue && FileManager.default.isExecutableFile(atPath: candidate.path)
19+
}
1520
do {
1621
// Check overriding environment variable
1722
let envVariable = executable.uppercased().replacingOccurrences(of: "-", with: "_") + "_PATH"
1823
if let path = ProcessInfo.processInfo.environment[envVariable] {
1924
let url = URL(fileURLWithPath: path).appendingPathComponent(executable)
20-
if FileManager.default.isExecutableFile(atPath: url.path) {
25+
if checkCandidate(url) {
2126
return url
2227
}
2328
}
@@ -31,7 +36,7 @@ internal func which(_ executable: String) throws -> URL {
3136
let paths = ProcessInfo.processInfo.environment["PATH"]!.split(separator: pathSeparator)
3237
for path in paths {
3338
let url = URL(fileURLWithPath: String(path)).appendingPathComponent(executable)
34-
if FileManager.default.isExecutableFile(atPath: url.path) {
39+
if checkCandidate(url) {
3540
return url
3641
}
3742
}

Plugins/PackageToJS/Sources/PackageToJS.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,17 @@ final class DefaultPackagingSystem: PackagingSystem {
317317
}
318318

319319
internal func which(_ executable: String) throws -> URL {
320+
func checkCandidate(_ candidate: URL) -> Bool {
321+
var isDirectory: ObjCBool = false
322+
let fileExists = FileManager.default.fileExists(atPath: candidate.path, isDirectory: &isDirectory)
323+
return fileExists && !isDirectory.boolValue && FileManager.default.isExecutableFile(atPath: candidate.path)
324+
}
320325
do {
321326
// Check overriding environment variable
322327
let envVariable = executable.uppercased().replacingOccurrences(of: "-", with: "_") + "_PATH"
323328
if let path = ProcessInfo.processInfo.environment[envVariable] {
324329
let url = URL(fileURLWithPath: path).appendingPathComponent(executable)
325-
if FileManager.default.isExecutableFile(atPath: url.path) {
330+
if checkCandidate(url) {
326331
return url
327332
}
328333
}
@@ -336,7 +341,7 @@ internal func which(_ executable: String) throws -> URL {
336341
let paths = ProcessInfo.processInfo.environment["PATH"]!.split(separator: pathSeparator)
337342
for path in paths {
338343
let url = URL(fileURLWithPath: String(path)).appendingPathComponent(executable)
339-
if FileManager.default.isExecutableFile(atPath: url.path) {
344+
if checkCandidate(url) {
340345
return url
341346
}
342347
}

0 commit comments

Comments
 (0)