diff --git a/packages/angular/src/generators/component/component.ts b/packages/angular/src/generators/component/component.ts index 3a41f3be37..b6ee1dc0de 100644 --- a/packages/angular/src/generators/component/component.ts +++ b/packages/angular/src/generators/component/component.ts @@ -12,7 +12,7 @@ import { normalizeOptions } from './lib/normalize-options'; import type { NormalizedSchema, Schema } from './schema'; export async function componentGenerator(tree: Tree, rawOptions: Schema) { - const options = normalizeOptions(tree, rawOptions); + const options = await normalizeOptions(tree, rawOptions); const { projectSourceRoot, ...schematicOptions } = options; checkPathUnderProjectRoot(tree, options); diff --git a/packages/angular/src/generators/component/lib/normalize-options.ts b/packages/angular/src/generators/component/lib/normalize-options.ts index 587e68b182..5aa0ced4cd 100644 --- a/packages/angular/src/generators/component/lib/normalize-options.ts +++ b/packages/angular/src/generators/component/lib/normalize-options.ts @@ -1,5 +1,6 @@ import type { Tree } from '@nrwl/devkit'; import { + createProjectGraphAsync, joinPathFragments, readCachedProjectGraph, readProjectConfiguration, @@ -11,15 +12,19 @@ import { findProjectForPath, } from 'nx/src/project-graph/utils/find-project-for-path'; -export function normalizeOptions( +async function findProjectFromOptions(options: Schema) { + const projectGraph = await createProjectGraphAsync(); + const projectRootMappings = createProjectRootMappings(projectGraph.nodes); + return findProjectForPath(options.path, projectRootMappings); +} + +export async function normalizeOptions( tree: Tree, options: Schema -): NormalizedSchema { - const projectGraph = readCachedProjectGraph(); - const projectRootMappings = createProjectRootMappings(projectGraph.nodes); +): Promise { const project = options.project ?? - findProjectForPath(options.path, projectRootMappings) ?? + (await findProjectFromOptions(options)) ?? readWorkspaceConfiguration(tree).defaultProject; const { projectType, root, sourceRoot } = readProjectConfiguration( tree, diff --git a/packages/angular/src/generators/library/library.spec.ts b/packages/angular/src/generators/library/library.spec.ts index e89ffc1bb7..ea0241d0cb 100644 --- a/packages/angular/src/generators/library/library.spec.ts +++ b/packages/angular/src/generators/library/library.spec.ts @@ -2,6 +2,7 @@ import { getProjects, NxJsonConfiguration, parseJson, + ProjectGraph, readJson, readProjectConfiguration, Tree, @@ -24,6 +25,14 @@ import libraryGenerator from './library'; import { Schema } from './schema'; import applicationGenerator from '../application/application'; +let projectGraph: ProjectGraph; +jest.mock('@nrwl/devkit', () => { + return { + ...jest.requireActual('@nrwl/devkit'), + createProjectGraphAsync: jest.fn().mockImplementation(() => projectGraph), + }; +}); + describe('lib', () => { let tree: Tree; @@ -1441,6 +1450,21 @@ describe('lib', () => { }); describe('--standalone', () => { + beforeEach(() => { + projectGraph = { + nodes: { + 'my-lib': { + name: 'my-lib', + type: 'lib', + data: { + root: 'libs/my-lib', + }, + }, + }, + dependencies: {}, + }; + }); + it('should generate a library with a standalone component as entry point', async () => { await runLibraryGeneratorWithOpts({ standalone: true });