diff --git a/Sources/TSCBasic/Process.swift b/Sources/TSCBasic/Process.swift index 1262b1ac..e13fd2de 100644 --- a/Sources/TSCBasic/Process.swift +++ b/Sources/TSCBasic/Process.swift @@ -351,35 +351,30 @@ public final class Process { pathString: ProcessEnv.path, currentWorkingDirectory: cwdOpt ) + var searchPaths: [AbsolutePath] = [] #if os(Windows) - var searchPaths = Array() var buffer = Array(repeating: 0, count: Int(MAX_PATH + 1)) - // The 32-bit Windows system directory - GetSystemDirectoryW(&buffer, .init(MAX_PATH + 1)) - searchPaths.append(AbsolutePath(String(decodingCString: buffer, as: UTF16.self))) - + if GetSystemDirectoryW(&buffer, .init(buffer.count)) > 0 { + searchPaths.append(AbsolutePath(String(decodingCString: buffer, as: UTF16.self))) + } // The 16-bit Windows system directory if let systemDrive = ProcessEnv.vars["systemdrive"], let systemPath = try? AbsolutePath(validating: "\(systemDrive))\\System") { searchPaths.append(systemPath) } - // The Windows directory - GetWindowsDirectoryW(&buffer, .init(MAX_PATH + 1)) - searchPaths.append(AbsolutePath(String(decodingCString: buffer, as: UTF16.self))) - - searchPaths.append(contentsOf: envSearchPaths) -#else - let searchPaths = envSearchPaths + if GetWindowsDirectoryW(&buffer, .init(buffer.count)) > 0 { + searchPaths.append(AbsolutePath(String(decodingCString: buffer, as: UTF16.self))) + } #endif + searchPaths.append(contentsOf: envSearchPaths) // Lookup and cache the executable path. let value = lookupExecutablePath( filename: program, currentWorkingDirectory: cwdOpt, searchPaths: searchPaths ) - Process.validatedExecutablesMap[program] = value return value } // This should cover the most common cases, i.e. when the cache is most helpful.