Skip to content

Commit

Permalink
fix bin links for link: protocol (fixes #5876) (#5879)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdwain authored and bestander committed May 25, 2018
1 parent 7f5947f commit ec7e671
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
9 changes: 9 additions & 0 deletions __tests__/commands/install/bin-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ test('Only top level (after hoisting) bin links should be linked', (): Promise<v
});
});

// fixes https://github.com/yarnpkg/yarn/issues/5876
test('can use link protocol to install a package that would not be found via node module resolution', (): Promise<
void,
> => {
return runInstall({binLinks: true}, {source: 'install-link-siblings', cwd: '/bar'}, async config => {
expect(await linkAt(config, 'node_modules', '.bin', 'standard')).toEqual('../standard/bin/cmd.js');
});
});

describe('with nohoist', () => {
// address https://github.com/yarnpkg/yarn/issues/5487
test('nohoist bin should be linked to its own local module', (): Promise<void> => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "bar",
"version": "0.0.0",
"main": "index.js",
"dependencies": {
"foo": "link:../foo"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "foo",
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"standard": "8.4.0"
}
}
11 changes: 9 additions & 2 deletions src/package-linker.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,14 @@ export default class PackageLinker {
const realLocations = await Promise.all(ref.locations.map(loc => fs.realpath(loc)));
realLocations.forEach(loc => allLocations.indexOf(loc) !== -1 || allLocations.push(loc));

const distancePairs = allLocations.map(loc => {
const locationBinLocPairs = allLocations.map(loc => [loc, binLoc]);
if (binLoc !== realBinLoc) {
locationBinLocPairs.push(...allLocations.map(loc => [loc, realBinLoc]));
}

const distancePairs = locationBinLocPairs.map(([loc, curBinLoc]) => {
let distance = 0;
let curLoc = realBinLoc;
let curLoc = curBinLoc;
let notFound = false;

while (path.join(curLoc, ref.name) !== loc && path.join(curLoc, moduleFolder, ref.name) !== loc) {
Expand All @@ -180,6 +185,8 @@ export default class PackageLinker {
const filteredDistancePairs: any = distancePairs.filter(d => d);
(filteredDistancePairs: Array<[string, number]>);

invariant(filteredDistancePairs.length > 0, `could not find a copy of ${pkg.name} to link in ${binLoc}`);

//get smallest distance from package location
const minItem = filteredDistancePairs.reduce((min, cur) => {
return cur[1] < min[1] ? cur : min;
Expand Down

0 comments on commit ec7e671

Please sign in to comment.