- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Description
Which project does this relate to?
Router
Describe the bug
In my vite config, I have root
set to "./src"
. Initially this resulted in an error from the TanStackRouterVite
plugin due to it trying to read the routes from ./src/src/routes
, but I resolved this by adding routesDirectory: "./src/routes"
and generatedRouteTree: "./src/routeTree.gen.ts"
to the plugin config.
With this resulting setup, everything seems to work, except that when I create new route files they don't get filled with the usual boilerplate code. And for the routeTree.gen.ts
to be updated I have to kill and restart the vite dev server.
Your Example Website or App
Steps to Reproduce the Bug or Issue
- Go to the linked stackblitz
- See that the app works fine
- Try creating a new route file - it doesn't get automatically filled with the route boilerplate code
- Manually add the code for the new route file - see that you get a TS error from createFileRoute (because the routeTree.gen.ts hasn't been properly updated) until you kill and restart the vite dev server
Expected behavior
- automatic filling of route files with boilerplate code should work regardless of
root
setting in vite config - routeTree.gen.ts should get properly updated when adding/updating route files regardless of
root
setting in vite config
Screenshots or Videos
No response
Platform
- OS: macOS
Additional context
No response
eliellis, evenfrost, killuox, LeCarbonator and Jaime02
Metadata
Metadata
Assignees
Labels
No labels
Activity
eliellis commentedon Mar 7, 2025
I'm having the same issue, for now I've worked around it (somewhat) with the following Vite config (using
vite-plugin-restart
):This restarts the vite process completely when any
.ts(x)?
file changes in the routes directory, which is definitely not ideal, but gets the job done until this gets sorted.MichaelCannucci commentedon Apr 8, 2025
Found a workaround, since there seems to be bug related to the Vite root config option.
Making sure the
routesDirectory
option is an absolute path should fix it.Here's an example of my
vite.config.ts
renchap commentedon Apr 28, 2025
I am also facing this. Some additional debugging:
Vite root is set as
app/client
With the plugin configured as:
In https://github.com/TanStack/router/blob/main/packages/router-plugin/src/core/router-generator-plugin.ts#L66 I can observe:
routesDirectoryPath = "<prefix>/app/client/app/client/routes"
You can notice that
routesDirectoryPath
addsroutesDirectory
after the Vite root path. Also the route tree is generated at the expected location (<prefix>/app/client/routeTree.gen.ts
) without prepending the Vite root path.Initial generation works when Vite starts, but then when files are updated the generator is not ran.
If I change the plugin settings to:
Then Vite crashes at startup with
So the issue seems to stem from the routes path not being computed the same way between the router start and a file update. In the first case the Vite root path is not appended, but it is in the 2nd case. It also seems to never be prepended to
generatedRouteTree
.A fix would probably involve unifying the behaviour here, and I would recommend always appending Vite's root path to the routes directory path and the generated route tree path for consistency.
RMHonor commentedon May 23, 2025
The docs state that the
routesDirectory
is relative to the CWD, while it seems to be relative toroot
on start-up.Based on this, expected behaviour with
root
of./src
androutesDirectory
of./src/routes
would be that it looks for routes in{cwd}/src/routes
, not./src/src/routes
as witnessed.The issue seems to be that the plugin uses the Vite root, rather than the process.cwd(), here.
This can be removed, and it'll give the documented behaviour.
RMHonor commentedon Jun 12, 2025
Looks to be fixed in v1.121 🙌