Skip to content

Commit 206a47e

Browse files
jaysooFrozenPandaz
authored andcommitted
fix(js): avoid nested paths in workspaces because they can lead to future issues (#29553)
For the new TS setup, we currently use nested glob patterns: `apps/**`, `libs/**`, and `packages/**`. Nested paths can result into too many projects being matched. For example, if `libs/mylib/src/__fixtures__/package.json` is there for testing, it will be matched as a project and likely result in an error. Other tools like turborepo also caution against this: https://turbo.build/repo/docs/crafting-your-repository/structuring-a-repository#declaring-directories-for-packages If users want to, they could change to nested `**` paths, but we should not use it by default. Note: For CNW, we only use `apps/*` by default since that is where the project lives. When users do `nx g lib packages/foo` then `packages/*` will be added automatically. <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> Use nested `**` paths. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> Don't use nested `**` paths. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes # (cherry picked from commit fb31800)
1 parent abfe5d9 commit 206a47e

File tree

7 files changed

+37
-23
lines changed

7 files changed

+37
-23
lines changed

e2e/node/src/node-ts-solution.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ describe('Node Applications', () => {
3333
'pnpm-workspace.yaml',
3434
`
3535
packages:
36-
- 'apps/**'
37-
- 'packages/**'
36+
- 'apps/*'
37+
- 'packages/*'
3838
`
3939
);
4040
} else {
4141
updateJson('package.json', (json) => {
42-
json.workspaces = ['apps/**', 'packages/**'];
42+
json.workspaces = ['apps/*', 'packages/*'];
4343
return json;
4444
});
4545
}

e2e/vue/src/vue-ts-solution.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ describe('Vue Plugin', () => {
2323
'pnpm-workspace.yaml',
2424
`
2525
packages:
26-
- 'apps/**'
27-
- 'packages/**'
26+
- 'apps/*'
27+
- 'packages/*'
2828
`
2929
);
3030
} else {
3131
updateJson('package.json', (json) => {
32-
json.workspaces = ['apps/**', 'packages/**'];
32+
json.workspaces = ['apps/*', 'packages/*'];
3333
return json;
3434
});
3535
}

packages/create-nx-workspace/src/create-workspace.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ function getWorkspaceGlobsFromPreset(preset: string): string[] {
115115
case Preset.RemixMonorepo:
116116
case Preset.VueMonorepo:
117117
case Preset.WebComponents:
118-
return ['apps/**', 'libs/**', 'packages/**'];
118+
return ['apps/*'];
119119
default:
120-
return ['libs/**', 'packages/**'];
120+
return ['packages/*'];
121121
}
122122
}

packages/js/src/utils/typescript/ts-solution-setup.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,11 @@ export function addProjectToTsSolutionWorkspace(
198198
tree: Tree,
199199
projectDir: string
200200
) {
201-
// If dir is "libs/foo" then use "libs/**" so we don't need so many entries in the workspace file.
201+
// If dir is "libs/foo" then use "libs/*" so we don't need so many entries in the workspace file.
202+
// If dir is nested like "libs/shared/foo" then we add "libs/shared/*".
202203
// If the dir is just "foo" then we have to add it as is.
203204
const baseDir = dirname(projectDir);
204-
const pattern = baseDir === '.' ? projectDir : `${baseDir}/**`;
205+
const pattern = baseDir === '.' ? projectDir : `${baseDir}/*`;
205206
if (tree.exists('pnpm-workspace.yaml')) {
206207
const { load, dump } = require('@zkochan/js-yaml');
207208
const workspaceFile = tree.read('pnpm-workspace.yaml', 'utf-8');

packages/react/src/generators/application/application.spec.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ describe('app', () => {
12791279
beforeEach(() => {
12801280
appTree = createTreeWithEmptyWorkspace();
12811281
updateJson(appTree, 'package.json', (json) => {
1282-
json.workspaces = ['packages/**', 'apps/**'];
1282+
json.workspaces = ['packages/*', 'apps/*'];
12831283
return json;
12841284
});
12851285
writeJson(appTree, 'tsconfig.base.json', {
@@ -1482,10 +1482,10 @@ describe('app', () => {
14821482

14831483
const packageJson = readJson(appTree, 'package.json');
14841484
expect(packageJson.workspaces).toEqual([
1485-
'packages/**',
1486-
'apps/**',
1485+
'packages/*',
1486+
'apps/*',
14871487
'myapp',
1488-
'libs/**',
1488+
'libs/*',
14891489
]);
14901490
});
14911491

@@ -1519,11 +1519,24 @@ describe('app', () => {
15191519
unitTestRunner: 'none',
15201520
e2eTestRunner: 'none',
15211521
});
1522+
await applicationGenerator(appTree, {
1523+
directory: 'packages/shared/util',
1524+
addPlugin: true,
1525+
linter: Linter.EsLint,
1526+
style: 'none',
1527+
bundler: 'vite',
1528+
unitTestRunner: 'none',
1529+
e2eTestRunner: 'none',
1530+
});
15221531

15231532
const pnpmContent = appTree.read('pnpm-workspace.yaml', 'utf-8');
15241533
const pnpmWorkspaceFile = load(pnpmContent);
15251534

1526-
expect(pnpmWorkspaceFile.packages).toEqual(['myapp', 'apps/**']);
1535+
expect(pnpmWorkspaceFile.packages).toEqual([
1536+
'myapp',
1537+
'apps/*',
1538+
'packages/shared/*',
1539+
]);
15271540
});
15281541
});
15291542

packages/workspace/src/generators/new/generate-workspace-files.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
230230
"scripts": {},
231231
"version": "0.0.0",
232232
"workspaces": [
233-
"packages/**",
233+
"packages/*",
234234
],
235235
}
236236
`);
@@ -294,7 +294,7 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
294294
defaultBase: 'main',
295295
packageManager: 'npm',
296296
isCustomPreset: false,
297-
workspaceGlobs: ['apps/**', 'packages/**'],
297+
workspaceGlobs: ['apps/*', 'packages/*'],
298298
});
299299

300300
const packageJson = readJson(tree, '/proj/package.json');
@@ -310,8 +310,8 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
310310
"scripts": {},
311311
"version": "0.0.0",
312312
"workspaces": [
313-
"apps/**",
314-
"packages/**",
313+
"apps/*",
314+
"packages/*",
315315
],
316316
}
317317
`);
@@ -326,14 +326,14 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
326326
defaultBase: 'main',
327327
packageManager: 'pnpm',
328328
isCustomPreset: false,
329-
workspaceGlobs: ['apps/**', 'packages/**'],
329+
workspaceGlobs: ['apps/*', 'packages/*'],
330330
});
331331

332332
const packageJson = tree.read('/proj/pnpm-workspace.yaml', 'utf-8');
333333
expect(packageJson).toMatchInlineSnapshot(`
334334
"packages:
335-
- "apps/**"
336-
- "packages/**"
335+
- "apps/*"
336+
- "packages/*"
337337
"
338338
`);
339339
});

packages/workspace/src/generators/new/generate-workspace-files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ function setUpWorkspacesInPackageJson(tree: Tree, options: NormalizedSchema) {
428428
options.preset === Preset.Express) &&
429429
options.workspaces)
430430
) {
431-
const workspaces = options.workspaceGlobs ?? ['packages/**'];
431+
const workspaces = options.workspaceGlobs ?? ['packages/*'];
432432
if (options.packageManager === 'pnpm') {
433433
tree.write(
434434
join(options.directory, 'pnpm-workspace.yaml'),

0 commit comments

Comments
 (0)