Skip to content

Commit

Permalink
fix(reviewing): filter the same print message (pnpm#7430)
Browse files Browse the repository at this point in the history
close pnpm#7429

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
  • Loading branch information
2 people authored and gluxon committed Dec 27, 2023
1 parent 97b450e commit 553b4b5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/brave-zoos-tan.md
@@ -0,0 +1,6 @@
---
"@pnpm/list": patch
"pnpm": patch
---

`pnpm list --parseable` should not print the same dependency multiple times [#7429](https://github.com/pnpm/pnpm/issues/7429).
22 changes: 17 additions & 5 deletions reviewing/list/src/renderParseable.ts
Expand Up @@ -14,20 +14,26 @@ export async function renderParseable (
search: boolean
}
) {
return pkgs.map((pkg) => renderParseableForPackage(pkg, opts)).filter(p => p.length !== 0).join('\n')
const depPaths = new Set<string>()
return pkgs
.map(renderParseableForPackage.bind(null, depPaths, opts))
.filter(p => p.length !== 0)
.join('\n')
}

function renderParseableForPackage (
pkg: PackageDependencyHierarchy,
depPaths: Set<string>,
opts: {
long: boolean
depth: number
alwaysPrintRootPackage: boolean
search: boolean
}
},
pkg: PackageDependencyHierarchy
) {
const pkgs = sortPackages(
flatten(
depPaths,
[
...(pkg.optionalDependencies ?? []),
...(pkg.dependencies ?? []),
Expand Down Expand Up @@ -66,13 +72,19 @@ interface PackageInfo {
}

function flatten (
depPaths: Set<string>,
nodes: PackageNode[]
): PackageInfo[] {
let packages: PackageInfo[] = []
for (const node of nodes) {
packages.push(node)
// The content output by renderParseable is flat,
// so we can deduplicate packages that are repeatedly dependent on multiple packages.
if (!depPaths.has(node.path)) {
depPaths.add(node.path)
packages.push(node)
}
if (node.dependencies?.length) {
packages = packages.concat(flatten(node.dependencies))
packages = packages.concat(flatten(depPaths, node.dependencies))
}
}
return packages
Expand Down
6 changes: 2 additions & 4 deletions reviewing/list/test/index.ts
Expand Up @@ -261,8 +261,7 @@ test('parseable list in workspace with private package', async () => {
lockfileDir: workspaceWithPrivatePkgs,
})).toBe(`${path.join(workspaceWithPrivatePkgs, 'packages/private')}
${path.join(workspaceWithPrivatePkgs, 'node_modules/.pnpm/is-positive@1.0.0/node_modules/is-positive')}
${path.join(workspaceWithPrivatePkgs, 'packages/public')}
${path.join(workspaceWithPrivatePkgs, 'node_modules/.pnpm/is-positive@1.0.0/node_modules/is-positive')}`)
${path.join(workspaceWithPrivatePkgs, 'packages/public')}`)
})

test('long parseable list in workspace with private package', async () => {
Expand All @@ -275,8 +274,7 @@ test('long parseable list in workspace with private package', async () => {
lockfileDir: workspaceWithPrivatePkgs,
})).toBe(`${path.join(workspaceWithPrivatePkgs, 'packages/private')}:private@1.0.0:PRIVATE
${path.join(workspaceWithPrivatePkgs, 'node_modules/.pnpm/is-positive@1.0.0/node_modules/is-positive')}:is-positive@1.0.0
${path.join(workspaceWithPrivatePkgs, 'packages/public')}:public@1.0.0
${path.join(workspaceWithPrivatePkgs, 'node_modules/.pnpm/is-positive@1.0.0/node_modules/is-positive')}:is-positive@1.0.0`)
${path.join(workspaceWithPrivatePkgs, 'packages/public')}:public@1.0.0`)
})

test('JSON list in workspace with private package', async () => {
Expand Down

0 comments on commit 553b4b5

Please sign in to comment.