Skip to content

feat: show warning when dependenciesMeta.{built,unplugged} is declared in non-top-level workspace #3923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .yarn/versions/42313c0a.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
releases:
"@yarnpkg/cli": minor
"@yarnpkg/core": minor

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export const mtme = (
const workspacePath = ppath.join(path, workspace as PortablePath);
await fsUtils.mkdirp(workspacePath);

await fsUtils.writeJson(ppath.join(workspacePath, Filename.manifest), await deepResolve(manifest));
await fsUtils.writeJson(ppath.join(workspacePath, Filename.manifest), await deepResolve({
name: ppath.basename(workspace as PortablePath),
...manifest,
}));
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,16 @@ describe(`Features`, () => {
let {stdout} = await run(`install`);
// sanity check
expect(stdout).toMatch(/Project validation/);
expect(stdout).toMatch(/Resolutions field will be ignored/);
expect(stdout).toMatch(/foo: Field only allowed in top-level workspace manifest, will be ignored: resolutions/);

await run(`config`, `set`, `logFilters`, `--json`, JSON.stringify([{
text: `foo: Resolutions field will be ignored`,
text: `foo: Field only allowed in top-level workspace manifest, will be ignored: resolutions`,
level: `discard`,
}]));

({stdout} = await run(`install`));
expect(stdout).not.toMatch(/Project validation/);
expect(stdout).not.toMatch(/Resolutions field will be ignored/);
expect(stdout).not.toMatch(/foo: Field only allowed in top-level workspace manifest, will be ignored: resolutions/);
}));

test(`it should not print the peer dependencies footer if the section contents are discarded via logFilters`, makeTemporaryEnv({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe(`Workspaces tests`, () => {
);

test(
`it should not implicitely make workspaces require-able`,
`it should not implicitly make workspaces require-able`,
makeTemporaryEnv(
{
private: true,
Expand All @@ -84,7 +84,7 @@ describe(`Workspaces tests`, () => {
);

test(
`it should allow workspaces to require each others`,
`it should allow workspaces to require each other`,
makeTemporaryEnv(
{
private: true,
Expand Down Expand Up @@ -281,4 +281,48 @@ describe(`Workspaces tests`, () => {
},
),
);

test(
`it should show warnings when top-level manifest fields are declared inside workspace manifests`,
makeTemporaryMonorepoEnv({
workspaces: [
`packages/*`,
],
}, {
[`packages/foo`]: {
resolutions: {
[`x`]: `1.0.0`,
},
},
[`packages/bar`]: {
dependenciesMeta: {
[`x`]: {
built: false,
},
},
},
[`packages/baz`]: {
dependenciesMeta: {
[`x`]: {
unplugged: true,
},
},
},
[`packages/qux`]: {
dependenciesMeta: {
[`x`]: {
optional: true,
},
},
},
}, async ({path, run, source}) => {
const {stdout} = await run(`install`);

expect(stdout).toContain(`foo: Field only allowed in top-level workspace manifest, will be ignored: resolutions`);
expect(stdout).toContain(`bar: Field only allowed in top-level workspace manifest, will be ignored: dependenciesMeta ➤ x ➤ built`);
expect(stdout).toContain(`baz: Field only allowed in top-level workspace manifest, will be ignored: dependenciesMeta ➤ x ➤ unplugged`);

expect(stdout).not.toContain(`qux: Field only allowed in top-level workspace manifest, will be ignored: dependenciesMeta ➤ x ➤ optional`);
}),
);
});
7 changes: 5 additions & 2 deletions packages/yarnpkg-core/sources/CorePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ export const CorePlugin: Plugin = {
// Validate manifest
const {manifest} = workspace;

if (manifest.resolutions.length && workspace.cwd !== workspace.project.cwd)
manifest.errors.push(new Error(`Resolutions field will be ignored`));
if (workspace.cwd !== workspace.project.cwd) {
for (const field of manifest.topLevelWorkspaceFields) {
manifest.errors.push(new Error(`Field only allowed in top-level workspace manifest, will be ignored: ${field}`));
}
}

for (const manifestError of manifest.errors) {
report.reportWarning(MessageName.INVALID_MANIFEST, manifestError.message);
Expand Down
15 changes: 14 additions & 1 deletion packages/yarnpkg-core/sources/Manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,15 @@ export class Manifest {
public raw: {[key: string]: any} = {};

/**
* errors found in the raw manifest while loading
* Errors found in the raw manifest while loading.
*/
public errors: Array<Error> = [];

/**
* Errors about these fields will be pushed to `this.errors` during the validation step if they are not declared inside the top-level workspace manifest.
*/
public topLevelWorkspaceFields: Set<string> = new Set();

static readonly fileName = `package.json` as Filename;

static readonly allDependencies: Array<AllDependencies> = [`dependencies`, `devDependencies`, `peerDependencies`];
Expand Down Expand Up @@ -186,6 +191,7 @@ export class Manifest {

this.raw = data;
const errors: Array<Error> = [];
const topLevelWorkspaceFields: Set<string> = new Set();

this.name = null;
if (typeof data.name === `string`) {
Expand Down Expand Up @@ -427,6 +433,8 @@ export class Manifest {
errors.push(new Error(`Invalid built meta field for '${pattern}'`));
continue;
}
if (typeof built !== `undefined`)
topLevelWorkspaceFields.add(`dependenciesMeta ➤ ${pattern} ➤ built`);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's really unfortunate that we can't use colors in here, but it feels wrong to pass configuration as part of an option bag parameter to Manifest.prototype.load.


const optional = tryParseOptionalBoolean(meta.optional, {yamlCompatibilityMode});
if (optional === null) {
Expand All @@ -439,6 +447,8 @@ export class Manifest {
errors.push(new Error(`Invalid unplugged meta field for '${pattern}'`));
continue;
}
if (typeof unplugged !== `undefined`)
topLevelWorkspaceFields.add(`dependenciesMeta ➤ ${pattern} ➤ unplugged`);

Object.assign(dependencyMeta, {built, optional, unplugged});
}
Expand Down Expand Up @@ -467,6 +477,8 @@ export class Manifest {

this.resolutions = [];
if (typeof data.resolutions === `object` && data.resolutions !== null) {
topLevelWorkspaceFields.add(`resolutions`);

for (const [pattern, reference] of Object.entries(data.resolutions)) {
if (typeof reference !== `string`) {
errors.push(new Error(`Invalid resolution entry for '${pattern}'`));
Expand Down Expand Up @@ -624,6 +636,7 @@ export class Manifest {
this.preferUnplugged = null;

this.errors = errors;
this.topLevelWorkspaceFields = topLevelWorkspaceFields;
}

getForScope(type: string) {
Expand Down