Skip to content

Commit 2cf6703

Browse files
committed
fix: normalize path separators in toCleanPath to fix missing tsconfig.json on Windows (#319)
1 parent e187a5a commit 2cf6703

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/create': patch
3+
---
4+
5+
fix: normalize path separators in toCleanPath to fix missing tsconfig.json on Windows

packages/create/src/file-helpers.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ export function getBinaryFile(content: string): string | null {
3737
}
3838

3939
/**
40-
* Convert an absolute path to a clean relative path by removing a base directory.
4140
* Returns a path without leading ./ or / prefix.
4241
*/
4342
export function toCleanPath(absolutePath: string, baseDir: string): string {
44-
let cleanPath = absolutePath.replace(baseDir, '')
45-
// Handle both Unix (/) and Windows (\) path separators
46-
if (cleanPath.startsWith('/') || cleanPath.startsWith('\\')) {
43+
// Normalize both paths to use forward slashes for consistent comparison
44+
const normalizedPath = absolutePath.replace(/\\/g, '/')
45+
const normalizedBase = baseDir.replace(/\\/g, '/')
46+
let cleanPath = normalizedPath.replace(normalizedBase, '')
47+
// Handle leading path separator
48+
if (cleanPath.startsWith('/')) {
4749
cleanPath = cleanPath.slice(1)
4850
}
4951
return cleanPath

packages/create/tests/file-helper.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,24 @@ describe('toCleanPath', () => {
2626
it('should strip Windows-style leading backslash', () => {
2727
expect(
2828
toCleanPath('C:\\Projects\\my-app\\src\\file.ts', 'C:\\Projects\\my-app'),
29-
).toBe('src\\file.ts')
29+
).toBe('src/file.ts')
3030
})
3131

3232
it('should handle paths without leading separator', () => {
3333
expect(toCleanPath('/projects/my-app', '/projects/my-app')).toBe('')
3434
})
35+
36+
it('should handle mixed path separators (forward slash path, backslash base)', () => {
37+
expect(
38+
toCleanPath('C:/Projects/my-app/src/file.ts', 'C:\\Projects\\my-app'),
39+
).toBe('src/file.ts')
40+
})
41+
42+
it('should handle mixed path separators (backslash path, forward slash base)', () => {
43+
expect(
44+
toCleanPath('C:\\Projects\\my-app\\src\\file.ts', 'C:/Projects/my-app'),
45+
).toBe('src/file.ts')
46+
})
3547
})
3648

3749
describe('relativePath', () => {

0 commit comments

Comments
 (0)