chore(core): move readNxJson out of Workspaces (#18127)

This commit is contained in:
Emily Xiong 2023-07-21 10:07:34 -04:00 committed by GitHub
parent e12e9bb9dd
commit 717a8dd027
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 78 additions and 144 deletions

View File

@ -6,7 +6,7 @@ import {
import type { DependentBuildableProjectNode } from '@nx/js/src/utils/buildable-libs-utils';
import { WebpackNxBuildCoordinationPlugin } from '@nx/webpack/src/plugins/webpack-nx-build-coordination-plugin';
import { existsSync } from 'fs';
import { readNxJson } from 'nx/src/project-graph/file-utils';
import { readNxJson } from 'nx/src/config/nx-json';
import { isNpmProject } from 'nx/src/project-graph/operators';
import { getDependencyConfigs } from 'nx/src/tasks-runner/utils';
import { from, Observable } from 'rxjs';

View File

@ -13,7 +13,7 @@ import {
findProjectForPath,
} from 'nx/src/project-graph/utils/find-project-for-path';
import { readProjectsConfigurationFromProjectGraph } from 'nx/src/project-graph/project-graph';
import { readNxJson } from 'nx/src/project-graph/file-utils';
import { readNxJson } from 'nx/src/config/nx-json';
export const CY_FILE_MATCHER = new RegExp(/\.cy\.[tj]sx?$/);
/**

View File

@ -13,7 +13,7 @@ import {
Tree,
workspaceRoot,
} from '@nx/devkit';
import { readNxJson } from 'nx/src/project-graph/file-utils';
import { readNxJson } from 'nx/src/config/nx-json';
import { readProjectsConfigurationFromProjectGraph } from 'nx/src/project-graph/project-graph';
interface FindTargetOptions {

View File

@ -47,11 +47,10 @@ export type {
*/
export { Workspaces } from 'nx/src/config/workspaces';
// TODO (v16): Change this to export from 'nx/src/config/configuration'
export {
readAllWorkspaceConfiguration,
workspaceLayout,
} from 'nx/src/project-graph/file-utils';
} from 'nx/src/config/configuration';
export type {
NxPlugin,

View File

@ -2,7 +2,7 @@ import type { Observable } from 'rxjs';
import type { Executor, ExecutorContext } from 'nx/src/config/misc-interfaces';
import { requireNx } from '../../nx';
const { Workspaces } = requireNx();
const { Workspaces, readNxJsonFromDisk } = requireNx();
/**
* Convert an Nx Executor into an Angular Devkit Builder
@ -13,7 +13,9 @@ const { Workspaces } = requireNx();
export function convertNxExecutor(executor: Executor) {
const builderFunction = (options, builderContext) => {
const workspaces = new Workspaces(builderContext.workspaceRoot);
const nxJsonConfiguration = workspaces.readNxJson();
const nxJsonConfiguration = readNxJsonFromDisk(
builderContext.workspaceRoot
);
const projectsConfigurations = workspaces.readProjectsConfigurations({
_includeProjectsFromAngularJson: true,
});

View File

@ -9,7 +9,7 @@ import {
createProjectRootMappings,
ProjectRootMappings,
} from 'nx/src/project-graph/utils/find-project-for-path';
import { readNxJson } from 'nx/src/project-graph/file-utils';
import { readNxJson } from 'nx/src/config/nx-json';
import { TargetProjectLocator } from '@nx/js/src/internal';
import { readProjectFileMapCache } from 'nx/src/project-graph/nx-deps-cache';

View File

@ -1,5 +1,5 @@
import { join } from 'path';
import { readNxJson } from 'nx/src/project-graph/file-utils';
import { readNxJson } from 'nx/src/config/nx-json';
import {
getTargetInputs,
filterUsingGlobPatterns,

View File

@ -10,10 +10,8 @@ import {
mapTargetDefaultsToDependencies,
} from '../../tasks-runner/create-task-graph';
import { NxJsonConfiguration } from '../../config/nx-json';
import { Workspaces } from '../../config/workspaces';
import { InProcessTaskHasher } from '../../hasher/task-hasher';
import { hashTask } from '../../hasher/hash-task';
import { workspaceRoot } from '../../utils/workspace-root';
import { getPackageManagerCommand } from '../../utils/package-manager';
import { fileHasher } from '../../hasher/file-hasher';
import { printAffectedDeprecationMessage } from './command-object';
@ -63,7 +61,6 @@ async function createTasks(
nxJson: NxJsonConfiguration,
overrides: yargs.Arguments
) {
const workspaces = new Workspaces(workspaceRoot);
const defaultDependencyConfigs = mapTargetDefaultsToDependencies(
nxJson.targetDefaults
);
@ -88,7 +85,7 @@ async function createTasks(
const tasks = Object.values(taskGraph.tasks);
await Promise.all(
tasks.map((t) => hashTask(workspaces, hasher, projectGraph, {} as any, t))
tasks.map((t) => hashTask(hasher, projectGraph, {} as any, t))
);
return tasks.map((task) => ({

View File

@ -1,8 +1,6 @@
import { filterAffected } from '../../project-graph/affected/affected-project-graph';
import {
calculateFileChanges,
readNxJson,
} from '../../project-graph/file-utils';
import { calculateFileChanges } from '../../project-graph/file-utils';
import { readNxJson } from '../../config/nx-json';
import {
NxArgs,
parseFiles,

View File

@ -2,10 +2,7 @@ import { Workspaces } from './workspaces';
import { workspaceRoot } from '../utils/workspace-root';
import { NxJsonConfiguration } from './nx-json';
import { ProjectsConfigurations } from './workspace-json-project-json';
export function readNxJson(): NxJsonConfiguration {
return new Workspaces(workspaceRoot).readNxJson();
}
import { readNxJson } from './nx-json';
// TODO(vsavkin): Remove after Nx 16 is out
/**
@ -26,3 +23,5 @@ export function workspaceLayout(): { appsDir: string; libsDir: string } {
libsDir: nxJson.workspaceLayout?.libsDir ?? 'libs',
};
}
export { readNxJson } from './nx-json';

View File

@ -1,3 +1,8 @@
import { dirname, join } from 'path';
import { existsSync } from 'fs';
import { readJsonFile } from '../utils/fileutils';
import { workspaceRoot } from '../utils/workspace-root';
import { PackageManager } from '../utils/package-manager';
import {
InputDefinition,
@ -152,3 +157,28 @@ export interface NxJsonConfiguration<T = '*' | string[]> {
*/
installation?: NxInstallationConfiguration;
}
export function readNxJson(root: string = workspaceRoot): NxJsonConfiguration {
const nxJson = join(root, 'nx.json');
if (existsSync(nxJson)) {
const nxJsonConfiguration = readJsonFile<NxJsonConfiguration>(nxJson);
if (nxJsonConfiguration.extends) {
const extendedNxJsonPath = require.resolve(nxJsonConfiguration.extends, {
paths: [dirname(nxJson)],
});
const baseNxJson = readJsonFile<NxJsonConfiguration>(extendedNxJsonPath);
return {
...baseNxJson,
...nxJsonConfiguration,
};
} else {
return nxJsonConfiguration;
}
} else {
try {
return readJsonFile(join(__dirname, '..', '..', 'presets', 'core.json'));
} catch (e) {
return {};
}
}
}

View File

@ -27,6 +27,7 @@ import {
findProjectForPath,
normalizeProjectRoot,
} from '../project-graph/utils/find-project-for-path';
import { readNxJson } from './nx-json';
export class Workspaces {
private cachedProjectsConfig: ProjectsConfigurations;
@ -79,7 +80,7 @@ export class Workspaces {
) {
return this.cachedProjectsConfig;
}
const nxJson = this.readNxJson();
const nxJson = readNxJson(this.root);
let projectsConfigurations = buildProjectsConfigurationsFromProjectPaths(
nxJson,
globForProjectFiles(
@ -123,7 +124,7 @@ export class Workspaces {
_ignorePluginInference?: boolean;
_includeProjectsFromAngularJson?: boolean;
}): ProjectsConfigurations & NxJsonConfiguration {
const nxJson = this.readNxJson();
const nxJson = readNxJson(this.root);
return { ...this.readProjectsConfigurations(opts), ...nxJson };
}
@ -158,37 +159,6 @@ export class Workspaces {
const nxJson = path.join(this.root, 'nx.json');
return existsSync(nxJson);
}
readNxJson(): NxJsonConfiguration {
const nxJson = path.join(this.root, 'nx.json');
if (existsSync(nxJson)) {
const nxJsonConfiguration = readJsonFile<NxJsonConfiguration>(nxJson);
if (nxJsonConfiguration.extends) {
const extendedNxJsonPath = require.resolve(
nxJsonConfiguration.extends,
{
paths: [dirname(nxJson)],
}
);
const baseNxJson =
readJsonFile<NxJsonConfiguration>(extendedNxJsonPath);
return {
...baseNxJson,
...nxJsonConfiguration,
};
} else {
return nxJsonConfiguration;
}
} else {
try {
return readJsonFile(
join(__dirname, '..', '..', 'presets', 'core.json')
);
} catch (e) {
return {};
}
}
}
}
function findMatchingProjectInCwd(

View File

@ -23,6 +23,7 @@ import {
retrieveProjectConfigurations,
} from '../../project-graph/utils/retrieve-workspace-files';
import { ProjectConfiguration } from '../../config/workspace-json-project-json';
import { readNxJson } from '../../config/nx-json';
let cachedSerializedProjectGraphPromise: Promise<{
error: Error | null;
@ -170,7 +171,7 @@ async function processCollectedUpdatedAndDeletedFiles() {
);
fileHasher.incrementalUpdate(updatedFiles, deletedFiles);
let nxJson = new Workspaces(workspaceRoot).readNxJson();
let nxJson = readNxJson(workspaceRoot);
const projectConfigurations = await retrieveProjectConfigurations(
workspaceRoot,

View File

@ -42,11 +42,10 @@ export type {
*/
export { Workspaces } from './config/workspaces';
// TODO (v16): Change this to export from './config/configuration'
export {
readAllWorkspaceConfiguration,
workspaceLayout,
} from './project-graph/file-utils';
} from './config/configuration';
export type { NxPlugin, ProjectTargetConfigurator } from './utils/nx-plugin';

View File

@ -5,3 +5,4 @@
*/
export { createTempNpmDirectory } from './utils/package-manager';
export { getExecutorInformation } from './command-line/run/executor-utils';
export { readNxJson as readNxJsonFromDisk } from './config/nx-json';

View File

@ -4,6 +4,7 @@
import type { Observable } from 'rxjs';
import { Workspaces } from '../../config/workspaces';
import { readNxJson } from '../../config/nx-json';
import { Executor, ExecutorContext } from '../../config/misc-interfaces';
/**
@ -17,7 +18,7 @@ export function convertNxExecutor(executor: Executor) {
const projectsConfigurations = workspaces.readProjectsConfigurations();
const promise = async () => {
const nxJsonConfiguration = workspaces.readNxJson();
const nxJsonConfiguration = readNxJson(builderContext.workspaceRoot);
const context: ExecutorContext = {
root: builderContext.workspaceRoot,
projectName: builderContext.target.project,

View File

@ -4,7 +4,7 @@ import type { NxJsonConfiguration } from '../../config/nx-json';
import type { Tree } from '../tree';
import { readJson, updateJson } from './json';
import { readNxJson as readNxJsonFromDisk } from '../../project-graph/file-utils';
import { readNxJson as readNxJsonFromDisk } from '../../config/nx-json';
/**
* @deprecated You must pass a {@link Tree}

View File

@ -3,11 +3,10 @@ import { getCustomHasher } from '../tasks-runner/utils';
import { readProjectsConfigurationFromProjectGraph } from '../project-graph/project-graph';
import { getInputs, TaskHasher } from './task-hasher';
import { ProjectGraph } from '../config/project-graph';
import { Workspaces } from '../config/workspaces';
import { NxJsonConfiguration } from '../config/nx-json';
import { readNxJson } from '../config/nx-json';
export async function hashTasksThatDoNotDependOnOutputsOfOtherTasks(
workspaces: Workspaces,
hasher: TaskHasher,
projectGraph: ProjectGraph,
taskGraph: TaskGraph,
@ -16,12 +15,7 @@ export async function hashTasksThatDoNotDependOnOutputsOfOtherTasks(
const tasks = Object.values(taskGraph.tasks);
const tasksWithHashers = await Promise.all(
tasks.map(async (task) => {
const customHasher = await getCustomHasher(
task,
workspaces,
workspaces.readNxJson(),
projectGraph
);
const customHasher = await getCustomHasher(task, projectGraph);
return { task, customHasher };
})
);
@ -48,18 +42,12 @@ export async function hashTasksThatDoNotDependOnOutputsOfOtherTasks(
}
export async function hashTask(
workspaces: Workspaces,
hasher: TaskHasher,
projectGraph: ProjectGraph,
taskGraph: TaskGraph,
task: Task
) {
const customHasher = await getCustomHasher(
task,
workspaces,
workspaces.readNxJson(),
projectGraph
);
const customHasher = await getCustomHasher(task, projectGraph);
const projectsConfigurations =
readProjectsConfigurationFromProjectGraph(projectGraph);
const { value, details } = await (customHasher
@ -69,7 +57,7 @@ export async function hashTask(
taskGraph,
workspaceConfig: projectsConfigurations, // to make the change non-breaking. Remove after v18
projectsConfigurations,
nxJsonConfiguration: workspaces.readNxJson(),
nxJsonConfiguration: readNxJson(),
} as any)
: hasher.hashTask(task));
task.hash = value;

View File

@ -9,7 +9,6 @@ import type { NxArgs } from '../utils/command-line-utils';
import { workspaceRoot } from '../utils/workspace-root';
import { readJsonFile } from '../utils/fileutils';
import { jsonDiff } from '../utils/json-diff';
import ignore from 'ignore';
import {
readCachedProjectGraph,
readProjectsConfigurationFromProjectGraph,
@ -155,8 +154,7 @@ export function readPackageJson(): any {
}
// Original Exports
export { FileData };
// TODO(v16): Remove these exports
// TODO(17): Remove these exports
export {
readNxJson,
readAllWorkspaceConfiguration,

View File

@ -16,7 +16,7 @@ import { fileExists } from '../utils/fileutils';
import { workspaceRoot } from '../utils/workspace-root';
import { performance } from 'perf_hooks';
import { retrieveWorkspaceFiles } from './utils/retrieve-workspace-files';
import { readNxJson } from './file-utils';
import { readNxJson } from '../config/nx-json';
/**
* Synchronously reads the latest cached copy of the workspace's ProjectGraph.

View File

@ -252,7 +252,6 @@ export async function invokeTasksRunner({
// a distributed fashion
performance.mark('hashing:start');
await hashTasksThatDoNotDependOnOutputsOfOtherTasks(
new Workspaces(workspaceRoot),
hasher,
projectGraph,
taskGraph,

View File

@ -1,9 +1,7 @@
import { defaultMaxListeners } from 'events';
import { performance } from 'perf_hooks';
import { Workspaces } from '../config/workspaces';
import { TaskHasher } from '../hasher/task-hasher';
import { ForkedProcessTaskRunner } from './forked-process-task-runner';
import { workspaceRoot } from '../utils/workspace-root';
import { Cache } from './cache';
import { DefaultTasksRunnerOptions } from './default-tasks-runner';
import { TaskStatus } from './tasks-runner';
@ -23,16 +21,12 @@ import { DaemonClient } from '../daemon/client/client';
export class TaskOrchestrator {
private cache = new Cache(this.options);
private workspace = new Workspaces(workspaceRoot);
private forkedProcessTaskRunner = new ForkedProcessTaskRunner(this.options);
private readonly nxJson = this.workspace.readNxJson();
private tasksSchedule = new TasksSchedule(
this.hasher,
this.nxJson,
this.projectGraph,
this.taskGraph,
this.workspace,
this.options
);
@ -456,12 +450,7 @@ export class TaskOrchestrator {
private async pipeOutputCapture(task: Task) {
try {
const { schema } = await getExecutorForTask(
task,
this.workspace,
this.projectGraph,
this.nxJson
);
const { schema } = await getExecutorForTask(task, this.projectGraph);
return (
schema.outputCapture === 'pipe' ||

View File

@ -3,6 +3,7 @@ import { Workspaces } from '../config/workspaces';
import { removeTasksFromTaskGraph } from './utils';
import { Task, TaskGraph } from '../config/task-graph';
import { DependencyType, ProjectGraph } from '../config/project-graph';
import * as nxJsonUtils from '../config/nx-json';
import * as executorUtils from '../command-line/run/executor-utils';
function createMockTask(id: string): Task {
@ -43,11 +44,7 @@ describe('TasksSchedule', () => {
},
roots: ['lib1:build', 'app2:build'],
};
const workspace: Partial<Workspaces> = {
readNxJson() {
return {};
},
};
jest.spyOn(nxJsonUtils, 'readNxJson').mockReturnValue({});
jest.spyOn(executorUtils, 'getExecutorInformation').mockReturnValue({
schema: {
version: 2,
@ -127,16 +124,9 @@ describe('TasksSchedule', () => {
endTask: jest.fn(),
scheduleTask: jest.fn(),
};
taskSchedule = new TasksSchedule(
hasher,
{},
projectGraph,
taskGraph,
workspace as Workspaces,
{
lifeCycle,
}
);
taskSchedule = new TasksSchedule(hasher, projectGraph, taskGraph, {
lifeCycle,
});
});
describe('Without Batch Mode', () => {
@ -269,11 +259,7 @@ describe('TasksSchedule', () => {
},
roots: ['app1:test', 'app2:test', 'lib1:test'],
};
const workspace: Partial<Workspaces> = {
readNxJson() {
return {};
},
};
jest.spyOn(nxJsonUtils, 'readNxJson').mockReturnValue({});
jest.spyOn(executorUtils, 'getExecutorInformation').mockReturnValue({
schema: {
version: 2,
@ -353,16 +339,9 @@ describe('TasksSchedule', () => {
endTask: jest.fn(),
scheduleTask: jest.fn(),
};
taskSchedule = new TasksSchedule(
hasher,
{},
projectGraph,
taskGraph,
workspace as Workspaces,
{
lifeCycle,
}
);
taskSchedule = new TasksSchedule(hasher, projectGraph, taskGraph, {
lifeCycle,
});
});
describe('Without Batch Mode', () => {

View File

@ -31,10 +31,8 @@ export class TasksSchedule {
constructor(
private readonly hasher: TaskHasher,
private readonly nxJson: NxJsonConfiguration,
private readonly projectGraph: ProjectGraph,
private readonly taskGraph: TaskGraph,
private readonly workspaces: Workspaces,
private readonly options: DefaultTasksRunnerOptions
) {}
@ -92,13 +90,7 @@ export class TasksSchedule {
const task = this.taskGraph.tasks[taskId];
if (!task.hash) {
await hashTask(
this.workspaces,
this.hasher,
this.projectGraph,
this.taskGraph,
task
);
await hashTask(this.hasher, this.projectGraph, this.taskGraph, task);
}
this.notScheduledTaskGraph = removeTasksFromTaskGraph(
@ -170,9 +162,7 @@ export class TasksSchedule {
const { batchImplementationFactory } = await getExecutorForTask(
task,
this.workspaces,
this.projectGraph,
this.nxJson
this.projectGraph
);
const executorName = await getExecutorNameForTask(task, this.projectGraph);
if (rootExecutorName !== executorName) {

View File

@ -17,6 +17,7 @@ import { isRelativePath } from '../utils/fileutils';
import { serializeOverridesIntoCommandLine } from '../utils/serialize-overrides-into-command-line';
import { splitByColons, splitTarget } from '../utils/split-target';
import { getExecutorInformation } from '../command-line/run/executor-utils';
import { CustomHasher } from '../config/misc-interfaces';
export function getCommandAsString(execCommand: string, task: Task) {
const args = getPrintableCommandArgsForTask(task);
@ -247,9 +248,7 @@ export async function getExecutorNameForTask(
export async function getExecutorForTask(
task: Task,
workspace: Workspaces,
projectGraph: ProjectGraph,
nxJson: NxJsonConfiguration
projectGraph: ProjectGraph
) {
const executor = await getExecutorNameForTask(task, projectGraph);
const [nodeModule, executorName] = executor.split(':');
@ -259,13 +258,9 @@ export async function getExecutorForTask(
export async function getCustomHasher(
task: Task,
workspace: Workspaces,
nxJson: NxJsonConfiguration,
projectGraph: ProjectGraph
) {
const factory = (
await getExecutorForTask(task, workspace, projectGraph, nxJson)
).hasherFactory;
): Promise<CustomHasher> | null {
const factory = (await getExecutorForTask(task, projectGraph)).hasherFactory;
return factory ? factory() : null;
}

View File

@ -2,7 +2,6 @@ import { Schema } from '../schema';
import {
getProjects,
readNxJson,
removeProjectConfiguration,
Tree,
updateNxJson,
updateProjectConfiguration,