Skip to content

Commit 6cc978a

Browse files
AgentEnderclaude[bot]
authored andcommitted
fix(devkit): include UPDATE changes in findCreatedProjectFiles for generator callbacks (#31429)
## Current Behavior getProjects(Tree) from project-configuration.ts does not return projects created in generators when called from generator callbacks. This happens because findCreatedProjectFiles only looks for changes with type === 'CREATE', but during callbacks, the tree has already been flushed to disk, so newly created project files are marked as 'UPDATE' instead of 'CREATE'. ## Expected Behavior getProjects(Tree) should return all projects, including those created during the current generator run, even when called from generator callbacks. ## Changes Made - Modified findCreatedProjectFiles() to include both CREATE and UPDATE changes - Added deduplication using Set to prevent duplicate project files - Added comprehensive test coverage for the callback scenario ## Related Issue(s) Fixes #29852 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: AgentEnder <[email protected]> (cherry picked from commit d298de5)
1 parent 3cb8217 commit 6cc978a

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

packages/nx/src/generators/utils/project-configuration.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,38 @@ describe('project configuration', () => {
173173
});
174174
});
175175

176+
it('should find projects created during generator run when called from callback', () => {
177+
// Simulate what happens during a generator callback:
178+
// 1. A project is created during generator execution
179+
addProjectConfiguration(tree, 'test-proj', {
180+
root: 'libs/test-proj',
181+
});
182+
183+
// Verify the project is found before callback
184+
let projects = getProjects(tree);
185+
expect(projects.size).toEqual(1);
186+
expect(projects.has('test-proj')).toBeTruthy();
187+
188+
// 2. Simulate changes being flushed to disk by modifying the tree
189+
// to mark the file as UPDATE instead of CREATE
190+
const projectJsonPath = 'libs/test-proj/project.json';
191+
const projectJsonContent = tree.read(projectJsonPath, 'utf-8');
192+
193+
// Clear the tree and write the file again to simulate it being flushed
194+
// This creates a scenario similar to what happens in callbacks
195+
tree.write(projectJsonPath, projectJsonContent);
196+
197+
// 3. getProjects should still find the project even when it's marked as UPDATE
198+
projects = getProjects(tree);
199+
expect(projects.size).toEqual(1);
200+
expect(projects.has('test-proj')).toBeTruthy();
201+
expect(projects.get('test-proj')).toEqual({
202+
$schema: '../../node_modules/nx/schemas/project-schema.json',
203+
name: 'test-proj',
204+
root: 'libs/test-proj',
205+
});
206+
});
207+
176208
describe('without nx.json', () => {
177209
beforeEach(() => tree.delete('nx.json'));
178210

packages/nx/src/generators/utils/project-configuration.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ function readAndCombineAllProjectConfigurations(tree: Tree): {
268268
const globbedFiles = globWithWorkspaceContextSync(tree.root, patterns);
269269
const createdFiles = findCreatedProjectFiles(tree, patterns);
270270
const deletedFiles = findDeletedProjectFiles(tree, patterns);
271-
const projectFiles = [...globbedFiles, ...createdFiles].filter(
271+
// Ensure we don't duplicate files that are both globbed and in tree changes
272+
const allProjectFiles = new Set([...globbedFiles, ...createdFiles]);
273+
const projectFiles = Array.from(allProjectFiles).filter(
272274
(r) => deletedFiles.indexOf(r) === -1
273275
);
274276

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

328330
for (const change of tree.listChanges()) {
329-
if (change.type === 'CREATE') {
331+
// Include both CREATE and UPDATE changes to handle project files
332+
// created during generator callbacks (which are marked as UPDATE
333+
// since the tree has already been flushed to disk)
334+
if (change.type === 'CREATE' || change.type === 'UPDATE') {
330335
const fileName = basename(change.path);
331336
if (
332337
globPatterns.some((pattern) =>

0 commit comments

Comments
 (0)