Skip to content

Commit 95b77f8

Browse files
authored
feat(core): flatten default base config to base (#19964)
1 parent 17e2248 commit 95b77f8

File tree

18 files changed

+160
-32
lines changed

18 files changed

+160
-32
lines changed

docs/generated/devkit/NxAffectedConfig.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Interface: NxAffectedConfig
22

3+
**`Deprecated`**
4+
5+
Use [NxJsonConfiguration#defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase) instead
6+
37
## Table of contents
48

59
### Properties

docs/generated/devkit/NxJsonConfiguration.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Nx.json configuration
2121
- [affected](../../devkit/documents/NxJsonConfiguration#affected): NxAffectedConfig
2222
- [cacheDirectory](../../devkit/documents/NxJsonConfiguration#cachedirectory): string
2323
- [cli](../../devkit/documents/NxJsonConfiguration#cli): Object
24+
- [defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase): string
2425
- [defaultProject](../../devkit/documents/NxJsonConfiguration#defaultproject): string
2526
- [extends](../../devkit/documents/NxJsonConfiguration#extends): string
2627
- [generators](../../devkit/documents/NxJsonConfiguration#generators): Object
@@ -47,6 +48,10 @@ Nx.json configuration
4748

4849
Default options for `nx affected`
4950

51+
**`Deprecated`**
52+
53+
use [defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase) instead. For more information see https://nx.dev/deprecated/affected-config#affected-config
54+
5055
---
5156

5257
### cacheDirectory
@@ -72,6 +77,14 @@ Default generator collection. It is used when no collection is provided.
7277

7378
---
7479

80+
### defaultBase
81+
82+
`Optional` **defaultBase**: `string`
83+
84+
Default value for --base used by `nx affected` and `nx format`.
85+
86+
---
87+
7588
### defaultProject
7689

7790
`Optional` **defaultProject**: `string`

docs/generated/devkit/Workspace.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use ProjectsConfigurations or NxJsonConfiguration
1919
- [affected](../../devkit/documents/Workspace#affected): NxAffectedConfig
2020
- [cacheDirectory](../../devkit/documents/Workspace#cachedirectory): string
2121
- [cli](../../devkit/documents/Workspace#cli): Object
22+
- [defaultBase](../../devkit/documents/Workspace#defaultbase): string
2223
- [defaultProject](../../devkit/documents/Workspace#defaultproject): string
2324
- [extends](../../devkit/documents/Workspace#extends): string
2425
- [generators](../../devkit/documents/Workspace#generators): Object
@@ -47,6 +48,10 @@ use ProjectsConfigurations or NxJsonConfiguration
4748

4849
Default options for `nx affected`
4950

51+
**`Deprecated`**
52+
53+
use [defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase) instead. For more information see https://nx.dev/deprecated/affected-config#affected-config
54+
5055
#### Inherited from
5156

5257
[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[affected](../../devkit/documents/NxJsonConfiguration#affected)
@@ -84,6 +89,18 @@ Default generator collection. It is used when no collection is provided.
8489

8590
---
8691

92+
### defaultBase
93+
94+
`Optional` **defaultBase**: `string`
95+
96+
Default value for --base used by `nx affected` and `nx format`.
97+
98+
#### Inherited from
99+
100+
[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase)
101+
102+
---
103+
87104
### defaultProject
88105

89106
`Optional` **defaultProject**: `string`

docs/shared/reference/nx-json.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ The following is an expanded example showing all options. Your `nx.json` will li
1616
],
1717
"parallel": 4,
1818
"cacheDirectory": "tmp/my-nx-cache",
19-
"affected": {
20-
"defaultBase": "main"
21-
},
19+
"defaultBase": "main",
2220
"namedInputs": {
2321
"default": ["{projectRoot}/**/*"],
2422
"production": ["!{projectRoot}/**/*.spec.tsx"]
@@ -123,9 +121,9 @@ nx run-many -t build --runner=another
123121

124122
The official types of `runner` supported by Nx are `"nx/tasks-runners/default"` and `"nx-cloud"`.
125123

126-
## Affected
124+
## Default Base
127125

128-
Tells Nx which branch and HEAD to use when calculating affected projects.
126+
Tells Nx which base branch to use when calculating affected projects.
129127

130128
- `defaultBase` defines the default base branch, defaults to `main`.
131129

e2e/angular-core/src/ng-add.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,7 @@ describe('convert Angular CLI workspace to an Nx workspace', () => {
143143
// check nx.json
144144
const nxJson = readJson('nx.json');
145145
expect(nxJson).toEqual({
146-
affected: {
147-
defaultBase: 'main',
148-
},
146+
defaultBase: 'main',
149147
namedInputs: {
150148
default: ['{projectRoot}/**/*', 'sharedGlobals'],
151149
production: [

packages/angular/src/generators/ng-add/__snapshots__/migrate-from-angular-cli.spec.ts.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ exports[`workspace move to nx layout should create a root eslint config 1`] = `
8080

8181
exports[`workspace move to nx layout should create nx.json 1`] = `
8282
{
83-
"affected": {
84-
"defaultBase": "main",
85-
},
83+
"defaultBase": "main",
8684
"defaultProject": "myApp",
8785
"namedInputs": {
8886
"default": [

packages/angular/src/generators/ng-add/utilities/workspace.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ export function createNxJson(
5050
const targets = getWorkspaceCommonTargets(tree);
5151

5252
writeJson<NxJsonConfiguration>(tree, 'nx.json', {
53-
affected: {
54-
defaultBase: options.defaultBase ?? deduceDefaultBase(),
55-
},
53+
defaultBase: options.defaultBase ?? deduceDefaultBase(),
5654
namedInputs: {
5755
sharedGlobals: [],
5856
default: ['{projectRoot}/**/*', 'sharedGlobals'],

packages/nx/migrations.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@
8989
"description": "Updates .env to disabled adding plugins when generating projects in an existing Nx workspace",
9090
"implementation": "./src/migrations/update-18-0-0/disable-crystal-for-existing-workspaces",
9191
"x-repair-skip": true
92+
},
93+
"move-default-base-to-nx-json-root": {
94+
"version": "18.1.0-beta.3",
95+
"description": "Moves affected.defaultBase to defaultBase in `nx.json`",
96+
"implementation": "./src/migrations/update-17-2-0/move-default-base"
9297
}
9398
}
9499
}

packages/nx/schemas/nx-schema.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
"description": "Default based branch used by affected commands."
1919
}
2020
},
21-
"additionalProperties": false
21+
"additionalProperties": false,
22+
"deprecated": "Use `defaultBase` instead. Support for setting `defaultBase` in `affected` will be removed in Nx 19."
23+
},
24+
"defaultBase": {
25+
"type": "string",
26+
"description": "Default --base used by affected logic."
2227
},
2328
"tasksRunnerOptions": {
2429
"type": "object",

packages/nx/src/adapter/compat.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const allowedProjectExtensions = [
5555
export const allowedWorkspaceExtensions = [
5656
'implicitDependencies',
5757
'affected',
58+
'defaultBase',
5859
'tasksRunnerOptions',
5960
'workspaceLayout',
6061
'plugins',

packages/nx/src/command-line/init/implementation/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ export function createNxJsonFile(
5858
delete nxJson.targetDefaults;
5959
}
6060

61-
nxJson.affected ??= {};
62-
nxJson.affected.defaultBase ??= deduceDefaultBase();
61+
nxJson.defaultBase ??= deduceDefaultBase();
6362
writeJsonFile(nxJsonPath, nxJson);
6463
}
6564

packages/nx/src/config/nx-json.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export interface ImplicitJsonSubsetDependency<T = '*' | string[]> {
1919
[key: string]: T | ImplicitJsonSubsetDependency<T>;
2020
}
2121

22+
/**
23+
* @deprecated Use {@link NxJsonConfiguration#defaultBase } instead
24+
*/
2225
export interface NxAffectedConfig {
2326
/**
2427
* Default based branch used by affected commands.
@@ -272,8 +275,15 @@ export interface NxJsonConfiguration<T = '*' | string[]> {
272275
targetDefaults?: TargetDefaults;
273276
/**
274277
* Default options for `nx affected`
278+
* @deprecated use {@link defaultBase} instead. For more information see https://nx.dev/deprecated/affected-config#affected-config
275279
*/
276280
affected?: NxAffectedConfig;
281+
282+
/**
283+
* Default value for --base used by `nx affected` and `nx format`.
284+
*/
285+
defaultBase?: string;
286+
277287
/**
278288
* Where new apps + libs should be placed
279289
*/
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace';
2+
import { Tree } from '../../generators/tree';
3+
import { readNxJson, updateNxJson } from '../../generators/utils/nx-json';
4+
5+
import update from './move-default-base';
6+
7+
describe('update-17.1.0 migration', () => {
8+
let tree: Tree;
9+
10+
beforeEach(() => {
11+
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
12+
});
13+
14+
it("shouldn't do anything if affected.defaultBase is not set", () => {
15+
tree.write('nx.json', JSON.stringify({}));
16+
update(tree);
17+
expect(readNxJson(tree)).toEqual({});
18+
});
19+
20+
it("shouldn't remove affected if other keys present", () => {
21+
updateNxJson(tree, {
22+
affected: {
23+
defaultBase: 'master',
24+
otherKey: 'otherValue',
25+
} as any,
26+
});
27+
update(tree);
28+
expect(readNxJson(tree)).toEqual({
29+
affected: {
30+
otherKey: 'otherValue',
31+
},
32+
defaultBase: 'master',
33+
});
34+
});
35+
36+
it('should remove affected if no other keys present', () => {
37+
updateNxJson(tree, {
38+
affected: {
39+
defaultBase: 'master',
40+
} as any,
41+
});
42+
update(tree);
43+
expect(readNxJson(tree)).toEqual({
44+
defaultBase: 'master',
45+
});
46+
});
47+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
import { readNxJson, updateNxJson } from '../../generators/utils/nx-json';
3+
import { Tree } from '../../generators/tree';
4+
import { NxJsonConfiguration } from '../../config/nx-json';
5+
6+
/**
7+
* Updates existing workspaces to move nx.json's affected.defaultBase to nx.json's base.
8+
*/
9+
export default function update(host: Tree) {
10+
const nxJson = readNxJson(host) as NxJsonConfiguration & {
11+
affected: { defaultBase?: string };
12+
};
13+
if (nxJson?.affected?.defaultBase) {
14+
nxJson.defaultBase = nxJson.affected.defaultBase;
15+
delete nxJson.affected.defaultBase;
16+
if (Object.keys(nxJson.affected).length === 0) {
17+
delete nxJson.affected;
18+
}
19+
updateNxJson(host, nxJson);
20+
}
21+
}

packages/nx/src/utils/command-line-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ export function splitArgsIntoNxArgsAndOverrides(
142142
}
143143

144144
if (!nxArgs.base) {
145-
nxArgs.base = nxJson.affected?.defaultBase || 'main';
145+
nxArgs.base =
146+
nxJson.defaultBase ?? nxJson.affected?.defaultBase ?? 'main';
146147

147148
// No user-provided arguments to set the affected criteria, so inform the user of the defaults being used
148149
if (

packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
NxJsonConfiguration,
33
PackageManager,
44
readJson,
5+
readNxJson,
56
Tree,
67
updateJson,
78
writeJson,
@@ -114,6 +115,21 @@ describe('CI Workflow generator', () => {
114115
);
115116
});
116117

118+
it('should prefix nx.json base with origin/ if ci is bitbucket-pipelines', async () => {
119+
setNxCloud(tree);
120+
121+
const nxJson = readNxJson(tree);
122+
nxJson.defaultBase = 'my-branch';
123+
writeJson(tree, 'nx.json', nxJson);
124+
125+
await ciWorkflowGenerator(tree, {
126+
ci: 'bitbucket-pipelines',
127+
name: 'CI',
128+
});
129+
130+
expect(readNxJson(tree).defaultBase).toEqual('origin/my-branch');
131+
});
132+
117133
it('should generate gitlab config', async () => {
118134
setNxCloud(tree);
119135
await ciWorkflowGenerator(tree, { ci: 'gitlab', name: 'CI' });

packages/workspace/src/generators/ci-workflow/ci-workflow.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,16 @@ function normalizeOptions(options: Schema, tree: Tree): Substitutes {
8585
}
8686

8787
function defaultBranchNeedsOriginPrefix(nxJson: NxJsonConfiguration): boolean {
88-
return !nxJson.affected?.defaultBase?.startsWith('origin/');
88+
const base = nxJson.defaultBase ?? nxJson.affected?.defaultBase;
89+
return !base?.startsWith('origin/');
8990
}
9091

9192
function appendOriginPrefix(nxJson: NxJsonConfiguration): NxJsonConfiguration {
92-
return {
93-
...nxJson,
94-
affected: {
95-
...(nxJson.affected ?? {}),
96-
defaultBase: nxJson.affected?.defaultBase
97-
? `origin/${nxJson.affected.defaultBase}`
98-
: 'origin/main',
99-
},
100-
};
93+
if (nxJson?.affected?.defaultBase) {
94+
nxJson.affected.defaultBase = `origin/${nxJson.affected.defaultBase}`;
95+
}
96+
if (nxJson.defaultBase || !nxJson.affected) {
97+
nxJson.defaultBase = `origin/${nxJson.defaultBase ?? deduceDefaultBase()}`;
98+
}
99+
return nxJson;
101100
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ function createNxJson(
6464
) {
6565
const nxJson: NxJsonConfiguration & { $schema: string } = {
6666
$schema: './node_modules/nx/schemas/nx-schema.json',
67-
affected: {
68-
defaultBase,
69-
},
67+
defaultBase,
7068
targetDefaults:
7169
process.env.NX_ADD_PLUGINS === 'false'
7270
? {
@@ -82,7 +80,7 @@ function createNxJson(
8280
};
8381

8482
if (defaultBase === 'main') {
85-
delete nxJson.affected;
83+
delete nxJson.defaultBase;
8684
}
8785
if (preset !== Preset.NPM) {
8886
nxJson.namedInputs = {

0 commit comments

Comments
 (0)