Skip to content

Commit 0d5bfe3

Browse files
authored
fix(misc): ensure all project generators add project to workspaces config (#29582)
- Update project generators to add the project to the workspaces setup in the new TS solution setup - Update some library generators that were not running package installation <!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- 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 --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent d30a84f commit 0d5bfe3

File tree

18 files changed

+166
-155
lines changed

18 files changed

+166
-155
lines changed

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,6 @@ describe('Node Applications', () => {
2828
packages: ['@nx/node', '@nx/express', '@nx/nest', '@nx/webpack'],
2929
preset: 'ts',
3030
});
31-
if (pm === 'pnpm') {
32-
updateFile(
33-
'pnpm-workspace.yaml',
34-
`
35-
packages:
36-
- 'apps/*'
37-
- 'packages/*'
38-
`
39-
);
40-
} else {
41-
updateJson('package.json', (json) => {
42-
json.workspaces = ['apps/*', 'packages/*'];
43-
return json;
44-
});
45-
}
4631
});
4732

4833
afterAll(() => {

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,6 @@ describe('Vue Plugin', () => {
1818
packages: ['@nx/vue'],
1919
preset: 'ts',
2020
});
21-
if (pm === 'pnpm') {
22-
updateFile(
23-
'pnpm-workspace.yaml',
24-
`
25-
packages:
26-
- 'apps/*'
27-
- 'packages/*'
28-
`
29-
);
30-
} else {
31-
updateJson('package.json', (json) => {
32-
json.workspaces = ['apps/*', 'packages/*'];
33-
return json;
34-
});
35-
}
3621
});
3722

3823
afterAll(() => cleanupProject());

packages/js/src/generators/library/library.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,50 @@ describe('lib', () => {
16331633
});
16341634
});
16351635

