Skip to content

router-generator: sibling physical() subtrees in a virtual route config generate duplicate identifiers #8879

Description

@karlshea

Which project does this relate to?

Router

Describe the bug

When a virtual route config mounts two physical() directories under different paths, route files with the same path relative to their mounted directory get the same generated variable name. The generated routeTree.gen.ts then declares the same import binding and the same const twice, so the Vite build fails with "Identifier IndexRouteImport has already been declared".

Route ids and paths are generated correctly (/accounts/, /billing/); only the variable names collide.

Complete minimal reproducer

https://stackblitz.com/edit/github-eufsm66w?file=vite.config.js

Steps to Reproduce the Bug

  1. npm install
  2. npm build or npm run dev
[PARSE_ERROR] Identifier `IndexRouteImport` has already been declared
    ╭─[ src/routeTree.gen.ts:12:19 ]
    │
 12 │ import { Route as IndexRouteImport } from './features/accounts/routes/index'
    │                   ────────┬───────  
    │                           ╰───────── `IndexRouteImport` has already been declared here
 13 │ import { Route as IndexRouteImport } from './features/billing/routes/index'
    │                   ────────┬───────  
    │                           ╰───────── It can not be redeclared here
────╯

Generated src/routeTree.gen.ts:

import { Route as rootRouteImport } from './routes/__root'
import { Route as IndexRouteImport } from './features/accounts/routes/index'
import { Route as IndexRouteImport } from './features/billing/routes/index'

const IndexRoute = IndexRouteImport.update({
  id: '/accounts/',
  path: '/accounts/',
  getParentRoute: () => rootRouteImport,
} as any)
const IndexRoute = IndexRouteImport.update({
  id: '/billing/',
  path: '/billing/',
  getParentRoute: () => rootRouteImport,
} as any)

Expected behavior

Distinct names derived from the route path, as file-based routing produces for routes/accounts/index.tsx and routes/billing/index.tsx: AccountsIndexRouteImport / BillingIndexRouteImport.

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Node: v24.20.0
  • @tanstack/router-plugin: 1.168.48
  • @tanstack/router-generator: 1.167.46 (same code on main)
  • @tanstack/virtual-file-routes: 1.162.0
  • @tanstack/react-router: 1.170.46
  • vite: 8.3.5

Additional context

Cause

packages/router-generator/src/filesystem/virtual/getRouteNodes.ts, physical branch of getRouteNodesRecursive. For each file in the mounted directory:

const pathPrefix = cleanPath(`${parent?.routePath ?? ''}${routePathPrefix}`) 
// '/accounts' + '' = '/accounts'

const routePath = cleanPath(`${pathPrefix}${subtreeNode.routePath}`)
// '/accounts' + '/' = '/accounts/' (correct)

subtreeNode.variableName = routePathToVariable(
  `${routePathPrefix}/${removeExt(subtreeNode.filePath)}`,
)
// '' + '/index' = 'Index' (collides)

The route id is built from pathPrefix, which includes the parent route's path. The variable name is built from routePathPrefix, the prefix given to physical(), which does not. Both mounts use '', so both index files are named Index.

Fix: build the name from pathPrefix as well.

subtreeNode.variableName = routePathToVariable(
  `${pathPrefix}/${removeExt(subtreeNode.filePath)}`,
)

With that change the repro builds and generates AccountsIndexRouteImport and BillingIndexRouteImport.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions