     }
 
     // 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 {

                    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)
        }
        
        // 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)

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

