Skip to content

Early return when module aliases are not defined #4193

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 2 commits into from
Mar 4, 2022
Merged
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
32 changes: 21 additions & 11 deletions Sources/PackageGraph/PackageGraph+Loading.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private func createResolvedPackages(
}

// Gather all module aliases specified for targets in all dependent packages
let pkgToAliasesMap = gatherModuleAliases(for: rootManifests.first?.key, with: packagesByIdentity, onError: observabilityScope)
let packageAliases = gatherModuleAliases(from: packageBuilders, for: rootManifests.first?.key, with: packagesByIdentity, onError: observabilityScope)

// Scan and validate the dependencies
for packageBuilder in packageBuilders {
Expand All @@ -276,7 +276,7 @@ private func createResolvedPackages(
metadata: package.diagnosticsMetadata
)

if let aliasMap = pkgToAliasesMap[package.identity] {
if let aliasMap = packageAliases?[package.identity] {
package.setModuleAliasesForTargets(with: aliasMap)
}

Expand Down Expand Up @@ -523,9 +523,20 @@ private func createResolvedPackages(
}

// Create a map between a package and module aliases specified for the targets in the package.
private func gatherModuleAliases(for rootPkgID: PackageIdentity?,
private func gatherModuleAliases(from packageBuilders: [ResolvedPackageBuilder],
for rootPkgID: PackageIdentity?,
with packagesByIdentity: [PackageIdentity: ResolvedPackageBuilder],
onError observabilityScope: ObservabilityScope) -> [PackageIdentity: [String: String]] {
onError observabilityScope: ObservabilityScope) -> [PackageIdentity: [String: String]]? {
// If there are no aliases, return early
let depsWithAliases = packageBuilders.map { $0.package.targets.map { $0.dependencies.filter { dep in
if case let .product(prodRef, _) = dep {
return prodRef.moduleAliases != nil
}
return false
}}}.flatMap{$0}.flatMap{$0}

guard !depsWithAliases.isEmpty else { return nil }

var result = [PackageIdentity: [String: String]]()

// There could be multiple root packages but the common cases involve
Expand Down Expand Up @@ -580,14 +591,13 @@ private func walkPkgTreeAndGetModuleAliases(for pkgID: PackageIdentity,
break
}
}

// Add pkgID to a stack used to keep track of multiple
// aliases specified in the package chain. Need to add
// pkgID here, otherwise need pkgID != pkgInChain check
// in the for loop above
pkgStack.append(pkgID)
}

// Add pkgID to a stack used to keep track of multiple
// aliases specified in the package chain. Need to add
// pkgID here, otherwise need pkgID != pkgInChain check
// in the for loop above
pkgStack.append(pkgID)

// Recursively (depth-first) walk the package dependency tree
for pkgDep in builder.package.manifest.dependencies {
walkPkgTreeAndGetModuleAliases(for: pkgDep.identity, with: packagesByIdentity, onError: observabilityScope, using: &pkgStack, output: &result)
Expand Down