diff --git a/.gitignore b/.gitignore index d96e0e81..b79a89e1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ node_modules/ +# Any node_modules in test fixtures are allowed. +!test/fixtures/**/node_modules/ + dist/ .eslintcache diff --git a/lib/workspace.ts b/lib/workspace.ts index 5f24c39e..fe9f4739 100644 --- a/lib/workspace.ts +++ b/lib/workspace.ts @@ -125,7 +125,12 @@ function expandWorkspaces(root: string, workspacePatterns: string[]): string[] { return [workspace]; } // Use cwd instead of passing join()'d paths to globby for Windows support: https://github.com/micromatch/micromatch/blob/34f44b4f57eacbdbcc74f64252e0845cf44bbdbd/README.md?plain=1#L822 - return globbySync(workspace, { onlyDirectories: true, cwd: root }); + // Ignore any node_modules that may be present due to the use of nohoist. + return globbySync(workspace, { + onlyDirectories: true, + cwd: root, + ignore: ['**/node_modules'], + }); }); } diff --git a/test/fixtures/index.ts b/test/fixtures/index.ts index f6165dce..3f8026c4 100644 --- a/test/fixtures/index.ts +++ b/test/fixtures/index.ts @@ -52,6 +52,10 @@ export const FIXTURE_PATH_VALID_WITH_PACKAGES = join( FIXTURE_PATH, 'valid-with-packages' ); +export const FIXTURE_PATH_VALID_NOHOIST_WITH_NODE_MODULES = join( + FIXTURE_PATH, + 'valid-nohoist-with-node-modules' +); export const FIXTURE_PATH_WORKSPACE_PACKAGE_NOT_AN_ARRAY = join( FIXTURE_PATH, 'workspace-packages-not-an-array' diff --git a/test/fixtures/valid-nohoist-with-node-modules/package.json b/test/fixtures/valid-nohoist-with-node-modules/package.json new file mode 100644 index 00000000..fe16a2f0 --- /dev/null +++ b/test/fixtures/valid-nohoist-with-node-modules/package.json @@ -0,0 +1,10 @@ +{ + "workspaces": { + "packages": [ + "packages/**" + ], + "nohoist": [ + "**/resolve" + ] + } +} diff --git a/test/fixtures/valid-nohoist-with-node-modules/packages/package-a/node_modules/resolve/package.json b/test/fixtures/valid-nohoist-with-node-modules/packages/package-a/node_modules/resolve/package.json new file mode 100644 index 00000000..b591e8ae --- /dev/null +++ b/test/fixtures/valid-nohoist-with-node-modules/packages/package-a/node_modules/resolve/package.json @@ -0,0 +1,3 @@ +{ + "name": "resolve" +} diff --git a/test/fixtures/valid-nohoist-with-node-modules/packages/package-a/node_modules/resolve/test/resolver/malformed_package_json/package.json b/test/fixtures/valid-nohoist-with-node-modules/packages/package-a/node_modules/resolve/test/resolver/malformed_package_json/package.json new file mode 100644 index 00000000..98232c64 --- /dev/null +++ b/test/fixtures/valid-nohoist-with-node-modules/packages/package-a/node_modules/resolve/test/resolver/malformed_package_json/package.json @@ -0,0 +1 @@ +{ diff --git a/test/fixtures/valid-nohoist-with-node-modules/packages/package-a/package.json b/test/fixtures/valid-nohoist-with-node-modules/packages/package-a/package.json new file mode 100644 index 00000000..47bc6398 --- /dev/null +++ b/test/fixtures/valid-nohoist-with-node-modules/packages/package-a/package.json @@ -0,0 +1,6 @@ +{ + "name": "package-a", + "dependencies": { + "resolve": "^1.22.1" + } +} diff --git a/test/fixtures/valid/node_modules/should-be-ignored/package.json b/test/fixtures/valid/node_modules/should-be-ignored/package.json new file mode 100644 index 00000000..1e15fd0c --- /dev/null +++ b/test/fixtures/valid/node_modules/should-be-ignored/package.json @@ -0,0 +1,3 @@ +{ + "name": "should-be-ignored" +} diff --git a/test/lib/workspace-test.ts b/test/lib/workspace-test.ts index d3a0ce6e..09f6a80d 100644 --- a/test/lib/workspace-test.ts +++ b/test/lib/workspace-test.ts @@ -5,6 +5,7 @@ import { FIXTURE_PATH_NO_PACKAGE_JSON, FIXTURE_PATH_VALID, FIXTURE_PATH_VALID_WITH_PACKAGES, + FIXTURE_PATH_VALID_NOHOIST_WITH_NODE_MODULES, FIXTURE_PATH_WORKSPACE_NOT_AN_ARRAY, FIXTURE_PATH_WORKSPACE_PACKAGE_NOT_AN_ARRAY, FIXTURE_PATH_NESTED_WORKSPACES, @@ -162,6 +163,22 @@ describe('Utils | workspace', function () { ].map((path) => join(FIXTURE_PATH_NESTED_WORKSPACES, path)) ); }); + + it('does not include packages in node_modules from nohoist usage (or crash from malformed package.json in node_modules test fixture)', function () { + expect( + getPackages( + FIXTURE_PATH_VALID_NOHOIST_WITH_NODE_MODULES, + [], + [], + [], + [] + ).map((package_) => package_.path) + ).toStrictEqual( + ['.', '/packages/package-a'].map((path) => + join(FIXTURE_PATH_VALID_NOHOIST_WITH_NODE_MODULES, path) + ) + ); + }); }); describe('#getWorkspaces', function () {