diff --git a/packages/devkit/src/executors/read-target-options.ts b/packages/devkit/src/executors/read-target-options.ts index bf9758eb477ac0..818c277e632bce 100644 --- a/packages/devkit/src/executors/read-target-options.ts +++ b/packages/devkit/src/executors/read-target-options.ts @@ -4,8 +4,12 @@ import type { ExecutorContext } from 'nx/src/config/misc-interfaces'; import { combineOptionsForExecutor } from 'nx/src/utils/params'; import { requireNx } from '../../nx'; -const { Workspaces, getExecutorInformation, calculateDefaultProjectName } = - requireNx(); +const { + Workspaces, + getExecutorInformation, + calculateDefaultProjectName, + relativeCwd, +} = requireNx(); /** * Reads and combines options for a given target. @@ -21,6 +25,7 @@ export function readTargetOptions( ).projects[project]; const targetConfiguration = projectConfiguration.targets[target]; + // TODO(v18): remove Workspaces. const ws = new Workspaces(context.root); const [nodeModule, executorName] = targetConfiguration.executor.split(':'); const { schema } = getExecutorInformation @@ -48,6 +53,9 @@ export function readTargetOptions( targetConfiguration, schema, defaultProject, - ws.relativeCwd(context.cwd) + relativeCwd + ? relativeCwd(context.cwd, context.root) + : // TODO(v18): remove relativeCwd. This is to be backwards compatible with Nx 16.5 and below. + (ws as any).relativeCwd(context.cwd) ) as T; } diff --git a/packages/nx/src/adapter/angular-json.ts b/packages/nx/src/adapter/angular-json.ts index 19c88d01a40687..66c5350f6789a7 100644 --- a/packages/nx/src/adapter/angular-json.ts +++ b/packages/nx/src/adapter/angular-json.ts @@ -6,7 +6,6 @@ import { ProjectsConfigurations, } from '../config/workspace-json-project-json'; import { renamePropertyWithStableKeys } from '../config/workspaces'; -import { workspaceRoot } from '../utils/workspace-root'; export function shouldMergeAngularProjects( root: string, diff --git a/packages/nx/src/command-line/generate/generate.ts b/packages/nx/src/command-line/generate/generate.ts index 57b4c901f3830f..8737190fb6bb11 100644 --- a/packages/nx/src/command-line/generate/generate.ts +++ b/packages/nx/src/command-line/generate/generate.ts @@ -3,7 +3,6 @@ import { prompt } from 'enquirer'; import { readNxJson } from '../../config/configuration'; import { ProjectsConfigurations } from '../../config/workspace-json-project-json'; -import { Workspaces } from '../../config/workspaces'; import { FileChange, flushChanges, FsTree } from '../../generators/tree'; import { createProjectGraphAsync, @@ -20,6 +19,7 @@ import { getLocalWorkspacePlugins } from '../../utils/plugins/local-plugins'; import { printHelp } from '../../utils/print-help'; import { workspaceRoot } from '../../utils/workspace-root'; import { NxJsonConfiguration } from '../../config/nx-json'; +import { relativeCwd } from '../../config/relative-cwd'; import { calculateDefaultProjectName } from '../../config/calculate-default-project-name'; import { findInstalledPlugins } from '../../utils/plugins/installed-plugins'; import type { Arguments } from 'yargs'; @@ -51,7 +51,6 @@ export function printChanges(fileChanges: FileChange[]) { async function promptForCollection( generatorName: string, - ws: Workspaces, interactive: boolean, projectsConfiguration: ProjectsConfigurations ): Promise { @@ -202,7 +201,6 @@ function parseGeneratorString(value: string): { async function convertToGenerateOptions( generatorOptions: { [p: string]: any }, - ws: Workspaces, defaultCollectionName: string, mode: 'generate' | 'new', projectsConfiguration?: ProjectsConfigurations @@ -221,7 +219,6 @@ async function convertToGenerateOptions( } else if (!defaultCollectionName) { const generatorString = await promptForCollection( generatorDescriptor, - ws, interactive, projectsConfiguration ); @@ -302,7 +299,6 @@ export async function generate(cwd: string, args: { [k: string]: any }) { } const verbose = process.env.NX_VERBOSE_LOGGING === 'true'; - const ws = new Workspaces(workspaceRoot); const nxJsonConfiguration = readNxJson(); const projectGraph = await createProjectGraphAsync({ exitOnError: true }); const projectsConfigurations = @@ -311,7 +307,6 @@ export async function generate(cwd: string, args: { [k: string]: any }) { return handleErrors(verbose, async () => { const opts = await convertToGenerateOptions( args, - ws, readDefaultCollection(nxJsonConfiguration), 'generate', projectsConfigurations @@ -366,7 +361,7 @@ export async function generate(cwd: string, args: { [k: string]: any }) { projectsConfigurations, nxJsonConfiguration ), - ws.relativeCwd(cwd), + relativeCwd(cwd, workspaceRoot), verbose ); diff --git a/packages/nx/src/command-line/run/run.ts b/packages/nx/src/command-line/run/run.ts index e6fd53a211b5b5..3acc4370da60c5 100644 --- a/packages/nx/src/command-line/run/run.ts +++ b/packages/nx/src/command-line/run/run.ts @@ -4,7 +4,6 @@ import { Schema, } from '../../utils/params'; import { printHelp } from '../../utils/print-help'; -import { Workspaces } from '../../config/workspaces'; import { NxJsonConfiguration } from '../../config/nx-json'; import { readJsonFile } from '../../utils/fileutils'; import { buildTargetFromScript, PackageJson } from '../../utils/package-json'; @@ -28,6 +27,7 @@ import { } from '../../project-graph/project-graph'; import { ProjectGraph } from '../../config/project-graph'; import { readNxJson } from '../../config/configuration'; +import { relativeCwd } from '../../config/relative-cwd'; import { getLastValueFromAsyncIterableIterator, isAsyncIterator, @@ -111,7 +111,6 @@ function createImplicitTargetConfig( } async function parseExecutorAndTarget( - ws: Workspaces, { project, target, configuration }: Target, root: string, projectsConfigurations: ProjectsConfigurations, @@ -147,9 +146,7 @@ async function printTargetRunHelpInternal( projectsConfigurations: ProjectsConfigurations, nxJsonConfiguration: NxJsonConfiguration ) { - const ws = new Workspaces(root); const { executor, nodeModule, schema } = await parseExecutorAndTarget( - ws, { project, target, configuration }, root, projectsConfigurations, @@ -176,10 +173,8 @@ async function runExecutorInternal( ): Promise> { validateProject(projectsConfigurations, project); - const ws = new Workspaces(root); const { executor, implementationFactory, nodeModule, schema, targetConfig } = await parseExecutorAndTarget( - ws, { project, target, configuration }, root, projectsConfigurations, @@ -193,7 +188,7 @@ async function runExecutorInternal( targetConfig, schema, project, - ws.relativeCwd(cwd), + relativeCwd(cwd, root), isVerbose ); diff --git a/packages/nx/src/config/relative-cwd.ts b/packages/nx/src/config/relative-cwd.ts new file mode 100644 index 00000000000000..a5ecd6d743335c --- /dev/null +++ b/packages/nx/src/config/relative-cwd.ts @@ -0,0 +1,5 @@ +import { relative } from 'path'; + +export function relativeCwd(cwd: string, root: string): string | null { + return relative(root, cwd).replace(/\\/g, '/') || null; +} diff --git a/packages/nx/src/devkit-internals.ts b/packages/nx/src/devkit-internals.ts index 0f6bf3e705cae2..be27e5e767d87d 100644 --- a/packages/nx/src/devkit-internals.ts +++ b/packages/nx/src/devkit-internals.ts @@ -7,3 +7,4 @@ export { createTempNpmDirectory } from './utils/package-manager'; export { getExecutorInformation } from './command-line/run/executor-utils'; export { readNxJson as readNxJsonFromDisk } from './config/nx-json'; export { calculateDefaultProjectName } from './config/calculate-default-project-name'; +export { relativeCwd } from './config/relative-cwd';