Skip to content
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

Fixes yarn why -R #6251

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions .yarn/versions/d6ab7cea.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/plugin-essentials": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-nm"
- "@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/core"
- "@yarnpkg/doctor"
211 changes: 211 additions & 0 deletions packages/acceptance-tests/pkg-tests-specs/sources/commands/why.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
import {ppath} from '@yarnpkg/fslib';
import {fs, misc} from 'pkg-tests-core';

describe(`Commands`, () => {
describe(`why`, () => {
test(
`it should list the workspaces using a specific dependency`,
makeTemporaryEnv({
workspaces: [`packages/*`],
}, async ({path, run, source}) => {
await fs.writeJson(ppath.join(path, `packages/a/package.json`), {
name: `a`,
dependencies: {
[`no-deps`]: `1.0.0`,
},
});

await fs.writeJson(ppath.join(path, `packages/b/package.json`), {
name: `b`,
});

await run(`install`);

const {stdout} = await run(`why`, `no-deps`, `--json`);

expect(misc.parseJsonStream(stdout)).toEqual([{
value: `a@workspace:packages/a`,
children: {
[`no-deps@npm:1.0.0`]: {
descriptor: `no-deps@npm:1.0.0`,
locator: `no-deps@npm:1.0.0`,
},
},
}]);
}),
);

test(
`it should list the packages using a specific dependency`,
makeTemporaryEnv({
workspaces: [`packages/*`],
}, async ({path, run, source}) => {
await fs.writeJson(ppath.join(path, `packages/a/package.json`), {
name: `a`,
dependencies: {
[`one-fixed-dep`]: `1.0.0`,
},
});

await fs.writeJson(ppath.join(path, `packages/b/package.json`), {
name: `b`,
});

await run(`install`);

const {stdout} = await run(`why`, `no-deps`, `--json`);

expect(misc.parseJsonStream(stdout)).toEqual([{
value: `one-fixed-dep@npm:1.0.0`,
children: {
[`no-deps@npm:1.0.0`]: {
descriptor: `no-deps@npm:1.0.0`,
locator: `no-deps@npm:1.0.0`,
},
},
}]);
}),
);

test(
`it should list workspaces transitively using a specific dependency`,
makeTemporaryEnv({
workspaces: [`packages/*`],
}, async ({path, run, source}) => {
await fs.writeJson(ppath.join(path, `packages/a/package.json`), {
name: `a`,
dependencies: {
[`one-fixed-dep`]: `1.0.0`,
},
});

await fs.writeJson(ppath.join(path, `packages/b/package.json`), {
name: `b`,
});

await run(`install`);

const {stdout} = await run(`why`, `-R`, `no-deps`, `--json`);

expect(misc.parseJsonStream(stdout)).toEqual([{
value: `a@workspace:packages/a`,
children: {
[`one-fixed-dep@npm:1.0.0`]: {
value: {
descriptor: `one-fixed-dep@npm:1.0.0`,
locator: `one-fixed-dep@npm:1.0.0`,
},
children: {
[`no-deps@npm:1.0.0`]: {
value: {
descriptor: `no-deps@npm:1.0.0`,
locator: `no-deps@npm:1.0.0`,
},
children: {},
},
},
},
},
}]);
}),
);

test(
`it should list workspaces transitively using a specific dependency (A = regular dep, B = workspace dep)`,
makeTemporaryEnv({
workspaces: [`packages/*`],
}, async ({path, run, source}) => {
await fs.writeJson(ppath.join(path, `packages/a/package.json`), {
name: `a`,
dependencies: {
[`no-deps`]: `1.0.0`,
},
});

await fs.writeJson(ppath.join(path, `packages/b/package.json`), {
name: `b`,
dependencies: {
[`a`]: `workspace:^`,
},
});

await run(`install`);

const {stdout} = await run(`why`, `-R`, `no-deps`, `--json`);

expect(misc.parseJsonStream(stdout)).toEqual([{
value: `a@workspace:packages/a`,
children: {
[`no-deps@npm:1.0.0`]: {
value: {
descriptor: `no-deps@npm:1.0.0`,
locator: `no-deps@npm:1.0.0`,
},
children: {},
},
},
}, {
value: `b@workspace:packages/b`,
children: {
[`a@workspace:packages/a`]: {
value: {
descriptor: `a@workspace:^`,
locator: `a@workspace:packages/a`,
},
children: {},
},
},
}]);
}),
);

test(
`it should list workspaces transitively using a specific dependency (A = workspace dep, B = regular dep)`,
makeTemporaryEnv({
workspaces: [`packages/*`],
}, async ({path, run, source}) => {
await fs.writeJson(ppath.join(path, `packages/a/package.json`), {
name: `a`,
dependencies: {
[`b`]: `workspace:^`,
},
});

await fs.writeJson(ppath.join(path, `packages/b/package.json`), {
name: `b`,
dependencies: {
[`no-deps`]: `1.0.0`,
},
});

await run(`install`);

const {stdout} = await run(`why`, `-R`, `no-deps`, `--json`);

expect(misc.parseJsonStream(stdout)).toEqual([{
value: `a@workspace:packages/a`,
children: {
[`b@workspace:packages/b`]: {
value: {
descriptor: `b@workspace:^`,
locator: `b@workspace:packages/b`,
},
children: {},
},
},
}, {
value: `b@workspace:packages/b`,
children: {
[`no-deps@npm:1.0.0`]: {
value: {
descriptor: `no-deps@npm:1.0.0`,
locator: `no-deps@npm:1.0.0`,
},
children: {},
},
},
}]);
}),
);
});
});
10 changes: 5 additions & 5 deletions packages/plugin-essentials/sources/commands/why.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,18 @@ function whyRecursive(project: Project, identHash: IdentHash, {configuration, pe
const key = structUtils.stringifyLocator(pkg);
parentChildren[key] = node;

// We don't want to print the children of our transitive workspace
// dependencies, as they will be printed in their own top-level branch
if (dependency !== null && project.tryWorkspaceByLocator(pkg))
return;

// We don't want to reprint the children for a package that already got
// printed as part of another branch
if (printed.has(pkg.locatorHash))
return;

printed.add(pkg.locatorHash);

// We don't want to print the children of our transitive workspace
// dependencies, as they will be printed in their own top-level branch
if (dependency !== null && project.tryWorkspaceByLocator(pkg))
return;

for (const dependency of pkg.dependencies.values()) {
if (!peers && pkg.peerDependencies.has(dependency.identHash))
continue;
Expand Down