fix: replace pnpm warnings with subfield warnings (#8415)

close #8413
This commit is contained in:
Khải
2024-08-13 21:55:54 +07:00
committed by GitHub
parent 0ffb3adcfe
commit 82f7ed2112
5 changed files with 43 additions and 12 deletions

View File

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

View File

@@ -61,13 +61,28 @@ export async function findWorkspacePackagesNoCheck (workspaceRoot: string, opts?
return pkgs
}
const uselessNonRootManifestFields: Array<keyof ProjectManifest> = ['resolutions']
type ProjectManifestPnpm = Required<ProjectManifest>['pnpm']
const usefulNonRootPnpmFields: Array<keyof ProjectManifestPnpm> = ['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,
})
}

View File

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

View File

@@ -1,5 +1,8 @@
{
"name": "foo",
"version": "1.0.0",
"pnpm": {}
"pnpm": {
"overrides": {},
"executionEnv": {}
}
}

View File

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