From 2cdd153777c2b41bae885206bc8ca119cdfebd59 Mon Sep 17 00:00:00 2001 From: riteshvish02 Date: Wed, 30 Sep 2026 18:55:11 +0530 Subject: [PATCH] fix(web): set correct intermediate directory paths in buildFileTree When constructing the file tree from a flat list in buildFileTree, new intermediate directory nodes were assigned item.path (the path of the leaf item being processed) instead of their actual directory path. This caused intermediate directory nodes to have full file paths instead of their directory path (e.g. directory 'src' receiving path 'src/components/Button.tsx'), breaking folder expansion state tracking (openPaths.has(node.path)) and corrupting directory paths returned by getTree. Reconstruct the node path using parts.slice(0, i + 1).join('/') so both directories and leaves have the correct path. --- packages/web/src/features/git/utils.test.ts | 25 +++++++++++++++++++++ packages/web/src/features/git/utils.ts | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/web/src/features/git/utils.test.ts b/packages/web/src/features/git/utils.test.ts index e4aa5f8e2..4f86f0615 100644 --- a/packages/web/src/features/git/utils.test.ts +++ b/packages/web/src/features/git/utils.test.ts @@ -103,3 +103,28 @@ test('buildFileTree builds a sorted tree from a flat list', () => { }); }); +test('buildFileTree correctly sets intermediate directory paths', () => { + const flatList: { type: string, path: string }[] = [ + { type: 'blob', path: 'src/components/buttons/PrimaryButton.tsx' }, + { type: 'blob', path: 'src/index.ts' }, + ]; + + const tree = buildFileTree(flatList); + + const srcNode = tree.children.find((c) => c.name === 'src'); + expect(srcNode?.path).toBe('src'); + expect(srcNode?.type).toBe('tree'); + + const compNode = srcNode?.children.find((c) => c.name === 'components'); + expect(compNode?.path).toBe('src/components'); + expect(compNode?.type).toBe('tree'); + + const btnDir = compNode?.children.find((c) => c.name === 'buttons'); + expect(btnDir?.path).toBe('src/components/buttons'); + expect(btnDir?.type).toBe('tree'); + + const btnFile = btnDir?.children.find((c) => c.name === 'PrimaryButton.tsx'); + expect(btnFile?.path).toBe('src/components/buttons/PrimaryButton.tsx'); + expect(btnFile?.type).toBe('blob'); +}); + diff --git a/packages/web/src/features/git/utils.ts b/packages/web/src/features/git/utils.ts index f9b1a71e7..126281012 100644 --- a/packages/web/src/features/git/utils.ts +++ b/packages/web/src/features/git/utils.ts @@ -67,7 +67,7 @@ export const buildFileTree = (flatList: { type: string, path: string }[]): FileT if (!next) { next = { name: part, - path: item.path, + path: parts.slice(0, i + 1).join('/'), type: nodeType, children: [], };