Skip to content

Commit 7940300

Browse files
committed
fix: handle Windows backslash path separator in toCleanPath
1 parent 0225132 commit 7940300

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

.changeset/late-lemons-battle.md

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+
Fixed windows \\ delimiter stripping

packages/create/src/file-helpers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ export function getBinaryFile(content: string): string | null {
4242
*/
4343
export function toCleanPath(absolutePath: string, baseDir: string): string {
4444
let cleanPath = absolutePath.replace(baseDir, '')
45-
if (cleanPath.startsWith('/')) cleanPath = cleanPath.slice(1)
45+
// Handle both Unix (/) and Windows (\) path separators
46+
if (cleanPath.startsWith('/') || cleanPath.startsWith('\\')) {
47+
cleanPath = cleanPath.slice(1)
48+
}
4649
return cleanPath
4750
}
4851

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
isDirectory,
77
readFileHelper,
88
relativePath,
9+
toCleanPath,
910
} from '../src/file-helpers.js'
1011

1112
vi.mock('node:fs', () => fs)
@@ -15,6 +16,24 @@ beforeEach(() => {
1516
vol.reset()
1617
})
1718

19+
describe('toCleanPath', () => {
20+
it('should strip Unix-style leading slash', () => {
21+
expect(toCleanPath('/projects/my-app/src/file.ts', '/projects/my-app')).toBe(
22+
'src/file.ts',
23+
)
24+
})
25+
26+
it('should strip Windows-style leading backslash', () => {
27+
expect(
28+
toCleanPath('C:\\Projects\\my-app\\src\\file.ts', 'C:\\Projects\\my-app'),
29+
).toBe('src\\file.ts')
30+
})
31+
32+
it('should handle paths without leading separator', () => {
33+
expect(toCleanPath('/projects/my-app', '/projects/my-app')).toBe('')
34+
})
35+
})
36+
1837
describe('relativePath', () => {
1938
it('relative path with the same directory and strip extension', () => {
2039
expect(relativePath('src/utils.ts', 'src/index.ts', true)).toBe('./index')

0 commit comments

Comments
 (0)