Skip to content

Commit 8a7bb12

Browse files
fix(server): normalize file tree paths to POSIX separators (#691)
Backend used OS-native separators when building the file tree, so on Windows leaf labels showed the full path (`internal\Benchmark\plan.md`) because the frontend splits on `/`. Also fixes git status lookup which compared OS-native paths against git's POSIX-format paths. Closes #677 Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
1 parent ec6d253 commit 8a7bb12

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

‎src/server/routes/filetree.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { resolveGlobPatterns } from '../../utils/glob';
99
type FileTreeItem = { path: string, status: string, isDirectory?: boolean } | { [key: string]: FileTree };
1010
type FileTree = FileTreeItem[];
1111

12+
const toPosixPath = (p: string): string => p.split(path.sep).join('/');
13+
1214
export const fileTreeRouter = (context: ServerContext): Router => {
1315
const { directory } = context;
1416
const router = Router();
@@ -72,8 +74,9 @@ const buildFileTreeFromPatterns = (
7274
sorted.forEach(filePath => {
7375
const dirPath = path.dirname(filePath);
7476
const targetTree = dirPath === '.' ? root : getOrCreateDir(dirPath);
75-
const status = getGitStatus(filePath, gitStatus);
76-
targetTree.push({ path: filePath, status });
77+
const posixPath = toPosixPath(filePath);
78+
const status = getGitStatus(posixPath, gitStatus);
79+
targetTree.push({ path: posixPath, status });
7780
});
7881

7982
return root;
@@ -108,8 +111,9 @@ const getFileTree = async (
108111
tree.push({ [entry.name]: subTree });
109112
}
110113
} else if (entry.name.endsWith('.md') || entry.name.endsWith('.markdown')) {
111-
const status = getGitStatus(entryPath, gitStatus);
112-
tree.push({ path: entryPath, status });
114+
const posixPath = toPosixPath(entryPath);
115+
const status = getGitStatus(posixPath, gitStatus);
116+
tree.push({ path: posixPath, status });
113117
}
114118
}
115119
return tree;

‎test/unit/server/routes/filetree.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express from 'express';
22
import fs from 'fs';
3+
import path from 'path';
34
import request from 'supertest';
45
import { fileTreeRouter } from '../../../../src/server/routes/filetree';
56

@@ -114,4 +115,40 @@ describe('filetree.ts', () => {
114115
expect(fs.readdirSync).not.toHaveBeenCalled();
115116
});
116117
});
118+
119+
describe('path separator normalization (Windows compatibility)', () => {
120+
let app: express.Application;
121+
const mockDirectory = '/mock/base/dir';
122+
const originalSep = path.sep;
123+
124+
beforeEach(() => {
125+
// Simulate Windows path separator
126+
Object.defineProperty(path, 'sep', { value: '\\', configurable: true });
127+
app = express();
128+
app.use('/api/filetree', fileTreeRouter({
129+
directory: mockDirectory,
130+
filePatterns: ['internal\\Benchmark\\plan.md', 'docs\\guide.md'],
131+
}));
132+
});
133+
134+
afterEach(() => {
135+
Object.defineProperty(path, 'sep', { value: originalSep, configurable: true });
136+
});
137+
138+
it('should normalize backslash separators to forward slashes in file paths', async () => {
139+
const response = await request(app).get('/api/filetree');
140+
expect(response.statusCode).toBe(200);
141+
const flattenPaths = (
142+
tree: unknown[],
143+
): string[] => tree.flatMap(item => {
144+
if (item && typeof item === 'object' && 'path' in item) return [(item as { path: string }).path];
145+
const value = Object.values(item as object)[0] as unknown[];
146+
return flattenPaths(value);
147+
});
148+
const paths = flattenPaths(response.body.fileTree);
149+
paths.forEach(p => expect(p).not.toContain('\\'));
150+
expect(paths).toContain('internal/Benchmark/plan.md');
151+
expect(paths).toContain('docs/guide.md');
152+
});
153+
});
117154
});

0 commit comments

Comments
 (0)