Skip to content

Commit 743c145

Browse files
rhburrowsGudahtt
authored andcommitted
Fix: handle empty bin strings (#6515)
* Fix: handle empty bin strings When the package.json of a dependency sets `bin` to an empty string, do not normalize it. This prevents creating a symlink to the dependency's root directory within the `.bin` directory if bin-links are enabled * Add an entry to the changelog * Add a test to make sure empty binlinks aren't created Test was suggested/provided by @rally25rs * Fix empty bin string test on Windows
1 parent 9bb2cfb commit 743c145

File tree

10 files changed

+51
-1
lines changed

10 files changed

+51
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa
44

55
## Master
66

7+
- Fixes handling of empty string entries for bin in package.json
8+
9+
[#6515](https://github.com/yarnpkg/yarn/pull/6515) - [**Ryan Burrows**](https://github.com/rhburrows)
10+
711
- Adds support for basic auth for registries with paths, such as artifactory
812

913
[#5322](https://github.com/yarnpkg/yarn/pull/5322) - [**Karolis Narkevicius**](https://twitter.com/KidkArolis)

__tests__/__snapshots__/normalize-manifest.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ Array [
9090
]
9191
`;
9292

93+
exports[`empty bin string: empty bin string 1`] = `
94+
Array [
95+
"foo: No license field",
96+
]
97+
`;
98+
9399
exports[`engines array: engines array 1`] = `
94100
Array [
95101
"No license field",

__tests__/commands/install/bin-links.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ test('can use link protocol to install a package that would not be found via nod
175175
});
176176
});
177177

178+
test('empty bin string does not create a link', (): Promise<void> => {
179+
return runInstall({binLinks: true}, 'install-empty-bin', async config => {
180+
const binScripts = await fs.walk(path.join(config.cwd, 'node_modules', '.bin'));
181+
const linkCount = process.platform === 'win32' ? 2 : 1;
182+
expect(binScripts).toHaveLength(linkCount);
183+
184+
expect(await linkAt(config, 'node_modules', '.bin', 'depB')).toEqual('../depB/depb.js');
185+
});
186+
});
187+
178188
describe('with nohoist', () => {
179189
// address https://github.com/yarnpkg/yarn/issues/5487
180190
test('nohoist bin should be linked to its own local module', (): Promise<void> => {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "depA",
3+
"version": "0.0.0",
4+
"bin": ""
5+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
console.log('depB');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "depB",
3+
"version": "0.0.0",
4+
"bin": "./depb.js"
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "test",
3+
"version": "0.0.0",
4+
"bin": "",
5+
"dependencies": {
6+
"depA": "file:depA",
7+
"depB": "file:depB"
8+
}
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "foo",
3+
"bin": ""
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "foo",
3+
"version": "",
4+
"bin": ""
5+
}

src/util/normalize-manifest/fix.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export default (async function(
153153
// if the `bin` field is as string then expand it to an object with a single property
154154
// based on the original `bin` field and `name field`
155155
// { name: "foo", bin: "cli.js" } -> { name: "foo", bin: { foo: "cli.js" } }
156-
if (typeof info.name === 'string' && typeof info.bin === 'string') {
156+
if (typeof info.name === 'string' && typeof info.bin === 'string' && info.bin.length > 0) {
157157
// Remove scoped package name for consistency with NPM's bin field fixing behaviour
158158
const name = info.name.replace(/^@[^\/]+\//, '');
159159
info.bin = {[name]: info.bin};

0 commit comments

Comments
 (0)