Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): accept various task runner options from root of nx.json #19243

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion docs/generated/devkit/NxJsonConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Nx.json configuration
### Properties

- [affected](../../devkit/documents/NxJsonConfiguration#affected): NxAffectedConfig
- [cacheDirectory](../../devkit/documents/NxJsonConfiguration#cachedirectory): string
- [cli](../../devkit/documents/NxJsonConfiguration#cli): Object
- [defaultProject](../../devkit/documents/NxJsonConfiguration#defaultproject): string
- [extends](../../devkit/documents/NxJsonConfiguration#extends): string
Expand All @@ -27,11 +28,14 @@ Nx.json configuration
- [installation](../../devkit/documents/NxJsonConfiguration#installation): NxInstallationConfiguration
- [namedInputs](../../devkit/documents/NxJsonConfiguration#namedinputs): Object
- [npmScope](../../devkit/documents/NxJsonConfiguration#npmscope): string
- [nxCloudAccessToken](../../devkit/documents/NxJsonConfiguration#nxcloudaccesstoken): string
- [parallel](../../devkit/documents/NxJsonConfiguration#parallel): number
- [plugins](../../devkit/documents/NxJsonConfiguration#plugins): string[]
- [pluginsConfig](../../devkit/documents/NxJsonConfiguration#pluginsconfig): Record<string, unknown>
- [release](../../devkit/documents/NxJsonConfiguration#release): NxReleaseConfiguration
- [targetDefaults](../../devkit/documents/NxJsonConfiguration#targetdefaults): TargetDefaults
- [tasksRunnerOptions](../../devkit/documents/NxJsonConfiguration#tasksrunneroptions): Object
- [useDaemonProcess](../../devkit/documents/NxJsonConfiguration#usedaemonprocess): boolean
- [workspaceLayout](../../devkit/documents/NxJsonConfiguration#workspacelayout): Object

## Properties
Expand All @@ -44,6 +48,14 @@ Default options for `nx affected`

---

### cacheDirectory

• `Optional` **cacheDirectory**: `string`

Changes the directory used by Nx to store its cache.

---

### cli

• `Optional` **cli**: `Object`
Expand Down Expand Up @@ -148,6 +160,23 @@ NPM Scope that the workspace uses

---

### nxCloudAccessToken

• `Optional` **nxCloudAccessToken**: `string`

If specified Nx will use nx-cloud by default with the given token.
To use a different runner that accepts an access token, define it in [tasksRunnerOptions](../../devkit/documents/Workspace#tasksrunneroptions)

---

### parallel

• `Optional` **parallel**: `number`

Specifies how many tasks can be run in parallel.

---

### plugins

• `Optional` **plugins**: `string`[]
Expand All @@ -174,7 +203,7 @@ Configuration for Nx Plugins

### targetDefaults

• `Optional` **targetDefaults**: `TargetDefaults`
• `Optional` **targetDefaults**: [`TargetDefaults`](../../devkit/documents/TargetDefaults)

Dependencies between different target names across all projects

Expand All @@ -192,6 +221,14 @@ Available Task Runners

---

### useDaemonProcess

• `Optional` **useDaemonProcess**: `boolean`

Set this to false to disable the daemon.

---

### workspaceLayout

• `Optional` **workspaceLayout**: `Object`
Expand Down
1 change: 1 addition & 0 deletions docs/generated/devkit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ It only uses language primitives and immutable objects
- [RawProjectGraphDependency](../../devkit/documents/RawProjectGraphDependency)
- [StaticDependency](../../devkit/documents/StaticDependency)
- [StringChange](../../devkit/documents/StringChange)
- [TargetDefaults](../../devkit/documents/TargetDefaults)
- [TaskGraphExecutor](../../devkit/documents/TaskGraphExecutor)
- [WorkspaceConfiguration](../../devkit/documents/WorkspaceConfiguration)
- [WorkspaceJsonConfiguration](../../devkit/documents/WorkspaceJsonConfiguration)
Expand Down
3 changes: 3 additions & 0 deletions docs/generated/devkit/TargetDefaults.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Type alias: TargetDefaults

Ƭ **TargetDefaults**: `Record`<`string`, `Partial`<[`TargetConfiguration`](../../devkit/documents/TargetConfiguration)\> & { `cache?`: `boolean` }\>
9 changes: 9 additions & 0 deletions docs/generated/devkit/Task.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ A representation of the invocation of an Executor

### Properties

- [cache](../../devkit/documents/Task#cache): boolean
- [endTime](../../devkit/documents/Task#endtime): number
- [hash](../../devkit/documents/Task#hash): string
- [hashDetails](../../devkit/documents/Task#hashdetails): Object
Expand All @@ -17,6 +18,14 @@ A representation of the invocation of an Executor

## Properties

### cache

• `Optional` **cache**: `boolean`

Determines if a given task should be cacheable.

---

### endTime

• `Optional` **endTime**: `number`
Expand Down
55 changes: 54 additions & 1 deletion docs/generated/devkit/Workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use ProjectsConfigurations or NxJsonConfiguration
### Properties

- [affected](../../devkit/documents/Workspace#affected): NxAffectedConfig
- [cacheDirectory](../../devkit/documents/Workspace#cachedirectory): string
- [cli](../../devkit/documents/Workspace#cli): Object
- [defaultProject](../../devkit/documents/Workspace#defaultproject): string
- [extends](../../devkit/documents/Workspace#extends): string
Expand All @@ -25,12 +26,15 @@ use ProjectsConfigurations or NxJsonConfiguration
- [installation](../../devkit/documents/Workspace#installation): NxInstallationConfiguration
- [namedInputs](../../devkit/documents/Workspace#namedinputs): Object
- [npmScope](../../devkit/documents/Workspace#npmscope): string
- [nxCloudAccessToken](../../devkit/documents/Workspace#nxcloudaccesstoken): string
- [parallel](../../devkit/documents/Workspace#parallel): number
- [plugins](../../devkit/documents/Workspace#plugins): string[]
- [pluginsConfig](../../devkit/documents/Workspace#pluginsconfig): Record&lt;string, unknown&gt;
- [projects](../../devkit/documents/Workspace#projects): Record&lt;string, ProjectConfiguration&gt;
- [release](../../devkit/documents/Workspace#release): NxReleaseConfiguration
- [targetDefaults](../../devkit/documents/Workspace#targetdefaults): TargetDefaults
- [tasksRunnerOptions](../../devkit/documents/Workspace#tasksrunneroptions): Object
- [useDaemonProcess](../../devkit/documents/Workspace#usedaemonprocess): boolean
- [version](../../devkit/documents/Workspace#version): number
- [workspaceLayout](../../devkit/documents/Workspace#workspacelayout): Object

Expand All @@ -48,6 +52,18 @@ Default options for `nx affected`

---

### cacheDirectory

• `Optional` **cacheDirectory**: `string`

Changes the directory used by Nx to store its cache.

#### Inherited from

[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[cacheDirectory](../../devkit/documents/NxJsonConfiguration#cachedirectory)

---

### cli

• `Optional` **cli**: `Object`
Expand Down Expand Up @@ -184,6 +200,31 @@ NPM Scope that the workspace uses

---

### nxCloudAccessToken

• `Optional` **nxCloudAccessToken**: `string`

If specified Nx will use nx-cloud by default with the given token.
To use a different runner that accepts an access token, define it in [tasksRunnerOptions](../../devkit/documents/Workspace#tasksrunneroptions)

#### Inherited from

[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[nxCloudAccessToken](../../devkit/documents/NxJsonConfiguration#nxcloudaccesstoken)

---

### parallel

• `Optional` **parallel**: `number`

Specifies how many tasks can be run in parallel.

#### Inherited from

[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[parallel](../../devkit/documents/NxJsonConfiguration#parallel)

---

### plugins

• `Optional` **plugins**: `string`[]
Expand Down Expand Up @@ -234,7 +275,7 @@ Projects' projects

### targetDefaults

• `Optional` **targetDefaults**: `TargetDefaults`
• `Optional` **targetDefaults**: [`TargetDefaults`](../../devkit/documents/TargetDefaults)

Dependencies between different target names across all projects

Expand All @@ -260,6 +301,18 @@ Available Task Runners

---

### useDaemonProcess

• `Optional` **useDaemonProcess**: `boolean`

Set this to false to disable the daemon.

#### Inherited from

[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[useDaemonProcess](../../devkit/documents/NxJsonConfiguration#usedaemonprocess)

---

### version

• **version**: `number`
Expand Down
1 change: 1 addition & 0 deletions docs/generated/packages/devkit/documents/nx_devkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ It only uses language primitives and immutable objects
- [RawProjectGraphDependency](../../devkit/documents/RawProjectGraphDependency)
- [StaticDependency](../../devkit/documents/StaticDependency)
- [StringChange](../../devkit/documents/StringChange)
- [TargetDefaults](../../devkit/documents/TargetDefaults)
- [TaskGraphExecutor](../../devkit/documents/TaskGraphExecutor)
- [WorkspaceConfiguration](../../devkit/documents/WorkspaceConfiguration)
- [WorkspaceJsonConfiguration](../../devkit/documents/WorkspaceJsonConfiguration)
Expand Down
16 changes: 16 additions & 0 deletions packages/nx/schemas/nx-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@
"defaultProject": {
"type": "string",
"description": "Default project. When project isn't provided, the default project will be used."
},
"accessToken": {
"type": "string",
"description": "The access token to use for nx-cloud. If set, the default tasks runner will be nx-cloud."
},
"parallel": {
"type": "number",
"description": "Specifies how many tasks are ran in parallel by Nx for the default tasks runner."
},
"cacheDirectory": {
"type": "string",
"description": "Specifies the default location of the cache directory."
},
"useDaemonProcess": {
"type": "boolean",
"description": "Specifies whether the daemon should be used for the default tasks runner."
}
},
"definitions": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function connectToNxCloudIfExplicitlyAsked(opts: {
export async function connectToNxCloudCommand(
promptOverride?: string
): Promise<boolean> {
if (isNxCloudUsed()) {
if (isNxCloudUsed(readNxJson())) {
output.log({
title: '✅ This workspace is already connected to Nx Cloud.',
bodyLines: [
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/src/command-line/connect/view-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { execSync } from 'child_process';
import { isNxCloudUsed } from '../../utils/nx-cloud-utils';
import { output } from '../../utils/output';
import { runNxSync } from '../../utils/child-process';
import { readNxJson } from '../../config/nx-json';

export async function viewLogs(): Promise<number> {
const cloudUsed = isNxCloudUsed();
const cloudUsed = isNxCloudUsed(readNxJson());
if (cloudUsed) {
output.error({
title: 'Your workspace is already connected to Nx Cloud',
Expand Down
9 changes: 5 additions & 4 deletions packages/nx/src/command-line/migrate/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1203,16 +1203,17 @@ async function generateMigrationsJsonAndUpdatePackageJson(
let originalPackageJson = existsSync(rootPkgJsonPath)
? readJsonFile<PackageJson>(rootPkgJsonPath)
: null;
const originalNxInstallation = readNxJson().installation;
const originalNxJson = readNxJson();
const from =
originalNxInstallation?.version ?? readNxVersion(originalPackageJson);
originalNxJson.installation?.version ??
readNxVersion(originalPackageJson);

try {
if (
['nx', '@nrwl/workspace'].includes(opts.targetPackage) &&
(await isMigratingToNewMajor(from, opts.targetVersion)) &&
!isCI() &&
!isNxCloudUsed()
!isNxCloudUsed(originalNxJson)
) {
const useCloud = await connectToNxCloudCommand(
messages.getPromptMessage('nxCloudMigration')
Expand All @@ -1237,7 +1238,7 @@ async function generateMigrationsJsonAndUpdatePackageJson(

const migrator = new Migrator({
packageJson: originalPackageJson,
nxInstallation: originalNxInstallation,
nxInstallation: originalNxJson.installation,
getInstalledPackageVersion: createInstalledPackageVersionsResolver(root),
fetch: createFetcher(),
from: opts.from,
Expand Down
32 changes: 31 additions & 1 deletion packages/nx/src/config/nx-json.ts
AgentEnder marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ export interface NxAffectedConfig {
defaultBase?: string;
}

export type TargetDefaults = Record<string, Partial<TargetConfiguration>>;
export type TargetDefaults = Record<
string,
Partial<TargetConfiguration> & {
/**
* Determines if Nx is able to cache a given target.
* Currently only supported in `targetDefaults`.
*/
cache?: boolean;
}
>;

export type TargetDependencies = Record<
string,
Expand Down Expand Up @@ -191,6 +200,27 @@ export interface NxJsonConfiguration<T = '*' | string[]> {
* **ALPHA**: Configuration for `nx release` (versioning and publishing of applications and libraries)
*/
release?: NxReleaseConfiguration;

/**
* If specified Nx will use nx-cloud by default with the given token.
* To use a different runner that accepts an access token, define it in {@link tasksRunnerOptions}
*/
nxCloudAccessToken?: string;

/**
* Specifies how many tasks can be run in parallel.
*/
parallel?: number;

/**
* Changes the directory used by Nx to store its cache.
*/
cacheDirectory?: string;

/**
* Set this to false to disable the daemon.
*/
useDaemonProcess?: boolean;
}

export function readNxJson(root: string = workspaceRoot): NxJsonConfiguration {
Expand Down
5 changes: 5 additions & 0 deletions packages/nx/src/config/task-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export interface Task {
* Unix timestamp of when a Batch Task ends
**/
endTime?: number;

/**
* Determines if a given task should be cacheable.
*/
cache?: boolean;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/nx/src/daemon/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export class DaemonClient {

enabled() {
if (this._enabled === undefined) {
// TODO(v18): Add migration to move it out of existing configs and remove the ?? here.
const useDaemonProcessOption =
this.nxJson.useDaemonProcess ??
AgentEnder marked this conversation as resolved.
Show resolved Hide resolved
this.nxJson.tasksRunnerOptions?.['default']?.options?.useDaemonProcess;
const env = process.env.NX_DAEMON;

Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/devkit-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export type {
ImplicitDependencyEntry,
ImplicitJsonSubsetDependency,
NxJsonConfiguration,
TargetDefaults,
NxAffectedConfig,
} from './config/nx-json';

Expand Down
Loading