1636+
it.each`
1637+
directory | expected
1638+
${'my-ts-lib'} | ${'my-ts-lib'}
1639+
${'libs/my-ts-lib'} | ${'libs/*'}
1640+
${'libs/shared/my-ts-lib'} | ${'libs/shared/*'}
1641+
`(
1642+
'should add "$expected" to the workspace when creating a library in "$directory" using pnpm',
1643+
async ({ directory, expected }) => {
1644+
tree.write('pnpm-workspace.yaml', '');
1645+
1646+
await libraryGenerator(tree, {
1647+
...defaultOptions,
1648+
directory,
1649+
});
1650+
1651+
expect(tree.read('pnpm-workspace.yaml', 'utf-8'))
1652+
.toMatchInlineSnapshot(`
1653+
"packages:
1654+
- '${expected}'
1655+
"
1656+
`);
1657+
}
1658+
);
1659+
1660+
it.each`
1661+
directory | expected
1662+
${'my-ts-lib'} | ${'my-ts-lib'}
1663+
${'libs/my-ts-lib'} | ${'libs/*'}
1664+
${'libs/shared/my-ts-lib'} | ${'libs/shared/*'}
1665+
`(
1666+
'should add "$expected" to the workspace when creating a library in "$directory" using npm or yarn',
1667+
async ({ directory, expected }) => {
1668+
// ensure there's no pnpm-workspace.yaml, so it uses the package.json workspaces
1669+
tree.delete('pnpm-workspace.yaml');
1670+
1671+
await libraryGenerator(tree, {
1672+
...defaultOptions,
1673+
directory,
1674+
});
1675+
1676+
expect(readJson(tree, 'package.json').workspaces).toContain(expected);
1677+
}
1678+
);
1679+
16361680
it('should map non-buildable libraries to source', async () => {
16371681
await libraryGenerator(tree, {
16381682
...defaultOptions,

packages/js/src/generators/library/library.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ import { join } from 'path';
3636
import type { CompilerOptions } from 'typescript';
3737
import { normalizeLinterOption } from '../../utils/generator-prompts';
3838
import { getUpdatedPackageJsonContent } from '../../utils/package-json/update-package-json';
39-
import {
40-
getProjectPackageManagerWorkspaceState,
41-
getProjectPackageManagerWorkspaceStateWarningTask,
42-
} from '../../utils/package-manager-workspaces';
4339
import { addSwcConfig } from '../../utils/swc/add-swc-config';
4440
import { getSwcDependencies } from '../../utils/swc/add-swc-dependencies';
4541
import { getNeededCompilerOptionOverrides } from '../../utils/typescript/configuration';
@@ -53,6 +49,7 @@ import {
5349
readTsConfigFromTree,
5450
} from '../../utils/typescript/ts-config';
5551
import {
52+
addProjectToTsSolutionWorkspace,
5653
isUsingTsSolutionSetup,
5754
isUsingTypeScriptPlugin,
5855
} from '../../utils/typescript/ts-solution-setup';
@@ -218,21 +215,14 @@ export async function libraryGeneratorInternal(
218215
);
219216
}
220217

221-
if (!options.skipFormat) {
222-
await formatFiles(tree);
218+
// If we are using the new TS solution
219+
// We need to update the workspace file (package.json or pnpm-workspaces.yaml) to include the new project
220+
if (options.isUsingTsSolutionConfig) {
221+
addProjectToTsSolutionWorkspace(tree, options.projectRoot);
223222
}
224223

225-
if (
226-
!options.skipWorkspacesWarning &&
227-
options.isUsingTsSolutionConfig &&
228-
options.projectPackageManagerWorkspaceState !== 'included'
229-
) {
230-
tasks.push(
231-
getProjectPackageManagerWorkspaceStateWarningTask(
232-
options.projectPackageManagerWorkspaceState,
233-
tree.root
234-
)
235-
);
224+
if (!options.skipFormat) {
225+
await formatFiles(tree);
236226
}
237227

238228
if (options.publishable) {
@@ -872,9 +862,6 @@ async function normalizeOptions(
872862

873863
options.minimal ??= false;
874864

875-
const projectPackageManagerWorkspaceState =
876-
getProjectPackageManagerWorkspaceState(tree, projectRoot);
877-
878865
// We default to generate a project.json file if the new setup is not being used
879866
options.useProjectJson ??= !isUsingTsSolutionConfig;
880867

@@ -888,7 +875,6 @@ async function normalizeOptions(
888875
importPath,
889876
hasPlugin,
890877
isUsingTsSolutionConfig,
891-
projectPackageManagerWorkspaceState,
892878
};
893879
}
894880

packages/js/src/generators/library/schema.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export interface LibraryGeneratorSchema {
3333
simpleName?: boolean;
3434
addPlugin?: boolean;
3535
useProjectJson?: boolean;
36-
skipWorkspacesWarning?: boolean;
3736
useTscExecutor?: boolean;
3837
}
3938

@@ -47,5 +46,4 @@ export interface NormalizedLibraryGeneratorOptions
4746
importPath?: string;
4847
hasPlugin: boolean;
4948
isUsingTsSolutionConfig: boolean;
50-
projectPackageManagerWorkspaceState: ProjectPackageManagerWorkspaceState;
5149
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,8 @@ export function addProjectToTsSolutionWorkspace(
206206
if (tree.exists('pnpm-workspace.yaml')) {
207207
const { load, dump } = require('@zkochan/js-yaml');
208208
const workspaceFile = tree.read('pnpm-workspace.yaml', 'utf-8');
209-
const yamlData = load(workspaceFile);
210-
211-
if (!yamlData?.packages) {
212-
yamlData.packages = [];
213-
}
209+
const yamlData = load(workspaceFile) ?? {};
210+
yamlData.packages ??= [];
214211

215212
if (!yamlData.packages.includes(pattern)) {
216213
yamlData.packages.push(pattern);

packages/nest/src/generators/library/library.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export async function libraryGeneratorInternal(
3030
rawOptions: LibraryGeneratorOptions
3131
): Promise<GeneratorCallback> {
3232
const options = await normalizeOptions(tree, rawOptions);
33-
await jsLibraryGenerator(tree, toJsLibraryGeneratorOptions(options));
33+
const jsLibraryTask = await jsLibraryGenerator(
34+
tree,
35+
toJsLibraryGeneratorOptions(options)
36+
);
3437
const initTask = await initGenerator(tree, rawOptions);
3538
const depsTask = ensureDependencies(tree);
3639
deleteFiles(tree, options);
@@ -45,6 +48,7 @@ export async function libraryGeneratorInternal(
4548

4649
return runTasksInSerial(
4750
...[
51+
jsLibraryTask,
4852
initTask,
4953
depsTask,
5054
() => {

packages/node/src/generators/application/application.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import { hasWebpackPlugin } from '../../utils/has-webpack-plugin';
5656
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/target-defaults-utils';
5757
import { logShowProjectCommand } from '@nx/devkit/src/utils/log-show-project-command';
5858
import {
59+
addProjectToTsSolutionWorkspace,
5960
isUsingTsSolutionSetup,
6061
updateTsconfigFiles,
6162
} from '@nx/js/src/utils/typescript/ts-solution-setup';
@@ -572,10 +573,6 @@ export async function applicationGeneratorInternal(tree: Tree, schema: Schema) {
572573
tasks.push(dockerTask);
573574
}
574575

575-
if (!options.skipFormat) {
576-
await formatFiles(tree);
577-
}
578-
579576
if (options.isUsingTsSolutionConfig) {
580577
updateTsconfigFiles(
581578
tree,
@@ -591,6 +588,16 @@ export async function applicationGeneratorInternal(tree: Tree, schema: Schema) {
591588
);
592589
}
593590

591+
// If we are using the new TS solution
592+
// We need to update the workspace file (package.json or pnpm-workspaces.yaml) to include the new project
593+
if (options.isUsingTsSolutionConfig) {
594+
addProjectToTsSolutionWorkspace(tree, options.appProjectRoot);
595+
}
596+
597+
if (!options.skipFormat) {
598+
await formatFiles(tree);
599+
}
600+
594601
tasks.push(() => {
595602
logShowProjectCommand(options.name);
596603
});

packages/node/src/generators/e2e-project/e2e-project.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ import {
3030
} from '@nx/eslint/src/generators/utils/eslint-file';
3131
import { logShowProjectCommand } from '@nx/devkit/src/utils/log-show-project-command';
3232
import { findRootJestPreset } from '@nx/jest/src/utils/config/config-file';
33-
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
33+
import {
34+
addProjectToTsSolutionWorkspace,
35+
isUsingTsSolutionSetup,
36+
} from '@nx/js/src/utils/typescript/ts-solution-setup';
3437
import { getImportPath } from '@nx/js/src/utils/get-import-path';
3538
import { relative } from 'node:path/posix';
3639

@@ -230,10 +233,6 @@ export async function e2eProjectGeneratorInternal(
230233
}
231234
}
232235

233-
if (!options.skipFormat) {
234-
await formatFiles(host);
235-
}
236-
237236
if (isUsingTsSolutionConfig) {
238237
updateJson(host, 'tsconfig.json', (json) => {
239238
json.references ??= [];
@@ -245,6 +244,16 @@ export async function e2eProjectGeneratorInternal(
245244
});
246245
}
247246

247+
// If we are using the new TS solution
248+
// We need to update the workspace file (package.json or pnpm-workspaces.yaml) to include the new project
249+
if (isUsingTsSolutionConfig) {
250+
addProjectToTsSolutionWorkspace(host, options.e2eProjectRoot);
251+
}
252+
253+
if (!options.skipFormat) {
254+
await formatFiles(host);
255+
}
256+
248257
tasks.push(() => {
249258
logShowProjectCommand(options.e2eProjectName);
250259
});

packages/node/src/generators/library/library.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import { tslibVersion, typesNodeVersion } from '../../utils/versions';
2929
import { initGenerator } from '../init/init';
3030
import { Schema } from './schema';
3131
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/target-defaults-utils';
32-
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
32+
import {
33+
addProjectToTsSolutionWorkspace,
34+
isUsingTsSolutionSetup,
35+
} from '@nx/js/src/utils/typescript/ts-solution-setup';
3336
import { getImportPath } from '@nx/js/src/utils/get-import-path';
3437

3538
export interface NormalizedSchema extends Schema {
@@ -108,6 +111,12 @@ export async function libraryGeneratorInternal(tree: Tree, schema: Schema) {
108111
tasks.push(() => installPackagesTask(tree, true));
109112
}
110113

114+
// If we are using the new TS solution
115+
// We need to update the workspace file (package.json or pnpm-workspaces.yaml) to include the new project
116+
if (options.isUsingTsSolutionConfig) {
117+
addProjectToTsSolutionWorkspace(tree, options.projectRoot);
118+
}
119+
111120
if (!schema.skipFormat) {
112121
await formatFiles(tree);
113122
}

packages/nuxt/src/generators/application/application.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ import {
3333
createNxCloudOnboardingURLForWelcomeApp,
3434
} from 'nx/src/nx-cloud/utilities/onboarding';
3535
import { getImportPath } from '@nx/js/src/utils/get-import-path';
36-
import { updateTsconfigFiles } from '@nx/js/src/utils/typescript/ts-solution-setup';
36+
import {
37+
addProjectToTsSolutionWorkspace,
38+
updateTsconfigFiles,
39+
} from '@nx/js/src/utils/typescript/ts-solution-setup';
3740

3841
export async function applicationGenerator(tree: Tree, schema: Schema) {
3942
const tasks: GeneratorCallback[] = [];
@@ -170,22 +173,6 @@ export async function applicationGenerator(tree: Tree, schema: Schema) {
170173

171174
if (options.js) toJS(tree);
172175

173-
if (!options.skipFormat) await formatFiles(tree);
174-
175-
tasks.push(() => {
176-
try {
177-
execSync(`npx -y nuxi prepare`, {
178-
cwd: options.appProjectRoot,
179-
180-
windowsHide: false,
181-
});
182-
} catch (e) {
183-
console.error(
184-
`Failed to run \`nuxi prepare\` in "${options.appProjectRoot}". Please run the command manually.`
185-
);
186-
}
187-
});
188-
189176
if (options.isUsingTsSolutionConfig) {
190177
updateTsconfigFiles(
191178
tree,
@@ -204,6 +191,28 @@ export async function applicationGenerator(tree: Tree, schema: Schema) {
204191
);
205192
}
206193

194+
// If we are using the new TS solution
195+
// We need to update the workspace file (package.json or pnpm-workspaces.yaml) to include the new project
196+
if (options.isUsingTsSolutionConfig) {
197+
addProjectToTsSolutionWorkspace(tree, options.appProjectRoot);
198+
}
199+
200+
if (!options.skipFormat) await formatFiles(tree);
201+
202+
tasks.push(() => {
203+
try {
204+
execSync(`npx -y nuxi prepare`, {
205+
cwd: options.appProjectRoot,
206+
207+
windowsHide: false,
208+
});
209+
} catch (e) {
210+
console.error(
211+
`Failed to run \`nuxi prepare\` in "${options.appProjectRoot}". Please run the command manually.`
212+
);
213+
}
214+
});
215+
207216
tasks.push(() => {
208217
logShowProjectCommand(options.projectName);
209218
});

0 commit comments

Comments
 (0)