Skip to content

Commit 616edfd

Browse files
authored
[6.0] Fix –-toolchain value shadowed by local executables (#7485)
Cherry-pick of #7483. **Explanation**: Behavior of `--toolchain` regressed in https://github.com/apple/swift-package-manager/pull/7296/files by preferring the first match in toolset root paths: when `swiftc` is available in the same directory next to the SwiftPM binary being invoked, that became preferred with `--toolchain` value ignored. **Scope**: limited to cross-compilation and infrequent builds that utilize this option. **Risk**: low due to limited scope, merged to `main` for a week with no regressions discovered. **Testing**: A newly added test verifies that the behavior is fixed. For that we had to inject `FileSystem` and `EnvironmentVariables` in a lot more places, with an added benefit of making our tests more consistent and isolated from an arbitrary build environment. **Issue**: rdar://126095653 **Reviewer**: @bnbarham
1 parent 741a11c commit 616edfd

25 files changed

+972
-154
lines changed

Sources/Basics/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ add_library(Basics
2929
Errors.swift
3030
FileSystem/AbsolutePath.swift
3131
FileSystem/FileSystem+Extensions.swift
32+
FileSystem/InMemoryFileSystem.swift
3233
FileSystem/NativePathExtensions.swift
3334
FileSystem/RelativePath.swift
3435
FileSystem/TemporaryFile.swift

Sources/Basics/FileSystem/FileSystem+Extensions.swift

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2020-2021 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2020-2024 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See http://swift.org/LICENSE.txt for license information
@@ -632,3 +632,48 @@ extension FileLock {
632632
return try Self.prepareLock(fileToLock: fileToLock.underlying, at: lockFilesDirectory?.underlying)
633633
}
634634
}
635+
636+
/// Convenience initializers for testing purposes.
637+
extension InMemoryFileSystem {
638+
/// Create a new file system with the given files, provided as a map from
639+
/// file path to contents.
640+
public convenience init(files: [String: ByteString]) {
641+
self.init()
642+
643+
for (path, contents) in files {
644+
let path = try! AbsolutePath(validating: path)
645+
try! createDirectory(path.parentDirectory, recursive: true)
646+
try! writeFileContents(path, bytes: contents)
647+
}
648+
}
649+
650+
/// Create a new file system with an empty file at each provided path.
651+
public convenience init(emptyFiles files: String...) {
652+
self.init(emptyFiles: files)
653+
}
654+
655+
/// Create a new file system with an empty file at each provided path.
656+
public convenience init(emptyFiles files: [String]) {
657+
self.init()
658+
self.createEmptyFiles(at: .root, files: files)
659+
}
660+
}
661+
662+
extension FileSystem {
663+
public func createEmptyFiles(at root: AbsolutePath, files: String...) {
664+
self.createEmptyFiles(at: root, files: files)
665+
}
666+
667+
public func createEmptyFiles(at root: AbsolutePath, files: [String]) {
668+
do {
669+
try createDirectory(root, recursive: true)
670+
for path in files {
671+
let path = try AbsolutePath(validating: String(path.dropFirst()), relativeTo: root)
672+
try createDirectory(path.parentDirectory, recursive: true)
673+
try writeFileContents(path, bytes: "")
674+
}
675+
} catch {
676+
fatalError("Failed to create empty files: \(error)")
677+
}
678+
}
679+
}

0 commit comments

Comments
 (0)