Skip to content

Commit be469c1

Browse files
committed
refactor: Fix normalizePackage resolver
1 parent f6c0a7b commit be469c1

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

internal/shrinkwrap-extractor/lib/convertPackageLockToShrinkwrap.js

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ export default async function convertPackageLockToShrinkwrap(workspaceRootDir, t
4949
});
5050
const tree = await arb.loadVirtual();
5151
const tops = Array.from(tree.tops.values());
52-
const cliNode = tops.find((node) => node.packageName === targetPackageName);
53-
if (!cliNode) {
52+
const targetNode = tops.find((node) => node.packageName === targetPackageName);
53+
if (!targetNode) {
5454
throw new Error(`Target package "${targetPackageName}" not found in workspace`);
5555
}
5656

5757
const relevantPackageLocations = new Map();
5858
// Collect all package keys using arborist
59-
collectDependencies(cliNode, relevantPackageLocations);
59+
collectDependencies(targetNode, relevantPackageLocations);
6060

6161
// Using the keys, extract relevant package-entries from package-lock.json
6262
const extractedPackages = Object.create(null);
@@ -74,8 +74,8 @@ export default async function convertPackageLockToShrinkwrap(workspaceRootDir, t
7474
throw new Error(`Duplicate root package entry for "${targetPackageName}"`);
7575
}
7676
} else {
77-
packageLoc = normalizePackageLocation(packageLoc, node, targetPackageName, tree.packageName,
78-
extractedPackages);
77+
packageLoc = normalizePackageLocation(
78+
packageLoc, node, targetPackageName, tree.packageName, relevantPackageLocations);
7979
}
8080
if (packageLoc !== "" && !pkg.resolved) {
8181
// For all but the root package, ensure that "resolved" and "integrity" fields are present
@@ -99,7 +99,7 @@ export default async function convertPackageLockToShrinkwrap(workspaceRootDir, t
9999
// Generate npm-shrinkwrap.json
100100
const shrinkwrap = {
101101
name: targetPackageName,
102-
version: cliNode.version,
102+
version: targetNode.version,
103103
lockfileVersion: 3,
104104
requires: true,
105105
packages: sortedExtractedPackages
@@ -121,36 +121,21 @@ export default async function convertPackageLockToShrinkwrap(workspaceRootDir, t
121121
* @param {object} node - Package node from arborist
122122
* @param {string} targetPackageName - Target package name for shrinkwrap file
123123
* @param {string} rootPackageName - Root / workspace package name
124-
* @param {Object<string, {pkg: object, originalLocation: string}>} extractedPackages - Already extracted packages
124+
* @param {Map<string, object>} relevantPackageLocations
125125
* @returns {string} - Normalized location for npm-shrinkwrap.json
126126
*/
127127
function normalizePackageLocation(
128-
location, node, targetPackageName, rootPackageName, extractedPackages
129-
) {
128+
location, node, targetPackageName, rootPackageName, relevantPackageLocations) {
130129
const topPackageName = node.top.packageName;
131-
const currentIsFromRoot = !location.startsWith("packages/");
132130

133131
if (topPackageName === targetPackageName) {
134132
// Package is within target package (e.g. @ui5/cli)
135133
const normalizedPath = location.substring(node.top.location.length + 1);
136-
const existing = extractedPackages[normalizedPath];
137-
138-
if (existing && existing.pkg.version !== node.version) {
139-
// Collision detected: Check which should be at root level
140-
const existingIsFromRoot = !existing.originalLocation.startsWith("packages/");
141-
142-
// Root packages always get priority at top level
143-
if (existingIsFromRoot && !currentIsFromRoot) {
144-
// Existing is from root, current is from workspace -> nest current
145-
return `node_modules/${topPackageName}/${normalizedPath}`;
146-
} else if (!existingIsFromRoot && currentIsFromRoot) {
147-
// Current is from root, existing is from workspace -> shouldn't happen
148-
// due to processing order, but handle gracefully
149-
const msg = `Unexpected collision: root package processed after workspace ` +
150-
`package at ${normalizedPath}`;
151-
throw new Error(msg);
152-
}
153-
// Both from same type of location with different versions -> nest current
134+
const existing = relevantPackageLocations.get(normalizedPath);
135+
136+
// Check for version collision
137+
if (existing && existing.version !== node.version) {
138+
// Different version exists - nest this one under the target package
154139
return `node_modules/${topPackageName}/${normalizedPath}`;
155140
}
156141

0 commit comments

Comments
 (0)