Skip to content

Commit 61c134a

Browse files
committed
Fixes incorrect entries in lockfiles
Fixes #2629. If lockfile pattern does not match a version it will be ignored and re-resolved
1 parent 205020b commit 61c134a

File tree

6 files changed

+51
-6
lines changed

6 files changed

+51
-6
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: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -438,15 +438,19 @@ export default class PackageResolver {
438438
if (!lockfileEntry) {
439439
this.newPatterns.push(req.pattern);
440440
} else {
441-
const {range} = PackageRequest.normalizePattern(req.pattern);
442-
// TODO anything about exotic?
441+
const {range, hasVersion} = PackageRequest.normalizePattern(req.pattern);
443442
// lockfileEntry is incorrect, remove it from lockfile cache and consider the pattern as new
444-
if (!PackageRequest.getExoticResolver(range) && !semver.satisfies(lockfileEntry.version, range)) {
445-
// TODO print warning about incorrect lockfile entry for pattern
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));
446451
this.removePattern(req.pattern);
447452
this.newPatterns.push(req.pattern);
448-
// TODO encapsulate in lockfile
449-
delete this.lockfile.cache[req.pattern];
453+
this.lockfile.removePattern(req.pattern);
450454
}
451455
}
452456

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)