From 14d48c624cfed0ddae974083a99b7ac14f988692 Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Tue, 15 Aug 2023 14:48:40 -0400 Subject: [PATCH] chore(core): remove readProjectsConfigurations from workspaces (#18329) Co-authored-by: FrozenPandaz (cherry picked from commit 40d66ec715cc23d935130cedac53bf71fb5eecab) --- docs/generated/devkit/README.md | 2 +- docs/generated/devkit/Workspaces.md | 61 ++-------- .../devkit/readAllWorkspaceConfiguration.md | 11 -- ...adProjectsConfigurationFromProjectGraph.md | 15 +++ .../packages/devkit/documents/nx_devkit.md | 2 +- .../src/module-federation.test.ts | 6 +- e2e/angular-core/src/projects.test.ts | 4 +- .../src/cypress-component-tests.test.ts | 6 +- e2e/angular-extensions/src/tailwind.test.ts | 2 +- e2e/esbuild/src/esbuild.test.ts | 9 +- e2e/expo/src/expo.test.ts | 4 +- e2e/js/src/js-node.test.ts | 20 ++-- e2e/js/src/js-packaging.test.ts | 6 +- e2e/next/src/next.test.ts | 4 +- e2e/node/src/node-server.test.ts | 2 +- e2e/node/src/node-webpack.test.ts | 2 +- e2e/node/src/node.test.ts | 10 +- e2e/nx-misc/src/extras.test.ts | 14 +-- e2e/nx-misc/src/workspace.test.ts | 72 ++++++------ e2e/nx-run/src/affected-graph.test.ts | 14 +-- e2e/nx-run/src/cache.test.ts | 2 +- e2e/nx-run/src/invoke-runner.test.ts | 2 +- e2e/nx-run/src/run.test.ts | 36 +++--- e2e/plugin/src/nx-plugin.test.ts | 8 +- .../src/react-module-federation.test.ts | 4 +- e2e/react-core/src/react-package.test.ts | 4 +- e2e/react-core/src/react.test.ts | 4 +- .../src/cypress-component-tests.test.ts | 8 +- e2e/rollup/src/rollup.test.ts | 8 +- e2e/utils/command-utils.ts | 16 ++- e2e/utils/project-config-utils.ts | 25 ++-- e2e/vite/src/vite.test.ts | 8 +- e2e/web/src/file-server.test.ts | 2 +- e2e/web/src/web.test.ts | 10 +- e2e/webpack/src/webpack.test.ts | 4 +- packages/devkit/nx-reexports-pre16.ts | 5 +- .../devkit/src/utils/convert-nx-executor.ts | 23 +++- .../config/calculate-default-project-name.ts | 2 +- packages/nx/src/config/configuration.ts | 13 --- packages/nx/src/config/workspaces.spec.ts | 16 ++- packages/nx/src/config/workspaces.ts | 109 +++--------------- ...project-graph-incremental-recomputation.ts | 17 +-- packages/nx/src/devkit-exports.ts | 7 +- packages/nx/src/devkit-internals.ts | 1 + .../executors/utils/convert-nx-executor.ts | 15 ++- .../explicit-project-dependencies.spec.ts | 5 +- .../src/project-graph/build-project-graph.ts | 39 +++---- packages/nx/src/project-graph/file-utils.ts | 22 ++-- .../src/project-graph/nx-deps-cache.spec.ts | 11 +- .../nx/src/project-graph/nx-deps-cache.ts | 6 +- .../nx/src/project-graph/project-graph.ts | 5 +- .../utils/retrieve-workspace-files.ts | 68 ++++++++++- packages/nx/src/tasks-runner/run-command.ts | 1 - .../src/tasks-runner/tasks-schedule.spec.ts | 1 - .../nx/src/tasks-runner/tasks-schedule.ts | 3 - .../utils/assert-workspace-validity.spec.ts | 34 +++--- .../nx/src/utils/assert-workspace-validity.ts | 14 +-- 57 files changed, 389 insertions(+), 435 deletions(-) delete mode 100644 docs/generated/devkit/readAllWorkspaceConfiguration.md create mode 100644 docs/generated/devkit/readProjectsConfigurationFromProjectGraph.md diff --git a/docs/generated/devkit/README.md b/docs/generated/devkit/README.md index 4758549129e12..1d833f7b4e75a 100644 --- a/docs/generated/devkit/README.md +++ b/docs/generated/devkit/README.md @@ -138,12 +138,12 @@ It only uses language primitives and immutable objects - [offsetFromRoot](../../devkit/documents/offsetFromRoot) - [parseJson](../../devkit/documents/parseJson) - [parseTargetString](../../devkit/documents/parseTargetString) -- [readAllWorkspaceConfiguration](../../devkit/documents/readAllWorkspaceConfiguration) - [readCachedProjectGraph](../../devkit/documents/readCachedProjectGraph) - [readJson](../../devkit/documents/readJson) - [readJsonFile](../../devkit/documents/readJsonFile) - [readNxJson](../../devkit/documents/readNxJson) - [readProjectConfiguration](../../devkit/documents/readProjectConfiguration) +- [readProjectsConfigurationFromProjectGraph](../../devkit/documents/readProjectsConfigurationFromProjectGraph) - [readRootPackageJson](../../devkit/documents/readRootPackageJson) - [readTargetOptions](../../devkit/documents/readTargetOptions) - [readWorkspaceConfiguration](../../devkit/documents/readWorkspaceConfiguration) diff --git a/docs/generated/devkit/Workspaces.md b/docs/generated/devkit/Workspaces.md index e05a7fb152f36..9701c1fedaa28 100644 --- a/docs/generated/devkit/Workspaces.md +++ b/docs/generated/devkit/Workspaces.md @@ -1,5 +1,9 @@ # Class: Workspaces +**`Deprecated`** + +This will be removed in v18. Use [readProjectsConfigurationFromProjectGraph](../../devkit/documents/readProjectsConfigurationFromProjectGraph) instead. + ## Table of contents ### Constructors @@ -8,13 +12,10 @@ ### Properties -- [cachedProjectsConfig](../../devkit/documents/Workspaces#cachedprojectsconfig) - [root](../../devkit/documents/Workspaces#root) ### Methods -- [mergeTargetDefaultsIntoProjectDescriptions](../../devkit/documents/Workspaces#mergetargetdefaultsintoprojectdescriptions) -- [readProjectsConfigurations](../../devkit/documents/Workspaces#readprojectsconfigurations) - [readWorkspaceConfiguration](../../devkit/documents/Workspaces#readworkspaceconfiguration) ## Constructors @@ -31,67 +32,19 @@ ## Properties -### cachedProjectsConfig - -• `Private` **cachedProjectsConfig**: [`ProjectsConfigurations`](../../devkit/documents/ProjectsConfigurations) - ---- - ### root • `Private` **root**: `string` ## Methods -### mergeTargetDefaultsIntoProjectDescriptions - -▸ `Private` **mergeTargetDefaultsIntoProjectDescriptions**(`projects`, `nxJson`): `Record`<`string`, [`ProjectConfiguration`](../../devkit/documents/ProjectConfiguration)\> - -#### Parameters - -| Name | Type | -| :--------- | :----------------------------------------------------------------------------------------- | -| `projects` | `Record`<`string`, [`ProjectConfiguration`](../../devkit/documents/ProjectConfiguration)\> | -| `nxJson` | [`NxJsonConfiguration`](../../devkit/documents/NxJsonConfiguration)<`string`[] \| `"*"`\> | - -#### Returns - -`Record`<`string`, [`ProjectConfiguration`](../../devkit/documents/ProjectConfiguration)\> - ---- - -### readProjectsConfigurations - -▸ **readProjectsConfigurations**(`opts?`): [`ProjectsConfigurations`](../../devkit/documents/ProjectsConfigurations) - -**`Deprecated`** - -#### Parameters - -| Name | Type | -| :-------------------------------------- | :-------- | -| `opts?` | `Object` | -| `opts._includeProjectsFromAngularJson?` | `boolean` | - -#### Returns - -[`ProjectsConfigurations`](../../devkit/documents/ProjectsConfigurations) - ---- - ### readWorkspaceConfiguration -▸ **readWorkspaceConfiguration**(`opts?`): [`ProjectsConfigurations`](../../devkit/documents/ProjectsConfigurations) & [`NxJsonConfiguration`](../../devkit/documents/NxJsonConfiguration)<`string`[] \| `"*"`\> +▸ **readWorkspaceConfiguration**(): [`ProjectsConfigurations`](../../devkit/documents/ProjectsConfigurations) & [`NxJsonConfiguration`](../../devkit/documents/NxJsonConfiguration)<`string`[] \| `"*"`\> -Deprecated. Use readProjectsConfigurations - -#### Parameters +**`Deprecated`** -| Name | Type | -| :-------------------------------------- | :-------- | -| `opts?` | `Object` | -| `opts._ignorePluginInference?` | `boolean` | -| `opts._includeProjectsFromAngularJson?` | `boolean` | +Use [readProjectsConfigurationFromProjectGraph](../../devkit/documents/readProjectsConfigurationFromProjectGraph) instead. #### Returns diff --git a/docs/generated/devkit/readAllWorkspaceConfiguration.md b/docs/generated/devkit/readAllWorkspaceConfiguration.md deleted file mode 100644 index 309682dcd6600..0000000000000 --- a/docs/generated/devkit/readAllWorkspaceConfiguration.md +++ /dev/null @@ -1,11 +0,0 @@ -# Function: readAllWorkspaceConfiguration - -▸ **readAllWorkspaceConfiguration**(): [`ProjectsConfigurations`](../../devkit/documents/ProjectsConfigurations) & [`NxJsonConfiguration`](../../devkit/documents/NxJsonConfiguration) - -**`Deprecated`** - -Use readProjectsConfigurationFromProjectGraph(await createProjectGraphAsync()) - -#### Returns - -[`ProjectsConfigurations`](../../devkit/documents/ProjectsConfigurations) & [`NxJsonConfiguration`](../../devkit/documents/NxJsonConfiguration) diff --git a/docs/generated/devkit/readProjectsConfigurationFromProjectGraph.md b/docs/generated/devkit/readProjectsConfigurationFromProjectGraph.md new file mode 100644 index 0000000000000..d30aa67146b69 --- /dev/null +++ b/docs/generated/devkit/readProjectsConfigurationFromProjectGraph.md @@ -0,0 +1,15 @@ +# Function: readProjectsConfigurationFromProjectGraph + +▸ **readProjectsConfigurationFromProjectGraph**(`projectGraph`): [`ProjectsConfigurations`](../../devkit/documents/ProjectsConfigurations) + +Get the [ProjectsConfigurations](../../devkit/documents/ProjectsConfigurations) from the [ProjectGraph](../../devkit/documents/ProjectGraph) + +#### Parameters + +| Name | Type | +| :------------- | :---------------------------------------------------- | +| `projectGraph` | [`ProjectGraph`](../../devkit/documents/ProjectGraph) | + +#### Returns + +[`ProjectsConfigurations`](../../devkit/documents/ProjectsConfigurations) diff --git a/docs/generated/packages/devkit/documents/nx_devkit.md b/docs/generated/packages/devkit/documents/nx_devkit.md index 4758549129e12..1d833f7b4e75a 100644 --- a/docs/generated/packages/devkit/documents/nx_devkit.md +++ b/docs/generated/packages/devkit/documents/nx_devkit.md @@ -138,12 +138,12 @@ It only uses language primitives and immutable objects - [offsetFromRoot](../../devkit/documents/offsetFromRoot) - [parseJson](../../devkit/documents/parseJson) - [parseTargetString](../../devkit/documents/parseTargetString) -- [readAllWorkspaceConfiguration](../../devkit/documents/readAllWorkspaceConfiguration) - [readCachedProjectGraph](../../devkit/documents/readCachedProjectGraph) - [readJson](../../devkit/documents/readJson) - [readJsonFile](../../devkit/documents/readJsonFile) - [readNxJson](../../devkit/documents/readNxJson) - [readProjectConfiguration](../../devkit/documents/readProjectConfiguration) +- [readProjectsConfigurationFromProjectGraph](../../devkit/documents/readProjectsConfigurationFromProjectGraph) - [readRootPackageJson](../../devkit/documents/readRootPackageJson) - [readTargetOptions](../../devkit/documents/readTargetOptions) - [readWorkspaceConfiguration](../../devkit/documents/readWorkspaceConfiguration) diff --git a/e2e/angular-core/src/module-federation.test.ts b/e2e/angular-core/src/module-federation.test.ts index 1626c86073de2..7bd382862d27c 100644 --- a/e2e/angular-core/src/module-federation.test.ts +++ b/e2e/angular-core/src/module-federation.test.ts @@ -173,8 +173,10 @@ describe('Angular Module Federation', () => { // ports const hostPort = 4500; - const remote1Port = readProjectConfig(remote1).targets.serve.options.port; - const remote2Port = readProjectConfig(remote2).targets.serve.options.port; + const remote1Port = (await readProjectConfig(remote1)).targets.serve.options + .port; + const remote2Port = (await readProjectConfig(remote2)).targets.serve.options + .port; const process = await runCommandUntil( `serve-ssr ${host} --port=${hostPort}`, diff --git a/e2e/angular-core/src/projects.test.ts b/e2e/angular-core/src/projects.test.ts index 3945a4b0eb693..879e005217027 100644 --- a/e2e/angular-core/src/projects.test.ts +++ b/e2e/angular-core/src/projects.test.ts @@ -268,7 +268,7 @@ describe('Angular Projects', () => { ); // update the angular.json - updateProjectConfig(app1, (config) => { + await updateProjectConfig(app1, (config) => { config.targets.build.executor = '@nx/angular:webpack-browser'; config.targets.build.options = { ...config.targets.build.options, @@ -276,7 +276,7 @@ describe('Angular Projects', () => { }; return config; }); - updateProjectConfig(esbuildApp, (config) => { + await updateProjectConfig(esbuildApp, (config) => { config.targets.build.executor = '@nx/angular:browser-esbuild'; config.targets.build.options = { ...config.targets.build.options, diff --git a/e2e/angular-extensions/src/cypress-component-tests.test.ts b/e2e/angular-extensions/src/cypress-component-tests.test.ts index 91fef23dfbfec..039e6affbda66 100644 --- a/e2e/angular-extensions/src/cypress-component-tests.test.ts +++ b/e2e/angular-extensions/src/cypress-component-tests.test.ts @@ -30,7 +30,7 @@ describe('Angular Cypress Component Tests', () => { createBuildableLib(projectName, buildableLibName); - useWorkspaceAssetsInApp(appName); + await useWorkspaceAssetsInApp(appName); }); afterAll(() => cleanupProject()); @@ -265,7 +265,7 @@ export class AppModule {} ); } -function useWorkspaceAssetsInApp(appName: string) { +async function useWorkspaceAssetsInApp(appName: string) { // make sure assets from the workspace root work. createFile('libs/assets/data.json', JSON.stringify({ data: 'data' })); createFile( @@ -279,7 +279,7 @@ function useWorkspaceAssetsInApp(appName: string) { } ` ); - updateProjectConfig(appName, (config) => { + await updateProjectConfig(appName, (config) => { config.targets['build'].options.stylePreprocessorOptions = { includePaths: ['assets/styles'], }; diff --git a/e2e/angular-extensions/src/tailwind.test.ts b/e2e/angular-extensions/src/tailwind.test.ts index cedb59c571138..537574cc534e3 100644 --- a/e2e/angular-extensions/src/tailwind.test.ts +++ b/e2e/angular-extensions/src/tailwind.test.ts @@ -360,7 +360,7 @@ describe('Tailwind support', () => { runCLI( `generate @nx/angular:app ${appWithTailwind} --add-tailwind --no-interactive` ); - updateProjectConfig(appWithTailwind, (config) => { + await updateProjectConfig(appWithTailwind, (config) => { config.targets.build.executor = '@nx/angular:webpack-browser'; config.targets.build.options = { ...config.targets.build.options, diff --git a/e2e/esbuild/src/esbuild.test.ts b/e2e/esbuild/src/esbuild.test.ts index 0eba36bc83d5a..46593702eb0dd 100644 --- a/e2e/esbuild/src/esbuild.test.ts +++ b/e2e/esbuild/src/esbuild.test.ts @@ -6,7 +6,6 @@ import { packageInstall, readFile, readJson, - rmDist, runCLI, runCommand, runCommandUntil, @@ -27,7 +26,7 @@ describe('EsBuild Plugin', () => { const myPkg = uniq('my-pkg'); runCLI(`generate @nx/js:lib ${myPkg} --bundler=esbuild`); updateFile(`libs/${myPkg}/src/index.ts`, `console.log('Hello');\n`); - updateProjectConfig(myPkg, (json) => { + await updateProjectConfig(myPkg, (json) => { json.targets.build.options.assets = [`libs/${myPkg}/assets/*`]; return json; }); @@ -179,12 +178,12 @@ describe('EsBuild Plugin', () => { expect(runCommand(`node dist/libs/${myPkg}`)).toMatch(/Hello/); }, 300_000); - it('should support additional entry points', () => { + it('should support additional entry points', async () => { const myPkg = uniq('my-pkg'); runCLI(`generate @nx/js:lib ${myPkg} --bundler=esbuild`); updateFile(`libs/${myPkg}/src/index.ts`, `console.log('main');\n`); updateFile(`libs/${myPkg}/src/extra.ts`, `console.log('extra');\n`); - updateProjectConfig(myPkg, (json) => { + await updateProjectConfig(myPkg, (json) => { json.targets.build.options.additionalEntryPoints = [ `libs/${myPkg}/src/extra.ts`, ]; @@ -212,7 +211,7 @@ describe('EsBuild Plugin', () => { `libs/${myPkg}/esbuild.config.js`, `console.log('custom config loaded');\nmodule.exports = {};\n` ); - updateProjectConfig(myPkg, (json) => { + await updateProjectConfig(myPkg, (json) => { delete json.targets.build.options.esbuildOptions; json.targets.build.options.esbuildConfig = `libs/${myPkg}/esbuild.config.js`; return json; diff --git a/e2e/expo/src/expo.test.ts b/e2e/expo/src/expo.test.ts index cb9f8a02f9cfe..1fce2d1d3c359 100644 --- a/e2e/expo/src/expo.test.ts +++ b/e2e/expo/src/expo.test.ts @@ -85,8 +85,8 @@ describe('expo', () => { it('should prebuild', async () => { // run prebuild command with git check disable // set a mock package name for ios and android in expo's app.json - const workspace = readResolvedConfiguration(); - const root = workspace.projects[appName].root; + const projects = await readResolvedConfiguration(); + const root = projects[appName].root; const appJsonPath = join(root, `app.json`); const appJson = await readJson(appJsonPath); if (appJson.expo.ios) { diff --git a/e2e/js/src/js-node.test.ts b/e2e/js/src/js-node.test.ts index edef1cc115b36..9ba591106f5ea 100644 --- a/e2e/js/src/js-node.test.ts +++ b/e2e/js/src/js-node.test.ts @@ -16,7 +16,7 @@ describe('js:node error handling', () => { afterEach(() => cleanupProject()); - it('should log out the error', () => { + it('should log out the error', async () => { const esbuildLib = uniq('esbuildlib'); runCLI( @@ -30,7 +30,7 @@ describe('js:node error handling', () => { `; }); - updateProjectConfig(esbuildLib, (config) => { + await updateProjectConfig(esbuildLib, (config) => { config.targets['run-node'] = { executor: '@nx/js:node', options: { @@ -48,7 +48,7 @@ describe('js:node error handling', () => { expect(output).toContain('This is an error'); }, 240_000); - it('should execute library compiled with rollup', () => { + it('should execute library compiled with rollup', async () => { const rollupLib = uniq('rolluplib'); runCLI( @@ -61,7 +61,7 @@ describe('js:node error handling', () => { `; }); - updateProjectConfig(rollupLib, (config) => { + await updateProjectConfig(rollupLib, (config) => { config.targets['run-node'] = { executor: '@nx/js:node', options: { @@ -76,7 +76,7 @@ describe('js:node error handling', () => { expect(output).toContain('Hello from my library!'); }, 240_000); - it('should execute library compiled with tsc', () => { + it('should execute library compiled with tsc', async () => { const tscLib = uniq('tsclib'); runCLI(`generate @nx/js:lib ${tscLib} --bundler=tsc --no-interactive`); @@ -87,7 +87,7 @@ describe('js:node error handling', () => { `; }); - updateProjectConfig(tscLib, (config) => { + await updateProjectConfig(tscLib, (config) => { config.targets['run-node'] = { executor: '@nx/js:node', options: { @@ -102,7 +102,7 @@ describe('js:node error handling', () => { expect(output).toContain('Hello from my tsc library!'); }, 240_000); - it('should execute library compiled with swc', () => { + it('should execute library compiled with swc', async () => { const swcLib = uniq('swclib'); runCLI(`generate @nx/js:lib ${swcLib} --bundler=swc --no-interactive`); @@ -113,7 +113,7 @@ describe('js:node error handling', () => { `; }); - updateProjectConfig(swcLib, (config) => { + await updateProjectConfig(swcLib, (config) => { config.targets['run-node'] = { executor: '@nx/js:node', options: { @@ -128,7 +128,7 @@ describe('js:node error handling', () => { expect(output).toContain('Hello from my swc library!'); }, 240_000); - it('should execute webpack app', () => { + it('should execute webpack app', async () => { const webpackProject = uniq('webpackproject'); runCLI( @@ -141,7 +141,7 @@ describe('js:node error handling', () => { `; }); - updateProjectConfig(webpackProject, (config) => { + await updateProjectConfig(webpackProject, (config) => { config.targets['run-node'] = { executor: '@nx/js:node', options: { diff --git a/e2e/js/src/js-packaging.test.ts b/e2e/js/src/js-packaging.test.ts index 68470b8874de5..7599b76839e9a 100644 --- a/e2e/js/src/js-packaging.test.ts +++ b/e2e/js/src/js-packaging.test.ts @@ -123,7 +123,7 @@ describe('bundling libs', () => { expect(output).toContain(rollupLib); }, 500_000); - it('should support tsc and swc for building libs', () => { + it('should support tsc and swc for building libs', async () => { const tscLib = uniq('tsclib'); const swcLib = uniq('swclib'); const tscEsmLib = uniq('tscesmlib'); @@ -154,7 +154,7 @@ describe('bundling libs', () => { ); // Add additional entry points for `exports` field - updateProjectConfig(tscLib, (json) => { + await updateProjectConfig(tscLib, (json) => { json.targets.build.options.additionalEntryPoints = [ `libs/${tscLib}/src/foo/*.ts`, ]; @@ -162,7 +162,7 @@ describe('bundling libs', () => { }); updateFile(`libs/${tscLib}/src/foo/bar.ts`, `export const bar = 'bar';`); updateFile(`libs/${tscLib}/src/foo/faz.ts`, `export const faz = 'faz';`); - updateProjectConfig(swcLib, (json) => { + await updateProjectConfig(swcLib, (json) => { json.targets.build.options.additionalEntryPoints = [ `libs/${swcLib}/src/foo/*.ts`, ]; diff --git a/e2e/next/src/next.test.ts b/e2e/next/src/next.test.ts index 6450c656622fc..2234e54dd5390 100644 --- a/e2e/next/src/next.test.ts +++ b/e2e/next/src/next.test.ts @@ -67,7 +67,7 @@ describe('Next.js Applications', () => { // Additional assets that should be copied to dist const sharedLib = uniq('sharedLib'); - updateProjectConfig(appName, (json) => { + await updateProjectConfig(appName, (json) => { json.targets.build.options.assets = [ { glob: '**/*', @@ -367,7 +367,7 @@ describe('Next.js Applications', () => { checkFilesExist(`dist/apps/${appName}/next.config.js`); // Make sure withNx works with run-commands. - updateProjectConfig(appName, (json) => { + await updateProjectConfig(appName, (json) => { json.targets.build = { command: 'npx next build', outputs: [`apps/${appName}/.next`], diff --git a/e2e/node/src/node-server.test.ts b/e2e/node/src/node-server.test.ts index 1ea42f516a3cf..a8994bf12b968 100644 --- a/e2e/node/src/node-server.test.ts +++ b/e2e/node/src/node-server.test.ts @@ -151,7 +151,7 @@ describe('Node Applications + webpack', () => { runCLI( `generate @nx/node:app ${nodeApp2} --framework=none --no-interactive` ); - updateProjectConfig(nodeApp1, (config) => { + await updateProjectConfig(nodeApp1, (config) => { config.targets.serve.options.waitUntilTargets = [`${nodeApp2}:build`]; return config; }); diff --git a/e2e/node/src/node-webpack.test.ts b/e2e/node/src/node-webpack.test.ts index ed023a0cc622e..d478c9b181073 100644 --- a/e2e/node/src/node-webpack.test.ts +++ b/e2e/node/src/node-webpack.test.ts @@ -55,7 +55,7 @@ describe('Node Applications + webpack', () => { const lib = uniq('nodelib'); runCLI(`generate @nx/js:lib ${lib} --bundler=esbuild --no-interactive`); - updateProjectConfig(app, (config) => { + await updateProjectConfig(app, (config) => { // Since we read from lib from dist, we should re-build it when lib changes. config.targets.build.options.buildLibsFromSource = false; config.targets.serve.options.runBuildTargetDependencies = true; diff --git a/e2e/node/src/node.test.ts b/e2e/node/src/node.test.ts index 4052d82b502e9..f776ac8e01cac 100644 --- a/e2e/node/src/node.test.ts +++ b/e2e/node/src/node.test.ts @@ -75,7 +75,7 @@ describe('Node Applications', () => { const nodeapp = uniq('nodeapp'); runCLI(`generate @nx/node:app ${nodeapp} --linter=eslint`); - updateProjectConfig(nodeapp, (config) => { + await updateProjectConfig(nodeapp, (config) => { config.targets.build.options.outputFileName = 'index.js'; return config; }); @@ -94,7 +94,7 @@ describe('Node Applications', () => { const lintResults = runCLI(`lint ${nodeapp}`); expect(lintResults).toContain('All files pass linting.'); - updateProjectConfig(nodeapp, (config) => { + await updateProjectConfig(nodeapp, (config) => { config.targets.build.options.additionalEntryPoints = [ { entryName: 'additional-main', @@ -254,7 +254,7 @@ describe('Node Applications', () => { config.target = 'es2020'; return config; }); - updateProjectConfig(esmapp, (config) => { + await updateProjectConfig(esmapp, (config) => { config.targets.build.options.outputFileName = 'main.mjs'; config.targets.build.options.assets = []; return config; @@ -436,7 +436,7 @@ ${jslib}(); packageInstall('@nestjs/swagger', undefined, '^6.0.0'); - updateProjectConfig(nestapp, (config) => { + await updateProjectConfig(nestapp, (config) => { config.targets.build.options.tsPlugins = ['@nestjs/swagger/plugin']; return config; }); @@ -549,7 +549,7 @@ describe('nest libraries', function () { packageInstall('@nestjs/swagger', undefined, '~6.3.0'); - updateProjectConfig(nestlib, (config) => { + await updateProjectConfig(nestlib, (config) => { config.targets.build.options.transformers = [ { name: '@nestjs/swagger/plugin', diff --git a/e2e/nx-misc/src/extras.test.ts b/e2e/nx-misc/src/extras.test.ts index 86859a38ad05b..e52cf86c7bdef 100644 --- a/e2e/nx-misc/src/extras.test.ts +++ b/e2e/nx-misc/src/extras.test.ts @@ -18,7 +18,7 @@ describe('Extra Nx Misc Tests', () => { it('should stream output', async () => { const myapp = 'abcdefghijklmon'; runCLI(`generate @nx/web:app ${myapp}`); - updateProjectConfig(myapp, (c) => { + await updateProjectConfig(myapp, (c) => { c.targets['inner'] = { command: 'echo inner', }; @@ -122,7 +122,7 @@ describe('Extra Nx Misc Tests', () => { process.platform === 'win32' ? `%SHARED_VAR% %ROOT_ONLY% %NESTED_ONLY%` // Windows : `$SHARED_VAR $ROOT_ONLY $NESTED_ONLY`; - updateProjectConfig(mylib, (config) => { + await updateProjectConfig(mylib, (config) => { config.targets.echoEnvVariables.options.command += ` ${command}`; return config; }); @@ -135,7 +135,7 @@ describe('Extra Nx Misc Tests', () => { }, 120000); it('should pass options', async () => { - updateProjectConfig(mylib, (config) => { + await updateProjectConfig(mylib, (config) => { config.targets.echo = { command: 'echo --var1={args.var1}', options: { @@ -151,7 +151,7 @@ describe('Extra Nx Misc Tests', () => { it('should interpolate provided arguments', async () => { const echoTarget = uniq('echo'); - updateProjectConfig(mylib, (config) => { + await updateProjectConfig(mylib, (config) => { config.targets[echoTarget] = { executor: 'nx:run-commands', options: { @@ -186,7 +186,7 @@ describe('Extra Nx Misc Tests', () => { }, 120000); it('should fail when a process exits non-zero', async () => { - updateProjectConfig(mylib, (config) => { + await updateProjectConfig(mylib, (config) => { config.targets.error = { executor: 'nx:run-commands', options: { @@ -207,7 +207,7 @@ describe('Extra Nx Misc Tests', () => { }); it('run command should not break if output property is missing in options and arguments', async () => { - updateProjectConfig(mylib, (config) => { + await updateProjectConfig(mylib, (config) => { config.targets.lint.outputs = ['{options.outputFile}']; return config; }); @@ -241,7 +241,7 @@ describe('Extra Nx Misc Tests', () => { : `mkdir -p ${folder}`, `echo dummy > ${folder}/dummy.txt`, ]; - updateProjectConfig(mylib, (config) => { + await updateProjectConfig(mylib, (config) => { delete config.targets.build.options.command; config.targets.build.options = { ...config.targets.build.options, diff --git a/e2e/nx-misc/src/workspace.test.ts b/e2e/nx-misc/src/workspace.test.ts index 4fa85faae2cb0..1444928b6cc05 100644 --- a/e2e/nx-misc/src/workspace.test.ts +++ b/e2e/nx-misc/src/workspace.test.ts @@ -113,7 +113,7 @@ describe('Workspace Tests', () => { */ runCLI(`generate @nx/js:lib ${lib3} --unitTestRunner=jest`); - updateProjectConfig(lib3, (config) => { + await updateProjectConfig(lib3, (config) => { config.implicitDependencies = [`${lib1}-data-access`]; return config; }); @@ -172,13 +172,13 @@ describe('Workspace Tests', () => { expect(moveOutput).toContain(`CREATE ${rootClassPath}`); checkFilesExist(rootClassPath); - let workspace = await readResolvedConfiguration(); - expect(workspace.projects[`${lib1}-data-access`]).toBeUndefined(); - const newConfig = readProjectConfig(newName); + let projects = await readResolvedConfiguration(); + expect(projects[`${lib1}-data-access`]).toBeUndefined(); + const newConfig = await readProjectConfig(newName); expect(newConfig).toMatchObject({ tags: [], }); - const lib3Config = readProjectConfig(lib3); + const lib3Config = await readProjectConfig(lib3); expect(lib3Config.implicitDependencies).toEqual([ `shared-${lib1}-data-access`, ]); @@ -194,9 +194,9 @@ describe('Workspace Tests', () => { ] ).toEqual([`libs/shared/${lib1}/data-access/src/index.ts`]); - workspace = readResolvedConfiguration(); - expect(workspace.projects[`${lib1}-data-access`]).toBeUndefined(); - const project = readProjectConfig(newName); + projects = await readResolvedConfiguration(); + expect(projects[`${lib1}-data-access`]).toBeUndefined(); + const project = await readProjectConfig(newName); expect(project).toBeTruthy(); expect(project.sourceRoot).toBe(`${newPath}/src`); expect(project.targets.lint.options.lintFilePatterns).toEqual([ @@ -251,7 +251,7 @@ describe('Workspace Tests', () => { */ runCLI(`generate @nx/js:lib ${lib3} --unitTestRunner=jest`); - updateProjectConfig(lib3, (config) => { + await updateProjectConfig(lib3, (config) => { config.implicitDependencies = [`${lib1}-data-access`]; return config; }); @@ -321,13 +321,13 @@ describe('Workspace Tests', () => { ] ).toEqual([`libs/shared/${lib1}/data-access/src/index.ts`]); - const workspace = await readResolvedConfiguration(); - expect(workspace.projects[`${lib1}-data-access`]).toBeUndefined(); - const project = readProjectConfig(newName); + const projects = await readResolvedConfiguration(); + expect(projects[`${lib1}-data-access`]).toBeUndefined(); + const project = await readProjectConfig(newName); expect(project).toBeTruthy(); expect(project.sourceRoot).toBe(`${newPath}/src`); expect(project.tags).toEqual([]); - const lib3Config = readProjectConfig(lib3); + const lib3Config = await readProjectConfig(lib3); expect(lib3Config.implicitDependencies).toEqual([newName]); expect(project.targets.lint.options.lintFilePatterns).toEqual([ @@ -384,7 +384,7 @@ describe('Workspace Tests', () => { */ runCLI(`generate @nx/js:lib ${lib3} --unitTestRunner=jest`); - updateProjectConfig(lib3, (config) => { + await updateProjectConfig(lib3, (config) => { config.implicitDependencies = [`${lib1}-data-access`]; return config; }); @@ -454,9 +454,9 @@ describe('Workspace Tests', () => { ] ).toEqual([`packages/shared/${lib1}/data-access/src/index.ts`]); - const workspace = await readResolvedConfiguration(); - expect(workspace.projects[`${lib1}-data-access`]).toBeUndefined(); - const project = readProjectConfig(newName); + const projects = await readResolvedConfiguration(); + expect(projects[`${lib1}-data-access`]).toBeUndefined(); + const project = await readProjectConfig(newName); expect(project).toBeTruthy(); expect(project.sourceRoot).toBe(`${newPath}/src`); expect(project.targets.lint.options.lintFilePatterns).toEqual([ @@ -513,7 +513,7 @@ describe('Workspace Tests', () => { */ runCLI(`generate @nx/js:lib ${lib3} --unitTestRunner=jest`); - updateProjectConfig(lib3, (config) => { + await updateProjectConfig(lib3, (config) => { config.implicitDependencies = [lib1]; return config; }); @@ -572,13 +572,13 @@ describe('Workspace Tests', () => { expect(moveOutput).toContain(`CREATE ${rootClassPath}`); checkFilesExist(rootClassPath); - let workspace = readResolvedConfiguration(); - expect(workspace.projects[lib1]).toBeUndefined(); - const newConfig = readProjectConfig(newName); + let projects = await readResolvedConfiguration(); + expect(projects[lib1]).toBeUndefined(); + const newConfig = await readProjectConfig(newName); expect(newConfig).toMatchObject({ tags: [], }); - const lib3Config = readProjectConfig(lib3); + const lib3Config = await readProjectConfig(lib3); expect(lib3Config.implicitDependencies).toEqual([`${lib1}-data-access`]); expect(moveOutput).toContain('UPDATE tsconfig.base.json'); @@ -590,9 +590,9 @@ describe('Workspace Tests', () => { rootTsConfig.compilerOptions.paths[`@${proj}/${lib1}/data-access`] ).toEqual([`libs/${lib1}/data-access/src/index.ts`]); - workspace = readResolvedConfiguration(); - expect(workspace.projects[lib1]).toBeUndefined(); - const project = readProjectConfig(newName); + projects = await readResolvedConfiguration(); + expect(projects[lib1]).toBeUndefined(); + const project = await readProjectConfig(newName); expect(project).toBeTruthy(); expect(project.sourceRoot).toBe(`${newPath}/src`); expect(project.targets.lint.options.lintFilePatterns).toEqual([ @@ -655,7 +655,7 @@ describe('Workspace Tests', () => { */ runCLI(`generate @nx/js:lib ${lib3} --unitTestRunner=jest`); - updateProjectConfig(lib3, (config) => { + await updateProjectConfig(lib3, (config) => { config.implicitDependencies = [`${lib1}-data-access`]; return config; }); @@ -686,11 +686,11 @@ describe('Workspace Tests', () => { expect(moveOutput).toContain(`CREATE ${rootClassPath}`); checkFilesExist(rootClassPath); - const newConfig = readProjectConfig(newName); + const newConfig = await readProjectConfig(newName); expect(newConfig).toMatchObject({ tags: [], }); - const lib3Config = readProjectConfig(lib3); + const lib3Config = await readProjectConfig(lib3); expect(lib3Config.implicitDependencies).toEqual([ `shared-${lib1}-data-access`, ]); @@ -704,9 +704,9 @@ describe('Workspace Tests', () => { rootTsConfig.compilerOptions.paths[`shared/${lib1}/data-access`] ).toEqual([`libs/shared/${lib1}/data-access/src/index.ts`]); - const projects = readResolvedConfiguration(); - expect(projects.projects[`${lib1}-data-access`]).toBeUndefined(); - const project = readProjectConfig(newName); + const projects = await readResolvedConfiguration(); + expect(projects[`${lib1}-data-access`]).toBeUndefined(); + const project = await readProjectConfig(newName); expect(project).toBeTruthy(); expect(project.sourceRoot).toBe(`${newPath}/src`); expect(project.targets.lint.options.lintFilePatterns).toEqual([ @@ -741,7 +741,7 @@ describe('Workspace Tests', () => { */ runCLI(`generate @nx/js:lib ${lib2} --unitTestRunner=jest`); - updateProjectConfig(lib2, (config) => { + await updateProjectConfig(lib2, (config) => { config.implicitDependencies = [lib1]; return config; }); @@ -775,12 +775,12 @@ describe('Workspace Tests', () => { expect(exists(tmpProjPath(`libs/${lib1}`))).toBeFalsy(); expect(removeOutputForced).not.toContain(`UPDATE nx.json`); - const projectsConfigurations = readResolvedConfiguration(); - expect(projectsConfigurations.projects[`${lib1}`]).toBeUndefined(); - const lib2Config = readProjectConfig(lib2); + const projects = await readResolvedConfiguration(); + expect(projects[`${lib1}`]).toBeUndefined(); + const lib2Config = await readProjectConfig(lib2); expect(lib2Config.implicitDependencies).toEqual([]); - expect(projectsConfigurations.projects[`${lib1}`]).toBeUndefined(); + expect(projects[`${lib1}`]).toBeUndefined(); }); }); }); diff --git a/e2e/nx-run/src/affected-graph.test.ts b/e2e/nx-run/src/affected-graph.test.ts index fd6e0890ac005..4a6e1b1cf4cbe 100644 --- a/e2e/nx-run/src/affected-graph.test.ts +++ b/e2e/nx-run/src/affected-graph.test.ts @@ -214,11 +214,11 @@ describe('Nx Affected and Graph Tests', () => { } }, 1000000); - it('should detect changes to projects based on tags changes', () => { + it('should detect changes to projects based on tags changes', async () => { // TODO: investigate why affected gives different results on windows if (isNotWindows()) { generateAll(); - updateProjectConfig(myapp, (config) => ({ + await updateProjectConfig(myapp, (config) => ({ ...config, tags: ['tag'], })); @@ -229,9 +229,9 @@ describe('Nx Affected and Graph Tests', () => { } }); - it('should affect all projects by removing projects', () => { + it('should affect all projects by removing projects', async () => { generateAll(); - const root = readResolvedConfiguration().projects[mylib].root; + const root = (await readResolvedConfiguration())[mylib].root; removeFile(root); const output = runCLI('print-affected --select projects'); expect(output).toContain(myapp); @@ -239,9 +239,9 @@ describe('Nx Affected and Graph Tests', () => { expect(output).not.toContain(mylib); }); - it('should detect changes to implicitly dependant projects', () => { + it('should detect changes to implicitly dependant projects', async () => { generateAll(); - updateProjectConfig(myapp, (config) => ({ + await updateProjectConfig(myapp, (config) => ({ ...config, implicitDependencies: ['*', `!${myapp2}`], })); @@ -256,7 +256,7 @@ describe('Nx Affected and Graph Tests', () => { expect(output).toContain(mylib); // Clear implicit deps to not interfere with other tests. - updateProjectConfig(myapp, (config) => ({ + await updateProjectConfig(myapp, (config) => ({ ...config, implicitDependencies: [], })); diff --git a/e2e/nx-run/src/cache.test.ts b/e2e/nx-run/src/cache.test.ts index af2a0a044c718..377beac1a6b88 100644 --- a/e2e/nx-run/src/cache.test.ts +++ b/e2e/nx-run/src/cache.test.ts @@ -156,7 +156,7 @@ describe('cache', () => { it('should support using globs as outputs', async () => { const mylib = uniq('mylib'); runCLI(`generate @nx/js:library ${mylib}`); - updateProjectConfig(mylib, (c) => { + await updateProjectConfig(mylib, (c) => { c.targets.build = { executor: 'nx:run-commands', outputs: ['{workspaceRoot}/dist/!(.next)/**/!(z|x).(txt|md)'], diff --git a/e2e/nx-run/src/invoke-runner.test.ts b/e2e/nx-run/src/invoke-runner.test.ts index 5e13e381355c4..9d53d15e73d0e 100644 --- a/e2e/nx-run/src/invoke-runner.test.ts +++ b/e2e/nx-run/src/invoke-runner.test.ts @@ -16,7 +16,7 @@ describe('Invoke Runner', () => { it('should invoke runner imperatively ', async () => { const mylib = uniq('mylib'); runCLI(`generate @nx/js:lib ${mylib}`); - updateProjectConfig(mylib, (c) => { + await updateProjectConfig(mylib, (c) => { c.targets['prebuild'] = { command: 'echo prebuild', }; diff --git a/e2e/nx-run/src/run.test.ts b/e2e/nx-run/src/run.test.ts index fed424a962590..b9f2e3cf40fb9 100644 --- a/e2e/nx-run/src/run.test.ts +++ b/e2e/nx-run/src/run.test.ts @@ -28,9 +28,9 @@ describe('Nx Running Tests', () => { describe('running targets', () => { describe('(forwarding params)', () => { let proj = uniq('proj'); - beforeAll(() => { + beforeAll(async () => { runCLI(`generate @nx/js:lib ${proj}`); - updateProjectConfig(proj, (c) => { + await updateProjectConfig(proj, (c) => { c.targets['echo'] = { command: 'echo ECHO:', }; @@ -57,7 +57,7 @@ describe('Nx Running Tests', () => { it('should execute long running tasks', async () => { const myapp = uniq('myapp'); runCLI(`generate @nx/web:app ${myapp}`); - updateProjectConfig(myapp, (c) => { + await updateProjectConfig(myapp, (c) => { c.targets['counter'] = { executor: '@nx/workspace:counter', options: { @@ -79,8 +79,8 @@ describe('Nx Running Tests', () => { runCLI(`generate @nx/node:lib ${mylib}`); // Used to restore targets to lib after test - const original = readProjectConfig(mylib); - updateProjectConfig(mylib, (j) => { + const original = await readProjectConfig(mylib); + await updateProjectConfig(mylib, (j) => { delete j.targets; return j; }); @@ -113,7 +113,7 @@ describe('Nx Running Tests', () => { `Cannot find configuration for task ${mylib}:echo:fail` ); - updateProjectConfig(mylib, (c) => original); + await updateProjectConfig(mylib, (c) => original); }, 1000000); describe('tokens support', () => { @@ -125,7 +125,7 @@ describe('Nx Running Tests', () => { }); it('should support using {projectRoot} in options blocks in project.json', async () => { - updateProjectConfig(app, (c) => { + await updateProjectConfig(app, (c) => { c.targets['echo'] = { command: `node -e 'console.log("{projectRoot}")'`, }; @@ -137,7 +137,7 @@ describe('Nx Running Tests', () => { }); it('should support using {projectName} in options blocks in project.json', async () => { - updateProjectConfig(app, (c) => { + await updateProjectConfig(app, (c) => { c.targets['echo'] = { command: `node -e 'console.log("{projectName}")'`, }; @@ -157,7 +157,7 @@ describe('Nx Running Tests', () => { }; return json; }); - updateProjectConfig(app, (c) => { + await updateProjectConfig(app, (c) => { c.targets['echo'] = {}; return c; }); @@ -174,7 +174,7 @@ describe('Nx Running Tests', () => { }; return json; }); - updateProjectConfig(app, (c) => { + await updateProjectConfig(app, (c) => { c.targets['echo'] = {}; return c; }); @@ -190,13 +190,13 @@ describe('Nx Running Tests', () => { const myapp2 = uniq('b'); runCLI(`generate @nx/web:app ${myapp1}`); runCLI(`generate @nx/web:app ${myapp2}`); - updateProjectConfig(myapp1, (c) => { + await updateProjectConfig(myapp1, (c) => { c.targets['error'] = { command: 'echo boom1 && exit 1', }; return c; }); - updateProjectConfig(myapp2, (c) => { + await updateProjectConfig(myapp2, (c) => { c.targets['error'] = { executor: 'nx:run-commands', options: { @@ -431,9 +431,9 @@ describe('Nx Running Tests', () => { ); }); - it('should be able to include deps using dependsOn', () => { - const originalWorkspace = readProjectConfig(myapp); - updateProjectConfig(myapp, (config) => { + it('should be able to include deps using dependsOn', async () => { + const originalWorkspace = await readProjectConfig(myapp); + await updateProjectConfig(myapp, (config) => { config.targets.prep = { executor: 'nx:run-commands', options: { @@ -453,12 +453,12 @@ describe('Nx Running Tests', () => { expect(output).toContain(mylib2); expect(output).toContain('PREP'); - updateProjectConfig(myapp, () => originalWorkspace); + await updateProjectConfig(myapp, () => originalWorkspace); }, 10000); - it('should be able to include deps using target defaults defined at the root', () => { + it('should be able to include deps using target defaults defined at the root', async () => { const nxJson = readJson('nx.json'); - updateProjectConfig(myapp, (config) => { + await updateProjectConfig(myapp, (config) => { config.targets.prep = { command: 'echo PREP > one.txt', }; diff --git a/e2e/plugin/src/nx-plugin.test.ts b/e2e/plugin/src/nx-plugin.test.ts index bca4be24b3ebe..a11e434d1d408 100644 --- a/e2e/plugin/src/nx-plugin.test.ts +++ b/e2e/plugin/src/nx-plugin.test.ts @@ -381,14 +381,14 @@ describe('Nx Plugin', () => { }); describe('--directory', () => { - it('should create a plugin in the specified directory', () => { + it('should create a plugin in the specified directory', async () => { const plugin = uniq('plugin'); runCLI( `generate @nx/plugin:plugin ${plugin} --linter=eslint --directory subdir --e2eTestRunner=jest` ); checkFilesExist(`libs/subdir/${plugin}/package.json`); - const pluginProject = readProjectConfig(`subdir-${plugin}`); - const pluginE2EProject = readProjectConfig(`subdir-${plugin}-e2e`); + const pluginProject = await readProjectConfig(`subdir-${plugin}`); + const pluginE2EProject = await readProjectConfig(`subdir-${plugin}-e2e`); expect(pluginProject.targets).toBeDefined(); expect(pluginE2EProject).toBeTruthy(); }, 90000); @@ -399,7 +399,7 @@ describe('Nx Plugin', () => { runCLI( `generate @nx/plugin:plugin ${plugin} --linter=eslint --tags=e2etag,e2ePackage ` ); - const pluginProject = readProjectConfig(plugin); + const pluginProject = await readProjectConfig(plugin); expect(pluginProject.tags).toEqual(['e2etag', 'e2ePackage']); }, 90000); }); diff --git a/e2e/react-core/src/react-module-federation.test.ts b/e2e/react-core/src/react-module-federation.test.ts index 8c45c5bcfafb3..329bc3356588a 100644 --- a/e2e/react-core/src/react-module-federation.test.ts +++ b/e2e/react-core/src/react-module-federation.test.ts @@ -114,8 +114,8 @@ describe('React Module Federation', () => { // } }, 500_000); - function readPort(appName: string): number { - const config = readProjectConfig(appName); + async function readPort(appName: string): Promise { + const config = await readProjectConfig(appName); return config.targets.serve.options.port; } }); diff --git a/e2e/react-core/src/react-package.test.ts b/e2e/react-core/src/react-package.test.ts index 8a61dad360b07..c30d5a67889f2 100644 --- a/e2e/react-core/src/react-package.test.ts +++ b/e2e/react-core/src/react-package.test.ts @@ -36,7 +36,7 @@ describe('Build React libraries and apps', () => { let proj: string; - beforeEach(() => { + beforeEach(async () => { app = uniq('app'); parentLib = uniq('parentlib'); childLib = uniq('childlib'); @@ -99,7 +99,7 @@ describe('Build React libraries and apps', () => { ); // Add assets to child lib - updateProjectConfig(childLib, (json) => { + await updateProjectConfig(childLib, (json) => { json.targets.build.options.assets = [`libs/${childLib}/src/assets`]; return json; }); diff --git a/e2e/react-core/src/react.test.ts b/e2e/react-core/src/react.test.ts index 4849f47b7c509..ae5963e8fa3b4 100644 --- a/e2e/react-core/src/react.test.ts +++ b/e2e/react-core/src/react.test.ts @@ -234,14 +234,14 @@ describe('React Applications', () => { ${'scss'} ${'less'} ${'styl'} - `('should support global and css modules', ({ style }) => { + `('should support global and css modules', async ({ style }) => { const appName = uniq('app'); runCLI( `generate @nx/react:app ${appName} --style=${style} --bundler=webpack --no-interactive` ); // make sure stylePreprocessorOptions works - updateProjectConfig(appName, (config) => { + await updateProjectConfig(appName, (config) => { config.targets.build.options.stylePreprocessorOptions = { includePaths: ['libs/shared/lib'], }; diff --git a/e2e/react-extensions/src/cypress-component-tests.test.ts b/e2e/react-extensions/src/cypress-component-tests.test.ts index 85e58fd7b4157..a5f61a859f078 100644 --- a/e2e/react-extensions/src/cypress-component-tests.test.ts +++ b/e2e/react-extensions/src/cypress-component-tests.test.ts @@ -17,7 +17,7 @@ describe('React Cypress Component Tests', () => { const usedInAppLibName = uniq('cy-react-lib'); const buildableLibName = uniq('cy-react-buildable-lib'); - beforeAll(() => { + beforeAll(async () => { projectName = newProject({ name: uniq('cy-react') }); ensureCypressInstallation(); @@ -131,7 +131,7 @@ export default Input; ` ); createFile('libs/assets/data.json', JSON.stringify({ data: 'data' })); - updateProjectConfig(appName, (config) => { + await updateProjectConfig(appName, (config) => { config.targets['build'].options.assets.push({ glob: '**/*', input: 'libs/assets', @@ -228,7 +228,7 @@ ${content}`; } }, 300_000); - it('should work with async webpack config', () => { + it('should work with async webpack config', async () => { // TODO: (caleb) for whatever reason the MF webpack config + CT is running, but cypress is not starting up? // are they overriding some option on top of each other causing cypress to not see it's running? createFile( @@ -252,7 +252,7 @@ ${content}`; ); ` ); - updateProjectConfig(appName, (config) => { + await updateProjectConfig(appName, (config) => { config.targets[ 'build' ].options.webpackConfig = `apps/${appName}/webpack.config.js`; diff --git a/e2e/rollup/src/rollup.test.ts b/e2e/rollup/src/rollup.test.ts index 59f81d4d1c162..5d9b38955d3c7 100644 --- a/e2e/rollup/src/rollup.test.ts +++ b/e2e/rollup/src/rollup.test.ts @@ -37,7 +37,7 @@ describe('Rollup Plugin', () => { let output = runCommand(`node dist/libs/${myPkg}/index.cjs.js`); expect(output).toMatch(/Hello/); - updateProjectConfig(myPkg, (config) => { + await updateProjectConfig(myPkg, (config) => { delete config.targets.build; return config; }); @@ -51,7 +51,7 @@ describe('Rollup Plugin', () => { output = runCommand(`node dist/libs/${myPkg}/index.cjs.js`); expect(output).toMatch(/Hello/); - updateProjectConfig(myPkg, (config) => { + await updateProjectConfig(myPkg, (config) => { delete config.targets.build; return config; }); @@ -66,13 +66,13 @@ describe('Rollup Plugin', () => { expect(output).toMatch(/Hello/); }, 500000); - it('should support additional entry-points', () => { + it('should support additional entry-points', async () => { const myPkg = uniq('my-pkg'); runCLI(`generate @nx/js:lib ${myPkg} --bundler=none`); runCLI( `generate @nx/rollup:configuration ${myPkg} --target=node --tsConfig=libs/${myPkg}/tsconfig.lib.json --main=libs/${myPkg}/src/index.ts --compiler=tsc` ); - updateProjectConfig(myPkg, (config) => { + await updateProjectConfig(myPkg, (config) => { config.targets.build.options.format = ['cjs', 'esm']; config.targets.build.options.generateExportsField = true; config.targets.build.options.additionalEntryPoints = [ diff --git a/e2e/utils/command-utils.ts b/e2e/utils/command-utils.ts index c6a5175a1a350..8c645679adf5e 100644 --- a/e2e/utils/command-utils.ts +++ b/e2e/utils/command-utils.ts @@ -14,10 +14,11 @@ import { TargetConfiguration } from '@nx/devkit'; import { ChildProcess, exec, execSync, ExecSyncOptions } from 'child_process'; import { join } from 'path'; import * as isCI from 'is-ci'; -import { Workspaces } from '../../packages/nx/src/config/workspaces'; import { fileExists, readJson, updateFile } from './file-utils'; import { logError, stripConsoleColors } from './log-utils'; import { existsSync } from 'fs-extra'; +import { retrieveProjectConfigurationsSync } from '../../packages/nx/src/project-graph/utils/retrieve-workspace-files'; +import { readNxJson } from '../../packages/nx/src/config/nx-json'; export interface RunCmdOpts { silenceError?: boolean; @@ -36,11 +37,14 @@ export interface RunCmdOpts { */ export function setMaxWorkers() { if (isCI) { - const ws = new Workspaces(tmpProjPath()); - const projectsConfigurations = ws.readProjectsConfigurations(); - - Object.keys(projectsConfigurations.projects).forEach((appName) => { - let project = projectsConfigurations.projects[appName]; + const root = tmpProjPath(); + const projects = retrieveProjectConfigurationsSync( + root, + readNxJson(root) + ).projectNodes; + + Object.keys(projects).forEach((appName) => { + let project = projects[appName]; const { build } = project.targets as { [targetName: string]: TargetConfiguration; }; diff --git a/e2e/utils/project-config-utils.ts b/e2e/utils/project-config-utils.ts index 9e13f64032568..37990e11c27f3 100644 --- a/e2e/utils/project-config-utils.ts +++ b/e2e/utils/project-config-utils.ts @@ -1,27 +1,34 @@ -import { ProjectConfiguration, Workspaces } from '@nx/devkit'; +import { ProjectConfiguration } from '@nx/devkit'; import { join } from 'path'; import { tmpProjPath } from './create-project-utils'; import { readJson, updateFile } from './file-utils'; +import { retrieveProjectConfigurations } from '../../packages/nx/src/project-graph/utils/retrieve-workspace-files'; +import { readNxJson } from '../../packages/nx/src/config/nx-json'; -export function updateProjectConfig( +export async function updateProjectConfig( projectName: string, callback: (c: ProjectConfiguration) => ProjectConfiguration ) { - const workspace = readResolvedConfiguration(); - const root = workspace.projects[projectName].root; + const projects = await readResolvedConfiguration(); + const root = projects[projectName].root; const path = join(root, 'project.json'); const current = readJson(path); updateFile(path, JSON.stringify(callback(current), null, 2)); } -export function readResolvedConfiguration() { +export async function readResolvedConfiguration(): Promise< + Record +> { process.env.NX_PROJECT_GLOB_CACHE = 'false'; - const ws = new Workspaces(tmpProjPath()); - return ws.readProjectsConfigurations(); + const root = tmpProjPath(); + return (await retrieveProjectConfigurations(root, readNxJson(root))) + .projectNodes; } -export function readProjectConfig(projectName: string): ProjectConfiguration { - const root = readResolvedConfiguration().projects[projectName].root; +export async function readProjectConfig( + projectName: string +): Promise { + const root = (await readResolvedConfiguration())[projectName].root; const path = join(root, 'project.json'); return readJson(path); } diff --git a/e2e/vite/src/vite.test.ts b/e2e/vite/src/vite.test.ts index 28ca5d8133c17..b946d883e499e 100644 --- a/e2e/vite/src/vite.test.ts +++ b/e2e/vite/src/vite.test.ts @@ -64,7 +64,7 @@ describe('Vite Plugin', () => { }); describe('set up new React app with --bundler=vite option', () => { - beforeEach(() => { + beforeEach(async () => { proj = newProject(); runCLI(`generate @nx/react:app ${myApp} --bundler=vite`); createFile(`apps/${myApp}/public/hello.md`, `# Hello World`); @@ -99,7 +99,7 @@ describe('Vite Plugin', () => { ` ); - updateProjectConfig(myApp, (config) => { + await updateProjectConfig(myApp, (config) => { config.targets.build.options.fileReplacements = [ { replace: `apps/${myApp}/src/environments/environment.ts`, @@ -380,7 +380,7 @@ export default App; }, 100_000); // TODO: This takes forever and times out everything - find out why - xit('should not delete the project directory when coverage is enabled', () => { + xit('should not delete the project directory when coverage is enabled', async () => { // when coverage is enabled in the vite.config.ts but reportsDirectory is removed // from the @nx/vite:test executor options, vite will delete the project root directory runCLI(`generate @nx/react:lib ${lib} --unitTestRunner=vitest`); @@ -416,7 +416,7 @@ export default defineConfig({ }); `; }); - updateProjectConfig(lib, (config) => { + await updateProjectConfig(lib, (config) => { delete config.targets.test.options.reportsDirectory; return config; }); diff --git a/e2e/web/src/file-server.test.ts b/e2e/web/src/file-server.test.ts index 19d36cede407d..a1a78b67ac5a4 100644 --- a/e2e/web/src/file-server.test.ts +++ b/e2e/web/src/file-server.test.ts @@ -20,7 +20,7 @@ describe('file-server', () => { const port = 4301; runCLI(`generate @nx/web:app ${appName} --no-interactive`); - updateProjectConfig(appName, (config) => { + await updateProjectConfig(appName, (config) => { config.targets['serve'].executor = '@nx/web:file-server'; return config; }); diff --git a/e2e/web/src/web.test.ts b/e2e/web/src/web.test.ts index c1c379510d008..68f27e70c9ac3 100644 --- a/e2e/web/src/web.test.ts +++ b/e2e/web/src/web.test.ts @@ -262,7 +262,7 @@ describe('Web Components Applications', () => { `generate @nx/web:app ${appName} --bundler=webpack --no-interactive` ); - updateProjectConfig(appName, (config) => { + await updateProjectConfig(appName, (config) => { config.targets.build.options.webpackConfig = `apps/${appName}/webpack.config.js`; return config; }); @@ -411,7 +411,7 @@ describe('CLI - Environment Variables', () => { }); describe('Build Options', () => { - it('should inject/bundle external scripts and styles', () => { + it('should inject/bundle external scripts and styles', async () => { newProject(); const appName = uniq('app'); @@ -444,7 +444,7 @@ describe('Build Options', () => { const barScriptsBundleName = 'bar-scripts'; const barStylesBundleName = 'bar-styles'; - updateProjectConfig(appName, (config) => { + await updateProjectConfig(appName, (config) => { const buildOptions = config.targets.build.options; buildOptions.scripts = [ @@ -492,7 +492,7 @@ describe('Build Options', () => { }); describe('index.html interpolation', () => { - test('should interpolate environment variables', () => { + test('should interpolate environment variables', async () => { const appName = uniq('app'); runCLI( @@ -530,7 +530,7 @@ describe('index.html interpolation', () => { updateFile(envFilePath, envFileContents); updateFile(indexPath, indexContent); - updateProjectConfig(appName, (config) => { + await updateProjectConfig(appName, (config) => { const buildOptions = config.targets.build.options; buildOptions.deployUrl = 'baz'; return config; diff --git a/e2e/webpack/src/webpack.test.ts b/e2e/webpack/src/webpack.test.ts index b1d8a094868fe..4a28c276cb75b 100644 --- a/e2e/webpack/src/webpack.test.ts +++ b/e2e/webpack/src/webpack.test.ts @@ -45,7 +45,7 @@ module.exports = composePlugins(withNx(), (config) => { expect(output).not.toMatch(/Conflicting/); expect(output).not.toMatch(/process.env.NODE_ENV/); - updateProjectConfig(myPkg, (config) => { + await updateProjectConfig(myPkg, (config) => { delete config.targets.build; return config; }); @@ -59,7 +59,7 @@ module.exports = composePlugins(withNx(), (config) => { output = runCommand(`node dist/libs/${myPkg}/main.js`); expect(output).toMatch(/Hello/); - updateProjectConfig(myPkg, (config) => { + await updateProjectConfig(myPkg, (config) => { delete config.targets.build; return config; }); diff --git a/packages/devkit/nx-reexports-pre16.ts b/packages/devkit/nx-reexports-pre16.ts index 7bbf0f812b6ef..f3ffe1e2ae1e7 100644 --- a/packages/devkit/nx-reexports-pre16.ts +++ b/packages/devkit/nx-reexports-pre16.ts @@ -47,10 +47,7 @@ export type { */ export { Workspaces } from 'nx/src/config/workspaces'; -export { - readAllWorkspaceConfiguration, - workspaceLayout, -} from 'nx/src/config/configuration'; +export { workspaceLayout } from 'nx/src/config/configuration'; export type { NxPlugin, diff --git a/packages/devkit/src/utils/convert-nx-executor.ts b/packages/devkit/src/utils/convert-nx-executor.ts index 77a1fe4d815ad..ae27857f7bbed 100644 --- a/packages/devkit/src/utils/convert-nx-executor.ts +++ b/packages/devkit/src/utils/convert-nx-executor.ts @@ -2,7 +2,11 @@ import type { Observable } from 'rxjs'; import type { Executor, ExecutorContext } from 'nx/src/config/misc-interfaces'; import { requireNx } from '../../nx'; -const { Workspaces, readNxJsonFromDisk } = requireNx(); +const { + Workspaces, + readNxJsonFromDisk, + retrieveProjectConfigurationsWithAngularProjects, +} = requireNx(); /** * Convert an Nx Executor into an Angular Devkit Builder @@ -17,11 +21,22 @@ export function convertNxExecutor(executor: Executor) { ? readNxJsonFromDisk(builderContext.workspaceRoot) : // TODO(v18): remove readNxJson. This is to be backwards compatible with Nx 16.5 and below. (workspaces as any).readNxJson(); - const projectsConfigurations = workspaces.readProjectsConfigurations({ - _includeProjectsFromAngularJson: true, - }); const promise = async () => { + const projectsConfigurations = + retrieveProjectConfigurationsWithAngularProjects + ? { + version: 2, + projects: await retrieveProjectConfigurationsWithAngularProjects( + builderContext.workspaceRoot, + nxJsonConfiguration + ), + } + : // TODO(v18): remove retrieveProjectConfigurations. This is to be backwards compatible with Nx 16.5 and below. + (workspaces as any).readProjectsConfigurations({ + _includeProjectsFromAngularJson: true, + }); + const context: ExecutorContext = { root: builderContext.workspaceRoot, projectName: builderContext.target.project, diff --git a/packages/nx/src/config/calculate-default-project-name.ts b/packages/nx/src/config/calculate-default-project-name.ts index ba0ea24f136ad..a1b5dced9bd74 100644 --- a/packages/nx/src/config/calculate-default-project-name.ts +++ b/packages/nx/src/config/calculate-default-project-name.ts @@ -44,7 +44,7 @@ export function calculateDefaultProjectName( } export function findMatchingProjectInCwd( - projects: { [projectName: string]: ProjectConfiguration }, + projects: Record, relativeCwd: string ): string | undefined { const projectRootMappings = new Map(); diff --git a/packages/nx/src/config/configuration.ts b/packages/nx/src/config/configuration.ts index c34e6650fb9d0..80ab4ce397d3b 100644 --- a/packages/nx/src/config/configuration.ts +++ b/packages/nx/src/config/configuration.ts @@ -1,18 +1,5 @@ -import { Workspaces } from './workspaces'; -import { workspaceRoot } from '../utils/workspace-root'; -import { NxJsonConfiguration } from './nx-json'; -import { ProjectsConfigurations } from './workspace-json-project-json'; import { readNxJson } from './nx-json'; -// TODO(vsavkin): Remove after Nx 16 is out -/** - * @deprecated Use readProjectsConfigurationFromProjectGraph(await createProjectGraphAsync()) - */ -export function readAllWorkspaceConfiguration(): ProjectsConfigurations & - NxJsonConfiguration { - return new Workspaces(workspaceRoot).readProjectsConfigurations(); -} - /** * Returns information about where apps and libs will be created. */ diff --git a/packages/nx/src/config/workspaces.spec.ts b/packages/nx/src/config/workspaces.spec.ts index 6ea92f56e8fdc..5a84c35616759 100644 --- a/packages/nx/src/config/workspaces.spec.ts +++ b/packages/nx/src/config/workspaces.spec.ts @@ -1,6 +1,8 @@ import { toProjectName, Workspaces } from './workspaces'; import { TempFs } from '../utils/testing/temp-fs'; import { withEnvironmentVariables } from '../../internal-testing-utils/with-environment'; +import { retrieveProjectConfigurations } from '../project-graph/utils/retrieve-workspace-files'; +import { readNxJson } from './configuration'; const libConfig = (root, name?: string) => ({ name: name ?? toProjectName(`${root}/some-file`), @@ -45,7 +47,7 @@ describe('Workspaces', () => { }); const workspaces = new Workspaces(fs.tempDir); - const resolved = workspaces.readProjectsConfigurations(); + const resolved = workspaces.readWorkspaceConfiguration(); expect(resolved.projects.lib1).toEqual(standaloneConfig); }); @@ -74,7 +76,7 @@ describe('Workspaces', () => { }); const workspaces = new Workspaces(fs.tempDir); - const { projects } = workspaces.readProjectsConfigurations(); + const { projects } = workspaces.readWorkspaceConfiguration(); // projects got merged for lib1 expect(projects['lib1']).toEqual({ @@ -112,10 +114,12 @@ describe('Workspaces', () => { { NX_WORKSPACE_ROOT: fs.tempDir, }, - () => { - const workspaces = new Workspaces(fs.tempDir); - const resolved = workspaces.readProjectsConfigurations(); - expect(resolved.projects['my-package']).toEqual({ + async () => { + const resolved = await retrieveProjectConfigurations( + fs.tempDir, + readNxJson(fs.tempDir) + ); + expect(resolved.projectNodes['my-package']).toEqual({ name: 'my-package', root: 'packages/my-package', sourceRoot: 'packages/my-package', diff --git a/packages/nx/src/config/workspaces.ts b/packages/nx/src/config/workspaces.ts index 989c52c9eb59d..98a9b9bde18eb 100644 --- a/packages/nx/src/config/workspaces.ts +++ b/packages/nx/src/config/workspaces.ts @@ -1,110 +1,29 @@ -import { dirname, join } from 'path'; -import { workspaceRoot } from '../utils/workspace-root'; -import { readJsonFile } from '../utils/fileutils'; -import { getNxRequirePaths } from '../utils/installation-directory'; -import { loadNxPlugins, loadNxPluginsSync } from '../utils/nx-plugin'; +import { dirname } from 'path'; import type { NxJsonConfiguration } from './nx-json'; import { readNxJson } from './nx-json'; -import { - ProjectConfiguration, - ProjectsConfigurations, -} from './workspace-json-project-json'; -import { - mergeAngularJsonAndProjects, - shouldMergeAngularProjects, -} from '../adapter/angular-json'; -import { retrieveProjectConfigurationPaths } from '../project-graph/utils/retrieve-workspace-files'; -import { - buildProjectsConfigurationsFromProjectPathsAndPlugins, - mergeTargetConfigurations, - readTargetDefaultsForTarget, -} from '../project-graph/utils/project-configuration-utils'; +import { ProjectsConfigurations } from './workspace-json-project-json'; +import { retrieveProjectConfigurationsSync } from '../project-graph/utils/retrieve-workspace-files'; +// TODO(v18): remove this class +/** + * @deprecated This will be removed in v18. Use {@link readProjectsConfigurationFromProjectGraph} instead. + */ export class Workspaces { - private cachedProjectsConfig: ProjectsConfigurations; - constructor(private root: string) {} /** - * @deprecated + * @deprecated Use {@link readProjectsConfigurationFromProjectGraph} instead. */ - readProjectsConfigurations(opts?: { - _includeProjectsFromAngularJson?: boolean; - }): ProjectsConfigurations { - if ( - this.cachedProjectsConfig && - process.env.NX_CACHE_PROJECTS_CONFIG !== 'false' - ) { - return this.cachedProjectsConfig; - } + readWorkspaceConfiguration(): ProjectsConfigurations & NxJsonConfiguration { const nxJson = readNxJson(this.root); - const projectPaths = retrieveProjectConfigurationPaths(this.root, nxJson); - let projectsConfigurations = - buildProjectsConfigurationsFromProjectPathsAndPlugins( - nxJson, - projectPaths, - loadNxPluginsSync( - nxJson.plugins, - getNxRequirePaths(this.root), - this.root - ), - this.root - ).projects; - if ( - shouldMergeAngularProjects( - this.root, - opts?._includeProjectsFromAngularJson - ) - ) { - projectsConfigurations = mergeAngularJsonAndProjects( - projectsConfigurations, - this.root - ); - } - this.cachedProjectsConfig = { + + return { version: 2, - projects: projectsConfigurations, + projects: retrieveProjectConfigurationsSync(this.root, nxJson) + .projectNodes, + ...nxJson, }; - return this.cachedProjectsConfig; - } - - /** - * Deprecated. Use readProjectsConfigurations - */ - readWorkspaceConfiguration(opts?: { - _ignorePluginInference?: boolean; - _includeProjectsFromAngularJson?: boolean; - }): ProjectsConfigurations & NxJsonConfiguration { - const nxJson = readNxJson(this.root); - return { ...this.readProjectsConfigurations(opts), ...nxJson }; - } - - private mergeTargetDefaultsIntoProjectDescriptions( - projects: Record, - nxJson: NxJsonConfiguration - ) { - for (const proj of Object.values(projects)) { - if (proj.targets) { - for (const targetName of Object.keys(proj.targets ?? {})) { - const projectTargetDefinition = proj.targets[targetName]; - const defaults = readTargetDefaultsForTarget( - targetName, - nxJson.targetDefaults, - projectTargetDefinition.executor - ); - - if (defaults) { - proj.targets[targetName] = mergeTargetConfigurations( - proj, - targetName, - defaults - ); - } - } - } - } - return projects; } } diff --git a/packages/nx/src/daemon/server/project-graph-incremental-recomputation.ts b/packages/nx/src/daemon/server/project-graph-incremental-recomputation.ts index a9cc1a2abc997..aa0f24b4577a5 100644 --- a/packages/nx/src/daemon/server/project-graph-incremental-recomputation.ts +++ b/packages/nx/src/daemon/server/project-graph-incremental-recomputation.ts @@ -15,7 +15,6 @@ import { import { fileExists } from '../../utils/fileutils'; import { notifyFileWatcherSockets } from './file-watching/file-watcher-sockets'; import { serverLogger } from './logger'; -import { Workspaces } from '../../config/workspaces'; import { workspaceRoot } from '../../utils/workspace-root'; import { execSync } from 'child_process'; import { fileHasher, hashArray } from '../../hasher/file-hasher'; @@ -23,7 +22,10 @@ import { retrieveWorkspaceFiles, retrieveProjectConfigurations, } from '../../project-graph/utils/retrieve-workspace-files'; -import { ProjectConfiguration } from '../../config/workspace-json-project-json'; +import { + ProjectConfiguration, + ProjectsConfigurations, +} from '../../config/workspace-json-project-json'; import { readNxJson } from '../../config/nx-json'; let cachedSerializedProjectGraphPromise: Promise<{ @@ -173,7 +175,7 @@ async function processCollectedUpdatedAndDeletedFiles() { ); fileHasher.incrementalUpdate(updatedFiles, deletedFiles); - let nxJson = readNxJson(workspaceRoot); + const nxJson = readNxJson(workspaceRoot); const { projectNodes } = await retrieveProjectConfigurations( workspaceRoot, @@ -264,16 +266,17 @@ async function createAndSerializeProjectGraph(): Promise<{ }> { try { performance.mark('create-project-graph-start'); - const projectsConfigurations = new Workspaces( - workspaceRoot - ).readProjectsConfigurations(); + const projectConfigurations = await retrieveProjectConfigurations( + workspaceRoot, + readNxJson(workspaceRoot) + ); const projectFileMap = copyFileMap(projectFileMapWithFiles.projectFileMap); const allWorkspaceFiles = copyFileData( projectFileMapWithFiles.allWorkspaceFiles ); const { projectGraph, projectFileMapCache } = await buildProjectGraphUsingProjectFileMap( - projectsConfigurations, + projectConfigurations.projectNodes, knownExternalNodes, projectFileMap, allWorkspaceFiles, diff --git a/packages/nx/src/devkit-exports.ts b/packages/nx/src/devkit-exports.ts index 286c45e06e983..4b894196bb7c1 100644 --- a/packages/nx/src/devkit-exports.ts +++ b/packages/nx/src/devkit-exports.ts @@ -37,15 +37,13 @@ export type { HasherContext, } from './config/misc-interfaces'; +// TODO(v18): Remove this export /** * @category Workspace */ export { Workspaces } from './config/workspaces'; -export { - readAllWorkspaceConfiguration, - workspaceLayout, -} from './config/configuration'; +export { workspaceLayout } from './config/configuration'; export type { NxPlugin, @@ -208,6 +206,7 @@ export { reverse } from './project-graph/operators'; export { createProjectGraphAsync, readCachedProjectGraph, + readProjectsConfigurationFromProjectGraph, } from './project-graph/project-graph'; /** diff --git a/packages/nx/src/devkit-internals.ts b/packages/nx/src/devkit-internals.ts index 0f6bf3e705cae..f4573e62b5de5 100644 --- a/packages/nx/src/devkit-internals.ts +++ b/packages/nx/src/devkit-internals.ts @@ -7,3 +7,4 @@ export { createTempNpmDirectory } from './utils/package-manager'; export { getExecutorInformation } from './command-line/run/executor-utils'; export { readNxJson as readNxJsonFromDisk } from './config/nx-json'; export { calculateDefaultProjectName } from './config/calculate-default-project-name'; +export { retrieveProjectConfigurationsWithAngularProjects } from './project-graph/utils/retrieve-workspace-files'; diff --git a/packages/nx/src/executors/utils/convert-nx-executor.ts b/packages/nx/src/executors/utils/convert-nx-executor.ts index 5f838dfde4995..bc618dcc4d528 100644 --- a/packages/nx/src/executors/utils/convert-nx-executor.ts +++ b/packages/nx/src/executors/utils/convert-nx-executor.ts @@ -3,9 +3,10 @@ */ import type { Observable } from 'rxjs'; -import { Workspaces } from '../../config/workspaces'; import { readNxJson } from '../../config/nx-json'; import { Executor, ExecutorContext } from '../../config/misc-interfaces'; +import { retrieveProjectConfigurations } from '../../project-graph/utils/retrieve-workspace-files'; +import { ProjectsConfigurations } from '../../config/workspace-json-project-json'; /** * Convert an Nx Executor into an Angular Devkit Builder @@ -14,11 +15,17 @@ import { Executor, ExecutorContext } from '../../config/misc-interfaces'; */ export function convertNxExecutor(executor: Executor) { const builderFunction = (options, builderContext) => { - const workspaces = new Workspaces(builderContext.workspaceRoot); - const projectsConfigurations = workspaces.readProjectsConfigurations(); - const promise = async () => { const nxJsonConfiguration = readNxJson(builderContext.workspaceRoot); + const projectsConfigurations: ProjectsConfigurations = { + version: 2, + projects: ( + await retrieveProjectConfigurations( + builderContext.workspaceRoot, + nxJsonConfiguration + ) + ).projectNodes, + }; const context: ExecutorContext = { root: builderContext.workspaceRoot, projectName: builderContext.target.project, diff --git a/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-project-dependencies.spec.ts b/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-project-dependencies.spec.ts index 131b41a64ddf6..20b89c3f76019 100644 --- a/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-project-dependencies.spec.ts +++ b/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-project-dependencies.spec.ts @@ -587,7 +587,10 @@ async function createVirtualWorkspace(config: VirtualWorkspaceConfig) { return { ctx: { - projectsConfigurations: projectConfigurations, + projectsConfigurations: { + version: 2, + projectConfigurations: projectConfigurations, + }, nxJsonConfiguration: nxJson, filesToProcess: projectFileMap, }, diff --git a/packages/nx/src/project-graph/build-project-graph.ts b/packages/nx/src/project-graph/build-project-graph.ts index 3de813a8e140c..24bcec03b7abe 100644 --- a/packages/nx/src/project-graph/build-project-graph.ts +++ b/packages/nx/src/project-graph/build-project-graph.ts @@ -23,10 +23,7 @@ import { import { readJsonFile } from '../utils/fileutils'; import { NxJsonConfiguration } from '../config/nx-json'; import { ProjectGraphBuilder } from './project-graph-builder'; -import { - ProjectConfiguration, - ProjectsConfigurations, -} from '../config/workspace-json-project-json'; +import { ProjectConfiguration } from '../config/workspace-json-project-json'; import { readNxJson } from '../config/configuration'; import { existsSync } from 'fs'; import { PackageJson } from '../utils/package-json'; @@ -49,7 +46,7 @@ export function getProjectFileMap(): { } export async function buildProjectGraphUsingProjectFileMap( - projectsConfigurations: ProjectsConfigurations, + projects: Record, externalNodes: Record, projectFileMap: ProjectFileMap, allWorkspaceFiles: FileData[], @@ -64,7 +61,7 @@ export async function buildProjectGraphUsingProjectFileMap( const nxJson = readNxJson(); const projectGraphVersion = '6.0'; - assertWorkspaceValidity(projectsConfigurations, nxJson); + assertWorkspaceValidity(projects, nxJson); const packageJsonDeps = readCombinedDeps(); const rootTsConfig = readRootTsConfig(); @@ -75,7 +72,7 @@ export async function buildProjectGraphUsingProjectFileMap( !shouldRecomputeWholeGraph( fileMap, packageJsonDeps, - projectsConfigurations, + projects, nxJson, rootTsConfig ); @@ -89,7 +86,7 @@ export async function buildProjectGraphUsingProjectFileMap( } const context = createContext( - projectsConfigurations, + projects, nxJson, projectFileMap, filesToProcess @@ -185,27 +182,27 @@ async function buildProjectGraphUsingContext( } function createContext( - projectsConfigurations: ProjectsConfigurations, + projects: Record, nxJson: NxJsonConfiguration, fileMap: ProjectFileMap, filesToProcess: ProjectFileMap ): ProjectGraphProcessorContext { - const projects = Object.keys(projectsConfigurations.projects).reduce( - (map, projectName) => { - map[projectName] = { - ...projectsConfigurations.projects[projectName], - }; - return map; - }, - {} as Record - ); + const clonedProjects = Object.keys(projects).reduce((map, projectName) => { + map[projectName] = { + ...projects[projectName], + }; + return map; + }, {} as Record); return { nxJsonConfiguration: nxJson, - projectsConfigurations, workspace: { - ...projectsConfigurations, + version: 2, + projects: clonedProjects, ...nxJson, - projects, + }, + projectsConfigurations: { + version: 2, + projects: clonedProjects, }, fileMap, filesToProcess, diff --git a/packages/nx/src/project-graph/file-utils.ts b/packages/nx/src/project-graph/file-utils.ts index 05be6ba360a59..d95d866fb3d4e 100644 --- a/packages/nx/src/project-graph/file-utils.ts +++ b/packages/nx/src/project-graph/file-utils.ts @@ -1,4 +1,3 @@ -import { Workspaces } from '../config/workspaces'; import { execSync } from 'child_process'; import { existsSync, readFileSync } from 'fs'; import { extname, join, relative, sep } from 'path'; @@ -15,6 +14,7 @@ import { } from './project-graph'; import { toOldFormat } from '../adapter/angular-json'; import { getIgnoreObject } from '../utils/ignore'; +import { retrieveProjectConfigurationsSync } from './utils/retrieve-workspace-files'; export interface Change { type: string; @@ -119,20 +119,28 @@ function defaultReadFileAtRevision( } } +/** + * TODO(v18): Remove this function + * @deprecated To get projects use {@link retrieveProjectConfigurations} instead + */ export function readWorkspaceConfig(opts: { format: 'angularCli' | 'nx'; path?: string; }): ProjectsConfigurations { let configuration: ProjectsConfigurations | null = null; + const root = opts.path || process.cwd(); + const nxJson = readNxJson(root); try { const projectGraph = readCachedProjectGraph(); configuration = { - ...readNxJson(), + ...nxJson, ...readProjectsConfigurationFromProjectGraph(projectGraph), }; } catch { - const ws = new Workspaces(opts.path || process.cwd()); - configuration = ws.readProjectsConfigurations(); + configuration = { + version: 2, + projects: retrieveProjectConfigurationsSync(root, nxJson).projectNodes, + }; } if (opts.format === 'angularCli') { return toOldFormat(configuration); @@ -155,8 +163,4 @@ export function readPackageJson(): any { // Original Exports export { FileData }; // TODO(17): Remove these exports -export { - readNxJson, - readAllWorkspaceConfiguration, - workspaceLayout, -} from '../config/configuration'; +export { readNxJson, workspaceLayout } from '../config/configuration'; diff --git a/packages/nx/src/project-graph/nx-deps-cache.spec.ts b/packages/nx/src/project-graph/nx-deps-cache.spec.ts index e16cd152c54ee..1935f32e2f947 100644 --- a/packages/nx/src/project-graph/nx-deps-cache.spec.ts +++ b/packages/nx/src/project-graph/nx-deps-cache.spec.ts @@ -4,7 +4,7 @@ import { ProjectFileMapCache, shouldRecomputeWholeGraph, } from './nx-deps-cache'; -import { ProjectsConfigurations } from '../config/workspace-json-project-json'; +import { ProjectConfiguration } from '../config/workspace-json-project-json'; import { NxJsonConfiguration } from '../config/nx-json'; import { nxVersion } from '../utils/versions'; @@ -299,11 +299,10 @@ describe('nx deps utils', () => { return { ...defaults, ...p }; } - function createProjectsConfiguration(p: any): ProjectsConfigurations { - const defaults = { - projects: { mylib: {} }, - } as any; - return { ...defaults, ...p }; + function createProjectsConfiguration( + p: any + ): Record { + return { mylib: {}, ...p }; } function createNxJson(p: Partial): NxJsonConfiguration { diff --git a/packages/nx/src/project-graph/nx-deps-cache.ts b/packages/nx/src/project-graph/nx-deps-cache.ts index 4d3547efb9af5..e0e47a7d2a94a 100644 --- a/packages/nx/src/project-graph/nx-deps-cache.ts +++ b/packages/nx/src/project-graph/nx-deps-cache.ts @@ -8,7 +8,7 @@ import { ProjectFileMap, ProjectGraph, } from '../config/project-graph'; -import { ProjectsConfigurations } from '../config/workspace-json-project-json'; +import { ProjectConfiguration } from '../config/workspace-json-project-json'; import { projectGraphCacheDirectory } from '../utils/cache-directory'; import { directoryExists, @@ -171,7 +171,7 @@ export function writeCache( export function shouldRecomputeWholeGraph( cache: ProjectFileMapCache, packageJsonDeps: Record, - projects: ProjectsConfigurations, + projects: Record, nxJson: NxJsonConfiguration, tsConfig: { compilerOptions: { paths: { [k: string]: any } } } ): boolean { @@ -184,7 +184,7 @@ export function shouldRecomputeWholeGraph( // we have a cached project that is no longer present const cachedNodes = Object.keys(cache.projectFileMap); - if (cachedNodes.some((p) => projects.projects[p] === undefined)) { + if (cachedNodes.some((p) => projects[p] === undefined)) { return true; } diff --git a/packages/nx/src/project-graph/project-graph.ts b/packages/nx/src/project-graph/project-graph.ts index 78468254d0e7e..237db5227cd8b 100644 --- a/packages/nx/src/project-graph/project-graph.ts +++ b/packages/nx/src/project-graph/project-graph.ts @@ -54,6 +54,9 @@ export function readCachedProjectConfiguration( return node.data; } +/** + * Get the {@link ProjectsConfigurations} from the {@link ProjectGraph} + */ export function readProjectsConfigurationFromProjectGraph( projectGraph: ProjectGraph ): ProjectsConfigurations { @@ -81,7 +84,7 @@ export async function buildProjectGraphWithoutDaemon() { const cacheEnabled = process.env.NX_CACHE_PROJECT_GRAPH !== 'false'; return ( await buildProjectGraphUsingProjectFileMap( - projectConfigurations, + projectConfigurations.projects, externalNodes, projectFileMap, allWorkspaceFiles, diff --git a/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts b/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts index 58c0e5f409956..5736f7330ee92 100644 --- a/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts +++ b/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts @@ -99,20 +99,75 @@ export async function retrieveProjectConfigurations( externalNodes: Record; projectNodes: Record; }> { - const { getProjectConfigurations } = - require('../../native') as typeof import('../../native'); const plugins = await loadNxPlugins( nxJson?.plugins ?? [], getNxRequirePaths(workspaceRoot), workspaceRoot ); const globs = configurationGlobs(workspaceRoot, plugins); + return _retrieveProjectConfigurations(workspaceRoot, nxJson, plugins, globs); +} + +export async function retrieveProjectConfigurationsWithAngularProjects( + workspaceRoot: string, + nxJson: NxJsonConfiguration +): Promise<{ + externalNodes: Record; + projectNodes: Record; +}> { + const plugins = await loadNxPlugins( + nxJson?.plugins ?? [], + getNxRequirePaths(workspaceRoot), + workspaceRoot + ); + const globs = configurationGlobs(workspaceRoot, plugins); + return _retrieveProjectConfigurations( + workspaceRoot, + nxJson, + plugins, + globs, + true + ); +} + +/** + * @deprecated Use {@link retrieveProjectConfigurations} instead. + */ +export function retrieveProjectConfigurationsSync( + workspaceRoot: string, + nxJson: NxJsonConfiguration +): { + externalNodes: Record; + projectNodes: Record; +} { + const plugins = loadNxPluginsSync( + nxJson?.plugins ?? [], + getNxRequirePaths(workspaceRoot), + workspaceRoot + ); + const globs = configurationGlobs(workspaceRoot, plugins); + return _retrieveProjectConfigurations(workspaceRoot, nxJson, plugins, globs); +} + +function _retrieveProjectConfigurations( + workspaceRoot: string, + nxJson: NxJsonConfiguration, + plugins: NxPluginV2[], + globs: string[], + includeProjectsFromAngularJson: boolean = false +): { + externalNodes: Record; + projectNodes: Record; +} { + const { getProjectConfigurations } = + require('../../native') as typeof import('../../native'); return getProjectConfigurations(workspaceRoot, globs, (configs: string[]) => { const projectConfigurations = createProjectConfigurations( workspaceRoot, nxJson, configs, - plugins + plugins, + includeProjectsFromAngularJson ); return { @@ -210,7 +265,8 @@ function createProjectConfigurations( workspaceRoot: string, nxJson: NxJsonConfiguration, configFiles: string[], - plugins: NxPluginV2[] + plugins: NxPluginV2[], + includeProjectsFromAngularJson: boolean = false ): { projects: Record; externalNodes: Record; @@ -227,7 +283,9 @@ function createProjectConfigurations( let projectConfigurations = projects; - if (shouldMergeAngularProjects(workspaceRoot, false)) { + if ( + shouldMergeAngularProjects(workspaceRoot, includeProjectsFromAngularJson) + ) { projectConfigurations = mergeAngularJsonAndProjects( projectConfigurations, workspaceRoot diff --git a/packages/nx/src/tasks-runner/run-command.ts b/packages/nx/src/tasks-runner/run-command.ts index c3cd0981b4c12..2686b2d562cab 100644 --- a/packages/nx/src/tasks-runner/run-command.ts +++ b/packages/nx/src/tasks-runner/run-command.ts @@ -24,7 +24,6 @@ import { createTaskGraph } from './create-task-graph'; import { findCycle, makeAcyclic } from './task-graph-utils'; import { TargetDependencyConfig } from '../config/workspace-json-project-json'; import { handleErrors } from '../utils/params'; -import { Workspaces } from '../config/workspaces'; import { DaemonBasedTaskHasher, InProcessTaskHasher, diff --git a/packages/nx/src/tasks-runner/tasks-schedule.spec.ts b/packages/nx/src/tasks-runner/tasks-schedule.spec.ts index 04985ad9dc27e..018ade6f23321 100644 --- a/packages/nx/src/tasks-runner/tasks-schedule.spec.ts +++ b/packages/nx/src/tasks-runner/tasks-schedule.spec.ts @@ -1,5 +1,4 @@ import { TasksSchedule } from './tasks-schedule'; -import { Workspaces } from '../config/workspaces'; import { removeTasksFromTaskGraph } from './utils'; import { Task, TaskGraph } from '../config/task-graph'; import { DependencyType, ProjectGraph } from '../config/project-graph'; diff --git a/packages/nx/src/tasks-runner/tasks-schedule.ts b/packages/nx/src/tasks-runner/tasks-schedule.ts index e8d722beffb1b..2a010e42f420c 100644 --- a/packages/nx/src/tasks-runner/tasks-schedule.ts +++ b/packages/nx/src/tasks-runner/tasks-schedule.ts @@ -1,5 +1,3 @@ -import { Workspaces } from '../config/workspaces'; - import { calculateReverseDeps, getExecutorForTask, @@ -10,7 +8,6 @@ import { DefaultTasksRunnerOptions } from './default-tasks-runner'; import { TaskHasher } from '../hasher/task-hasher'; import { Task, TaskGraph } from '../config/task-graph'; import { ProjectGraph } from '../config/project-graph'; -import { NxJsonConfiguration } from '../config/nx-json'; import { hashTask } from '../hasher/hash-task'; import { findAllProjectNodeDependencies } from '../utils/project-graph-utils'; import { reverse } from '../project-graph/operators'; diff --git a/packages/nx/src/utils/assert-workspace-validity.spec.ts b/packages/nx/src/utils/assert-workspace-validity.spec.ts index be5fc66cbba3b..b38debaff6b07 100644 --- a/packages/nx/src/utils/assert-workspace-validity.spec.ts +++ b/packages/nx/src/utils/assert-workspace-validity.spec.ts @@ -1,38 +1,36 @@ import { assertWorkspaceValidity } from './assert-workspace-validity'; describe('assertWorkspaceValidity', () => { - let mockWorkspaceJson: any; + let mockProjects: any; beforeEach(() => { - mockWorkspaceJson = { - projects: { - app1: {}, - 'app1-e2e': {}, - app2: {}, - 'app2-e2e': {}, - lib1: {}, - lib2: {}, - }, + mockProjects = { + app1: {}, + 'app1-e2e': {}, + app2: {}, + 'app2-e2e': {}, + lib1: {}, + lib2: {}, }; }); it('should not throw for a valid workspace', () => { - assertWorkspaceValidity(mockWorkspaceJson, {}); + assertWorkspaceValidity(mockProjects, {}); }); it('should not throw for a project-level implicit dependency with a glob', () => { - mockWorkspaceJson.projects.app2.implicitDependencies = ['lib*']; + mockProjects.app2.implicitDependencies = ['lib*']; expect(() => { - assertWorkspaceValidity(mockWorkspaceJson, {}); + assertWorkspaceValidity(mockProjects, {}); }).not.toThrow(); }); it('should throw for an invalid project-level implicit dependency', () => { - mockWorkspaceJson.projects.app2.implicitDependencies = ['invalidproj']; - mockWorkspaceJson.projects.lib1.implicitDependencies = '*'; + mockProjects.app2.implicitDependencies = ['invalidproj']; + mockProjects.lib1.implicitDependencies = '*'; - expect(() => assertWorkspaceValidity(mockWorkspaceJson, {})) + expect(() => assertWorkspaceValidity(mockProjects, {})) .toThrowErrorMatchingInlineSnapshot(` "Configuration Error The following implicitDependencies should be an array of strings: @@ -45,10 +43,10 @@ describe('assertWorkspaceValidity', () => { }); it('should throw for an invalid project-level implicit dependency with glob', () => { - mockWorkspaceJson.projects.app2.implicitDependencies = ['invalid*']; + mockProjects.app2.implicitDependencies = ['invalid*']; try { - assertWorkspaceValidity(mockWorkspaceJson, {}); + assertWorkspaceValidity(mockProjects, {}); fail('should not reach'); } catch (e) { expect(e.message).toContain( diff --git a/packages/nx/src/utils/assert-workspace-validity.ts b/packages/nx/src/utils/assert-workspace-validity.ts index 03035c80c48fb..73f0883ee41c9 100644 --- a/packages/nx/src/utils/assert-workspace-validity.ts +++ b/packages/nx/src/utils/assert-workspace-validity.ts @@ -1,16 +1,16 @@ -import { ProjectsConfigurations } from '../config/workspace-json-project-json'; +import { ProjectConfiguration } from '../config/workspace-json-project-json'; import { NxJsonConfiguration } from '../config/nx-json'; import { findMatchingProjects } from './find-matching-projects'; import { output } from './output'; import { ProjectGraphProjectNode } from '../config/project-graph'; export function assertWorkspaceValidity( - projectsConfigurations: ProjectsConfigurations, + projects: Record, nxJson: NxJsonConfiguration ) { - const projectNames = Object.keys(projectsConfigurations.projects); + const projectNames = Object.keys(projects); const projectGraphNodes = projectNames.reduce((graph, project) => { - const projectConfiguration = projectsConfigurations.projects[project]; + const projectConfiguration = projects[project]; graph[project] = { name: project, type: projectConfiguration.projectType === 'library' ? 'lib' : 'app', // missing fallback to `e2e` @@ -21,10 +21,6 @@ export function assertWorkspaceValidity( return graph; }, {} as Record); - const projects = { - ...projectsConfigurations.projects, - }; - const invalidImplicitDependencies = new Map(); if (nxJson.implicitDependencies) { @@ -110,7 +106,7 @@ function detectAndSetInvalidProjectGlobValues( map: Map, sourceName: string, desiredImplicitDeps: string[], - projectConfigurations: ProjectsConfigurations['projects'], + projectConfigurations: Record, projects: Record ) { const invalidProjectsOrGlobs = desiredImplicitDeps.filter((implicit) => {