Skip to content

Commit

Permalink
Fixes incorrect entries in lockfiles
Browse files Browse the repository at this point in the history
Fixes #2629.
If lockfile pattern does not match a version it will be ignored and re-resolved
  • Loading branch information
bestander committed Apr 11, 2017
1 parent 205020b commit 61c134a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
15 changes: 15 additions & 0 deletions __tests__/commands/install/lockfiles.js
Expand Up @@ -288,3 +288,18 @@ test.concurrent('install should rewrite lockfile if patterns can be merged', ():
expect(lockContent).not.toContain('https://fakepath.wont.download.com/mime-db/-/mime-db-1.0.0.tgz');
});
});

test.concurrent('install should fix if lockfile patterns don\'t match resolved version', (): Promise<void> => {
const fixture = 'lockfile-fixed';

return runInstall({}, fixture, async (config, reporter) => {
const lockContent = await fs.readFile(
path.join(config.cwd, 'yarn.lock'),
);
expect(lockContent).not.toContain('mime-db-1.24.0.tgz');
expect(lockContent).toContain('mime-db-1.23.0.tgz');
expect(lockContent).not.toContain('left-pad-1.1.3.tgz');
expect(lockContent).toContain('left-pad-1.1.2.tgz');

});
});
6 changes: 6 additions & 0 deletions __tests__/fixtures/install/lockfile-fixed/package.json
@@ -0,0 +1,6 @@
{
"dependencies": {
"left-pad": ">=1.0.0 <1.1.3",
"mime-db": "1.23.0"
}
}
11 changes: 11 additions & 0 deletions __tests__/fixtures/install/lockfile-fixed/yarn.lock
@@ -0,0 +1,11 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"left-pad@>=1.0.0 <1.1.3":
version "1.1.3"
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.1.3.tgz#612f61c033f3a9e08e939f1caebeea41b6f3199a"

mime-db@1.23.0:
version "1.24.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.24.0.tgz#e2d13f939f0016c6e4e9ad25a8652f126c467f0c"
8 changes: 8 additions & 0 deletions src/lockfile/wrapper.js
Expand Up @@ -122,6 +122,14 @@ export default class Lockfile {
return undefined;
}

removePattern(pattern: string) {
const cache = this.cache;
if (!cache) {
return;
}
delete cache[pattern];
}

getLockfile(patterns: {
[packagePattern: string]: Manifest
}): Object {
Expand Down
16 changes: 10 additions & 6 deletions src/package-resolver.js
Expand Up @@ -438,15 +438,19 @@ export default class PackageResolver {
if (!lockfileEntry) {
this.newPatterns.push(req.pattern);
} else {
const {range} = PackageRequest.normalizePattern(req.pattern);
// TODO anything about exotic?
const {range, hasVersion} = PackageRequest.normalizePattern(req.pattern);
// lockfileEntry is incorrect, remove it from lockfile cache and consider the pattern as new
if (!PackageRequest.getExoticResolver(range) && !semver.satisfies(lockfileEntry.version, range)) {
// TODO print warning about incorrect lockfile entry for pattern
if (
semver.validRange(range) &&
semver.valid(lockfileEntry.version) &&
!semver.satisfies(lockfileEntry.version, range) &&
!PackageRequest.getExoticResolver(range) &&
hasVersion
) {
this.reporter.warn(this.reporter.lang('incorrectLockfileEntry', req.pattern));
this.removePattern(req.pattern);
this.newPatterns.push(req.pattern);
// TODO encapsulate in lockfile
delete this.lockfile.cache[req.pattern];
this.lockfile.removePattern(req.pattern);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/reporters/lang/en.js
Expand Up @@ -94,6 +94,7 @@ const messages = {
frozenLockfileError: 'Your lockfile needs to be updated, but yarn was run with `--frozen-lockfile`.',
fileWriteError: 'Could not write file $0: $1',
multiplePackagesCantUnpackInSameDestination: 'Pattern $0 is trying to unpack in the same destination $1 as pattern $2. This could result in a non deterministic behavior, skipping.',
incorrectLockfileEntry: 'Lockfile has incorrect entry for $0. Ingoring it.',

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

0 comments on commit 61c134a

Please sign in to comment.