Skip to content

Commit

Permalink
Track dev and peer dependencies as optional. Include dev dependencies…
Browse files Browse the repository at this point in the history
… in the dependency tree for pnpm

Signed-off-by: Prabhu Subramanian <[email protected]>
  • Loading branch information
prabhu committed Jan 20, 2025
1 parent a1122d7 commit e5d74db
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
39 changes: 30 additions & 9 deletions lib/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2105,6 +2105,10 @@ export async function parsePnpmLock(
lockfileVersion >= 6
? yamlObj.importers["."]?.optionalDependencies || {}
: {};
const rootPeerDeps =
lockfileVersion >= 6
? yamlObj.importers["."]?.peerDependencies || {}
: {};
const ddeplist = new Set();
// Find the root optional dependencies
for (const rdk of Object.keys(rootDevDeps)) {
Expand All @@ -2119,7 +2123,7 @@ export async function parsePnpmLock(
).toString();
possibleOptionalDeps[decodeURIComponent(dpurl)] = true;
}
for (const rdk of Object.keys(rootOptionalDeps)) {
for (const rdk of Object.keys({ ...rootOptionalDeps, ...rootPeerDeps })) {
const version = await getVersionNumPnpm(rootOptionalDeps[rdk]);
const dpurl = new PackageURL(
"npm",
Expand Down Expand Up @@ -2161,6 +2165,8 @@ export async function parsePnpmLock(
const componentOptionalDeps =
yamlObj?.importers[importedComponentName]["optionalDependencies"] ||
{};
const componentPeerDeps =
yamlObj?.importers[importedComponentName]["peerDependencies"] || {};
let compPurl = undefined;
let pkgSrcFile = undefined;
let fallbackMode = true;
Expand Down Expand Up @@ -2262,6 +2268,7 @@ export async function parsePnpmLock(
null,
).toString();
const depRef = decodeURIComponent(dpurl);
// This is a definite dependency of this component
comDepList.add(depRef);
possibleOptionalDeps[depRef] = false;
// Track the package.json files
Expand All @@ -2272,10 +2279,6 @@ export async function parsePnpmLock(
srcFilesMap[depRef].push(pkgSrcFile);
}
}
dependenciesList.push({
ref: decodeURIComponent(compPurl),
dependsOn: [...comDepList].sort(),
});
for (const cdk of Object.keys(componentDevDeps)) {
const version = await getVersionNumPnpm(componentDevDeps[cdk]);
const dpurl = new PackageURL(
Expand All @@ -2286,9 +2289,15 @@ export async function parsePnpmLock(
null,
null,
).toString();
possibleOptionalDeps[decodeURIComponent(dpurl)] = true;
}
for (const cdk of Object.keys(componentOptionalDeps)) {
const devDpRef = decodeURIComponent(dpurl);
possibleOptionalDeps[devDpRef] = true;
// This is also a dependency of this component
comDepList.add(devDpRef);
}
for (const cdk of Object.keys({
...componentOptionalDeps,
...componentPeerDeps,
})) {
const version = await getVersionNumPnpm(componentOptionalDeps[cdk]);
const dpurl = new PackageURL(
"npm",
Expand All @@ -2300,6 +2309,10 @@ export async function parsePnpmLock(
).toString();
possibleOptionalDeps[decodeURIComponent(dpurl)] = true;
}
dependenciesList.push({
ref: decodeURIComponent(compPurl),
dependsOn: [...comDepList].sort(),
});
}
dependenciesList.push({
ref: decodeURIComponent(ppurl),
Expand Down Expand Up @@ -2567,6 +2580,10 @@ export async function parsePnpmLock(
depsWorkspaceRefs[purlString] ||
[],
)) {
// This cycle shouldn't happen, but we can't be sure
if (wref === purlString) {
continue;
}
properties.push({
name: "internal:workspaceRef",
value: wref,
Expand All @@ -2582,7 +2599,7 @@ export async function parsePnpmLock(
if (!depsWorkspaceRefs[dref]) {
depsWorkspaceRefs[dref] = [];
}
depsWorkspaceRefs[dref].push(dref);
depsWorkspaceRefs[dref].push(wref);
}
}
const thePkg = {
Expand Down Expand Up @@ -2666,6 +2683,10 @@ export async function parsePnpmLock(
);
if (!wsprops.length) {
for (const wref of depsWorkspaceRefs[apkg["bom-ref"]]) {
// Such a cycle should never happen, but we can't sure
if (wref === apkg["bom-ref"]) {
continue;
}
apkg.properties.push({
name: "internal:workspaceRef",
value: wref,
Expand Down
2 changes: 1 addition & 1 deletion types/lib/helpers/utils.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e5d74db

Please sign in to comment.