From bd235794da530cafa8e2ba95a13da52edccaecb7 Mon Sep 17 00:00:00 2001 From: await-ovo <13152410380@163.com> Date: Fri, 9 Jun 2023 19:02:47 +0800 Subject: [PATCH] fix: should output warning info when "pnpm" or "resolutions" is configured in a non-root workspace project (#6658) close #6636 --------- Co-authored-by: Zoltan Kochan --- .changeset/chatty-hounds-hammer.md | 5 ++++ .changeset/nine-eagles-peel.md | 5 ++++ pnpm-lock.yaml | 3 +++ .../find-workspace-packages/package.json | 3 +++ .../find-workspace-packages/src/index.ts | 17 ++++++++++++- .../warning-for-non-root-project/package.json | 7 ++++++ .../packages/bar/package.json | 6 +++++ .../packages/foo/package.json | 5 ++++ .../pnpm-workspace.yaml | 2 ++ .../find-workspace-packages/test/index.ts | 24 ++++++++++++++++++- 10 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 .changeset/chatty-hounds-hammer.md create mode 100644 .changeset/nine-eagles-peel.md create mode 100644 workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/package.json create mode 100644 workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/packages/bar/package.json create mode 100644 workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/packages/foo/package.json create mode 100644 workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/pnpm-workspace.yaml diff --git a/.changeset/chatty-hounds-hammer.md b/.changeset/chatty-hounds-hammer.md new file mode 100644 index 0000000000..47685aaa74 --- /dev/null +++ b/.changeset/chatty-hounds-hammer.md @@ -0,0 +1,5 @@ +--- +"@pnpm/find-workspace-packages": major +--- + +`@pnpm/logger` is added as a peer dependency. diff --git a/.changeset/nine-eagles-peel.md b/.changeset/nine-eagles-peel.md new file mode 100644 index 0000000000..df0698253e --- /dev/null +++ b/.changeset/nine-eagles-peel.md @@ -0,0 +1,5 @@ +--- +"@pnpm/find-workspace-packages": patch +"pnpm": patch +--- +Output a warning message when "pnpm" or "resolutions" are configured in a non-root workspace project [#6636](https://github.com/pnpm/pnpm/issues/6636) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 67bcd93082..1f426fd7d3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5975,6 +5975,9 @@ importers: '@pnpm/fs.find-packages': specifier: workspace:* version: link:../../fs/find-packages + '@pnpm/logger': + specifier: ^5.0.0 + version: 5.0.0 '@pnpm/types': specifier: workspace:* version: link:../../packages/types diff --git a/workspace/find-workspace-packages/package.json b/workspace/find-workspace-packages/package.json index 80d39e7a02..1d37c7634d 100644 --- a/workspace/find-workspace-packages/package.json +++ b/workspace/find-workspace-packages/package.json @@ -40,6 +40,9 @@ "devDependencies": { "@pnpm/find-workspace-packages": "workspace:*" }, + "peerDependencies": { + "@pnpm/logger": "^5.0.0" + }, "exports": { ".": "./lib/index.js" } diff --git a/workspace/find-workspace-packages/src/index.ts b/workspace/find-workspace-packages/src/index.ts index 2e7a192155..1f6ddff1eb 100644 --- a/workspace/find-workspace-packages/src/index.ts +++ b/workspace/find-workspace-packages/src/index.ts @@ -1,9 +1,10 @@ import path from 'path' import { packageIsInstallable } from '@pnpm/cli-utils' import { WORKSPACE_MANIFEST_FILENAME } from '@pnpm/constants' -import { type Project } from '@pnpm/types' +import { type ProjectManifest, type Project } from '@pnpm/types' import { lexCompare } from '@pnpm/util.lex-comparator' import { findPackages } from '@pnpm/fs.find-packages' +import { logger } from '@pnpm/logger' import readYamlFile from 'read-yaml-file' export type { Project } @@ -19,6 +20,9 @@ export async function findWorkspacePackages ( const pkgs = await findWorkspacePackagesNoCheck(workspaceRoot, opts) for (const pkg of pkgs) { packageIsInstallable(pkg.dir, pkg.manifest, opts ?? {}) + if (pkg.dir !== workspaceRoot) { + checkNonRootProjectManifest(pkg) + } } return pkgs @@ -67,3 +71,14 @@ export function arrayOfWorkspacePackagesToMap ( return acc }, {} as ArrayOfWorkspacePackagesToMapResult) } + +function checkNonRootProjectManifest ({ manifest, dir }: Project) { + for (const rootOnlyField of ['pnpm', 'resolutions']) { + if (manifest?.[rootOnlyField as keyof ProjectManifest]) { + logger.warn({ + message: `The field "${rootOnlyField}" was found in ${dir}/package.json. This will not take effect. You should configure "${rootOnlyField}" at the root of the workspace instead.`, + prefix: dir, + }) + } + } +} diff --git a/workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/package.json b/workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/package.json new file mode 100644 index 0000000000..27a558a390 --- /dev/null +++ b/workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/package.json @@ -0,0 +1,7 @@ +{ + "name": "root", + "version": "1.0.0", + "pnpm": { + "overrides": {} + } +} diff --git a/workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/packages/bar/package.json b/workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/packages/bar/package.json new file mode 100644 index 0000000000..e71697df59 --- /dev/null +++ b/workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/packages/bar/package.json @@ -0,0 +1,6 @@ +{ + "name": "bar", + "version": "1.0.0", + "pnpm": {}, + "resolutions": {} +} diff --git a/workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/packages/foo/package.json b/workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/packages/foo/package.json new file mode 100644 index 0000000000..4c2122ec73 --- /dev/null +++ b/workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/packages/foo/package.json @@ -0,0 +1,5 @@ +{ + "name": "foo", + "version": "1.0.0", + "pnpm": {} +} diff --git a/workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/pnpm-workspace.yaml b/workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/pnpm-workspace.yaml new file mode 100644 index 0000000000..924b55f42e --- /dev/null +++ b/workspace/find-workspace-packages/test/__fixtures__/warning-for-non-root-project/pnpm-workspace.yaml @@ -0,0 +1,2 @@ +packages: + - packages/* diff --git a/workspace/find-workspace-packages/test/index.ts b/workspace/find-workspace-packages/test/index.ts index 2db0f3f7e0..5d069776be 100644 --- a/workspace/find-workspace-packages/test/index.ts +++ b/workspace/find-workspace-packages/test/index.ts @@ -1,5 +1,14 @@ import path from 'path' -import { findWorkspacePackagesNoCheck, arrayOfWorkspacePackagesToMap } from '@pnpm/find-workspace-packages' +import { findWorkspacePackagesNoCheck, arrayOfWorkspacePackagesToMap, findWorkspacePackages } from '@pnpm/find-workspace-packages' +import { logger } from '@pnpm/logger' + +beforeEach(() => { + jest.spyOn(logger, 'warn') +}) + +afterEach(() => { + (logger.warn as jest.Mock).mockRestore() +}) // This is supported for compatibility with Yarn's implementation // see https://github.com/pnpm/pnpm/issues/2648 @@ -21,3 +30,16 @@ test('findWorkspacePackagesNoCheck() skips engine checks', async () => { expect(pkgs.length).toBe(1) expect(pkgs[0].manifest.name).toBe('pkg') }) + +test('findWorkspacePackages() output warnings for non-root workspace project', async () => { + const fixturePath = path.join(__dirname, '__fixtures__/warning-for-non-root-project') + + const pkgs = await findWorkspacePackages(fixturePath) + expect(pkgs.length).toBe(3) + expect(logger.warn).toBeCalledTimes(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.` }) +})