Skip to content
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

Replace ProcessEnv.vars with block w/o API breakage #7390

Merged
merged 2 commits into from Mar 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Basics/Concurrency/ConcurrencyHelpers.swift
Expand Up @@ -20,7 +20,7 @@ import func TSCBasic.tsc_await

public enum Concurrency {
public static var maxOperations: Int {
ProcessEnv.vars["SWIFTPM_MAX_CONCURRENT_OPERATIONS"].flatMap(Int.init) ?? ProcessInfo.processInfo
ProcessEnv.block["SWIFTPM_MAX_CONCURRENT_OPERATIONS"].flatMap(Int.init) ?? ProcessInfo.processInfo
.activeProcessorCount
}
}
Expand Down
22 changes: 14 additions & 8 deletions Sources/Build/BuildPlan/BuildPlan.swift
Expand Up @@ -95,7 +95,7 @@ extension BuildParameters {
get throws {
// FIXME: We use this hack to let swiftpm's functional test use shared
// cache so it doesn't become painfully slow.
if let path = ProcessEnv.vars["SWIFTPM_TESTS_MODULECACHE"] {
if let path = ProcessEnv.block["SWIFTPM_TESTS_MODULECACHE"] {
return try AbsolutePath(validating: path)
}
return buildPath.appending("ModuleCache")
Expand Down Expand Up @@ -158,7 +158,7 @@ extension BuildParameters {

/// Returns the scoped view of build settings for a given target.
func createScope(for target: ResolvedTarget) -> BuildSettings.Scope {
return BuildSettings.Scope(target.underlying.buildSettings, environment: buildEnvironment)
BuildSettings.Scope(target.underlying.buildSettings, environment: buildEnvironment)
}
}

Expand Down Expand Up @@ -446,7 +446,8 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
for item in derivedTestTargets {
var derivedTestTargets = [item.entryPointTargetBuildDescription.target]

targetMap[item.entryPointTargetBuildDescription.target.id] = .swift(item.entryPointTargetBuildDescription)
targetMap[item.entryPointTargetBuildDescription.target.id] =
.swift(item.entryPointTargetBuildDescription)

if let discoveryTargetBuildDescription = item.discoveryTargetBuildDescription {
targetMap[discoveryTargetBuildDescription.target.id] = .swift(discoveryTargetBuildDescription)
Expand Down Expand Up @@ -552,9 +553,9 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
}

// Add search paths from the system library targets.
for target in graph.reachableTargets {
for target in self.graph.reachableTargets {
if let systemLib = target.underlying as? SystemLibraryTarget {
arguments.append(contentsOf: try self.pkgConfig(for: systemLib).cFlags)
try arguments.append(contentsOf: self.pkgConfig(for: systemLib).cFlags)
// Add the path to the module map.
arguments += ["-I", systemLib.moduleMapPath.parentDirectory.pathString]
}
Expand Down Expand Up @@ -589,7 +590,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
}

// Add search paths from the system library targets.
for target in graph.reachableTargets {
for target in self.graph.reachableTargets {
if let systemLib = target.underlying as? SystemLibraryTarget {
arguments += try self.pkgConfig(for: systemLib).cFlags
}
Expand Down Expand Up @@ -645,7 +646,12 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
extension Basics.Diagnostic {
static var swiftBackDeployError: Self {
.warning(
"Swift compiler no longer supports statically linking the Swift libraries. They're included in the OS by default starting with macOS Mojave 10.14.4 beta 3. For macOS Mojave 10.14.3 and earlier, there's an optional Swift library package that can be downloaded from \"More Downloads\" for Apple Developers at https://developer.apple.com/download/more/"
"""
Swift compiler no longer supports statically linking the Swift libraries. They're included in the OS by \
default starting with macOS Mojave 10.14.4 beta 3. For macOS Mojave 10.14.3 and earlier, there's an \
optional Swift library package that can be downloaded from \"More Downloads\" for Apple Developers at \
https://developer.apple.com/download/more/
"""
)
}

Expand Down Expand Up @@ -728,7 +734,7 @@ extension ResolvedProduct {
}

private var isBinaryOnly: Bool {
return self.targets.filter({ !($0.underlying is BinaryTarget) }).isEmpty
self.targets.filter { !($0.underlying is BinaryTarget) }.isEmpty
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved
}

private var isPlugin: Bool {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Commands/PackageCommands/Format.swift
Expand Up @@ -37,7 +37,7 @@ extension SwiftPackageCommand {
func run(_ swiftCommandState: SwiftCommandState) async throws {
// Look for swift-format binary.
// FIXME: This should be moved to user toolchain.
let swiftFormatInEnv = lookupExecutablePath(filename: ProcessEnv.vars["SWIFT_FORMAT"])
let swiftFormatInEnv = lookupExecutablePath(filename: ProcessEnv.block["SWIFT_FORMAT"])
guard let swiftFormat = swiftFormatInEnv ?? Process.findExecutable("swift-format").flatMap(AbsolutePath.init) else {
swiftCommandState.observabilityScope.emit(error: "Could not find swift-format in PATH or SWIFT_FORMAT")
throw TSCUtility.Diagnostics.fatalError
Expand Down
4 changes: 2 additions & 2 deletions Sources/Commands/SwiftTestCommand.swift
Expand Up @@ -933,7 +933,7 @@ final class ParallelTestRunner {

// command's result output goes on stdout
// ie "swift test" should output to stdout
if ProcessEnv.vars["SWIFTPM_TEST_RUNNER_PROGRESS_BAR"] == "lit" {
if ProcessEnv.block["SWIFTPM_TEST_RUNNER_PROGRESS_BAR"] == "lit" {
self.progressAnimation = ProgressAnimation.percent(
stream: TSCBasic.stdoutStream,
verbose: false,
Expand Down Expand Up @@ -1292,7 +1292,7 @@ extension TestCommandOptions {

/// Returns the test case specifier if overridden in the env.
private func skippedTestsOverride(fileSystem: FileSystem) -> TestCaseSpecifier? {
guard let override = ProcessEnv.vars["_SWIFTPM_SKIP_TESTS_LIST"] else {
guard let override = ProcessEnv.block["_SWIFTPM_SKIP_TESTS_LIST"] else {
return nil
}

Expand Down
16 changes: 13 additions & 3 deletions Sources/PackageLoading/ManifestLoader.swift
Expand Up @@ -857,7 +857,9 @@ public final class ManifestLoader: ManifestLoaderProtocol {

// FIXME: Workaround for the module cache bug that's been haunting Swift CI
// <rdar://problem/48443680>
let moduleCachePath = try (ProcessEnv.vars["SWIFTPM_MODULECACHE_OVERRIDE"] ?? ProcessEnv.vars["SWIFTPM_TESTS_MODULECACHE"]).flatMap{ try AbsolutePath(validating: $0) }
let moduleCachePath = try (
ProcessEnv.block["SWIFTPM_MODULECACHE_OVERRIDE"] ??
ProcessEnv.block["SWIFTPM_TESTS_MODULECACHE"]).flatMap { try AbsolutePath(validating: $0) }

var cmd: [String] = []
cmd += [self.toolchain.swiftCompilerPathForManifests.pathString]
Expand Down Expand Up @@ -955,7 +957,11 @@ public final class ManifestLoader: ManifestLoaderProtocol {
evaluationResult.compilerCommandLine = cmd

// Compile the manifest.
TSCBasic.Process.popen(arguments: cmd, environment: self.toolchain.swiftCompilerEnvironment, queue: callbackQueue) { result in
TSCBasic.Process.popen(
arguments: cmd,
environment: self.toolchain.swiftCompilerEnvironment,
queue: callbackQueue
) { result in
dispatchPrecondition(condition: .onQueue(callbackQueue))

var cleanupIfError = DelayableAction(target: tmpDir, action: cleanupTmpDir)
Expand Down Expand Up @@ -1054,7 +1060,11 @@ public final class ManifestLoader: ManifestLoaderProtocol {
#endif

let cleanupAfterRunning = cleanupIfError.delay()
TSCBasic.Process.popen(arguments: cmd, environment: environment, queue: callbackQueue) { result in
TSCBasic.Process.popen(
arguments: cmd,
environment: environment,
queue: callbackQueue
) { result in
dispatchPrecondition(condition: .onQueue(callbackQueue))

defer { cleanupAfterRunning.perform() }
Expand Down
4 changes: 2 additions & 2 deletions Sources/PackageLoading/PkgConfig.swift
Expand Up @@ -126,7 +126,7 @@ public struct PkgConfig {

private static var envSearchPaths: [AbsolutePath] {
get throws {
if let configPath = ProcessEnv.vars["PKG_CONFIG_PATH"] {
if let configPath = ProcessEnv.block["PKG_CONFIG_PATH"] {
#if os(Windows)
return try configPath.split(separator: ";").map({ try AbsolutePath(validating: String($0)) })
#else
Expand Down Expand Up @@ -183,7 +183,7 @@ internal struct PkgConfigParser {
variables["pcfiledir"] = pcFile.parentDirectory.pathString

// Add pc_sysrootdir variable. This is the path of the sysroot directory for pc files.
variables["pc_sysrootdir"] = ProcessEnv.vars["PKG_CONFIG_SYSROOT_DIR"] ?? AbsolutePath.root.pathString
variables["pc_sysrootdir"] = ProcessEnv.block["PKG_CONFIG_SYSROOT_DIR"] ?? AbsolutePath.root.pathString

let fileContents: String = try fileSystem.readFileContents(pcFile)
for line in fileContents.components(separatedBy: "\n") {
Expand Down
6 changes: 3 additions & 3 deletions Sources/PackageModel/SwiftSDKs/SwiftSDK.swift
Expand Up @@ -465,10 +465,10 @@ public struct SwiftSDK: Equatable {
) throws -> SwiftSDK {
let originalWorkingDirectory = originalWorkingDirectory ?? localFileSystem.currentWorkingDirectory
// Select the correct binDir.
if ProcessEnv.vars["SWIFTPM_CUSTOM_BINDIR"] != nil {
if ProcessEnv.block["SWIFTPM_CUSTOM_BINDIR"] != nil {
print("SWIFTPM_CUSTOM_BINDIR was deprecated in favor of SWIFTPM_CUSTOM_BIN_DIR")
}
let customBinDir = (ProcessEnv.vars["SWIFTPM_CUSTOM_BIN_DIR"] ?? ProcessEnv.vars["SWIFTPM_CUSTOM_BINDIR"])
let customBinDir = (ProcessEnv.block["SWIFTPM_CUSTOM_BIN_DIR"] ?? ProcessEnv.block["SWIFTPM_CUSTOM_BINDIR"])
.flatMap { try? AbsolutePath(validating: $0) }
let binDir = try customBinDir ?? binDir ?? SwiftSDK.hostBinDir(
fileSystem: localFileSystem,
Expand All @@ -478,7 +478,7 @@ public struct SwiftSDK: Equatable {
let sdkPath: AbsolutePath?
#if os(macOS)
// Get the SDK.
if let value = ProcessEnv.vars["SDKROOT"] {
if let value = ProcessEnv.block["SDKROOT"] {
sdkPath = try AbsolutePath(validating: value)
} else {
// No value in env, so search for it.
Expand Down
4 changes: 2 additions & 2 deletions Sources/SPMBuildCore/BuildSystem/BuildSystem.swift
Expand Up @@ -175,8 +175,8 @@ package enum BuildSystemUtilities {
/// Returns the build path from the environment, if present.
public static func getEnvBuildPath(workingDir: AbsolutePath) throws -> AbsolutePath? {
// Don't rely on build path from env for SwiftPM's own tests.
guard ProcessEnv.vars["SWIFTPM_TESTS_MODULECACHE"] == nil else { return nil }
guard let env = ProcessEnv.vars["SWIFTPM_BUILD_DIR"] else { return nil }
guard ProcessEnv.block["SWIFTPM_TESTS_MODULECACHE"] == nil else { return nil }
guard let env = ProcessEnv.block["SWIFTPM_BUILD_DIR"] else { return nil }
return try AbsolutePath(validating: env, relativeTo: workingDir)
}
}
2 changes: 1 addition & 1 deletion Sources/SourceControl/RepositoryManager.swift
Expand Up @@ -331,7 +331,7 @@ public class RepositoryManager: Cancellable {
}

// We are expecting handle.repository.url to always be a resolved absolute path.
let shouldCacheLocalPackages = ProcessEnv.vars["SWIFTPM_TESTS_PACKAGECACHE"] == "1" || cacheLocalPackages
let shouldCacheLocalPackages = ProcessEnv.block["SWIFTPM_TESTS_PACKAGECACHE"] == "1" || cacheLocalPackages

if let cachePath, !(handle.repository.isLocal && !shouldCacheLocalPackages) {
let cachedRepositoryPath = try cachePath.appending(handle.repository.storagePath())
Expand Down
2 changes: 1 addition & 1 deletion Sources/Workspace/DefaultPluginScriptRunner.swift
Expand Up @@ -209,7 +209,7 @@ public struct DefaultPluginScriptRunner: PluginScriptRunner, Cancellable {
#endif

// Honor any module cache override that's set in the environment.
let moduleCachePath = ProcessEnv.vars["SWIFTPM_MODULECACHE_OVERRIDE"] ?? ProcessEnv.vars["SWIFTPM_TESTS_MODULECACHE"]
let moduleCachePath = ProcessEnv.block["SWIFTPM_MODULECACHE_OVERRIDE"] ?? ProcessEnv.block["SWIFTPM_TESTS_MODULECACHE"]
if let moduleCachePath {
commandLine += ["-module-cache-path", moduleCachePath]
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Workspace/Workspace+Configuration.swift
Expand Up @@ -89,7 +89,7 @@ extension Workspace {
public var localMirrorsConfigurationFile: AbsolutePath {
get throws {
// backwards compatibility
if let customPath = ProcessEnv.vars["SWIFTPM_MIRROR_CONFIG"] {
if let customPath = ProcessEnv.block["SWIFTPM_MIRROR_CONFIG"] {
return try AbsolutePath(validating: customPath)
}
return DefaultLocations.mirrorsConfigurationFile(at: self.localConfigurationDirectory)
Expand Down
2 changes: 1 addition & 1 deletion Sources/XCBuildSupport/XcodeBuildSystem.swift
Expand Up @@ -90,7 +90,7 @@ package final class XcodeBuildSystem: SPMBuildCore.BuildSystem {
self.fileSystem = fileSystem
self.observabilityScope = observabilityScope.makeChildScope(description: "Xcode Build System")

if let xcbuildTool = ProcessEnv.vars["XCBUILD_TOOL"] {
if let xcbuildTool = ProcessEnv.block["XCBUILD_TOOL"] {
xcbuildPath = try AbsolutePath(validating: xcbuildTool)
} else {
let xcodeSelectOutput = try TSCBasic.Process.popen(args: "xcode-select", "-p").utf8Output().spm_chomp()
Expand Down
2 changes: 1 addition & 1 deletion Tests/CommandsTests/APIDiffTests.swift
Expand Up @@ -42,7 +42,7 @@ final class APIDiffTests: CommandsTestCase {
try skipIfApiDigesterUnsupported()
// The following is added to separate out the integration point testing of the API
// diff digester with SwiftPM from the functionality tests of the digester itself
guard ProcessEnv.vars["SWIFTPM_TEST_API_DIFF_OUTPUT"] == "1" else {
guard ProcessEnv.block["SWIFTPM_TEST_API_DIFF_OUTPUT"] == "1" else {
throw XCTSkip("Env var SWIFTPM_TEST_API_DIFF_OUTPUT must be set to test the output")
}
}
Expand Down
Expand Up @@ -339,7 +339,7 @@ class GitHubPackageMetadataProviderTests: XCTestCase {
httpClient.configuration.requestHeaders = .init()
httpClient.configuration.requestHeaders!.add(name: "Cache-Control", value: "no-cache")
var configuration = GitHubPackageMetadataProvider.Configuration(disableCache: true) // Disable cache so we hit the API
if let token = ProcessEnv.vars["GITHUB_API_TOKEN"] {
if let token = ProcessEnv.block["GITHUB_API_TOKEN"] {
configuration.authTokens = { [.github("github.com"): token] }
}
configuration.apiLimitWarningThreshold = 50
Expand Down