Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly resolve peerDependencies for concurrent installs #2801

Merged
merged 2 commits into from
Feb 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions __tests__/commands/add.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @flow */

import {ConsoleReporter} from '../../src/reporters/index.js';
import * as reporters from '../../src/reporters/index.js';
import {getPackageVersion, createLockfile, explodeLockfile, run as buildRun, runInstall} from './_helpers.js';
import {Add} from '../../src/cli/commands/add.js';
import * as constants from '../../src/constants.js';
Expand Down Expand Up @@ -661,3 +662,55 @@ test.concurrent('install with latest tag and --prefer-offline flag', (): Promise
assert.notEqual(version, '1.1.0');
});
});

test.concurrent('doesn\'t warn when peer dependency is met during add', (): Promise<void> => {
return buildRun(
reporters.BufferReporter,
fixturesLoc,
async (args, flags, config, reporter, lockfile): Promise<void> => {
const add = new Add(args, flags, config, reporter, lockfile);
await add.init();
const output = reporter.getBuffer();
const warnings = output.filter((entry) => entry.type === 'warning');
assert(!warnings.some((warning) => warning.data.toString().toLowerCase().includes('unmet peer')));
assert(!warnings.some((warning) => warning.data.toString().toLowerCase().includes('incorrect peer')));
},
['[email protected]', '[email protected]'],
{},
'add-with-peer-dependency-met',
);
});

test.concurrent('warns when peer dependency is not met during add', (): Promise<void> => {
return buildRun(
reporters.BufferReporter,
fixturesLoc,
async (args, flags, config, reporter, lockfile): Promise<void> => {
const add = new Add(args, flags, config, reporter, lockfile);
await add.init();
const output = reporter.getBuffer();
const warnings = output.filter((entry) => entry.type === 'warning');
assert(warnings.some((warning) => warning.data.toString().toLowerCase().includes('unmet peer')));
},
['[email protected]'],
{},
'add-with-peer-dependency-not-met',
);
});

test.concurrent('warns when peer dependency is incorrect during add', (): Promise<void> => {
return buildRun(
reporters.BufferReporter,
fixturesLoc,
async (args, flags, config, reporter, lockfile): Promise<void> => {
const add = new Add(args, flags, config, reporter, lockfile);
await add.init();
const output = reporter.getBuffer();
const warnings = output.filter((entry) => entry.type === 'warning');
assert(warnings.some((warning) => warning.data.toString().toLowerCase().includes('incorrect peer')));
},
['[email protected]', '[email protected]'],
{},
'add-with-peer-dependency-incorrect',
);
});
Empty file.
Empty file.
Empty file.
50 changes: 9 additions & 41 deletions src/package-linker.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,48 +282,16 @@ export default class PackageLinker {

for (const name in peerDeps) {
const range = peerDeps[name];

// find a dependency in the tree above us that matches
let searchPatterns: Array<string> = [];
for (let request of ref.requests) {
do {
// get resolved pattern for this request
const dep = this.resolver.getResolvedPattern(request.pattern);
if (!dep) {
continue;
}

//
const ref = dep._reference;
invariant(ref, 'expected reference');
searchPatterns = searchPatterns.concat(ref.dependencies);
} while (request = request.parentRequest);
}

// if the resolver already knows about the peer dependency, add those patterns as well
const packagePatterns = this.resolver.patternsByPackage[name];
if (packagePatterns) {
searchPatterns = searchPatterns.concat(packagePatterns);
}

// include root seed patterns last
searchPatterns = searchPatterns.concat(this.resolver.seedPatterns);

// find matching dep in search patterns
let foundDep: ?{pattern: string, version: string};
for (const pattern of searchPatterns) {
const dep = this.resolver.getResolvedPattern(pattern);
if (dep && dep.name === name) {
foundDep = {pattern, version: dep.version};
break;
}
}

// validate found peer dependency
if (foundDep && this._satisfiesPeerDependency(range, foundDep.version)) {
ref.addDependencies([foundDep.pattern]);
const patterns = this.resolver.patternsByPackage[name] || [];
const foundPattern = patterns.find((pattern) => {
const resolvedPattern = this.resolver.getResolvedPattern(pattern);
return resolvedPattern ? this._satisfiesPeerDependency(range, resolvedPattern.version) : false;
});

if (foundPattern) {
ref.addDependencies([foundPattern]);
} else {
const depError = foundDep ? 'incorrectPeer' : 'unmetPeer';
const depError = patterns.length > 0 ? 'incorrectPeer' : 'unmetPeer';
const [pkgHuman, depHuman] = [`${pkg.name}@${pkg.version}`, `${name}@${range}`];
this.reporter.warn(this.reporter.lang(depError, pkgHuman, depHuman));
}
Expand Down