Skip to content

SwiftPM6: Fix to skip locking package build directory #38

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
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
27 changes: 26 additions & 1 deletion Sources/DependencyCalculator/DependencyGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Foundation
import Git
import PathKit
import SelectiveTestLogger
import SelectiveTestShell
import Workspace
import XcodeProj

Expand Down Expand Up @@ -200,8 +201,14 @@ extension WorkspaceInfo {
} == nil
}

// SwiftPM6 locks build directory up when parsing multiple packages concurrently
let isSwiftVersion6Plus = try isSwiftVersion6Plus()

return Array(allPackages).concurrentMap { path in
try? PackageTargetMetadata.parse(at: path.parent())
try? PackageTargetMetadata.parse(
at: path.parent(),
addingIgnoreLockOption: isSwiftVersion6Plus
)
}.compactMap { $0 }.reduce([PackageTargetMetadata]()) { partialResult, new in
var result = partialResult
result.append(contentsOf: new)
Expand Down Expand Up @@ -347,4 +354,22 @@ extension WorkspaceInfo {
dependencyStructure: DependencyGraph(dependsOn: dependsOn),
candidateTestPlan: candidateTestPlan)
}

private static func isSwiftVersion6Plus() throws -> Bool {
guard let regex = try? NSRegularExpression(pattern: #"Apple Swift version (\d+)"#) else {
return false
}

let versionString = try Shell.execOrFail("swift --version")
let range = NSRange(versionString.startIndex..<versionString.endIndex, in: versionString)
if let match = regex.firstMatch(in: versionString, options: [], range: range),
let majorVersionRange = Range(match.range(at: 1), in: versionString),
let majorVersion = Int(versionString[majorVersionRange]),
majorVersion > 5
{
return true
} else {
return false
}
}
}
14 changes: 11 additions & 3 deletions Sources/DependencyCalculator/PackageMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ struct PackageTargetMetadata {
let testTarget: Bool

// TODO: Split in several methods
static func parse(at path: Path) throws -> [PackageTargetMetadata] {
// NB: Flag `--disable-sandbox` is required to allow running SPM from an SPM plugin
let manifest = try Shell.execOrFail("cd \(path) && swift package dump-package --disable-sandbox").trimmingCharacters(in: .newlines)
static func parse(at path: Path, addingIgnoreLockOption: Bool = false) throws -> [PackageTargetMetadata] {
// NB:
// - Flag `--disable-sandbox` is required to allow running SPM from an SPM plugin
// - Flag `--ignore-lock` is required to avoid locking the package build directory when parsing is done concurrently (Swift 6).
var flags = ["--disable-sandbox"]
if addingIgnoreLockOption {
flags.append("--ignore-lock")
}

let manifest = try Shell.execOrFail("cd \(path) && swift package dump-package \(flags.joined(separator: " "))")
.trimmingCharacters(in: .newlines)
guard let manifestData = manifest.data(using: .utf8),
let manifestJson = try JSONSerialization.jsonObject(with: manifestData, options: []) as? [String: Any],
let targets = manifestJson["targets"] as? [[String: Any]]
Expand Down