Skip to content

generator refactor #4337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 39 commits into
base: alpha
Choose a base branch
from
Open

generator refactor #4337

wants to merge 39 commits into from

Conversation

schiller-manuel
Copy link
Contributor

@schiller-manuel schiller-manuel commented Jun 7, 2025

This PR introduces a refactoring of the router-generator:

The router-generator is now plugin based. This way we can realize a clean separation of Router and Start while also keeping code duplication at a minimum. The plugin API is not public.

The route manifest is not embedded into the generated route tree file anymore. Instead, it is handled by a generator plugin that is installed by start-plugin-core. This plugin builds up the route manifest and provides it for others to consume via globalThis. Thus, no file is written for the route manifest.

As a consequence, the disableManifestGeneration option was removed from the generator config.

The plugin mechanism allows to remove the separate server-route-generator and implement the Start specific logic via a router-generator plugin. The serverRouteTree is now also written into the same routeTree.gen.ts file as the routeTree.
For the client build, the serverRouteTree and all of its imports / types are stripped out so now server stuff ends up in the client bundle.

Since the regex based approach for handling route file rewrites finally reached its limitations, route file rewrites are now handled via an AST based approach.
see https://github.com/TanStack/router/blob/generator-refactor/packages/router-generator/src/transform/transform.ts

The router-generator is now optimized for dev by leveraging a double-buffer cache mechanism.
See https://github.com/TanStack/router/blob/generator-refactor/packages/router-generator/src/generator.ts#L144-L153
When a single route file is saved and the change does not result in outputting a new route tree file (i.e. no relevant exports (Route, ServerRoute) have been added or removed), router-generator will process this change in ~2-3 ms.

The router-generator now detects whether a file was changed by another process before writing to it.
https://github.com/TanStack/router/blob/generator-refactor/packages/router-generator/src/generator.ts#L929-L966
File writes are realized atomically via renaming.
router-generator will write the new content to a temporary file and then will rename the temporary file to the target file name. This can lead to issues when temp directory and target directory are not on the same device.
For example on a github runner, this leads to the following error:

Error: EXDEV: cross-device link not permitted, rename '/tmp/tanstack-router-7wje9z/35d0e8564e9868341e48407dc66dab17' -> '/home/workflows/workspace/e2e/react-router/rspack-basic-virtual-named-export-config-file-based/src/routeTree.gen.ts'

Hence a new config option tmpDir was introduced which allows customization of the temp directory location for router-generator.

This PR also changes the default of verboseFileRoutes for Start to the same as for router-generator, so users have to op-in into the new syntax and import handling.

Copy link

nx-cloud bot commented Jun 7, 2025

View your CI Pipeline Execution ↗ for commit 942b67e.

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ❌ Failed 48s View ↗

☁️ Nx Cloud last updated this comment at 2025-06-09 12:17:59 UTC

Copy link

pkg-pr-new bot commented Jun 7, 2025

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@4337

@tanstack/directive-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@4337

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@4337

@tanstack/history

npm i https://pkg.pr.new/TanStack/router/@tanstack/history@4337

@tanstack/react-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@4337

@tanstack/react-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@4337

@tanstack/react-router-with-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-with-query@4337

@tanstack/react-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@4337

@tanstack/react-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@4337

@tanstack/react-start-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-plugin@4337

@tanstack/react-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@4337

@tanstack/router-cli

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@4337

@tanstack/router-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@4337

@tanstack/router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@4337

@tanstack/router-devtools-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@4337

@tanstack/router-generator

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@4337

@tanstack/router-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@4337

@tanstack/router-utils

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@4337

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@4337

@tanstack/server-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@4337

@tanstack/solid-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@4337

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@4337

@tanstack/solid-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@4337

@tanstack/solid-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@4337

@tanstack/solid-start-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-plugin@4337

@tanstack/solid-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@4337

@tanstack/start-client-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@4337

@tanstack/start-plugin-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@4337

@tanstack/start-server-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@4337

@tanstack/start-server-functions-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-client@4337

@tanstack/start-server-functions-fetcher

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-fetcher@4337

@tanstack/start-server-functions-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-server@4337

@tanstack/valibot-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@4337

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@4337

@tanstack/zod-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@4337

commit: 942b67e

schiller-manuel and others added 8 commits June 7, 2025 02:13
this ensures they are not formatted by prettier since 'snapshots'  is already ignored by prettier
otherwise, the following error occurs:

> Error: EXDEV: cross-device link not permitted, rename '/tmp/tanstack-router-7wje9z/35d0e8564e9868341e48407dc66dab17' -> '/home/workflows/workspace/e2e/react-router/rspack-basic-virtual-named-export-config-file-based/src/routeTree.gen.ts'
@github-actions github-actions bot added the documentation Everything documentation related label Jun 7, 2025
the type errors raised are due these old TS versions not being as smart about inference.
however, the type errors errors are only relevant at build time of the library and not when consuming the library
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants