Skip to content

Commit fb00ef6

Browse files
committed
Remove workaround for executable files on Windows
Use our FSProxy abstraction instead of Foundation, which doesn't suffer from swiftlang/swift-foundation#860
1 parent 0a6b2d5 commit fb00ef6

File tree

4 files changed

+5
-38
lines changed

4 files changed

+5
-38
lines changed

Sources/SWBTestSupport/Misc.swift

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -196,36 +196,6 @@ extension ProcessInfo {
196196
return false
197197
}
198198

199-
/// Whether the process is running translated, e.g. using Rosetta on macOS, or Prism on Windows.
200-
package var isTranslated: Bool {
201-
#if os(Windows)
202-
var pProcessMachine = USHORT(IMAGE_FILE_MACHINE_UNKNOWN)
203-
var pNativeMachine = USHORT(IMAGE_FILE_MACHINE_UNKNOWN)
204-
guard IsWow64Process2(GetCurrentProcess(), &pProcessMachine, &pNativeMachine) else {
205-
return false
206-
}
207-
// Checking of specific architectures is a bit fragile, but we only support AMD64 and ARM64 on Windows for now.
208-
guard pNativeMachine == IMAGE_FILE_MACHINE_ARM64 else {
209-
return false
210-
}
211-
var systemInfo = SYSTEM_INFO()
212-
GetNativeSystemInfo(&systemInfo)
213-
return systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64
214-
#elseif canImport(Darwin)
215-
let sysctl_proc_translated = "sysctl.proc_translated"
216-
var len: Int = 0
217-
if sysctlbyname(sysctl_proc_translated, nil, &len, nil, 0) == 0 {
218-
var p = [CChar](repeating: 0, count: len)
219-
if len > 0 && sysctlbyname(sysctl_proc_translated, &p, &len, nil, 0) == 0 {
220-
return p[0] == 1
221-
}
222-
}
223-
return false
224-
#else
225-
return false
226-
#endif
227-
}
228-
229199
// Get memory usage of current process in bytes
230200
package var memoryUsage: UInt64 {
231201
#if canImport(Darwin)

Sources/SWBUtil/FSProxy.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public struct FileInfo: Equatable, Sendable {
7171

7272
public var isExecutable: Bool {
7373
#if os(Windows)
74+
// Per https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions, "user execute bits are set according to the filename extension".
75+
// Don't use FileManager.isExecutableFile due to https://github.com/swiftlang/swift-foundation/issues/860
7476
return (statBuf.st_mode & UInt16(_S_IEXEC)) != 0
7577
#else
7678
return (statBuf.st_mode & S_IXUSR) != 0
@@ -148,6 +150,7 @@ public protocol FSProxy: AnyObject, Sendable {
148150
// FIXME: Need to document behavior w.r.t. error handling.
149151
func isDirectory(_ path: Path) -> Bool
150152

153+
/// Checks whether the given path has the execute bit (which on Windows is determined by the file extension).
151154
func isExecutable(_ path: Path) throws -> Bool
152155

153156
/// Checks whether the given path is a symlink, also returning whether the linked file exists.

Sources/SWBUtil/Process.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ extension Process {
129129
throw StubError.error("\(url) is not an absolute file URL")
130130
}
131131
let executableFilePath = try url.standardizedFileURL.filePath
132-
if !FileManager.default.isExecutableFile(atPath: executableFilePath.str) {
132+
if try !localFS.isExecutable(executableFilePath) {
133133
throw StubError.error("\(executableFilePath.str) is not an executable file")
134134
}
135135
let process = Process()

Tests/SWBUtilTests/ProcessTests.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,7 @@ extension SWBUtil.Process {
226226
let hostOS = try ProcessInfo.processInfo.hostOperatingSystem()
227227
let scriptString = try await script(hostOS)
228228
if hostOS == .windows {
229-
// https://github.com/apple/swift-foundation/issues/860
230-
if ProcessInfo.processInfo.isTranslated {
231-
let systemRoot = try #require(getEnvironmentVariable("SystemRoot"), "Can't determine path to cmd.exe because the SystemRoot environment variable is not set")
232-
commandShellPath = "\(systemRoot)\\SysWOW64\\cmd.exe"
233-
} else {
234-
commandShellPath = try #require(getEnvironmentVariable("ComSpec"), "Can't determine path to cmd.exe because the ComSpec environment variable is not set")
235-
}
229+
commandShellPath = try #require(getEnvironmentVariable("ComSpec"), "Can't determine path to cmd.exe because the ComSpec environment variable is not set")
236230
arguments = ["/c", scriptString]
237231
} else {
238232
commandShellPath = "/bin/sh"

0 commit comments

Comments
 (0)