diff --git a/docs/generated/devkit/NxAffectedConfig.md b/docs/generated/devkit/NxAffectedConfig.md index 5ce232df2b..159cb43fa8 100644 --- a/docs/generated/devkit/NxAffectedConfig.md +++ b/docs/generated/devkit/NxAffectedConfig.md @@ -1,5 +1,9 @@ # Interface: NxAffectedConfig +**`Deprecated`** + +Use [NxJsonConfiguration#defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase) instead + ## Table of contents ### Properties diff --git a/docs/generated/devkit/NxJsonConfiguration.md b/docs/generated/devkit/NxJsonConfiguration.md index 65f0619ad9..22c30cebb6 100644 --- a/docs/generated/devkit/NxJsonConfiguration.md +++ b/docs/generated/devkit/NxJsonConfiguration.md @@ -21,6 +21,7 @@ Nx.json configuration - [affected](../../devkit/documents/NxJsonConfiguration#affected): NxAffectedConfig - [cacheDirectory](../../devkit/documents/NxJsonConfiguration#cachedirectory): string - [cli](../../devkit/documents/NxJsonConfiguration#cli): Object +- [defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase): string - [defaultProject](../../devkit/documents/NxJsonConfiguration#defaultproject): string - [extends](../../devkit/documents/NxJsonConfiguration#extends): string - [generators](../../devkit/documents/NxJsonConfiguration#generators): Object @@ -47,6 +48,10 @@ Nx.json configuration Default options for `nx affected` +**`Deprecated`** + +use [defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase) instead. For more information see https://nx.dev/deprecated/affected-config#affected-config + --- ### cacheDirectory @@ -72,6 +77,14 @@ Default generator collection. It is used when no collection is provided. --- +### defaultBase + +• `Optional` **defaultBase**: `string` + +Default value for --base used by `nx affected` and `nx format`. + +--- + ### defaultProject • `Optional` **defaultProject**: `string` diff --git a/docs/generated/devkit/Workspace.md b/docs/generated/devkit/Workspace.md index 280d4004ee..1a86757ce6 100644 --- a/docs/generated/devkit/Workspace.md +++ b/docs/generated/devkit/Workspace.md @@ -19,6 +19,7 @@ use ProjectsConfigurations or NxJsonConfiguration - [affected](../../devkit/documents/Workspace#affected): NxAffectedConfig - [cacheDirectory](../../devkit/documents/Workspace#cachedirectory): string - [cli](../../devkit/documents/Workspace#cli): Object +- [defaultBase](../../devkit/documents/Workspace#defaultbase): string - [defaultProject](../../devkit/documents/Workspace#defaultproject): string - [extends](../../devkit/documents/Workspace#extends): string - [generators](../../devkit/documents/Workspace#generators): Object @@ -47,6 +48,10 @@ use ProjectsConfigurations or NxJsonConfiguration Default options for `nx affected` +**`Deprecated`** + +use [defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase) instead. For more information see https://nx.dev/deprecated/affected-config#affected-config + #### Inherited from [NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[affected](../../devkit/documents/NxJsonConfiguration#affected) @@ -84,6 +89,18 @@ Default generator collection. It is used when no collection is provided. --- +### defaultBase + +• `Optional` **defaultBase**: `string` + +Default value for --base used by `nx affected` and `nx format`. + +#### Inherited from + +[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase) + +--- + ### defaultProject • `Optional` **defaultProject**: `string` diff --git a/docs/shared/reference/nx-json.md b/docs/shared/reference/nx-json.md index 72d5723d41..fb84b2c16c 100644 --- a/docs/shared/reference/nx-json.md +++ b/docs/shared/reference/nx-json.md @@ -16,9 +16,7 @@ The following is an expanded example showing all options. Your `nx.json` will li ], "parallel": 4, "cacheDirectory": "tmp/my-nx-cache", - "affected": { - "defaultBase": "main" - }, + "defaultBase": "main", "namedInputs": { "default": ["{projectRoot}/**/*"], "production": ["!{projectRoot}/**/*.spec.tsx"] @@ -123,9 +121,9 @@ nx run-many -t build --runner=another The official types of `runner` supported by Nx are `"nx/tasks-runners/default"` and `"nx-cloud"`. -## Affected +## Default Base -Tells Nx which branch and HEAD to use when calculating affected projects. +Tells Nx which base branch to use when calculating affected projects. - `defaultBase` defines the default base branch, defaults to `main`. diff --git a/e2e/angular-core/src/ng-add.test.ts b/e2e/angular-core/src/ng-add.test.ts index cef8af5613..e0afc436b6 100644 --- a/e2e/angular-core/src/ng-add.test.ts +++ b/e2e/angular-core/src/ng-add.test.ts @@ -143,9 +143,7 @@ describe('convert Angular CLI workspace to an Nx workspace', () => { // check nx.json const nxJson = readJson('nx.json'); expect(nxJson).toEqual({ - affected: { - defaultBase: 'main', - }, + defaultBase: 'main', namedInputs: { default: ['{projectRoot}/**/*', 'sharedGlobals'], production: [ diff --git a/packages/angular/src/generators/ng-add/__snapshots__/migrate-from-angular-cli.spec.ts.snap b/packages/angular/src/generators/ng-add/__snapshots__/migrate-from-angular-cli.spec.ts.snap index 0a05e6f56a..9b8175483a 100644 --- a/packages/angular/src/generators/ng-add/__snapshots__/migrate-from-angular-cli.spec.ts.snap +++ b/packages/angular/src/generators/ng-add/__snapshots__/migrate-from-angular-cli.spec.ts.snap @@ -80,9 +80,7 @@ exports[`workspace move to nx layout should create a root eslint config 1`] = ` exports[`workspace move to nx layout should create nx.json 1`] = ` { - "affected": { - "defaultBase": "main", - }, + "defaultBase": "main", "defaultProject": "myApp", "namedInputs": { "default": [ diff --git a/packages/angular/src/generators/ng-add/utilities/workspace.ts b/packages/angular/src/generators/ng-add/utilities/workspace.ts index 6ca0605e45..492a351fca 100644 --- a/packages/angular/src/generators/ng-add/utilities/workspace.ts +++ b/packages/angular/src/generators/ng-add/utilities/workspace.ts @@ -50,9 +50,7 @@ export function createNxJson( const targets = getWorkspaceCommonTargets(tree); writeJson(tree, 'nx.json', { - affected: { - defaultBase: options.defaultBase ?? deduceDefaultBase(), - }, + defaultBase: options.defaultBase ?? deduceDefaultBase(), namedInputs: { sharedGlobals: [], default: ['{projectRoot}/**/*', 'sharedGlobals'], diff --git a/packages/nx/migrations.json b/packages/nx/migrations.json index 373764d17b..181d22ac37 100644 --- a/packages/nx/migrations.json +++ b/packages/nx/migrations.json @@ -89,6 +89,11 @@ "description": "Updates .env to disabled adding plugins when generating projects in an existing Nx workspace", "implementation": "./src/migrations/update-18-0-0/disable-crystal-for-existing-workspaces", "x-repair-skip": true + }, + "move-default-base-to-nx-json-root": { + "version": "18.1.0-beta.3", + "description": "Moves affected.defaultBase to defaultBase in `nx.json`", + "implementation": "./src/migrations/update-17-2-0/move-default-base" } } } diff --git a/packages/nx/schemas/nx-schema.json b/packages/nx/schemas/nx-schema.json index 3d601ac723..ea32715b88 100644 --- a/packages/nx/schemas/nx-schema.json +++ b/packages/nx/schemas/nx-schema.json @@ -18,7 +18,12 @@ "description": "Default based branch used by affected commands." } }, - "additionalProperties": false + "additionalProperties": false, + "deprecated": "Use `defaultBase` instead. Support for setting `defaultBase` in `affected` will be removed in Nx 19." + }, + "defaultBase": { + "type": "string", + "description": "Default --base used by affected logic." }, "tasksRunnerOptions": { "type": "object", diff --git a/packages/nx/src/adapter/compat.ts b/packages/nx/src/adapter/compat.ts index 57e1330053..d7934d1a3b 100644 --- a/packages/nx/src/adapter/compat.ts +++ b/packages/nx/src/adapter/compat.ts @@ -55,6 +55,7 @@ export const allowedProjectExtensions = [ export const allowedWorkspaceExtensions = [ 'implicitDependencies', 'affected', + 'defaultBase', 'tasksRunnerOptions', 'workspaceLayout', 'plugins', diff --git a/packages/nx/src/command-line/init/implementation/utils.ts b/packages/nx/src/command-line/init/implementation/utils.ts index 40f624709f..72cd69f2ee 100644 --- a/packages/nx/src/command-line/init/implementation/utils.ts +++ b/packages/nx/src/command-line/init/implementation/utils.ts @@ -58,8 +58,7 @@ export function createNxJsonFile( delete nxJson.targetDefaults; } - nxJson.affected ??= {}; - nxJson.affected.defaultBase ??= deduceDefaultBase(); + nxJson.defaultBase ??= deduceDefaultBase(); writeJsonFile(nxJsonPath, nxJson); } diff --git a/packages/nx/src/config/nx-json.ts b/packages/nx/src/config/nx-json.ts index d686d607a7..cd72363bfe 100644 --- a/packages/nx/src/config/nx-json.ts +++ b/packages/nx/src/config/nx-json.ts @@ -19,6 +19,9 @@ export interface ImplicitJsonSubsetDependency { [key: string]: T | ImplicitJsonSubsetDependency; } +/** + * @deprecated Use {@link NxJsonConfiguration#defaultBase } instead + */ export interface NxAffectedConfig { /** * Default based branch used by affected commands. @@ -272,8 +275,15 @@ export interface NxJsonConfiguration { targetDefaults?: TargetDefaults; /** * Default options for `nx affected` + * @deprecated use {@link defaultBase} instead. For more information see https://nx.dev/deprecated/affected-config#affected-config */ affected?: NxAffectedConfig; + + /** + * Default value for --base used by `nx affected` and `nx format`. + */ + defaultBase?: string; + /** * Where new apps + libs should be placed */ diff --git a/packages/nx/src/migrations/update-17-2-0/move-default-base.spec.ts b/packages/nx/src/migrations/update-17-2-0/move-default-base.spec.ts new file mode 100644 index 0000000000..4df8939458 --- /dev/null +++ b/packages/nx/src/migrations/update-17-2-0/move-default-base.spec.ts @@ -0,0 +1,47 @@ +import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace'; +import { Tree } from '../../generators/tree'; +import { readNxJson, updateNxJson } from '../../generators/utils/nx-json'; + +import update from './move-default-base'; + +describe('update-17.1.0 migration', () => { + let tree: Tree; + + beforeEach(() => { + tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); + }); + + it("shouldn't do anything if affected.defaultBase is not set", () => { + tree.write('nx.json', JSON.stringify({})); + update(tree); + expect(readNxJson(tree)).toEqual({}); + }); + + it("shouldn't remove affected if other keys present", () => { + updateNxJson(tree, { + affected: { + defaultBase: 'master', + otherKey: 'otherValue', + } as any, + }); + update(tree); + expect(readNxJson(tree)).toEqual({ + affected: { + otherKey: 'otherValue', + }, + defaultBase: 'master', + }); + }); + + it('should remove affected if no other keys present', () => { + updateNxJson(tree, { + affected: { + defaultBase: 'master', + } as any, + }); + update(tree); + expect(readNxJson(tree)).toEqual({ + defaultBase: 'master', + }); + }); +}); diff --git a/packages/nx/src/migrations/update-17-2-0/move-default-base.ts b/packages/nx/src/migrations/update-17-2-0/move-default-base.ts new file mode 100644 index 0000000000..a968faa83e --- /dev/null +++ b/packages/nx/src/migrations/update-17-2-0/move-default-base.ts @@ -0,0 +1,21 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ +import { readNxJson, updateNxJson } from '../../generators/utils/nx-json'; +import { Tree } from '../../generators/tree'; +import { NxJsonConfiguration } from '../../config/nx-json'; + +/** + * Updates existing workspaces to move nx.json's affected.defaultBase to nx.json's base. + */ +export default function update(host: Tree) { + const nxJson = readNxJson(host) as NxJsonConfiguration & { + affected: { defaultBase?: string }; + }; + if (nxJson?.affected?.defaultBase) { + nxJson.defaultBase = nxJson.affected.defaultBase; + delete nxJson.affected.defaultBase; + if (Object.keys(nxJson.affected).length === 0) { + delete nxJson.affected; + } + updateNxJson(host, nxJson); + } +} diff --git a/packages/nx/src/utils/command-line-utils.ts b/packages/nx/src/utils/command-line-utils.ts index cc5a731c92..1208c6c51b 100644 --- a/packages/nx/src/utils/command-line-utils.ts +++ b/packages/nx/src/utils/command-line-utils.ts @@ -142,7 +142,8 @@ export function splitArgsIntoNxArgsAndOverrides( } if (!nxArgs.base) { - nxArgs.base = nxJson.affected?.defaultBase || 'main'; + nxArgs.base = + nxJson.defaultBase ?? nxJson.affected?.defaultBase ?? 'main'; // No user-provided arguments to set the affected criteria, so inform the user of the defaults being used if ( diff --git a/packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts b/packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts index 4a9c88a3fc..4a3f3ac74d 100644 --- a/packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts +++ b/packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts @@ -2,6 +2,7 @@ import { NxJsonConfiguration, PackageManager, readJson, + readNxJson, Tree, updateJson, writeJson, @@ -114,6 +115,21 @@ describe('CI Workflow generator', () => { ); }); + it('should prefix nx.json base with origin/ if ci is bitbucket-pipelines', async () => { + setNxCloud(tree); + + const nxJson = readNxJson(tree); + nxJson.defaultBase = 'my-branch'; + writeJson(tree, 'nx.json', nxJson); + + await ciWorkflowGenerator(tree, { + ci: 'bitbucket-pipelines', + name: 'CI', + }); + + expect(readNxJson(tree).defaultBase).toEqual('origin/my-branch'); + }); + it('should generate gitlab config', async () => { setNxCloud(tree); await ciWorkflowGenerator(tree, { ci: 'gitlab', name: 'CI' }); diff --git a/packages/workspace/src/generators/ci-workflow/ci-workflow.ts b/packages/workspace/src/generators/ci-workflow/ci-workflow.ts index c22aa1b274..caa187c845 100644 --- a/packages/workspace/src/generators/ci-workflow/ci-workflow.ts +++ b/packages/workspace/src/generators/ci-workflow/ci-workflow.ts @@ -85,17 +85,16 @@ function normalizeOptions(options: Schema, tree: Tree): Substitutes { } function defaultBranchNeedsOriginPrefix(nxJson: NxJsonConfiguration): boolean { - return !nxJson.affected?.defaultBase?.startsWith('origin/'); + const base = nxJson.defaultBase ?? nxJson.affected?.defaultBase; + return !base?.startsWith('origin/'); } function appendOriginPrefix(nxJson: NxJsonConfiguration): NxJsonConfiguration { - return { - ...nxJson, - affected: { - ...(nxJson.affected ?? {}), - defaultBase: nxJson.affected?.defaultBase - ? `origin/${nxJson.affected.defaultBase}` - : 'origin/main', - }, - }; + if (nxJson?.affected?.defaultBase) { + nxJson.affected.defaultBase = `origin/${nxJson.affected.defaultBase}`; + } + if (nxJson.defaultBase || !nxJson.affected) { + nxJson.defaultBase = `origin/${nxJson.defaultBase ?? deduceDefaultBase()}`; + } + return nxJson; } diff --git a/packages/workspace/src/generators/new/generate-workspace-files.ts b/packages/workspace/src/generators/new/generate-workspace-files.ts index c30b73e1f0..1d69654752 100644 --- a/packages/workspace/src/generators/new/generate-workspace-files.ts +++ b/packages/workspace/src/generators/new/generate-workspace-files.ts @@ -64,9 +64,7 @@ function createNxJson( ) { const nxJson: NxJsonConfiguration & { $schema: string } = { $schema: './node_modules/nx/schemas/nx-schema.json', - affected: { - defaultBase, - }, + defaultBase, targetDefaults: process.env.NX_ADD_PLUGINS === 'false' ? { @@ -82,7 +80,7 @@ function createNxJson( }; if (defaultBase === 'main') { - delete nxJson.affected; + delete nxJson.defaultBase; } if (preset !== Preset.NPM) { nxJson.namedInputs = {