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 <z@kochan.io>
This commit is contained in:
await-ovo
2023-06-09 19:02:47 +08:00
committed by GitHub
parent d9da627cd3
commit bd235794da
10 changed files with 75 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/find-workspace-packages": major
---
`@pnpm/logger` is added as a peer dependency.

View File

@@ -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)

3
pnpm-lock.yaml generated
View File

@@ -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

View File

@@ -40,6 +40,9 @@
"devDependencies": {
"@pnpm/find-workspace-packages": "workspace:*"
},
"peerDependencies": {
"@pnpm/logger": "^5.0.0"
},
"exports": {
".": "./lib/index.js"
}

View File

@@ -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,
})
}
}
}

View File

@@ -0,0 +1,7 @@
{
"name": "root",
"version": "1.0.0",
"pnpm": {
"overrides": {}
}
}

View File

@@ -0,0 +1,6 @@
{
"name": "bar",
"version": "1.0.0",
"pnpm": {},
"resolutions": {}
}

View File

@@ -0,0 +1,5 @@
{
"name": "foo",
"version": "1.0.0",
"pnpm": {}
}

View File

@@ -0,0 +1,2 @@
packages:
- packages/*

View File

@@ -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.` })
})