From 3bbd80afba3bf1273872a5c61841750776b71333 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Tue, 27 Jun 2023 11:09:41 +0300 Subject: [PATCH] fix(js): properly identify buildable project --- .../src/builders/utilities/buildable-libs.ts | 1 - .../delegate-build/delegate-build.impl.ts | 1 - .../src/executors/package/package.impl.ts | 1 - packages/js/src/executors/node/node.impl.ts | 1 - .../batch/build-task-info-per-tsconfig-map.ts | 1 - .../js/src/utils/buildable-libs-utils.spec.ts | 159 ++++++++++++----- packages/js/src/utils/buildable-libs-utils.ts | 48 +++-- packages/js/src/utils/check-dependencies.ts | 1 - packages/next/plugins/with-nx.ts | 1 - .../next/src/executors/export/export.impl.ts | 1 - .../src/executors/rollup/rollup.impl.ts | 1 - packages/vite/src/utils/executor-utils.ts | 1 - .../executors/dev-server/dev-server.impl.ts | 1 - .../src/executors/webpack/webpack.impl.ts | 1 - .../utilities/buildable-libs-utils.spec.ts | 166 +++++++++++++++++- .../src/utilities/buildable-libs-utils.ts | 45 ++++- 16 files changed, 351 insertions(+), 79 deletions(-) diff --git a/packages/angular/src/builders/utilities/buildable-libs.ts b/packages/angular/src/builders/utilities/buildable-libs.ts index 76dea8f624adf2..6c53a4eb609189 100644 --- a/packages/angular/src/builders/utilities/buildable-libs.ts +++ b/packages/angular/src/builders/utilities/buildable-libs.ts @@ -16,7 +16,6 @@ export function createTmpTsConfigForBuildableLibs( options?.projectGraph ?? readCachedProjectGraph(), context.workspaceRoot, context.target.project, - options?.target ?? context.target.target, context.target.configuration ); dependencies = result.dependencies; diff --git a/packages/angular/src/executors/delegate-build/delegate-build.impl.ts b/packages/angular/src/executors/delegate-build/delegate-build.impl.ts index d877e3a40a8e35..96ca31c46bdfcc 100644 --- a/packages/angular/src/executors/delegate-build/delegate-build.impl.ts +++ b/packages/angular/src/executors/delegate-build/delegate-build.impl.ts @@ -15,7 +15,6 @@ export async function* delegateBuildExecutor( context.projectGraph, context.root, context.projectName, - context.targetName, context.configurationName ); diff --git a/packages/angular/src/executors/package/package.impl.ts b/packages/angular/src/executors/package/package.impl.ts index 04f2f18d70c50e..ca9592207ce397 100644 --- a/packages/angular/src/executors/package/package.impl.ts +++ b/packages/angular/src/executors/package/package.impl.ts @@ -76,7 +76,6 @@ export function createLibraryExecutor( context.projectGraph, context.root, context.projectName, - context.targetName, context.configurationName ); if ( diff --git a/packages/js/src/executors/node/node.impl.ts b/packages/js/src/executors/node/node.impl.ts index 2e274422f2f767..f3c82e8b394f67 100644 --- a/packages/js/src/executors/node/node.impl.ts +++ b/packages/js/src/executors/node/node.impl.ts @@ -273,7 +273,6 @@ function calculateResolveMappings( context.projectGraph, context.root, parsed.project, - parsed.target, parsed.configuration ); return dependencies.reduce((m, c) => { diff --git a/packages/js/src/executors/tsc/lib/batch/build-task-info-per-tsconfig-map.ts b/packages/js/src/executors/tsc/lib/batch/build-task-info-per-tsconfig-map.ts index 437fc9a90f6d78..b39fc55d0c3594 100644 --- a/packages/js/src/executors/tsc/lib/batch/build-task-info-per-tsconfig-map.ts +++ b/packages/js/src/executors/tsc/lib/batch/build-task-info-per-tsconfig-map.ts @@ -101,7 +101,6 @@ function createTaskInfo( context.projectGraph, context.root, context.taskGraph.tasks[taskName].target.project, - context.taskGraph.tasks[taskName].target.target, context.taskGraph.tasks[taskName].target.configuration ); diff --git a/packages/js/src/utils/buildable-libs-utils.spec.ts b/packages/js/src/utils/buildable-libs-utils.spec.ts index 07709f67437406..7a73b09576bef0 100644 --- a/packages/js/src/utils/buildable-libs-utils.spec.ts +++ b/packages/js/src/utils/buildable-libs-utils.spec.ts @@ -83,43 +83,55 @@ describe('calculateProjectDependencies', () => { }); }); - it('should include npm packages in dependency list and sort them correctly', async () => { + it('should identify buildable libraries correctly', () => { const graph: ProjectGraph = { nodes: { - example: { + somejslib1: { + name: 'somejslib1', type: 'lib', - name: 'example', - data: { - root: '/root/example', - }, - }, - }, - externalNodes: { - 'npm:some-lib': { - type: 'npm', - name: 'npm:some-lib', data: { - packageName: 'some-lib', - version: '0.0.0', + name: 'somejslib1', + sourceRoot: 'libs/somejslib1/src', + projectType: 'library', + targets: { + 'my-custom-build-target': { + executor: '@nx/js:tsc', + outputs: ['{options.outputPath}'], + options: { + outputPath: 'dist/libs/somejslib1', + main: 'libs/somejslib1/src/index.ts', + }, + }, + }, + tags: [], + root: 'libs/somejslib1', + implicitDependencies: [], }, }, - 'npm:formik': { - type: 'npm', - name: 'npm:formik', + somejslib2: { + name: 'somejslib2', + type: 'lib', data: { - packageName: 'formik', - version: '0.0.0', - }, - }, - 'npm:@prefixed-lib': { - type: 'npm', - name: 'npm:@prefixed-lib', - data: { - packageName: '@prefixed-lib', - version: '0.0.0', + name: 'somejslib2', + sourceRoot: 'libs/somejslib2/src', + projectType: 'library', + targets: { + 'my-other-build-target': { + executor: '@nx/js:swc', + outputs: ['{options.outputPath}'], + options: { + outputPath: 'dist/libs/somejslib2', + main: 'libs/somejslib2/src/index.ts', + }, + }, + }, + tags: [], + root: 'libs/somejslib2', + implicitDependencies: [], }, }, }, + externalNodes: {}, dependencies: { example: [ { @@ -127,36 +139,103 @@ describe('calculateProjectDependencies', () => { target: 'npm:some-lib', type: DependencyType.static, }, + ], + somejslib1: [ { - source: 'example', - target: 'npm:formik', - type: DependencyType.static, - }, - { - source: 'example', - target: 'npm:@prefixed-lib', + source: 'somejslib2', + target: 'somejslib2', type: DependencyType.static, }, ], }, }; - const results = await calculateProjectDependencies( + const results = calculateProjectDependencies( graph, 'root', - 'example', + 'somejslib1', 'build', undefined ); expect(results).toMatchObject({ target: { + name: 'somejslib1', type: 'lib', - name: 'example', + data: { + name: 'somejslib1', + sourceRoot: 'libs/somejslib1/src', + projectType: 'library', + targets: { + 'my-custom-build-target': { + executor: '@nx/js:tsc', + outputs: ['{options.outputPath}'], + options: { + outputPath: 'dist/libs/somejslib1', + main: 'libs/somejslib1/src/index.ts', + }, + }, + }, + tags: [], + root: 'libs/somejslib1', + implicitDependencies: [], + }, }, dependencies: [ - { name: '@prefixed-lib' }, - { name: 'formik' }, - { name: 'some-lib' }, + { + name: 'somejslib2', + outputs: ['dist/libs/somejslib2'], + node: { + name: 'somejslib2', + type: 'lib', + data: { + name: 'somejslib2', + sourceRoot: 'libs/somejslib2/src', + projectType: 'library', + targets: { + 'my-other-build-target': { + executor: '@nx/js:swc', + outputs: ['{options.outputPath}'], + options: { + outputPath: 'dist/libs/somejslib2', + main: 'libs/somejslib2/src/index.ts', + }, + }, + }, + tags: [], + root: 'libs/somejslib2', + implicitDependencies: [], + }, + }, + }, + ], + nonBuildableDependencies: [], + topLevelDependencies: [ + { + name: 'somejslib2', + outputs: ['dist/libs/somejslib2'], + node: { + name: 'somejslib2', + type: 'lib', + data: { + name: 'somejslib2', + sourceRoot: 'libs/somejslib2/src', + projectType: 'library', + targets: { + 'my-other-build-target': { + executor: '@nx/js:swc', + outputs: ['{options.outputPath}'], + options: { + outputPath: 'dist/libs/somejslib2', + main: 'libs/somejslib2/src/index.ts', + }, + }, + }, + tags: [], + root: 'libs/somejslib2', + implicitDependencies: [], + }, + }, + }, ], }); }); diff --git a/packages/js/src/utils/buildable-libs-utils.ts b/packages/js/src/utils/buildable-libs-utils.ts index 0801a959fd2fb6..bed226f88a494e 100644 --- a/packages/js/src/utils/buildable-libs-utils.ts +++ b/packages/js/src/utils/buildable-libs-utils.ts @@ -12,17 +12,41 @@ import type * as ts from 'typescript'; import { unlinkSync } from 'fs'; import { output } from 'nx/src/utils/output'; import { isNpmProject } from 'nx/src/project-graph/operators'; -import { ensureTypescript } from './typescript/ensure-typescript'; import { readTsConfigPaths } from './typescript/ts-config'; -let tsModule: typeof import('typescript'); - -function isBuildable(target: string, node: ProjectGraphProjectNode): boolean { - return ( - node.data.targets && - node.data.targets[target] && - node.data.targets[target].executor !== '' - ); +function isBuildable(node: ProjectGraphProjectNode): string { + let buildTargetName: string | undefined = undefined; + // TODO(katerina): Remove @nrwl/* for Nx 17 + const listOfBuildExecutors = [ + '@nx/js:swc', + '@nx/js:tsc', + '@nx/rollup:rollup', + '@nx/rspack:rspack', + '@nx/vite:build', + '@nx/esbuild:esbuild', + '@nx/webpack:webpack', + '@nx/angular:ng-packagr-lite', + '@nx/angular:package', + '@nx/next:build', + '@nrwl/js:swc', + '@nrwl/js:tsc', + '@nrwl/rollup:rollup', + '@nrwl/rspack:rspack', + '@nrwl/vite:build', + '@nrwl/esbuild:esbuild', + '@nrwl/webpack:webpack', + '@nrwl/angular:ng-packagr-lite', + '@nrwl/angular:package', + '@nrwl/next:build', + ]; + for (const targetName of Object.keys(node.data.targets ?? {})) { + const target = node.data.targets[targetName]; + if (listOfBuildExecutors.includes(target.executor)) { + buildTargetName = targetName; + break; + } + } + return buildTargetName; } export type DependentBuildableProjectNode = { @@ -35,7 +59,6 @@ export function calculateProjectDependencies( projGraph: ProjectGraph, root: string, projectName: string, - targetName: string, configurationName: string, shallow?: boolean ): { @@ -73,7 +96,8 @@ export function calculateProjectDependencies( let project: DependentBuildableProjectNode = null; const depNode = projGraph.nodes[dep] || projGraph.externalNodes[dep]; if (depNode.type === 'lib') { - if (isBuildable(targetName, depNode)) { + const dependencyTargetName = isBuildable(depNode); + if (dependencyTargetName) { const libPackageJsonPath = join( root, depNode.data.root, @@ -89,7 +113,7 @@ export function calculateProjectDependencies( overrides: {}, target: { project: projectName, - target: targetName, + target: dependencyTargetName, configuration: configurationName, }, }, diff --git a/packages/js/src/utils/check-dependencies.ts b/packages/js/src/utils/check-dependencies.ts index 6044ef41bf90af..2de1c0dbc4efd0 100644 --- a/packages/js/src/utils/check-dependencies.ts +++ b/packages/js/src/utils/check-dependencies.ts @@ -18,7 +18,6 @@ export function checkDependencies( context.projectGraph, context.root, context.projectName, - context.targetName, context.configurationName ); const projectRoot = target.data.root; diff --git a/packages/next/plugins/with-nx.ts b/packages/next/plugins/with-nx.ts index 615e5e6d63ac79..cfc9414e5a9d10 100644 --- a/packages/next/plugins/with-nx.ts +++ b/packages/next/plugins/with-nx.ts @@ -165,7 +165,6 @@ function withNx( graph, workspaceRoot, project, - targetName, configurationName ); dependencies = result.dependencies; diff --git a/packages/next/src/executors/export/export.impl.ts b/packages/next/src/executors/export/export.impl.ts index 27c0ad8b7e9c95..4eeaacbf6f3c45 100644 --- a/packages/next/src/executors/export/export.impl.ts +++ b/packages/next/src/executors/export/export.impl.ts @@ -47,7 +47,6 @@ export default async function exportExecutor( context.projectGraph, context.root, context.projectName, - 'build', // this should be generalized context.configurationName ); dependencies = result.dependencies; diff --git a/packages/rollup/src/executors/rollup/rollup.impl.ts b/packages/rollup/src/executors/rollup/rollup.impl.ts index 881dbcd593e3a3..b5532b03d9d60d 100644 --- a/packages/rollup/src/executors/rollup/rollup.impl.ts +++ b/packages/rollup/src/executors/rollup/rollup.impl.ts @@ -56,7 +56,6 @@ export async function* rollupExecutor( context.projectGraph, context.root, context.projectName, - context.targetName, context.configurationName, true ); diff --git a/packages/vite/src/utils/executor-utils.ts b/packages/vite/src/utils/executor-utils.ts index 470f1ff73d8038..c76c9e25119160 100644 --- a/packages/vite/src/utils/executor-utils.ts +++ b/packages/vite/src/utils/executor-utils.ts @@ -40,7 +40,6 @@ export function registerPaths( context.projectGraph, context.root, context.projectName, - context.targetName, context.configurationName ); const tmpTsConfig = createTmpTsConfig( diff --git a/packages/webpack/src/executors/dev-server/dev-server.impl.ts b/packages/webpack/src/executors/dev-server/dev-server.impl.ts index a53c0807dd85fb..44a990141f9f86 100644 --- a/packages/webpack/src/executors/dev-server/dev-server.impl.ts +++ b/packages/webpack/src/executors/dev-server/dev-server.impl.ts @@ -47,7 +47,6 @@ export async function* devServerExecutor( context.projectGraph, context.root, context.projectName, - 'build', // should be generalized context.configurationName ); buildOptions.tsConfig = createTmpTsConfig( diff --git a/packages/webpack/src/executors/webpack/webpack.impl.ts b/packages/webpack/src/executors/webpack/webpack.impl.ts index 73982730683a3e..3b699d7ef77686 100644 --- a/packages/webpack/src/executors/webpack/webpack.impl.ts +++ b/packages/webpack/src/executors/webpack/webpack.impl.ts @@ -125,7 +125,6 @@ export async function* webpackExecutor( context.projectGraph, context.root, context.projectName, - context.targetName, context.configurationName ); options.tsConfig = createTmpTsConfig( diff --git a/packages/workspace/src/utilities/buildable-libs-utils.spec.ts b/packages/workspace/src/utilities/buildable-libs-utils.spec.ts index 6a44a9449443b7..e18b9fe7c2307e 100644 --- a/packages/workspace/src/utilities/buildable-libs-utils.spec.ts +++ b/packages/workspace/src/utilities/buildable-libs-utils.spec.ts @@ -71,7 +71,6 @@ describe('calculateProjectDependencies', () => { graph, 'root', 'example', - 'build', undefined ); expect(results).toMatchObject({ @@ -145,7 +144,6 @@ describe('calculateProjectDependencies', () => { graph, 'root', 'example', - 'build', undefined ); expect(results).toMatchObject({ @@ -171,7 +169,7 @@ describe('calculateProjectDependencies', () => { root: '/root/example', targets: { build: { - executor: 'x', + executor: '@nx/js:tsc', }, }, }, @@ -183,7 +181,7 @@ describe('calculateProjectDependencies', () => { root: '/root/example2', targets: { build: { - executor: 'x', + executor: '@nx/js:tsc', }, }, }, @@ -241,7 +239,6 @@ describe('calculateProjectDependencies', () => { graph, 'root', 'example', - 'build', undefined ); expect(results).toMatchObject({ @@ -255,6 +252,163 @@ describe('calculateProjectDependencies', () => { ], }); }); + + it('should identify buildable libraries correctly', () => { + const graph: ProjectGraph = { + nodes: { + somejslib1: { + name: 'somejslib1', + type: 'lib', + data: { + name: 'somejslib1', + sourceRoot: 'libs/somejslib1/src', + projectType: 'library', + targets: { + 'my-custom-build-target': { + executor: '@nx/js:tsc', + outputs: ['{options.outputPath}'], + options: { + outputPath: 'dist/libs/somejslib1', + main: 'libs/somejslib1/src/index.ts', + }, + }, + }, + tags: [], + root: 'libs/somejslib1', + implicitDependencies: [], + }, + }, + somejslib2: { + name: 'somejslib2', + type: 'lib', + data: { + name: 'somejslib2', + sourceRoot: 'libs/somejslib2/src', + projectType: 'library', + targets: { + 'my-other-build-target': { + executor: '@nx/js:swc', + outputs: ['{options.outputPath}'], + options: { + outputPath: 'dist/libs/somejslib2', + main: 'libs/somejslib2/src/index.ts', + }, + }, + }, + tags: [], + root: 'libs/somejslib2', + implicitDependencies: [], + }, + }, + }, + externalNodes: {}, + dependencies: { + example: [ + { + source: 'example', + target: 'npm:some-lib', + type: DependencyType.static, + }, + ], + somejslib1: [ + { + source: 'somejslib2', + target: 'somejslib2', + type: DependencyType.static, + }, + ], + }, + }; + + const results = calculateProjectDependencies( + graph, + 'root', + 'somejslib1', + 'build', + undefined + ); + expect(results).toMatchObject({ + target: { + name: 'somejslib1', + type: 'lib', + data: { + name: 'somejslib1', + sourceRoot: 'libs/somejslib1/src', + projectType: 'library', + targets: { + 'my-custom-build-target': { + executor: '@nx/js:tsc', + outputs: ['{options.outputPath}'], + options: { + outputPath: 'dist/libs/somejslib1', + main: 'libs/somejslib1/src/index.ts', + }, + }, + }, + tags: [], + root: 'libs/somejslib1', + implicitDependencies: [], + }, + }, + dependencies: [ + { + name: 'somejslib2', + outputs: ['dist/libs/somejslib2'], + node: { + name: 'somejslib2', + type: 'lib', + data: { + name: 'somejslib2', + sourceRoot: 'libs/somejslib2/src', + projectType: 'library', + targets: { + 'my-other-build-target': { + executor: '@nx/js:swc', + outputs: ['{options.outputPath}'], + options: { + outputPath: 'dist/libs/somejslib2', + main: 'libs/somejslib2/src/index.ts', + }, + }, + }, + tags: [], + root: 'libs/somejslib2', + implicitDependencies: [], + }, + }, + }, + ], + nonBuildableDependencies: [], + topLevelDependencies: [ + { + name: 'somejslib2', + outputs: ['dist/libs/somejslib2'], + node: { + name: 'somejslib2', + type: 'lib', + data: { + name: 'somejslib2', + sourceRoot: 'libs/somejslib2/src', + projectType: 'library', + targets: { + 'my-other-build-target': { + executor: '@nx/js:swc', + outputs: ['{options.outputPath}'], + options: { + outputPath: 'dist/libs/somejslib2', + main: 'libs/somejslib2/src/index.ts', + }, + }, + }, + tags: [], + root: 'libs/somejslib2', + implicitDependencies: [], + }, + }, + }, + ], + }); + }); }); describe('missingDependencies', () => { @@ -282,7 +436,7 @@ describe('missingDependencies', () => { }; expect(() => - calculateProjectDependencies(graph, 'root', 'example', 'build', undefined) + calculateProjectDependencies(graph, 'root', 'example', undefined) ).toThrow(); }); }); diff --git a/packages/workspace/src/utilities/buildable-libs-utils.ts b/packages/workspace/src/utilities/buildable-libs-utils.ts index 17745dab7ff2fe..01f7003ec70a1d 100644 --- a/packages/workspace/src/utilities/buildable-libs-utils.ts +++ b/packages/workspace/src/utilities/buildable-libs-utils.ts @@ -16,12 +16,39 @@ import { ensureTypescript } from './typescript'; let tsModule: typeof import('typescript'); -function isBuildable(target: string, node: ProjectGraphProjectNode): boolean { - return ( - node.data.targets && - node.data.targets[target] && - node.data.targets[target].executor !== '' - ); +function isBuildable(node: ProjectGraphProjectNode): string { + let buildTargetName: string | undefined = undefined; + // TODO(katerina): Remove @nrwl/* for Nx 17 + const listOfBuildExecutors = [ + '@nx/js:swc', + '@nx/js:tsc', + '@nx/rollup:rollup', + '@nx/rspack:rspack', + '@nx/vite:build', + '@nx/esbuild:esbuild', + '@nx/webpack:webpack', + '@nx/angular:ng-packagr-lite', + '@nx/angular:package', + '@nx/next:build', + '@nrwl/js:swc', + '@nrwl/js:tsc', + '@nrwl/rollup:rollup', + '@nrwl/rspack:rspack', + '@nrwl/vite:build', + '@nrwl/esbuild:esbuild', + '@nrwl/webpack:webpack', + '@nrwl/angular:ng-packagr-lite', + '@nrwl/angular:package', + '@nrwl/next:build', + ]; + for (const targetName of Object.keys(node.data.targets ?? {})) { + const target = node.data.targets[targetName]; + if (listOfBuildExecutors.includes(target.executor)) { + buildTargetName = targetName; + break; + } + } + return buildTargetName; } /** @@ -40,7 +67,6 @@ export function calculateProjectDependencies( projGraph: ProjectGraph, root: string, projectName: string, - targetName: string, configurationName: string, shallow?: boolean ): { @@ -78,7 +104,8 @@ export function calculateProjectDependencies( let project: DependentBuildableProjectNode = null; const depNode = projGraph.nodes[dep] || projGraph.externalNodes[dep]; if (depNode.type === 'lib') { - if (isBuildable(targetName, depNode)) { + const dependencyTargetName = isBuildable(depNode); + if (dependencyTargetName) { const libPackageJsonPath = join( root, depNode.data.root, @@ -94,7 +121,7 @@ export function calculateProjectDependencies( overrides: {}, target: { project: projectName, - target: targetName, + target: dependencyTargetName, configuration: configurationName, }, },