Skip to content

Commit

Permalink
chore(core): move readNxJson out of Workspaces (#18127)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi authored Jul 21, 2023
1 parent e12e9bb commit 717a8dd
Show file tree
Hide file tree
Showing 26 changed files with 78 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/cypress/src/utils/ct-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?$/);
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/cypress/src/utils/find-target-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions packages/devkit/nx-reexports-pre16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions packages/devkit/src/utils/convert-nx-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/utils/project-graph-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/utils/find-npm-dependencies.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
5 changes: 1 addition & 4 deletions packages/nx/src/command-line/affected/print-affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -63,7 +61,6 @@ async function createTasks(
nxJson: NxJsonConfiguration,
overrides: yargs.Arguments
) {
const workspaces = new Workspaces(workspaceRoot);
const defaultDependencyConfigs = mapTargetDefaultsToDependencies(
nxJson.targetDefaults
);
Expand All @@ -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) => ({
Expand Down
6 changes: 2 additions & 4 deletions packages/nx/src/command-line/show/show.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
7 changes: 3 additions & 4 deletions packages/nx/src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
Expand All @@ -26,3 +23,5 @@ export function workspaceLayout(): { appsDir: string; libsDir: string } {
libsDir: nxJson.workspaceLayout?.libsDir ?? 'libs',
};
}

export { readNxJson } from './nx-json';
30 changes: 30 additions & 0 deletions packages/nx/src/config/nx-json.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 {};
}
}
}
36 changes: 3 additions & 33 deletions packages/nx/src/config/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,7 +80,7 @@ export class Workspaces {
) {
return this.cachedProjectsConfig;
}
const nxJson = this.readNxJson();
const nxJson = readNxJson(this.root);
let projectsConfigurations = buildProjectsConfigurationsFromProjectPaths(
nxJson,
globForProjectFiles(
Expand Down Expand Up @@ -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 };
}

Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions packages/nx/src/devkit-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/devkit-internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
3 changes: 2 additions & 1 deletion packages/nx/src/executors/utils/convert-nx-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/generators/utils/nx-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
20 changes: 4 additions & 16 deletions packages/nx/src/hasher/hash-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 };
})
);
Expand All @@ -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
Expand All @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions packages/nx/src/project-graph/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/project-graph/project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion packages/nx/src/tasks-runner/run-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ export async function invokeTasksRunner({
// a distributed fashion
performance.mark('hashing:start');
await hashTasksThatDoNotDependOnOutputsOfOtherTasks(
new Workspaces(workspaceRoot),
hasher,
projectGraph,
taskGraph,
Expand Down
Loading

1 comment on commit 717a8dd

@vercel
Copy link

@vercel vercel bot commented on 717a8dd Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.