Skip to content

Commit

Permalink
fix(js): avoid nested paths in workspaces because they can lead to fu…
Browse files Browse the repository at this point in the history
…ture issues
  • Loading branch information
jaysoo committed Jan 8, 2025
1 parent 71653dc commit f8ad3fb
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 23 deletions.
6 changes: 3 additions & 3 deletions e2e/node/src/node-ts-solution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ describe('Node Applications', () => {
'pnpm-workspace.yaml',
`
packages:
- 'apps/**'
- 'packages/**'
- 'apps/*'
- 'packages/*'
`
);
} else {
updateJson('package.json', (json) => {
json.workspaces = ['apps/**', 'packages/**'];
json.workspaces = ['apps/*', 'packages/*'];
return json;
});
}
Expand Down
6 changes: 3 additions & 3 deletions e2e/vue/src/vue-ts-solution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ describe('Vue Plugin', () => {
'pnpm-workspace.yaml',
`
packages:
- 'apps/**'
- 'packages/**'
- 'apps/*'
- 'packages/*'
`
);
} else {
updateJson('package.json', (json) => {
json.workspaces = ['apps/**', 'packages/**'];
json.workspaces = ['apps/*', 'packages/*'];
return json;
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/create-nx-workspace/src/create-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ function getWorkspaceGlobsFromPreset(preset: string): string[] {
case Preset.RemixMonorepo:
case Preset.VueMonorepo:
case Preset.WebComponents:
return ['apps/**', 'libs/**', 'packages/**'];
return ['apps/*'];
default:
return ['libs/**', 'packages/**'];
return ['packages/*'];
}
}
5 changes: 3 additions & 2 deletions packages/js/src/utils/typescript/ts-solution-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,11 @@ export function addProjectToTsSolutionWorkspace(
tree: Tree,
projectDir: string
) {
// If dir is "libs/foo" then use "libs/**" so we don't need so many entries in the workspace file.
// If dir is "libs/foo" then use "libs/*" so we don't need so many entries in the workspace file.
// If dir is nested like "libs/shared/foo" then we add "libs/shared/*".
// If the dir is just "foo" then we have to add it as is.
const baseDir = dirname(projectDir);
const pattern = baseDir === '.' ? projectDir : `${baseDir}/**`;
const pattern = baseDir === '.' ? projectDir : `${baseDir}/*`;
if (tree.exists('pnpm-workspace.yaml')) {
const { load, dump } = require('@zkochan/js-yaml');
const workspaceFile = tree.read('pnpm-workspace.yaml', 'utf-8');
Expand Down
23 changes: 18 additions & 5 deletions packages/react/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ describe('app', () => {
beforeEach(() => {
appTree = createTreeWithEmptyWorkspace();
updateJson(appTree, 'package.json', (json) => {
json.workspaces = ['packages/**', 'apps/**'];
json.workspaces = ['packages/*', 'apps/*'];
return json;
});
writeJson(appTree, 'tsconfig.base.json', {
Expand Down Expand Up @@ -1482,10 +1482,10 @@ describe('app', () => {

const packageJson = readJson(appTree, 'package.json');
expect(packageJson.workspaces).toEqual([
'packages/**',
'apps/**',
'packages/*',
'apps/*',
'myapp',
'libs/**',
'libs/*',
]);
});

Expand Down Expand Up @@ -1519,11 +1519,24 @@ describe('app', () => {
unitTestRunner: 'none',
e2eTestRunner: 'none',
});
await applicationGenerator(appTree, {
directory: 'packages/shared/util',
addPlugin: true,
linter: Linter.EsLint,
style: 'none',
bundler: 'vite',
unitTestRunner: 'none',
e2eTestRunner: 'none',
});

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

expect(pnpmWorkspaceFile.packages).toEqual(['myapp', 'apps/**']);
expect(pnpmWorkspaceFile.packages).toEqual([
'myapp',
'apps/*',
'packages/shared/*',
]);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
"scripts": {},
"version": "0.0.0",
"workspaces": [
"packages/**",
"packages/*",
],
}
`);
Expand Down Expand Up @@ -294,7 +294,7 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
defaultBase: 'main',
packageManager: 'npm',
isCustomPreset: false,
workspaceGlobs: ['apps/**', 'packages/**'],
workspaceGlobs: ['apps/*', 'packages/*'],
});

const packageJson = readJson(tree, '/proj/package.json');
Expand All @@ -310,8 +310,8 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
"scripts": {},
"version": "0.0.0",
"workspaces": [
"apps/**",
"packages/**",
"apps/*",
"packages/*",
],
}
`);
Expand All @@ -326,14 +326,14 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
defaultBase: 'main',
packageManager: 'pnpm',
isCustomPreset: false,
workspaceGlobs: ['apps/**', 'packages/**'],
workspaceGlobs: ['apps/*', 'packages/*'],
});

const packageJson = tree.read('/proj/pnpm-workspace.yaml', 'utf-8');
expect(packageJson).toMatchInlineSnapshot(`
"packages:
- "apps/**"
- "packages/**"
- "apps/*"
- "packages/*"
"
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ function setUpWorkspacesInPackageJson(tree: Tree, options: NormalizedSchema) {
options.preset === Preset.Express) &&
options.workspaces)
) {
const workspaces = options.workspaceGlobs ?? ['packages/**'];
const workspaces = options.workspaceGlobs ?? ['packages/*'];
if (options.packageManager === 'pnpm') {
tree.write(
join(options.directory, 'pnpm-workspace.yaml'),
Expand Down

0 comments on commit f8ad3fb

Please sign in to comment.