Skip to content

gracefully fail workspace state loading when duplicate entries found #3974

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
Jan 5, 2022
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
18 changes: 15 additions & 3 deletions Sources/Workspace/ManagedArtifact.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,22 @@ extension Workspace {
AnyCollection(self.artifactMap.values.lazy.flatMap{ $0.values })
}

init(_ artifacts: [ManagedArtifact] = []) {
init() {
self.artifactMap = [:]
}

init(_ artifacts: [ManagedArtifact]) throws {
let artifactsByPackagePath = Dictionary(grouping: artifacts, by: { $0.packageRef.identity })
self.artifactMap = artifactsByPackagePath.mapValues{ artifacts in
Dictionary(uniqueKeysWithValues: artifacts.map{ ($0.targetName, $0) })
self.artifactMap = try artifactsByPackagePath.mapValues{ artifacts in
// rdar://86857825 do not use Dictionary(uniqueKeysWithValues:) as it can crash the process when input is incorrect such as in older versions of SwiftPM
var map = [String: ManagedArtifact]()
for artifact in artifacts {
if map[artifact.targetName] != nil {
throw StringError("binary artifact for '\(artifact.targetName)' already exists in managed artifacts")
}
map[artifact.targetName] = artifact
}
return map
}
}

Expand Down
16 changes: 13 additions & 3 deletions Sources/Workspace/ManagedDependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,21 @@ extension Workspace.ManagedDependency: CustomStringConvertible {
extension Workspace {
/// A collection of managed dependencies.
final public class ManagedDependencies {
// FIXME: this should be identity based
private var dependencies: [PackageIdentity: ManagedDependency]

init(_ dependencies: [ManagedDependency] = []) {
self.dependencies = Dictionary(uniqueKeysWithValues: dependencies.map{ ($0.packageRef.identity, $0) })
init() {
self.dependencies = [:]
}

init(_ dependencies: [ManagedDependency]) throws {
// rdar://86857825 do not use Dictionary(uniqueKeysWithValues:) as it can crash the process when input is incorrect such as in older versions of SwiftPM
self.dependencies = [:]
for dependency in dependencies {
if self.dependencies[dependency.packageRef.identity] != nil {
throw StringError("\(dependency.packageRef.identity) already exists in managed dependencies")
}
self.dependencies[dependency.packageRef.identity] = dependency
}
}

public subscript(identity: PackageIdentity) -> ManagedDependency? {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Workspace/WorkspaceState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ fileprivate struct WorkspaceStateStorage {
let v4 = try self.decoder.decode(path: self.path, fileSystem: self.fileSystem, as: V4.self)
let dependencies = try v4.object.dependencies.map{ try Workspace.ManagedDependency($0) }
let artifacts = try v4.object.artifacts.map{ try Workspace.ManagedArtifact($0) }
return (dependencies: .init(dependencies), artifacts: .init(artifacts))
return try (dependencies: .init(dependencies), artifacts: .init(artifacts))
case 5:
let v5 = try self.decoder.decode(path: self.path, fileSystem: self.fileSystem, as: V5.self)
let dependencies = try v5.object.dependencies.map{ try Workspace.ManagedDependency($0) }
let artifacts = try v5.object.artifacts.map{ try Workspace.ManagedArtifact($0) }
return (dependencies: .init(dependencies), artifacts: .init(artifacts))
return try (dependencies: .init(dependencies), artifacts: .init(artifacts))
default:
throw StringError("unknown 'WorkspaceStateStorage' version '\(version.version)' at '\(self.path)'")
}
Expand Down
Loading