From 82f7ed21124d2fa345b962370eb5293aaa894955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kh=E1=BA=A3i?= Date: Tue, 13 Aug 2024 21:55:54 +0700 Subject: [PATCH] fix: replace pnpm warnings with subfield warnings (#8415) close #8413 --- .changeset/smart-planes-tease.md | 6 +++++ workspace/find-packages/src/index.ts | 27 ++++++++++++++----- .../packages/bar/package.json | 5 +++- .../packages/foo/package.json | 5 +++- workspace/find-packages/test/index.ts | 12 ++++++--- 5 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 .changeset/smart-planes-tease.md diff --git a/.changeset/smart-planes-tease.md b/.changeset/smart-planes-tease.md new file mode 100644 index 0000000000..24f4b89c79 --- /dev/null +++ b/.changeset/smart-planes-tease.md @@ -0,0 +1,6 @@ +--- +"@pnpm/workspace.find-packages": patch +"pnpm": patch +--- + +Remove warnings for non-root `pnpm` field, add warnings for non-root `pnpm` subfields that aren't `executionEnv` [#8143](https://github.com/pnpm/pnpm/issues/8413). diff --git a/workspace/find-packages/src/index.ts b/workspace/find-packages/src/index.ts index 88c4e19e9d..35a8484007 100644 --- a/workspace/find-packages/src/index.ts +++ b/workspace/find-packages/src/index.ts @@ -61,13 +61,28 @@ export async function findWorkspacePackagesNoCheck (workspaceRoot: string, opts? return pkgs } +const uselessNonRootManifestFields: Array = ['resolutions'] + +type ProjectManifestPnpm = Required['pnpm'] +const usefulNonRootPnpmFields: Array = ['executionEnv'] + function checkNonRootProjectManifest ({ manifest, rootDir }: Project): void { - for (const rootOnlyField of ['pnpm', 'resolutions']) { - if (manifest?.[rootOnlyField as keyof ProjectManifest]) { - logger.warn({ - message: `The field "${rootOnlyField}" was found in ${rootDir}/package.json. This will not take effect. You should configure "${rootOnlyField}" at the root of the workspace instead.`, - prefix: rootDir, - }) + const warn = printNonRootFieldWarning.bind(null, rootDir) + for (const field of uselessNonRootManifestFields) { + if (field in manifest) { + warn(field) + } + } + for (const field in manifest.pnpm) { + if (!usefulNonRootPnpmFields.includes(field as keyof ProjectManifestPnpm)) { + warn(`pnpm.${field}`) } } } + +function printNonRootFieldWarning (prefix: string, propertyPath: string): void { + logger.warn({ + message: `The field "${propertyPath}" was found in ${prefix}/package.json. This will not take effect. You should configure "${propertyPath}" at the root of the workspace instead.`, + prefix, + }) +} diff --git a/workspace/find-packages/test/__fixtures__/warning-for-non-root-project/packages/bar/package.json b/workspace/find-packages/test/__fixtures__/warning-for-non-root-project/packages/bar/package.json index e71697df59..049c715695 100644 --- a/workspace/find-packages/test/__fixtures__/warning-for-non-root-project/packages/bar/package.json +++ b/workspace/find-packages/test/__fixtures__/warning-for-non-root-project/packages/bar/package.json @@ -1,6 +1,9 @@ { "name": "bar", "version": "1.0.0", - "pnpm": {}, + "pnpm": { + "overrides": {}, + "executionEnv": {} + }, "resolutions": {} } diff --git a/workspace/find-packages/test/__fixtures__/warning-for-non-root-project/packages/foo/package.json b/workspace/find-packages/test/__fixtures__/warning-for-non-root-project/packages/foo/package.json index 4c2122ec73..16deac19f7 100644 --- a/workspace/find-packages/test/__fixtures__/warning-for-non-root-project/packages/foo/package.json +++ b/workspace/find-packages/test/__fixtures__/warning-for-non-root-project/packages/foo/package.json @@ -1,5 +1,8 @@ { "name": "foo", "version": "1.0.0", - "pnpm": {} + "pnpm": { + "overrides": {}, + "executionEnv": {} + } } diff --git a/workspace/find-packages/test/index.ts b/workspace/find-packages/test/index.ts index 5d4bfdb528..66a537ab05 100644 --- a/workspace/find-packages/test/index.ts +++ b/workspace/find-packages/test/index.ts @@ -42,10 +42,14 @@ test('findWorkspacePackages() output warnings for non-root workspace project', a sharedWorkspaceLockfile: true, }) expect(pkgs.length).toBe(3) - expect(logger.warn).toHaveBeenCalledTimes(3) const fooPath = path.join(fixturePath, 'packages/foo') const barPath = path.join(fixturePath, 'packages/bar') - expect(logger.warn).toHaveBeenNthCalledWith(1, { prefix: barPath, message: `The field "pnpm" was found in ${barPath}/package.json. This will not take effect. You should configure "pnpm" at the root of the workspace instead.` }) - expect(logger.warn).toHaveBeenNthCalledWith(2, { prefix: barPath, message: `The field "resolutions" was found in ${barPath}/package.json. This will not take effect. You should configure "resolutions" at the root of the workspace instead.` }) - expect(logger.warn).toHaveBeenNthCalledWith(3, { prefix: fooPath, message: `The field "pnpm" was found in ${fooPath}/package.json. This will not take effect. You should configure "pnpm" at the root of the workspace instead.` }) + expect( + (logger.warn as jest.Mock).mock.calls + .sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b))) + ).toStrictEqual([ + [{ prefix: barPath, message: `The field "pnpm.overrides" was found in ${barPath}/package.json. This will not take effect. You should configure "pnpm.overrides" at the root of the workspace instead.` }], + [{ prefix: fooPath, message: `The field "pnpm.overrides" was found in ${fooPath}/package.json. This will not take effect. You should configure "pnpm.overrides" at the root of the workspace instead.` }], + [{ prefix: barPath, message: `The field "resolutions" was found in ${barPath}/package.json. This will not take effect. You should configure "resolutions" at the root of the workspace instead.` }], + ]) })