Skip to content

Commit

Permalink
Fix: handle empty bin strings (#6515)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rhburrows authored and Gudahtt committed Oct 9, 2018
1 parent 9bb2cfb commit 743c145
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,10 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa

## Master

- Fixes handling of empty string entries for bin in package.json

[#6515](https://github.com/yarnpkg/yarn/pull/6515) - [**Ryan Burrows**](https://github.com/rhburrows)

- Adds support for basic auth for registries with paths, such as artifactory

[#5322](https://github.com/yarnpkg/yarn/pull/5322) - [**Karolis Narkevicius**](https://twitter.com/KidkArolis)
Expand Down
6 changes: 6 additions & 0 deletions __tests__/__snapshots__/normalize-manifest.js.snap
Expand Up @@ -90,6 +90,12 @@ Array [
]
`;

exports[`empty bin string: empty bin string 1`] = `
Array [
"foo: No license field",
]
`;

exports[`engines array: engines array 1`] = `
Array [
"No license field",
Expand Down
10 changes: 10 additions & 0 deletions __tests__/commands/install/bin-links.js
Expand Up @@ -175,6 +175,16 @@ test('can use link protocol to install a package that would not be found via nod
});
});

test('empty bin string does not create a link', (): Promise<void> => {
return runInstall({binLinks: true}, 'install-empty-bin', async config => {
const binScripts = await fs.walk(path.join(config.cwd, 'node_modules', '.bin'));
const linkCount = process.platform === 'win32' ? 2 : 1;
expect(binScripts).toHaveLength(linkCount);

expect(await linkAt(config, 'node_modules', '.bin', 'depB')).toEqual('../depB/depb.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
@@ -0,0 +1,5 @@
{
"name": "depA",
"version": "0.0.0",
"bin": ""
}
2 changes: 2 additions & 0 deletions __tests__/fixtures/install/install-empty-bin/depB/depb.js
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log('depB');
@@ -0,0 +1,5 @@
{
"name": "depB",
"version": "0.0.0",
"bin": "./depb.js"
}
9 changes: 9 additions & 0 deletions __tests__/fixtures/install/install-empty-bin/package.json
@@ -0,0 +1,9 @@
{
"name": "test",
"version": "0.0.0",
"bin": "",
"dependencies": {
"depA": "file:depA",
"depB": "file:depB"
}
}
@@ -0,0 +1,4 @@
{
"name": "foo",
"bin": ""
}
@@ -0,0 +1,5 @@
{
"name": "foo",
"version": "",
"bin": ""
}
2 changes: 1 addition & 1 deletion src/util/normalize-manifest/fix.js
Expand Up @@ -153,7 +153,7 @@ export default (async function(
// if the `bin` field is as string then expand it to an object with a single property
// based on the original `bin` field and `name field`
// { name: "foo", bin: "cli.js" } -> { name: "foo", bin: { foo: "cli.js" } }
if (typeof info.name === 'string' && typeof info.bin === 'string') {
if (typeof info.name === 'string' && typeof info.bin === 'string' && info.bin.length > 0) {
// Remove scoped package name for consistency with NPM's bin field fixing behaviour
const name = info.name.replace(/^@[^\/]+\//, '');
info.bin = {[name]: info.bin};
Expand Down

0 comments on commit 743c145

Please sign in to comment.