Skip to content

Fall back to launching subprocesses without a working directory #1272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/SKSupport/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_library(SKSupport STATIC
DocumentURI+CustomLogStringConvertible.swift
FileSystem.swift
LineTable.swift
Process+LaunchWithWorkingDirectoryIfPossible.swift
Process+WaitUntilExitWithCancellation.swift
Random.swift
Result.swift
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import LSPLogging

import struct TSCBasic.AbsolutePath
import class TSCBasic.Process
import enum TSCBasic.ProcessEnv
import struct TSCBasic.ProcessEnvironmentBlock

extension Process {
/// Launches a new process with the given parameters.
///
/// - Important: If `workingDirectory` is not supported on this platform, this logs an error and falls back to launching the
/// process without the working directory set.
public static func launch(
arguments: [String],
environmentBlock: ProcessEnvironmentBlock = ProcessEnv.block,
workingDirectory: AbsolutePath?,
outputRedirection: OutputRedirection = .collect,
startNewProcessGroup: Bool = true,
loggingHandler: LoggingHandler? = .none
) throws -> Process {
let process =
if let workingDirectory {
Process(
arguments: arguments,
environmentBlock: environmentBlock,
workingDirectory: workingDirectory,
outputRedirection: outputRedirection,
startNewProcessGroup: startNewProcessGroup,
loggingHandler: loggingHandler
)
} else {
self.init(
arguments: arguments,
environmentBlock: environmentBlock,
outputRedirection: outputRedirection,
startNewProcessGroup: startNewProcessGroup,
loggingHandler: loggingHandler
)
}
do {
try process.launch()
} catch Process.Error.workingDirectoryNotSupported where workingDirectory != nil {
// TODO (indexing): We need to figure out how to set the working directory on all platforms.
logger.error(
"Working directory not supported on the platform. Launching process without working directory \(workingDirectory!.pathString)"
)
return try Process.launch(
arguments: arguments,
environmentBlock: environmentBlock,
workingDirectory: nil,
outputRedirection: outputRedirection,
startNewProcessGroup: startNewProcessGroup,
loggingHandler: loggingHandler
)
}
return process
}
}
14 changes: 4 additions & 10 deletions Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,10 @@ public struct UpdateIndexStoreTaskDescription: TaskDescriptionProtocol {
return
}

let process =
if let workingDirectory = buildSettings.workingDirectory {
Process(
arguments: [swiftc.pathString] + indexingArguments,
workingDirectory: try AbsolutePath(validating: workingDirectory)
)
} else {
Process(arguments: [swiftc.pathString] + indexingArguments)
}
try process.launch()
let process = try Process.launch(
arguments: [swiftc.pathString] + indexingArguments,
workingDirectory: buildSettings.workingDirectory.map(AbsolutePath.init(validating:))
)
let result = try await process.waitUntilExitSendingSigIntOnTaskCancellation()
switch result.exitStatus.exhaustivelySwitchable {
case .terminated(code: 0):
Expand Down