Skip to content

Commit c32d696

Browse files
authored
Fix resolver incorrect lockfile (issue #2629) (#3106)
* wip fixing resolver when lockfile has incorrect semver entries * Fixes incorrect entries in lockfiles Fixes #2629. If lockfile pattern does not match a version it will be ignored and re-resolved
1 parent c92411d commit c32d696

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

__tests__/commands/install/lockfiles.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,18 @@ test.concurrent('install should rewrite lockfile if patterns can be merged', ():
288288
expect(lockContent).not.toContain('https://fakepath.wont.download.com/mime-db/-/mime-db-1.0.0.tgz');
289289
});
290290
});
291+
292+
test.concurrent('install should fix if lockfile patterns don\'t match resolved version', (): Promise<void> => {
293+
const fixture = 'lockfile-fixed';
294+
295+
return runInstall({}, fixture, async (config, reporter) => {
296+
const lockContent = await fs.readFile(
297+
path.join(config.cwd, 'yarn.lock'),
298+
);
299+
expect(lockContent).not.toContain('mime-db-1.24.0.tgz');
300+
expect(lockContent).toContain('mime-db-1.23.0.tgz');
301+
expect(lockContent).not.toContain('left-pad-1.1.3.tgz');
302+
expect(lockContent).toContain('left-pad-1.1.2.tgz');
303+
304+
});
305+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"dependencies": {
3+
"left-pad": ">=1.0.0 <1.1.3",
4+
"mime-db": "1.23.0"
5+
}
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
"left-pad@>=1.0.0 <1.1.3":
6+
version "1.1.3"
7+
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.1.3.tgz#612f61c033f3a9e08e939f1caebeea41b6f3199a"
8+
9+
10+
version "1.24.0"
11+
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.24.0.tgz#e2d13f939f0016c6e4e9ad25a8652f126c467f0c"

src/lockfile/wrapper.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ export default class Lockfile {
122122
return undefined;
123123
}
124124

125+
removePattern(pattern: string) {
126+
const cache = this.cache;
127+
if (!cache) {
128+
return;
129+
}
130+
delete cache[pattern];
131+
}
132+
125133
getLockfile(patterns: {
126134
[packagePattern: string]: Manifest
127135
}): Object {

src/package-resolver.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,24 @@ export default class PackageResolver {
434434
this.activity.tick(req.pattern);
435435
}
436436

437-
if (!this.lockfile.getLocked(req.pattern, true)) {
437+
const lockfileEntry = this.lockfile.getLocked(req.pattern);
438+
if (!lockfileEntry) {
438439
this.newPatterns.push(req.pattern);
440+
} else {
441+
const {range, hasVersion} = PackageRequest.normalizePattern(req.pattern);
442+
// lockfileEntry is incorrect, remove it from lockfile cache and consider the pattern as new
443+
if (
444+
semver.validRange(range) &&
445+
semver.valid(lockfileEntry.version) &&
446+
!semver.satisfies(lockfileEntry.version, range) &&
447+
!PackageRequest.getExoticResolver(range) &&
448+
hasVersion
449+
) {
450+
this.reporter.warn(this.reporter.lang('incorrectLockfileEntry', req.pattern));
451+
this.removePattern(req.pattern);
452+
this.newPatterns.push(req.pattern);
453+
this.lockfile.removePattern(req.pattern);
454+
}
439455
}
440456

441457
const request = new PackageRequest(req, this);

src/reporters/lang/en.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const messages = {
9494
frozenLockfileError: 'Your lockfile needs to be updated, but yarn was run with `--frozen-lockfile`.',
9595
fileWriteError: 'Could not write file $0: $1',
9696
multiplePackagesCantUnpackInSameDestination: 'Pattern $0 is trying to unpack in the same destination $1 as pattern $2. This could result in a non deterministic behavior, skipping.',
97+
incorrectLockfileEntry: 'Lockfile has incorrect entry for $0. Ingoring it.',
9798

9899
yarnOutdated: "Your current version of Yarn is out of date. The latest version is $0 while you're on $1.",
99100
yarnOutdatedInstaller: 'To upgrade, download the latest installer at $0.',

0 commit comments

Comments
 (0)