Skip to content

Commit

Permalink
[Workspace] Diagnose duplicate root packages
Browse files Browse the repository at this point in the history
<rdar://problem/47031631>
  • Loading branch information
aciidgh committed Jan 5, 2019
1 parent c924e56 commit 035b0c0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
3 changes: 1 addition & 2 deletions Sources/TestSupport/TestWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public final class TestWorkspace {
toolsVersion: ToolsVersion = ToolsVersion.currentToolsVersion,
skipUpdate: Bool = false
) throws {
precondition(Set(roots.map({$0.name})).count == roots.count, "Root packages should be unique")
self.sandbox = sandbox
self.fs = fs
self.config = SwiftPMConfig(path: sandbox.appending(component: "swiftpm"), fs: fs)
Expand Down Expand Up @@ -155,7 +154,7 @@ public final class TestWorkspace {
}

public func rootPaths(for packages: [String]) -> [AbsolutePath] {
return packages.map({ rootsDir.appending(component: $0) })
return packages.map({ rootsDir.appending(RelativePath($0)) })
}

public struct PackageDependency {
Expand Down
12 changes: 12 additions & 0 deletions Sources/Workspace/Diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,16 @@ public enum WorkspaceDiagnostics {
}
)
}

public struct DuplicateRoots: DiagnosticData {
public static let id = DiagnosticID(
type: DuplicateRoots.self,
name: "org.swift.diags.workspace.\(DuplicateRoots.self)",
description: {
$0 <<< "found multiple top-level packages named" <<< { "'" + $0.name + "'" }
}
)

let name: String
}
}
13 changes: 11 additions & 2 deletions Sources/Workspace/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,18 @@ extension Workspace {
packages: [AbsolutePath],
diagnostics: DiagnosticsEngine
) -> [Manifest] {
return packages.compactMap({ package -> Manifest? in
loadManifest(packagePath: package, url: package.asString, diagnostics: diagnostics)
let rootManifests = packages.compactMap({ package -> Manifest? in
loadManifest(packagePath: package, url: package.asString, diagnostics: diagnostics)
})

// Check for duplicate root packages.
let duplicateRoots = rootManifests.spm_findDuplicateElements(by: \.name)
if !duplicateRoots.isEmpty {
diagnostics.emit(data: WorkspaceDiagnostics.DuplicateRoots(name: duplicateRoots[0][0].name))
return []
}

return rootManifests
}
}

Expand Down
36 changes: 36 additions & 0 deletions Tests/WorkspaceTests/WorkspaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,42 @@ final class WorkspaceTests: XCTestCase {
}
}

func testDuplicateRootPackages() throws {
let sandbox = AbsolutePath("/tmp/ws/")
let fs = InMemoryFileSystem()

let workspace = try TestWorkspace(
sandbox: sandbox,
fs: fs,
roots: [
TestPackage(
name: "Foo",
targets: [
TestTarget(name: "Foo", dependencies: []),
],
products: [],
dependencies: []
),
TestPackage(
name: "Foo",
path: "Nested/Foo",
targets: [
TestTarget(name: "Foo", dependencies: []),
],
products: [],
dependencies: []
),
],
packages: []
)

workspace.checkPackageGraph(roots: ["Foo", "Nested/Foo"]) { (graph, diagnostics) in
DiagnosticsEngineTester(diagnostics) { result in
result.check(diagnostic: .equal("found multiple top-level packages named 'Foo'"), behavior: .error)
}
}
}

/// Test that the remote repository is not resolved when a root package with same name is already present.
func testRootAsDependency1() throws {
let sandbox = AbsolutePath("/tmp/ws/")
Expand Down
1 change: 1 addition & 0 deletions Tests/WorkspaceTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ extension WorkspaceTests {
("testDependencyManifestsOrder", testDependencyManifestsOrder),
("testDependencyResolutionWithEdit", testDependencyResolutionWithEdit),
("testDependencySwitchWithSameIdentity", testDependencySwitchWithSameIdentity),
("testDuplicateRootPackages", testDuplicateRootPackages),
("testEditDependency", testEditDependency),
("testForceResolveToResolvedVersions", testForceResolveToResolvedVersions),
("testGraphRootDependencies", testGraphRootDependencies),
Expand Down

0 comments on commit 035b0c0

Please sign in to comment.