Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/nx/src/generators/utils/project-configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,38 @@ describe('project configuration', () => {
});
});

it('should find projects created during generator run when called from callback', () => {
// Simulate what happens during a generator callback:
// 1. A project is created during generator execution
addProjectConfiguration(tree, 'test-proj', {
root: 'libs/test-proj',
});

// Verify the project is found before callback
let projects = getProjects(tree);
expect(projects.size).toEqual(1);
expect(projects.has('test-proj')).toBeTruthy();

// 2. Simulate changes being flushed to disk by modifying the tree
// to mark the file as UPDATE instead of CREATE
const projectJsonPath = 'libs/test-proj/project.json';
const projectJsonContent = tree.read(projectJsonPath, 'utf-8');

// Clear the tree and write the file again to simulate it being flushed
// This creates a scenario similar to what happens in callbacks
tree.write(projectJsonPath, projectJsonContent);

// 3. getProjects should still find the project even when it's marked as UPDATE
projects = getProjects(tree);
expect(projects.size).toEqual(1);
expect(projects.has('test-proj')).toBeTruthy();
expect(projects.get('test-proj')).toEqual({
$schema: '../../node_modules/nx/schemas/project-schema.json',
name: 'test-proj',
root: 'libs/test-proj',
});
});

describe('without nx.json', () => {
beforeEach(() => tree.delete('nx.json'));

Expand Down
9 changes: 7 additions & 2 deletions packages/nx/src/generators/utils/project-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ function readAndCombineAllProjectConfigurations(tree: Tree): {
const globbedFiles = globWithWorkspaceContextSync(tree.root, patterns);
const createdFiles = findCreatedProjectFiles(tree, patterns);
const deletedFiles = findDeletedProjectFiles(tree, patterns);
const projectFiles = [...globbedFiles, ...createdFiles].filter(
// Ensure we don't duplicate files that are both globbed and in tree changes
const allProjectFiles = new Set([...globbedFiles, ...createdFiles]);
const projectFiles = Array.from(allProjectFiles).filter(
(r) => deletedFiles.indexOf(r) === -1
);

Expand Down Expand Up @@ -326,7 +328,10 @@ function findCreatedProjectFiles(tree: Tree, globPatterns: string[]) {
const createdProjectFiles = [];

for (const change of tree.listChanges()) {
if (change.type === 'CREATE') {
// Include both CREATE and UPDATE changes to handle project files
// created during generator callbacks (which are marked as UPDATE
// since the tree has already been flushed to disk)
if (change.type === 'CREATE' || change.type === 'UPDATE') {
const fileName = basename(change.path);
if (
globPatterns.some((pattern) =>
Expand Down
Loading