router icon indicating copy to clipboard operation
router copied to clipboard

[File-based-router] Wrong routeTree.gen.ts generated after change in route files

Open simonedelmann opened this issue 2 years ago • 0 comments

Describe the bug

Whenever the cli or the Vite plugin has to replace/fix code in route files, routeTree.gen.ts is not generated correctly.

Your Example Website or App

https://stackblitz.com/github/tanstack/router/tree/main/examples/react/basic-file-based?embed=1&theme=light

Steps to Reproduce the Bug or Issue

  1. Take any file-based StackBlitz example from the docs.
  2. Stop npm run dev and run npx @tanstack/router-cli generate. (Just to start fresh.)
  3. Open /src/routes/posts.tsx and change createFileRoute('/posts') to createFileRoute('/something-else')
  4. Run npx @tanstack/router-cli generate.
  5. See wrong code in routeTree.gen.ts.
  6. Run npx @tanstack/router-cli generate again.
  7. See correct code in routeTree.gen.ts.

Expected behavior

routeTree.gen.ts

// After step 5: Wrong parent route
const PostsPostIdRoute = PostsPostIdImport.update({
  path: '/posts/$postId',
  getParentRoute: () => rootRoute,
} as any)

// After step 7: Correct parent route
const PostsPostIdRoute = PostsPostIdImport.update({
  path: '/$postId',
  getParentRoute: () => PostsRoute,
} as any)

Possible ideas

I guess this has something to do how parent routes get tracked by the generator:

https://github.com/TanStack/router/blob/21ce1351e5e88d89d8d8924666c4f97251da89ef/packages/router-generator/src/generator.ts#L203-L204 https://github.com/TanStack/router/blob/21ce1351e5e88d89d8d8924666c4f97251da89ef/packages/router-generator/src/generator.ts#L310-L315

Edit: Realized that this not only occurs for layout routes.

simonedelmann avatar Feb 11 '24 21:02 simonedelmann