Skip to content

Commit 73f0354

Browse files
spanishpeararcanis
andauthored
fix: correctly install implicit nested types dependencies (#6800)
## What's the problem this PR addresses? Closes #6442 By default - yarn will automatically add `@types/foo` for a package, if the package has a peer dependency on `foo`. This happens in the `normalizePackage` core, and exists as packages often don't specify the correct peer dependencies. However, given a scenario where: ```json "dependencies": { "peer-deps-implicit-types-conflict": "1.0.0", "@types/no-deps": "1.0.0", } ``` where `peer-deps-implicit-types-conflict` has a dependency on `@types/no-deps:2.0.0`, ```json "dependencies": { "@types/no-deps": "2.0.0" }, "peerDependencies": { "no-deps": "1.0.0" } ``` yarn will only install `@types/no-deps:1.0.0` - even though `@types/no-deps: 2.0.0` is specified as a dependency! ## How did you fix it? The fix is rather trivial and thanks to the helpful comment here - #6442 (comment) - simply don't install the automatic `@types/foo` for a package that has `foo` as a peer-dependency, if the package already specifies `@types/foo` as a **dependency**. There is already logic for this behavior when it comes to `@types/` already existing in `peerDependencies` or `peerDependenciesMeta` - this PR just extends that to `dependencies` as well. ... ## Checklist <!--- Don't worry if you miss something, chores are automatically tested. --> <!--- This checklist exists to help you remember doing the chores when you submit a PR. --> <!--- Put an `x` in all the boxes that apply. --> - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). <!-- See https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released for more details. --> <!-- Check with `yarn version check` and fix with `yarn version check -i` --> - [x] I have set the packages that need to be released for my changes to be effective. <!-- The "Testing chores" workflow validates that your PR follows our guidelines. --> <!-- If it doesn't pass, click on it to see details as to what your PR might be missing. --> - [x] I will check that all automated PR checks pass before the PR gets reviewed. --------- Co-authored-by: Maël Nison <[email protected]>
1 parent 139617c commit 73f0354

File tree

6 files changed

+76
-6
lines changed

6 files changed

+76
-6
lines changed

.pnp.cjs

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.yarn/versions/29002943.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/core": patch
4+
5+
declined:
6+
- "@yarnpkg/plugin-compat"
7+
- "@yarnpkg/plugin-constraints"
8+
- "@yarnpkg/plugin-dlx"
9+
- "@yarnpkg/plugin-essentials"
10+
- "@yarnpkg/plugin-exec"
11+
- "@yarnpkg/plugin-file"
12+
- "@yarnpkg/plugin-git"
13+
- "@yarnpkg/plugin-github"
14+
- "@yarnpkg/plugin-http"
15+
- "@yarnpkg/plugin-init"
16+
- "@yarnpkg/plugin-interactive-tools"
17+
- "@yarnpkg/plugin-jsr"
18+
- "@yarnpkg/plugin-link"
19+
- "@yarnpkg/plugin-nm"
20+
- "@yarnpkg/plugin-npm"
21+
- "@yarnpkg/plugin-npm-cli"
22+
- "@yarnpkg/plugin-pack"
23+
- "@yarnpkg/plugin-patch"
24+
- "@yarnpkg/plugin-pnp"
25+
- "@yarnpkg/plugin-pnpm"
26+
- "@yarnpkg/plugin-stage"
27+
- "@yarnpkg/plugin-typescript"
28+
- "@yarnpkg/plugin-version"
29+
- "@yarnpkg/plugin-workspace-tools"
30+
- "@yarnpkg/builder"
31+
- "@yarnpkg/doctor"
32+
- "@yarnpkg/extensions"
33+
- "@yarnpkg/nm"
34+
- "@yarnpkg/pnpify"
35+
- "@yarnpkg/sdks"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = require(`./package.json`);
2+
3+
for (const key of [`dependencies`, `devDependencies`, `peerDependencies`]) {
4+
for (const dep of Object.keys(module.exports[key] || {})) {
5+
module.exports[key][dep] = require(dep);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "peer-deps-implicit-types-conflict",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"@types/no-deps": "2.0.0"
6+
},
7+
"peerDependencies": {
8+
"no-deps": "1.0.0"
9+
}
10+
}

packages/acceptance-tests/pkg-tests-specs/sources/features/peerDependenciesMeta.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,29 @@ describe(`Features`, () => {
100100
},
101101
),
102102
);
103+
104+
test(
105+
`it should correctly resolve nested dependencies with different versions of types packages`,
106+
makeTemporaryEnv(
107+
{
108+
dependencies: {
109+
'peer-deps-implicit-types-conflict': `1.0.0`,
110+
'@types/no-deps': `1.0.0`,
111+
},
112+
},
113+
async ({path, run, source}) => {
114+
await run(`install`);
115+
116+
await expect(
117+
source(`require('@types/no-deps').version`),
118+
).resolves.toEqual(`1.0.0`);
119+
120+
await expect(
121+
source(`require(require.resolve('@types/no-deps', { paths: [require.resolve('peer-deps-implicit-types-conflict/package.json')] })).version`),
122+
).resolves.toEqual(`2.0.0`);
123+
},
124+
),
125+
);
103126
});
104127
});
105128

packages/yarnpkg-core/sources/Configuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,7 @@ export class Configuration {
19711971
const typesIdent = structUtils.makeIdent(`types`, typesName);
19721972
const stringifiedTypesIdent = structUtils.stringifyIdent(typesIdent);
19731973

1974-
if (pkg.peerDependencies.has(typesIdent.identHash) || pkg.peerDependenciesMeta.has(stringifiedTypesIdent))
1974+
if (pkg.peerDependencies.has(typesIdent.identHash) || pkg.peerDependenciesMeta.has(stringifiedTypesIdent) || pkg.dependencies.has(typesIdent.identHash))
19751975
continue;
19761976

19771977
pkg.peerDependencies.set(typesIdent.identHash, structUtils.makeDescriptor(typesIdent, `*`));

0 commit comments

Comments
 (0)