TanStack / router

🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering.
https://tanstack.com/router
MIT License
7.19k stars 486 forks source link

Vite Plugin not re-generating routes with a non-standard Vite root configured #1550

Closed a-maas closed 2 weeks ago

a-maas commented 2 weeks ago

Describe the bug

I've provided minimal example project that demonstrates a potential bug with the TanStack Router Vite Plugin not detecting and generating when using a non-standard vite root configuration. In this example, the vite root config is set to app in the vite.config.ts file. With this configuration, TanStack Router does not detect tsr.config.json in the project root, but will detect it if the file is moved to the app directory. Once moved to the app directory, the TanStack Router Vite plugin will successfully generate the routes on vite dev boot, but will not detect and re-generate based on adding and removing files in the app/routes directory.

Your Example Website or App

https://github.com/a-maas/ts-router-ex

Steps to Reproduce the Bug or Issue

  1. Clone this repository
  2. Run npm install
  3. Run npm run dev
  4. Observe that the routes are generated on boot.
  5. Add a test.tsx file to the app/routes directory.
  6. Observe that the routes are not re-generated.

Expected behavior

As a user, I expect the TanStack Router Vite plugin to re-generate routes when I add and remove files from the configured routes directory, even when Vite is configured with a non-standard root.

Screenshots or Videos

No response

Platform

Additional context

No response

schiller-manuel commented 2 weeks ago

this PR seems to be related: https://github.com/TanStack/router/pull/1259

do you want to provide a PR to fix this issue?

a-maas commented 2 weeks ago

Thanks for the link, I didn't find that when searching for this issue before submitting. Yes, that PR and related issue look like a very similar issue to what I described above, though, the issue still persists.

I will take some time and try to work out a fix PR. Any guidance would be appreciated since I'm still pretty new to Vite and TSR.

a-maas commented 2 weeks ago

@schiller-manuel it seems like there are two options to fix this. The problem stems from initially setting ROOT to process.cwd(), but later redefining ROOT as vite's configured root. This leads to a mismatch if you have vite's root set to anything other than the process.cwd().

Option 1: To be consistent, use process.cwd() everywhere, which would mean only changing this line to process.cwd() instead of config.root. I'm not sure if this would cause confusion or future issues for people using vite.root.

Option 2: Use vite.root, if set, everywhere, with a process.cwd() fallback. This would have a few consequences:

a-maas commented 2 weeks ago

Seems like in the prior PR, you were weighing both of these options, and ended up going with Option 1, but Option 2 may be more desirable.

schiller-manuel commented 2 weeks ago

would option 2 be a breaking change?