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

Fix transient symlinks overriding direct ones v2 #5016

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
11 changes: 11 additions & 0 deletions __tests__/commands/install/bin-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ test('first dep is installed when same level and reference count and one is a de
});
});

// Scenario: Transitive dependency having bin link with a name that's conflicting with that of a direct dependency.
// Behavior: a-dep and b-dep is linked in node_modules/.bin rather than c-dep and d-dep
test('direct dependency is linked when bin name conflicts with transitive dependency', (): Promise<void> => {
return runInstall({binLinks: true}, 'install-bin-links-conflicting-names', async config => {
const stdout1 = await execCommand(config.cwd, ['node_modules', '.bin', 'binlink1'], []);
const stdout2 = await execCommand(config.cwd, ['node_modules', '.bin', 'binlink2'], []);
expect(stdout1[0]).toEqual('direct a-dep');
expect(stdout2[0]).toEqual('direct f-dep');
});
});

// fixes https://github.com/yarnpkg/yarn/issues/3535
// quite a heavy test, did not find a way to isolate
test('Only top level (after hoisting) bin links should be linked', (): Promise<void> => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log('direct a-dep');
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log('transient c-dep');
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "c-dep",
"version": "0.0.1",
"bin": {
"binlink2": "./bin"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "a-dep",
"version": "0.0.1",
"bin": {
"binlink1": "./bin"
},
"dependencies": {
"c-dep": "file:c-dep"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log('direct f-dep');
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log('transient d-dep');
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "d-dep",
"version": "0.0.1",
"bin": {
"binlink1": "./bin"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "b-dep",
"version": "0.0.1",
"bin": {
"binlink2": "./bin"
},
"dependencies": {
"d-dep": "file:d-dep"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"a-dep": "file:a-dep",
"f-dep": "file:f-dep"
}
}
27 changes: 21 additions & 6 deletions src/package-linker.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ export default class PackageLinker {

// create binary links
if (this.config.binLinks) {
const topLevelDependencies = this.determineTopLevelBinLinks(flatTree);
const topLevelDependencies = this.determineTopLevelBinLinkOrder(flatTree);
const tickBin = this.reporter.progress(flatTree.length + topLevelDependencies.length);

// create links in transient dependencies
Expand All @@ -420,7 +420,7 @@ export default class PackageLinker {
// create links at top level for all dependencies.
await promise.queue(
topLevelDependencies,
async ([dest, pkg]) => {
async ([dest, {pkg}]) => {
if (pkg._reference && pkg._reference.location && pkg.bin && Object.keys(pkg.bin).length) {
const binLoc = path.join(this.config.lockfileFolder, this.config.getFolder(pkg));
await this.linkSelfDependencies(pkg, dest, binLoc);
Expand All @@ -436,17 +436,32 @@ export default class PackageLinker {
}
}

determineTopLevelBinLinks(flatTree: HoistManifestTuples): Array<[string, Manifest]> {
determineTopLevelBinLinkOrder(flatTree: HoistManifestTuples): HoistManifestTuples {
const linksToCreate = new Map();
for (const [dest, {pkg, isDirectRequire}] of flatTree) {
for (const [dest, hoistManifest] of flatTree) {
const {pkg, isDirectRequire} = hoistManifest;
const {name} = pkg;

if (isDirectRequire || (this.topLevelBinLinking && !linksToCreate.has(name))) {
linksToCreate.set(name, [dest, pkg]);
linksToCreate.set(name, [dest, hoistManifest]);
}
}

return Array.from(linksToCreate.values());
// Sort the array so that direct dependencies will be linked last.
// Bin links are overwritten if they already exist, so this will cause direct deps to take precedence.
// If someone finds this to be incorrect later, you could also consider sorting descending by
// `linkToCreate.level` which is the dependency tree depth. Direct deps will have level 0 and transitive
// deps will have level > 0.
const transientBins = [];
const topLevelBins = [];
for (const linkToCreate of Array.from(linksToCreate.values())) {
if (linkToCreate[1].isDirectRequire) {
topLevelBins.push(linkToCreate);
} else {
transientBins.push(linkToCreate);
}
}
return [...transientBins, ...topLevelBins];
}

resolvePeerModules() {
Expand Down