Skip to content

Commit d57fc87

Browse files
committed
fix: handle out of sync in resolutions for yarn 2
1 parent 6f3dcdc commit d57fc87

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

lib/dep-graph-builders/yarn-lock-v2/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ export const getYarnLockV2ChildNode = (
118118

119119
if (resolvedVersionFromResolution) {
120120
const childNodeKeyFromResolution = `${name}@${resolvedVersionFromResolution}`;
121+
if (!pkgs[childNodeKeyFromResolution]) {
122+
if (strictOutOfSync && !/^file:/.test(resolvedVersionFromResolution)) {
123+
throw new OutOfSyncError(
124+
childNodeKeyFromResolution,
125+
LockfileType.yarn2,
126+
);
127+
} else {
128+
return {
129+
id: childNodeKeyFromResolution,
130+
name: name,
131+
version: resolvedVersionFromResolution,
132+
dependencies: {},
133+
isDev: depInfo.isDev,
134+
missingLockFileEntry: true,
135+
};
136+
}
137+
}
138+
121139
const {
122140
version: versionFromResolution,
123141
dependencies,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"schemaVersion": "1.3.0",
3+
"pkgManager": {
4+
"name": "yarn"
5+
},
6+
"pkgs": [
7+
{
8+
9+
"info": {
10+
"name": "resolutions-out-of-sync",
11+
"version": "0.0.1"
12+
}
13+
},
14+
{
15+
16+
"info": {
17+
"name": "debug",
18+
"version": "4.3.1"
19+
}
20+
},
21+
{
22+
23+
"info": {
24+
"name": "ms",
25+
"version": "1.0.0"
26+
}
27+
}
28+
],
29+
"graph": {
30+
"rootNodeId": "root-node",
31+
"nodes": [
32+
{
33+
"nodeId": "root-node",
34+
"pkgId": "[email protected]",
35+
"deps": [
36+
{
37+
"nodeId": "[email protected]"
38+
}
39+
]
40+
},
41+
{
42+
"nodeId": "[email protected]",
43+
"pkgId": "[email protected]",
44+
"deps": [
45+
{
46+
"nodeId": "[email protected]"
47+
}
48+
],
49+
"info": {
50+
"labels": {
51+
"scope": "prod"
52+
}
53+
}
54+
},
55+
{
56+
"nodeId": "[email protected]",
57+
"pkgId": "[email protected]",
58+
"deps": [],
59+
"info": {
60+
"labels": {
61+
"scope": "prod"
62+
}
63+
}
64+
}
65+
]
66+
}
67+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "resolutions-out-of-sync",
3+
"version": "0.0.1",
4+
"dependencies": {
5+
"debug": "4.3.1"
6+
},
7+
"resolutions": {
8+
"ms": "1.0.0"
9+
}
10+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This file is generated by running "yarn install" inside your project.
2+
# Manual changes might be lost - proceed with caution!
3+
4+
__metadata:
5+
version: 4
6+
cacheKey: 7
7+
8+
"debug@npm:4.3.1":
9+
version: 4.3.1
10+
resolution: "debug@npm:4.3.1"
11+
dependencies:
12+
ms: 2.1.2
13+
peerDependenciesMeta:
14+
supports-color:
15+
optional: true
16+
checksum: 0d41ba5177510e8b388dfd7df143ab0f9312e4abdaba312595461511dac88e9ef8101939d33b4e6d37e10341af6a5301082e4d7d6f3deb4d57bc05fc7d296fad
17+
languageName: node
18+
linkType: hard
19+
20+
"resolutions-simple@workspace:.":
21+
version: 0.0.0-use.local
22+
resolution: "resolutions-simple@workspace:."
23+
dependencies:
24+
debug: 4.3.1
25+
languageName: unknown
26+
linkType: soft

test/jest/dep-graph-builders/yarn-lock-v2.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { readFileSync } from 'fs';
33
import { join } from 'path';
44
import { YarnLockV2ProjectParseOptions } from '../../../lib/dep-graph-builders/types';
55
import { parseYarnLockV2Project } from '../../../lib/dep-graph-builders/yarn-lock-v2/simple';
6+
import { LockfileType, OutOfSyncError } from '../../../lib';
67

78
describe('yarn.lock v2 "real" projects', () => {
89
describe.each([
910
'git-remote-url',
1011
'goof',
1112
'resolutions-simple',
1213
'resolutions-scoped',
14+
'out-of-sync-resolutions',
1315
])('[simple tests] project: %s ', (fixtureName) => {
1416
test('matches expected - no pruning', async () => {
1517
const pkgJsonContent = readFileSync(
@@ -56,6 +58,31 @@ describe('yarn.lock v2 "real" projects', () => {
5658
});
5759
});
5860

61+
test('project: out-of-sync-resolutions -> throws OutOfSyncError', async () => {
62+
const fixtureName = 'out-of-sync-resolutions';
63+
const pkgJsonContent = readFileSync(
64+
join(
65+
__dirname,
66+
`./fixtures/yarn-lock-v2/real/${fixtureName}/package.json`,
67+
),
68+
'utf8',
69+
);
70+
const yarnLockContent = readFileSync(
71+
join(__dirname, `./fixtures/yarn-lock-v2/real/${fixtureName}/yarn.lock`),
72+
'utf8',
73+
);
74+
const opts: YarnLockV2ProjectParseOptions = {
75+
includeDevDeps: false,
76+
includeOptionalDeps: true,
77+
strictOutOfSync: true,
78+
pruneWithinTopLevelDeps: false,
79+
};
80+
81+
await expect(
82+
parseYarnLockV2Project(pkgJsonContent, yarnLockContent, opts),
83+
).rejects.toThrow(new OutOfSyncError('[email protected]', LockfileType.yarn2));
84+
});
85+
5986
it('Workspace with resolutions', async () => {
6087
const rootPkgJsonContent = readFileSync(
6188
join(

0 commit comments

Comments
 (0)