From 553b4b5b158b45719bbfa1e03672b9aee12353bd Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Tue, 26 Dec 2023 19:54:41 +0800 Subject: [PATCH] fix(reviewing): filter the same print message (#7430) close #7429 --------- Co-authored-by: Zoltan Kochan --- .changeset/brave-zoos-tan.md | 6 ++++++ reviewing/list/src/renderParseable.ts | 22 +++++++++++++++++----- reviewing/list/test/index.ts | 6 ++---- 3 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 .changeset/brave-zoos-tan.md diff --git a/.changeset/brave-zoos-tan.md b/.changeset/brave-zoos-tan.md new file mode 100644 index 00000000000..4439bf84781 --- /dev/null +++ b/.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). diff --git a/reviewing/list/src/renderParseable.ts b/reviewing/list/src/renderParseable.ts index 2e35d280ac1..f053089504b 100644 --- a/reviewing/list/src/renderParseable.ts +++ b/reviewing/list/src/renderParseable.ts @@ -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() + return pkgs + .map(renderParseableForPackage.bind(null, depPaths, opts)) + .filter(p => p.length !== 0) + .join('\n') } function renderParseableForPackage ( - pkg: PackageDependencyHierarchy, + depPaths: Set, opts: { long: boolean depth: number alwaysPrintRootPackage: boolean search: boolean - } + }, + pkg: PackageDependencyHierarchy ) { const pkgs = sortPackages( flatten( + depPaths, [ ...(pkg.optionalDependencies ?? []), ...(pkg.dependencies ?? []), @@ -66,13 +72,19 @@ interface PackageInfo { } function flatten ( + depPaths: Set, 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 diff --git a/reviewing/list/test/index.ts b/reviewing/list/test/index.ts index 499b5abf6bf..6f5915ad1e8 100644 --- a/reviewing/list/test/index.ts +++ b/reviewing/list/test/index.ts @@ -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 () => { @@ -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 () => {