Skip to content
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

fix(core): Adding a yarn berry version of isRootVersion utility #14321

Closed
wants to merge 3 commits into from
Closed
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
58 changes: 57 additions & 1 deletion packages/nx/src/lock-file/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ function mapPackages(
versions[versionKeys[0]].rootVersion = true;
} else {
const rootVersionKey = versionKeys.find((v) =>
isRootVersion(packageName, versions[v].version)
isBerry
? isRootVersionBerry(
v,
versions[v].version,
workspacePackages,
mappedPackages
)
: isRootVersion(v, versions[v].version)
);

// this should never happen, but just in case
Expand Down Expand Up @@ -531,3 +538,52 @@ function ensureMetaVersion(

return packageMeta;
}

/**
* A function equivalent to isRootVersion, but for yarn berry
* @param packageName name of the package
* @param version version of the package to check
*/
function isRootVersionBerry(
packageName: string,
version: string,
workspacePackages: LockFileDependencies,
mappedPackages: Record<string, PackageVersions>
): boolean {
const rootVersionString = getRootVersionStringBerry(
packageName,
workspacePackages
);

if (!rootVersionString) {
return false;
}

const packageMeta =
mappedPackages[packageName]?.[`${packageName}@${version}`]?.packageMeta ||
[];

return packageMeta.some(
(v: string) =>
v.startsWith(`${packageName}@`) && v.endsWith(rootVersionString)
);
}

function getRootVersionStringBerry(
packageName: string,
workspacePackages: LockFileDependencies
) {
for (const workspacePackageName in workspacePackages) {
if (
workspacePackages[workspacePackageName].resolution.endsWith(
'@workspace:.' // this is the root package
)
) {
const { dependencies } = workspacePackages[packageName];

return dependencies[packageName];
}
}

return null;
}