diff --git a/packages/nx/src/generators/tree.spec.ts b/packages/nx/src/generators/tree.spec.ts index 18207753c6ba9a..6af5a2b062cf31 100644 --- a/packages/nx/src/generators/tree.spec.ts +++ b/packages/nx/src/generators/tree.spec.ts @@ -355,6 +355,16 @@ describe('tree', () => { ]); }); + it('should support nested dirs with same name as parent', () => { + tree.write('/parent-a/parent-a/parent-a-file.txt', 'parent content'); + expect(tree.children('/parent-a')).toEqual([ + 'parent-a/parent-a-file.txt', + ]); + expect(tree.children('/parent-a/parent-a')).toEqual([ + 'parent-a-file.txt', + ]); + }); + describe('at the root', () => { it('should return a list of children', () => { expect(tree.children('')).toEqual(['parent', 'root-file.txt']); diff --git a/packages/nx/src/generators/tree.ts b/packages/nx/src/generators/tree.ts index 6e79fcce28f5ec..36df191c7441b7 100644 --- a/packages/nx/src/generators/tree.ts +++ b/packages/nx/src/generators/tree.ts @@ -403,8 +403,12 @@ export class FsTree implements Tree { } Object.keys(this.recordedChanges).forEach((f) => { if (f.startsWith(`${path}/`)) { - const [_, file] = f.split(`${path}/`); - res[file.split('/')[0]] = true; + // Remove the current folder's path from the directory + const file = f.substring(path.length + 1); + // Split the path on segments, and take the first one + const basePath = file.split('/')[0]; + // Mark it as a child of the current directory + res[basePath] = true; } });