Skip to content

Commit 071939b

Browse files
committed
Start adopting the new Environment type throughout the codebase
Mostly starting at the "edges", in tests and in some central APIs. Does nothing for Unix-like platforms, but will fix some case sensitivity issues on Windows when accessing specific environment variables. Closes #296
1 parent 7f75d72 commit 071939b

31 files changed

+124
-84
lines changed

Sources/SWBBuildService/Messages.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ extension SetSessionWorkspaceContainerPathRequest: PIFProvidingRequest {
184184
try fs.createDirectory(dir, recursive: true)
185185
let pifPath = dir.join(Foundation.UUID().description + ".json")
186186
let argument = isProject ? "-project" : "-workspace"
187-
let result = try await Process.getOutput(url: URL(fileURLWithPath: "/usr/bin/xcrun"), arguments: ["xcodebuild", "-dumpPIF", pifPath.str, argument, path.str], currentDirectoryURL: URL(fileURLWithPath: containerPath.dirname.str, isDirectory: true), environment: ProcessInfo.processInfo.cleanEnvironment.merging(["DEVELOPER_DIR": session.core.developerPath.str], uniquingKeysWith: { _, new in new }))
187+
let result = try await Process.getOutput(url: URL(fileURLWithPath: "/usr/bin/xcrun"), arguments: ["xcodebuild", "-dumpPIF", pifPath.str, argument, path.str], currentDirectoryURL: URL(fileURLWithPath: containerPath.dirname.str, isDirectory: true), environment: Environment.current.addingContents(of: [.developerDir: session.core.developerPath.str]))
188188
if !result.exitStatus.isSuccess {
189189
throw StubError.error("Could not dump PIF for '\(path.str)': \(String(decoding: result.stderr, as: UTF8.self))")
190190
}

Sources/SWBBuildSystem/CleanOperation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ package final class CleanOperation: BuildSystemOperation, TargetDependencyResolv
214214
let taskIdentifier = task.identifier
215215
let taskOutputDelegate = delegate.taskStarted(self, taskIdentifier: taskIdentifier, task: task, dependencyInfo: nil)
216216

217-
let resolvedExecutable = StackedSearchPath(environment: environment, fs: workspaceContext.fs).lookup(Path(executable)) ?? Path(executable)
217+
let resolvedExecutable = StackedSearchPath(environment: .init(environment), fs: workspaceContext.fs).lookup(Path(executable)) ?? Path(executable)
218218

219219
do {
220-
let result = try await Process.getMergedOutput(url: URL(fileURLWithPath: resolvedExecutable.str), arguments: arguments, currentDirectoryURL: URL(fileURLWithPath: workingDirectory.str), environment: environment)
220+
let result = try await Process.getMergedOutput(url: URL(fileURLWithPath: resolvedExecutable.str), arguments: arguments, currentDirectoryURL: URL(fileURLWithPath: workingDirectory.str), environment: .init(environment))
221221

222222
if !result.exitStatus.isSuccess {
223223
taskOutputDelegate.emitError("Failed to clean target '\(configuredTarget.target.name)': \(String(decoding: result.output, as: UTF8.self))")

Sources/SWBCore/SWBFeatureFlag.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ public struct SWBFeatureFlagProperty {
3535

3636
/// Whether the feature flag is actually set at all.
3737
public var hasValue: Bool {
38-
return SWBUtil.UserDefaults.hasValue(forKey: key) || getEnvironmentVariable(key) != nil
38+
return SWBUtil.UserDefaults.hasValue(forKey: key) || getEnvironmentVariable(EnvironmentKey(key)) != nil
3939
}
4040

4141
/// Indicates whether the feature flag is currently active in the calling environment.
4242
public var value: Bool {
4343
if !hasValue {
4444
return defaultValue
4545
}
46-
return SWBUtil.UserDefaults.bool(forKey: key) || getEnvironmentVariable(key)?.boolValue == true
46+
return SWBUtil.UserDefaults.bool(forKey: key) || getEnvironmentVariable(EnvironmentKey(key))?.boolValue == true
4747
}
4848

4949
fileprivate init(_ key: String, defaultValue: Bool = false) {
@@ -59,7 +59,7 @@ public struct SWBOptionalFeatureFlagProperty {
5959
/// Returns nil if neither environment variable nor User Default are set. An implementation can then pick a default behavior.
6060
/// If both the environment variable and User Default are set, the two values are logically AND'd together; this allows the set false value of either to force the feature flag off.
6161
public var value: Bool? {
62-
let envValue = getEnvironmentVariable(key)
62+
let envValue = getEnvironmentVariable(EnvironmentKey(key))
6363
let envHasValue = envValue != nil
6464
let defHasValue = SWBUtil.UserDefaults.hasValue(forKey: key)
6565
if !envHasValue && !defHasValue {

Sources/SWBCore/Settings/StackedSearchPaths.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public final class StackedSearchPath: Sendable {
3333
self.fs = fs
3434
}
3535

36-
public init(environment: [String: String], fs: any FSProxy) {
37-
self.paths = environment["PATH"]?.split(separator: Path.pathEnvironmentSeparator).map(Path.init) ?? []
36+
public init(environment: Environment, fs: any FSProxy) {
37+
self.paths = environment[.path]?.split(separator: Path.pathEnvironmentSeparator).map(Path.init) ?? []
3838
self.fs = fs
3939
}
4040

Sources/SWBCore/SpecImplementations/CommandLineToolSpec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ open class CommandLineToolSpec : PropertyDomainSpec, SpecType, TaskTypeDescripti
13901390
public func executeExternalTool<T>(_ cbc: CommandBuildContext, _ delegate: any TaskGenerationDelegate, commandLine: [String], workingDirectory: String?, environment: [String: String], executionDescription: String?, _ parse: @escaping (ByteString) throws -> T) async throws -> T {
13911391
let executionResult = try await delegate.executeExternalTool(commandLine: commandLine, workingDirectory: workingDirectory, environment: environment, executionDescription: executionDescription)
13921392
guard executionResult.exitStatus.isSuccess else {
1393-
throw RunProcessNonZeroExitError(args: commandLine, workingDirectory: workingDirectory, environment: environment, status: executionResult.exitStatus, stdout: ByteString(executionResult.stdout), stderr: ByteString(executionResult.stderr))
1393+
throw RunProcessNonZeroExitError(args: commandLine, workingDirectory: workingDirectory, environment: .init(environment), status: executionResult.exitStatus, stdout: ByteString(executionResult.stdout), stderr: ByteString(executionResult.stderr))
13941394
}
13951395
return try parse(ByteString(executionResult.stdout))
13961396
}

Sources/SWBCore/TaskGeneration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ extension CoreClientDelegate {
781781
}
782782

783783
return try await externalToolExecutionQueue.withOperation {
784-
try await Process.getOutput(url: url, arguments: Array(commandLine.dropFirst()), currentDirectoryURL: workingDirectory.map(URL.init(fileURLWithPath:)), environment: ProcessInfo.processInfo.cleanEnvironment.merging(environment, uniquingKeysWith: { _, new in new }))
784+
try await Process.getOutput(url: url, arguments: Array(commandLine.dropFirst()), currentDirectoryURL: workingDirectory.map(URL.init(fileURLWithPath:)), environment: Environment.current.addingContents(of: .init(environment)))
785785
}
786786
case let .result(status, stdout, stderr):
787787
return Processes.ExecutionResult(exitStatus: status, stdout: stdout, stderr: stderr)

Sources/SWBCore/ToolchainRegistry.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ public final class ToolchainRegistry: @unchecked Sendable {
472472
return
473473
}
474474

475-
if let swift = StackedSearchPath(environment: ProcessInfo.processInfo.cleanEnvironment, fs: fs).lookup(Path("swift")), fs.exists(swift) {
475+
if let swift = StackedSearchPath(environment: .current, fs: fs).lookup(Path("swift")), fs.exists(swift) {
476476
let hasUsrBin = swift.normalize().str.hasSuffix("/usr/bin/swift")
477477
let hasUsrLocalBin = swift.normalize().str.hasSuffix("/usr/local/bin/swift")
478478
let path: Path

Sources/SWBQNXPlatform/Plugin.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct QNXEnvironmentExtension: EnvironmentExtension {
4646

4747
func additionalEnvironmentVariables(context: any EnvironmentExtensionAdditionalEnvironmentVariablesContext) async throws -> [String : String] {
4848
if let latest = try await plugin.cachedQNXSDPInstallations(host: context.hostOperatingSystem).first {
49-
return latest.environment
49+
return .init(latest.environment)
5050
}
5151
return [:]
5252
}

Sources/SWBQNXPlatform/QNXSDP.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ struct QNXSDP: Sendable {
2828
self.sysroot = sysroot
2929
self.configurationPath = configurationPath
3030

31-
let environment = [
31+
var environment: Environment = [
3232
"QNX_TARGET": sysroot.str,
33-
"QNX_HOST": hostPath?.str,
3433
"QNX_CONFIGURATION_EXCLUSIVE": configurationPath.str,
35-
].compactMapValues { $0 }
34+
]
35+
if let hostPath {
36+
environment["QNX_HOST"] = hostPath.str
37+
}
3638
self.environment = environment
3739

3840
self.version = try await {
@@ -60,7 +62,7 @@ struct QNXSDP: Sendable {
6062
/// Equivalent to `QNX_HOST`.
6163
public let hostPath: Path?
6264

63-
public let environment: [String: String]
65+
public let environment: Environment
6466

6567
private static func hostPath(host: OperatingSystem, path: Path) -> Path? {
6668
switch host {

Sources/SWBTaskExecution/TaskActions/EmbedSwiftStdLibTaskAction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ public final class EmbedSwiftStdLibTaskAction: TaskAction {
525525
}
526526

527527
guard !failed else {
528-
throw RunProcessNonZeroExitError(args: args, workingDirectory: task.workingDirectory.str, environment: effectiveEnvironment, status: {
528+
throw RunProcessNonZeroExitError(args: args, workingDirectory: task.workingDirectory.str, environment: .init(effectiveEnvironment), status: {
529529
if case let .exit(exitStatus, _) = processDelegate.outputDelegate.result {
530530
return exitStatus
531531
}

0 commit comments

Comments
 (0)