Skip to content

Actually skip not needed dependencies during dependency resolution #3166

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 3 commits into from
Jan 11, 2021
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: 7 additions & 11 deletions Sources/PackageModel/Manifest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,21 @@ public final class Manifest: ObjectIdentifierProtocol {
return dependencies
}
#else
guard toolsVersion >= .v5_2 && productFilter != .everything else {
guard toolsVersion >= .v5_2 && packageKind != .root else {
return dependencies
}

var requiredDependencies: Set<PackageDependencyDescription> = []

var requiredDependencyURLs: Set<String> = []
for target in targetsRequired(for: products) {
for targetDependency in target.dependencies {
if let dependency = packageDependency(referencedBy: targetDependency) {
requiredDependencies.insert(dependency)
requiredDependencyURLs.insert(dependency.url)
}
}
}

return Array(requiredDependencies)
return dependencies.filter { requiredDependencyURLs.contains($0.url) }
#endif
}

Expand Down Expand Up @@ -233,7 +233,6 @@ public final class Manifest: ObjectIdentifierProtocol {
}

return dependencies.compactMap { dependency in
#if ENABLE_TARGET_BASED_DEPENDENCY_RESOLUTION
if let filter = associations[dependency.name] {
return dependency.filtered(by: filter)
} else if keepUnused {
Expand All @@ -243,9 +242,6 @@ public final class Manifest: ObjectIdentifierProtocol {
// Dependencies known to not have any relevant products are discarded.
return nil
}
#else
return dependency.filtered(by: .everything)
#endif
}
}

Expand Down
22 changes: 12 additions & 10 deletions Tests/WorkspaceTests/WorkspaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4067,14 +4067,17 @@ final class WorkspaceTests: XCTestCase {
}

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

let barProducts: [MockProduct]
#if ENABLE_TARGET_BASED_DEPENDENCY_RESOLUTION
barProducts = [MockProduct(name: "Bar", targets: ["Bar"]), MockProduct(name: "BarUnused", targets: ["BarUnused"])]
#else
try XCTSkipIf(true)
// Whether a product is being used does not affect dependency resolution in this case, so we omit the unused product.
barProducts = [MockProduct(name: "Bar", targets: ["Bar"])]
#endif

let sandbox = AbsolutePath("/tmp/ws/")
let fs = InMemoryFileSystem()

let workspace = try MockWorkspace(
sandbox: sandbox,
fs: fs,
Expand Down Expand Up @@ -4119,10 +4122,7 @@ final class WorkspaceTests: XCTestCase {
MockTarget(name: "BarUnused", dependencies: ["Biz"]),
MockTarget(name: "BarTests", dependencies: ["TestHelper2"], type: .test),
],
products: [
MockProduct(name: "Bar", targets: ["Bar"]),
MockProduct(name: "BarUnused", targets: ["BarUnused"]),
],
products: barProducts,
dependencies: [
MockDependency(name: "TestHelper2", requirement: .upToNextMajor(from: "1.0.0")),
MockDependency(name: "Biz", requirement: .upToNextMajor(from: "1.0.0")),
Expand Down Expand Up @@ -5068,7 +5068,8 @@ final class WorkspaceTests: XCTestCase {
dependencies: [
MockDependency(name: nil, path: "bar", requirement: .upToNextMajor(from: "1.0.0")),
],
versions: ["1.0.0"]
versions: ["1.0.0"],
toolsVersion: .v5
),
MockPackage(
name: "BarPackage",
Expand All @@ -5084,7 +5085,8 @@ final class WorkspaceTests: XCTestCase {
dependencies: [
MockDependency(name: nil, path: "other/utility", requirement: .upToNextMajor(from: "1.0.0")),
],
versions: ["1.0.0"]
versions: ["1.0.0"],
toolsVersion: .v5
),
// this package never gets loaded since its identity is the same as "FooPackage"
MockPackage(
Expand Down