Skip to content

Commit 2f18517

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 2f18517

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

Plugins/BridgeJS/Sources/TS2Skeleton/TS2Skeleton.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,25 @@
55
@preconcurrency import class Foundation.FileManager
66
@preconcurrency import struct Foundation.URL
77
@preconcurrency import struct Foundation.Data
8+
@preconcurrency import struct Foundation.ObjCBool
89
@preconcurrency import func Foundation.kill
910
@preconcurrency import var Foundation.SIGINT
1011
@preconcurrency import var Foundation.SIGTERM
1112
import protocol Dispatch.DispatchSourceSignal
1213
import class Dispatch.DispatchSource
1314

1415
internal func which(_ executable: String) throws -> URL {
16+
func checkCandidate(_ candidate: URL) -> Bool {
17+
var isDirectory: ObjCBool = false
18+
let fileExists = FileManager.default.fileExists(atPath: candidate.path, isDirectory: &isDirectory)
19+
return fileExists && !isDirectory.boolValue && FileManager.default.isExecutableFile(atPath: candidate.path)
20+
}
1521
do {
1622
// Check overriding environment variable
1723
let envVariable = executable.uppercased().replacingOccurrences(of: "-", with: "_") + "_PATH"
1824
if let path = ProcessInfo.processInfo.environment[envVariable] {
1925
let url = URL(fileURLWithPath: path).appendingPathComponent(executable)
20-
if FileManager.default.isExecutableFile(atPath: url.path) {
26+
if checkCandidate(url) {
2127
return url
2228
}
2329
}
@@ -31,7 +37,7 @@ internal func which(_ executable: String) throws -> URL {
3137
let paths = ProcessInfo.processInfo.environment["PATH"]!.split(separator: pathSeparator)
3238
for path in paths {
3339
let url = URL(fileURLWithPath: String(path)).appendingPathComponent(executable)
34-
if FileManager.default.isExecutableFile(atPath: url.path) {
40+
if checkCandidate(url) {
3541
return url
3642
}
3743
}

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)