Skip to content

Commit 012af22

Browse files
fix(router-generator): normalize virtual physical subtree paths
fixes #3219
1 parent 8e7f4c8 commit 012af22

12 files changed

Lines changed: 198 additions & 6 deletions

File tree

‎.changeset/five-badgers-listen.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/router-generator': patch
3+
---
4+
5+
Fix malformed generated paths when a `physical()` mount points at a subtree rooted by `__virtual.ts`, including nested virtual layouts that mount additional physical routes.

‎packages/router-generator/src/filesystem/physical/getRouteNodes.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,19 @@ export async function getRouteNodes(
121121
)
122122
allPhysicalDirectories.push(...physicalDirectories)
123123
virtualRouteNodes.forEach((node) => {
124-
const filePath = replaceBackslash(path.join(dir, node.filePath))
125-
const routePath = `/${dir}${node.routePath}`
124+
const normalizedDir = dir === './' ? '' : dir
125+
const filePath = replaceBackslash(
126+
path.join(normalizedDir, node.filePath),
127+
)
128+
const routePath = `/${normalizedDir}${node.routePath}`
126129

127130
node.variableName = routePathToVariable(
128-
`${dir}/${removeExt(node.filePath)}`,
131+
`${normalizedDir}/${removeExt(node.filePath)}`,
129132
)
130133
node.routePath = routePath
131134
// Keep originalRoutePath aligned with routePath for escape detection
132135
if (node.originalRoutePath) {
133-
node.originalRoutePath = `/${dir}${node.originalRoutePath}`
136+
node.originalRoutePath = `/${normalizedDir}${node.originalRoutePath}`
134137
}
135138
node.filePath = filePath
136139
// Virtual subtree nodes (from __virtual.ts) are embedded in a

‎packages/router-generator/src/filesystem/virtual/getRouteNodes.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path, { join, resolve } from 'node:path'
22
import {
3+
cleanPath,
34
determineInitialRoutePath,
45
removeExt,
56
removeLeadingSlash,
@@ -164,10 +165,14 @@ export async function getRouteNodesRecursive(
164165
subtreeNode.variableName = routePathToVariable(
165166
`${node.pathPrefix}/${removeExt(subtreeNode.filePath)}`,
166167
)
167-
subtreeNode.routePath = `${parent?.routePath ?? ''}${node.pathPrefix}${subtreeNode.routePath}`
168+
subtreeNode.routePath = cleanPath(
169+
`${parent?.routePath ?? ''}${node.pathPrefix}${subtreeNode.routePath}`,
170+
)
168171
// Keep originalRoutePath aligned with routePath for escape detection
169172
if (subtreeNode.originalRoutePath) {
170-
subtreeNode.originalRoutePath = `${parent?.routePath ?? ''}${node.pathPrefix}${subtreeNode.originalRoutePath}`
173+
subtreeNode.originalRoutePath = cleanPath(
174+
`${parent?.routePath ?? ''}${node.pathPrefix}${subtreeNode.originalRoutePath}`,
175+
)
171176
}
172177
subtreeNode.filePath = `${node.directory}/${subtreeNode.filePath}`
173178
})
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/* eslint-disable */
2+
3+
// @ts-nocheck
4+
5+
// noinspection JSUnusedGlobalSymbols
6+
7+
// This file was automatically generated by TanStack Router.
8+
// You should NOT make any changes in this file as it will be overwritten.
9+
// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified.
10+
11+
import { Route as rootRouteImport } from './routes/__root'
12+
import { Route as indexRouteImport } from './routes/index'
13+
import { Route as S1LayoutRouteImport } from './subtree/routes/_layout'
14+
import { Route as S1DotDotFooRoutesRouteRouteImport } from './subtree/foo/routes/route'
15+
import { Route as S1DotDotFooRoutesIndexRouteImport } from './subtree/foo/routes/index'
16+
17+
const indexRoute = indexRouteImport.update({
18+
id: '/',
19+
path: '/',
20+
getParentRoute: () => rootRouteImport,
21+
} as any)
22+
const S1LayoutRoute = S1LayoutRouteImport.update({
23+
id: '/s1/_layout',
24+
path: '/s1',
25+
getParentRoute: () => rootRouteImport,
26+
} as any)
27+
const S1DotDotFooRoutesRouteRoute = S1DotDotFooRoutesRouteRouteImport.update({
28+
id: '/foo',
29+
path: '/foo',
30+
getParentRoute: () => S1LayoutRoute,
31+
} as any)
32+
const S1DotDotFooRoutesIndexRoute = S1DotDotFooRoutesIndexRouteImport.update({
33+
id: '/',
34+
path: '/',
35+
getParentRoute: () => S1DotDotFooRoutesRouteRoute,
36+
} as any)
37+
38+
export interface FileRoutesByFullPath {
39+
'/': typeof indexRoute
40+
'/s1': typeof S1LayoutRouteWithChildren
41+
'/s1/foo': typeof S1DotDotFooRoutesRouteRouteWithChildren
42+
'/s1/foo/': typeof S1DotDotFooRoutesIndexRoute
43+
}
44+
export interface FileRoutesByTo {
45+
'/': typeof indexRoute
46+
'/s1': typeof S1LayoutRouteWithChildren
47+
'/s1/foo': typeof S1DotDotFooRoutesIndexRoute
48+
}
49+
export interface FileRoutesById {
50+
__root__: typeof rootRouteImport
51+
'/': typeof indexRoute
52+
'/s1/_layout': typeof S1LayoutRouteWithChildren
53+
'/s1/_layout/foo': typeof S1DotDotFooRoutesRouteRouteWithChildren
54+
'/s1/_layout/foo/': typeof S1DotDotFooRoutesIndexRoute
55+
}
56+
export interface FileRouteTypes {
57+
fileRoutesByFullPath: FileRoutesByFullPath
58+
fullPaths: '/' | '/s1' | '/s1/foo' | '/s1/foo/'
59+
fileRoutesByTo: FileRoutesByTo
60+
to: '/' | '/s1' | '/s1/foo'
61+
id: '__root__' | '/' | '/s1/_layout' | '/s1/_layout/foo' | '/s1/_layout/foo/'
62+
fileRoutesById: FileRoutesById
63+
}
64+
export interface RootRouteChildren {
65+
indexRoute: typeof indexRoute
66+
S1LayoutRoute: typeof S1LayoutRouteWithChildren
67+
}
68+
69+
declare module '@tanstack/react-router' {
70+
interface FileRoutesByPath {
71+
'/': {
72+
id: '/'
73+
path: '/'
74+
fullPath: '/'
75+
preLoaderRoute: typeof indexRouteImport
76+
parentRoute: typeof rootRouteImport
77+
}
78+
'/s1/_layout': {
79+
id: '/s1/_layout'
80+
path: '/s1'
81+
fullPath: '/s1'
82+
preLoaderRoute: typeof S1LayoutRouteImport
83+
parentRoute: typeof rootRouteImport
84+
}
85+
'/s1/_layout/foo': {
86+
id: '/s1/_layout/foo'
87+
path: '/foo'
88+
fullPath: '/s1/foo'
89+
preLoaderRoute: typeof S1DotDotFooRoutesRouteRouteImport
90+
parentRoute: typeof S1LayoutRoute
91+
}
92+
'/s1/_layout/foo/': {
93+
id: '/s1/_layout/foo/'
94+
path: '/'
95+
fullPath: '/s1/foo/'
96+
preLoaderRoute: typeof S1DotDotFooRoutesIndexRouteImport
97+
parentRoute: typeof S1DotDotFooRoutesRouteRoute
98+
}
99+
}
100+
}
101+
102+
interface S1DotDotFooRoutesRouteRouteChildren {
103+
S1DotDotFooRoutesIndexRoute: typeof S1DotDotFooRoutesIndexRoute
104+
}
105+
106+
const S1DotDotFooRoutesRouteRouteChildren: S1DotDotFooRoutesRouteRouteChildren =
107+
{
108+
S1DotDotFooRoutesIndexRoute: S1DotDotFooRoutesIndexRoute,
109+
}
110+
111+
const S1DotDotFooRoutesRouteRouteWithChildren =
112+
S1DotDotFooRoutesRouteRoute._addFileChildren(
113+
S1DotDotFooRoutesRouteRouteChildren,
114+
)
115+
116+
interface S1LayoutRouteChildren {
117+
S1DotDotFooRoutesRouteRoute: typeof S1DotDotFooRoutesRouteRouteWithChildren
118+
}
119+
120+
const S1LayoutRouteChildren: S1LayoutRouteChildren = {
121+
S1DotDotFooRoutesRouteRoute: S1DotDotFooRoutesRouteRouteWithChildren,
122+
}
123+
124+
const S1LayoutRouteWithChildren = S1LayoutRoute._addFileChildren(
125+
S1LayoutRouteChildren,
126+
)
127+
128+
const rootRouteChildren: RootRouteChildren = {
129+
indexRoute: indexRoute,
130+
S1LayoutRoute: S1LayoutRouteWithChildren,
131+
}
132+
export const routeTree = rootRouteImport
133+
._addFileChildren(rootRouteChildren)
134+
._addFileTypes<FileRouteTypes>()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { index, physical, rootRoute } from '@tanstack/virtual-file-routes'
2+
3+
export const routes = rootRoute('__root.tsx', [
4+
index('index.tsx'),
5+
physical('/s1', '../subtree/routes'),
6+
])
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createRootRoute, Outlet } from '@tanstack/react-router'
2+
3+
export const Route = createRootRoute({
4+
component: () => <Outlet />,
5+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
3+
export const Route = createFileRoute('/')({
4+
component: () => <div>Home</div>,
5+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
3+
export const Route = createFileRoute('/s1/.//_layout/foo/')({
4+
component: () => <div>Nested home</div>,
5+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Outlet, createFileRoute } from '@tanstack/react-router'
2+
3+
export const Route = createFileRoute('/s1/.//_layout/foo')({
4+
component: () => <Outlet />,
5+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {
2+
defineVirtualSubtreeConfig,
3+
layout,
4+
physical,
5+
} from '@tanstack/virtual-file-routes'
6+
7+
export default defineVirtualSubtreeConfig([
8+
layout('./_layout.tsx', [physical('/foo', '../foo/routes')]),
9+
])

0 commit comments

Comments
 (0)