Repository: TanStack/router
Pull request #2314 (2024-09-10 17:19:03)

fix(router-generator): correctly generate route tree if only rootRoute exists
fixes #2308

nx-cloud[bot]: # ☁️ Nx Cloud Report

CI is running for commit 3035dfb65af38c09309f483aba8e55e7768d1daa.

[📂 Click to track the progress, see the status, the terminal output, and the build insights.](https://cloud.nx.app/cipes/66e07fa5b3ce4f70ceaf9509?utm_source=pull-request&utm_medium=comment)


---

Sent with 💌 from [NxCloud.](https://nx.app?utm_source=pull-request&utm_medium=comment)
pkg-pr-new[bot]: 
[Open in Stackblitz](https://pkg.pr.new/template/0f48a894-5c9d-42a0-8cef-4484be60f4b5)
<details><summary><b>More templates</b></summary><p>

- [tanstack-router-react-example-authenticated-routes](https://pkg.pr.new/template/392f79d7-0a50-45be-98ad-c7b3b0eaf5c1)
- [tanstack-router-react-example-basic](https://pkg.pr.new/template/29d2ad87-ad0a-457d-b235-a80159cbeae8)
- [tanstack-router-react-example-basic-default-search-params](https://pkg.pr.new/template/f4f61b64-b1e8-4f24-b5dc-0260b6e98f60)
- [tanstack-router-react-example-basic-file-based](https://pkg.pr.new/template/365e1757-bb2e-499e-ae7e-bb81d4377d94)
- [tanstack-router-react-example-basic-file-based-codesplitting](https://pkg.pr.new/template/8fc808ee-96d5-44ea-acf9-146ad11d0f74)
- [tanstack-router-react-example-react-query](https://pkg.pr.new/template/1d7a72aa-ac25-4f5e-9686-dbd9a3a9fda2)
- [tanstack-router-react-example-basic-react-query-file-based](https://pkg.pr.new/template/42c22075-ba94-44d4-9428-9fc25b68290e)
- [tanstack-router-react-example-basic-ssr-file-based](https://pkg.pr.new/template/bcf9dcc4-56df-412b-94fb-481035435b96)
- [tanstack-router-react-example-basic-ssr-streaming-file-based](https://pkg.pr.new/template/b9d48f3a-d355-47ab-b0cb-d70e24043e35)
- [tanstack-router-react-example-basic-virtual-file-based](https://pkg.pr.new/template/a7a4bc62-8058-41cc-b87d-c06654498485)
- [tanstack-router-react-example-basic-virtual-inside-file-based](https://pkg.pr.new/template/77694164-dcda-4964-b9b8-41b767a7971a)
- [tanstack-router-react-example-deferred-data](https://pkg.pr.new/template/a7c88fe3-9100-4695-8ee6-f0ec9e5f2715)
- [tanstack-router-react-example-kitchen-sink](https://pkg.pr.new/template/f59235f8-c556-4e51-8379-62e1f9d4fc9c)
- [tanstack-router-react-example-kitchen-sink-file-based](https://pkg.pr.new/template/411b7641-45fa-4e16-be10-c4801a59deac)
- [tanstack-router-react-example-kitchen-sink-react-query](https://pkg.pr.new/template/72b282ac-c786-4aa8-a02e-a7dbd91f2c78)
- [tanstack-router-react-example-kitchen-sink-react-query-file-based](https://pkg.pr.new/template/4f9157f1-65bf-464f-8725-ea98bd30135b)
- [tanstack-router-react-example-large-file-based](https://pkg.pr.new/template/3ee32c35-4c90-473a-9d82-3739a06a40e6)
- [tanstack-router-react-example-location-masking](https://pkg.pr.new/template/080fc350-adc8-4aff-b5f4-b425a7a5d7cc)
- [tanstack-router-react-example-quickstart](https://pkg.pr.new/template/9b040383-dee8-4856-947a-659145bc4c6b)
- [tanstack-router-react-example-navigation-blocking](https://pkg.pr.new/template/65c5778a-cd49-45ac-a205-2737181de46d)
- [tanstack-router-react-example-quickstart-file-based](https://pkg.pr.new/template/70da8648-4300-47ed-9914-a80c956c21c3)
- [tanstack-router-react-example-quickstart-rspack-file-based](https://pkg.pr.new/template/9d0bf703-c561-4f6f-bcb6-cc140ce0f04c)
- [tanstack-router-react-example-scroll-restoration](https://pkg.pr.new/template/ce954e1d-62e1-4a86-af88-f4aa5cd44b1b)
- [tanstack-router-react-example-quickstart-webpack-file-based](https://pkg.pr.new/template/c128a829-0e19-4cf7-bfc3-94d759e52b22)
- [tanstack-router-search-validator-adapters](https://pkg.pr.new/template/25150624-fb58-4c8a-871d-101c5abe2fb8)
- [tanstack-router-example-react-start-basic](https://pkg.pr.new/template/043e2067-4c20-47ec-9f24-7556e5176aaf)
- [tanstack-router-example-react-start-basic-counter](https://pkg.pr.new/template/4e621bde-78fb-4540-b20c-5407d2f42a99)
- [tanstack-router-example-react-start-basic-auth](https://pkg.pr.new/template/270d370e-938a-46ff-b426-b832d10b705d)
- [tanstack-router-example-react-start-basic-rsc](https://pkg.pr.new/template/1536f20b-a09a-4948-bab8-7939df7feebc)
- [tanstack-router-example-react-start-basic-react-query](https://pkg.pr.new/template/a5b979ef-26d5-4131-b571-f19e855425e4)
- [tanstack-router-example-react-start-trello-trellaux](https://pkg.pr.new/template/01a5507b-3795-4bc6-be12-1de02bb7848e)
- [tanstack-router-example-react-start-clerk-basic](https://pkg.pr.new/template/33dfe20e-ce79-47a5-b0ba-2e4910ed57cb)
- [tanstack-router-example-react-start-supabase-basic](https://pkg.pr.new/template/ef53e064-d51a-4bc5-b10f-4b96bb6f8dd6)
- [tanstack-router-example-react-start-trellaux](https://pkg.pr.new/template/e30b0505-f4fc-4241-b8b3-ae3682df1f83)
- [tanstack-router-react-example-with-framer-motion](https://pkg.pr.new/template/0ae9efcf-39af-4442-8696-0249b1607cd6)
- [tanstack-router-react-example-with-trpc](https://pkg.pr.new/template/2ad99861-7737-4991-99ca-092ea545ad4c)
- [tanstack-router-react-example-with-trpc-react-query](https://pkg.pr.new/template/bc52824a-4f63-47a6-ba6f-fecee9f407de)

</p></details>
      
    


<details><summary><b>@tanstack/create-router</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/create-router@2314
```

</p></details>
      
    

<details><summary><b>@tanstack/history</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/history@2314
```

</p></details>
      
    

<details><summary><b>@tanstack/react-cross-context</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/react-cross-context@2314
```

</p></details>
      
    

<details><summary><b>@tanstack/react-router</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/react-router@2314
```

</p></details>
      
    

<details><summary><b>@tanstack/react-router-with-query</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/react-router-with-query@2314
```

</p></details>
      
    

<details><summary><b>@tanstack/router-arktype-adapter</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-arktype-adapter@2314
```

</p></details>
      
    

<details><summary><b>@tanstack/router-cli</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-cli@2314
```

</p></details>
      
    

<details><summary><b>@tanstack/router-generator</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-generator@2314
```

</p></details>
      
    

<details><summary><b>@tanstack/router-devtools</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-devtools@2314
```

</p></details>
      
    

<details><summary><b>@tanstack/router-plugin</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-plugin@2314
```

</p></details>
      
    

<details><summary><b>@tanstack/router-valibot-adapter</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-valibot-adapter@2314
```

</p></details>
      
    

<details><summary><b>@tanstack/router-zod-adapter</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-zod-adapter@2314
```

</p></details>
      
    

<details><summary><b>@tanstack/router-vite-plugin</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-vite-plugin@2314
```

</p></details>
      
    

<details><summary><b>@tanstack/start</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/start@2314
```

</p></details>
      
    

<details><summary><b>@tanstack/start-vite-plugin</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/start-vite-plugin@2314
```

</p></details>
      
    

<details><summary><b>@tanstack/virtual-file-routes</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/virtual-file-routes@2314
```

</p></details>
      
    

_commit: <a href="https://github.com/TanStack/router/runs/29948873086"><code>3035dfb</code></a>_


Patch:
diff --git a/packages/router-generator/src/generator.ts b/packages/router-generator/src/generator.ts
index 4bfd231f4..7626b8f9e 100644
--- a/packages/router-generator/src/generator.ts
+++ b/packages/router-generator/src/generator.ts
@@ -616,10 +616,10 @@ export const Route = createAPIFileRoute('${escapedRoutePath}')({
 }`,
           `export interface FileRouteTypes {
   fileRoutesByFullPath: FileRoutesByFullPath
-  fullPaths: ${[...createRouteNodesByFullPath(routeNodes).keys()].map((fullPath) => `'${fullPath}'`).join('|')}
+  fullPaths: ${routeNodes.length > 0 ? [...createRouteNodesByFullPath(routeNodes).keys()].map((fullPath) => `'${fullPath}'`).join('|') : 'never'}
   fileRoutesByTo: FileRoutesByTo
-  to: ${[...createRouteNodesByTo(routeNodes).keys()].map((to) => `'${to}'`).join('|')}
-  id: '__root__' | ${[...createRouteNodesById(routeNodes).keys()].map((id) => `'${id}'`).join('|')}
+  to: ${routeNodes.length > 0 ? [...createRouteNodesByTo(routeNodes).keys()].map((to) => `'${to}'`).join('|') : 'never'}
+  id: ${[`'__root__'`, ...[...createRouteNodesById(routeNodes).keys()].map((id) => `'${id}'`)].join('|')}
   fileRoutesById: FileRoutesById
 }`,
           `export interface RootRouteChildren {


Test patch:
diff --git a/packages/router-generator/tests/generator/only-root/routeTree.snapshot.ts b/packages/router-generator/tests/generator/only-root/routeTree.snapshot.ts
new file mode 100644
index 000000000..a65ec67d0
--- /dev/null
+++ b/packages/router-generator/tests/generator/only-root/routeTree.snapshot.ts
@@ -0,0 +1,61 @@
+/* prettier-ignore-start */
+
+/* eslint-disable */
+
+// @ts-nocheck
+
+// noinspection JSUnusedGlobalSymbols
+
+// This file is auto-generated by TanStack Router
+
+// Import Routes
+
+import { Route as rootRoute } from './routes/__root'
+
+// Create/Update Routes
+
+// Populate the FileRoutesByPath interface
+
+declare module '@tanstack/react-router' {
+  interface FileRoutesByPath {}
+}
+
+// Create and export the route tree
+
+export interface FileRoutesByFullPath {}
+
+export interface FileRoutesByTo {}
+
+export interface FileRoutesById {
+  __root__: typeof rootRoute
+}
+
+export interface FileRouteTypes {
+  fileRoutesByFullPath: FileRoutesByFullPath
+  fullPaths: never
+  fileRoutesByTo: FileRoutesByTo
+  to: never
+  id: '__root__'
+  fileRoutesById: FileRoutesById
+}
+
+export interface RootRouteChildren {}
+
+const rootRouteChildren: RootRouteChildren = {}
+
+export const routeTree = rootRoute
+  ._addFileChildren(rootRouteChildren)
+  ._addFileTypes<FileRouteTypes>()
+
+/* prettier-ignore-end */
+
+/* ROUTE_MANIFEST_START
+{
+  "routes": {
+    "__root__": {
+      "filePath": "__root.tsx",
+      "children": []
+    }
+  }
+}
+ROUTE_MANIFEST_END */
diff --git a/packages/router-generator/tests/generator/only-root/routes/__root.tsx b/packages/router-generator/tests/generator/only-root/routes/__root.tsx
new file mode 100644
index 000000000..f89644e82
--- /dev/null
+++ b/packages/router-generator/tests/generator/only-root/routes/__root.tsx
@@ -0,0 +1,11 @@
+import * as React from 'react'
+import { Outlet, createRootRoute } from '@tanstack/react-router'
+
+export const Route = createRootRoute({
+  component: () => (
+    <React.Fragment>
+      <div>Hello "__root"!</div>
+      <Outlet />
+    </React.Fragment>
+  ),
+})