Skip to content

Commit 73cec82

Browse files
authored
Merge pull request #1272 from ahoppen/working-directory
Fall back to launching subprocesses without a working directory
2 parents 80694a3 + a1d10d8 commit 73cec82

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

Sources/SKSupport/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_library(SKSupport STATIC
1111
DocumentURI+CustomLogStringConvertible.swift
1212
FileSystem.swift
1313
LineTable.swift
14+
Process+LaunchWithWorkingDirectoryIfPossible.swift
1415
Process+WaitUntilExitWithCancellation.swift
1516
Random.swift
1617
Result.swift
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import LSPLogging
14+
15+
import struct TSCBasic.AbsolutePath
16+
import class TSCBasic.Process
17+
import enum TSCBasic.ProcessEnv
18+
import struct TSCBasic.ProcessEnvironmentBlock
19+
20+
extension Process {
21+
/// Launches a new process with the given parameters.
22+
///
23+
/// - Important: If `workingDirectory` is not supported on this platform, this logs an error and falls back to launching the
24+
/// process without the working directory set.
25+
public static func launch(
26+
arguments: [String],
27+
environmentBlock: ProcessEnvironmentBlock = ProcessEnv.block,
28+
workingDirectory: AbsolutePath?,
29+
outputRedirection: OutputRedirection = .collect,
30+
startNewProcessGroup: Bool = true,
31+
loggingHandler: LoggingHandler? = .none
32+
) throws -> Process {
33+
let process =
34+
if let workingDirectory {
35+
Process(
36+
arguments: arguments,
37+
environmentBlock: environmentBlock,
38+
workingDirectory: workingDirectory,
39+
outputRedirection: outputRedirection,
40+
startNewProcessGroup: startNewProcessGroup,
41+
loggingHandler: loggingHandler
42+
)
43+
} else {
44+
self.init(
45+
arguments: arguments,
46+
environmentBlock: environmentBlock,
47+
outputRedirection: outputRedirection,
48+
startNewProcessGroup: startNewProcessGroup,
49+
loggingHandler: loggingHandler
50+
)
51+
}
52+
do {
53+
try process.launch()
54+
} catch Process.Error.workingDirectoryNotSupported where workingDirectory != nil {
55+
// TODO (indexing): We need to figure out how to set the working directory on all platforms.
56+
logger.error(
57+
"Working directory not supported on the platform. Launching process without working directory \(workingDirectory!.pathString)"
58+
)
59+
return try Process.launch(
60+
arguments: arguments,
61+
environmentBlock: environmentBlock,
62+
workingDirectory: nil,
63+
outputRedirection: outputRedirection,
64+
startNewProcessGroup: startNewProcessGroup,
65+
loggingHandler: loggingHandler
66+
)
67+
}
68+
return process
69+
}
70+
}

Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,10 @@ public struct UpdateIndexStoreTaskDescription: TaskDescriptionProtocol {
188188
return
189189
}
190190

191-
let process =
192-
if let workingDirectory = buildSettings.workingDirectory {
193-
Process(
194-
arguments: [swiftc.pathString] + indexingArguments,
195-
workingDirectory: try AbsolutePath(validating: workingDirectory)
196-
)
197-
} else {
198-
Process(arguments: [swiftc.pathString] + indexingArguments)
199-
}
200-
try process.launch()
191+
let process = try Process.launch(
192+
arguments: [swiftc.pathString] + indexingArguments,
193+
workingDirectory: buildSettings.workingDirectory.map(AbsolutePath.init(validating:))
194+
)
201195
let result = try await process.waitUntilExitSendingSigIntOnTaskCancellation()
202196
switch result.exitStatus.exhaustivelySwitchable {
203197
case .terminated(code: 0):

0 commit comments

Comments
 (0)