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

Use cwd node_modules/.bin entry first in workspace #8591

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions __tests__/commands/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'bin');
const runBin = buildRun.bind(null, BufferReporter, fixturesLoc, (args, flags, config, reporter): Promise<void> => {
return run(config, reporter, flags, args);
});
const runInWorkspacePackage = function(cwd, config, reporter, flags, args): Promise<void> {
const originalCwd = config.cwd;
config.cwd = path.join(originalCwd, cwd);
const retVal = run(config, reporter, flags, args);
retVal.then(() => {
config.cwd = originalCwd;
});
return retVal;
};

test('running bin without arguments should return the folder where the binaries are stored', (): Promise<void> => {
return runBin([], {}, '../install/install-production-bin', (config, reporter): ?Promise<void> => {
Expand All @@ -24,3 +33,15 @@ test('running bin with a binary name as the argument should return its full path
expect(reporter.getBufferText()).toMatch(/[\\\/]node_modules[\\\/]\.bin[\\\/]rimraf$/);
});
});

test('should use .bin in workspace node modules respectively', (): Promise<void> => {
return runInstall({binLinks: true}, 'workspaces-install-link-bin-2-versions', async (config): ?Promise<void> => {
const reporter1 = new BufferReporter();
await runInWorkspacePackage('packages/workspace-1', config, reporter1, {}, ['myBin']);
expect(reporter1.getBufferText()).toMatch(/[\\\/]workspace-1[\\\/]node_modules[\\\/]\.bin[\\\/]myBin$/);

const reporter2 = new BufferReporter();
await runInWorkspacePackage('packages/workspace-2', config, reporter2, {}, ['myBin']);
expect(reporter2.getBufferText()).toMatch(/[\\\/]workspace-2[\\\/]node_modules[\\\/]\.bin[\\\/]myBin$/);
});
});
2 changes: 1 addition & 1 deletion __tests__/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ test('adds workspace root node_modules/.bin to path when in a workspace', (): Pr
expect(envPaths).toContain(path.join(config.cwd, 'packages', 'pkg1', 'node_modules', '.bin'));
}));

test('adds cwd node_modules/.bin to path when in a workspace usig nohoist', (): Promise<void> =>
test('adds cwd node_modules/.bin to path when in a workspace using nohoist', (): Promise<void> =>
runRunInWorkspacePackage('packages/pkg1', ['env'], {}, 'nohoist-workspace', (config, reporter): ?Promise<void> => {
const logEntry = reporter.getBuffer().find(entry => entry.type === 'log');
const parsedLogData = JSON.parse(logEntry ? logEntry.data.toString() : '{}');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node

console.log('v1')

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "my_project_with_bin",
"version": "1.0.0",
"private": true,
"bin": {
"myBin": "./cli_v1.js"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node

console.log('v2')

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "my_project_with_bin",
"version": "2.0.0",
"private": true,
"bin": {
"myBin": "./cli_v2.js"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "my-project",
"private": true,
"workspaces": [
"packages/*"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "workspace-1",
"version": "1.0.0",
"private": true,
"dependencies": {
"my_project_with_bin": "file:../../my_project_with_bin/v1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "workspace-2",
"version": "1.0.0",
"private": true,
"dependencies": {
"my_project_with_bin": "file:../../my_project_with_bin/v2"
}
}
2 changes: 1 addition & 1 deletion src/cli/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export async function getBinEntries(config: Config): Promise<Map<string, string>

// Setup the node_modules/.bin folders for analysis
for (const registryFolder of config.registryFolders) {
binFolders.add(path.resolve(config.cwd, registryFolder, '.bin'));
binFolders.add(path.resolve(config.lockfileFolder, registryFolder, '.bin'));
binFolders.add(path.resolve(config.cwd, registryFolder, '.bin'));
}

// Same thing, but for the pnp dependencies, located inside the cache
Expand Down