Skip to content

bug: Internal getVirtualRouteConfigFromFileExport function appears to incorrectly load config file #3122

Closed

Description

@jorroll

Which project does this relate to?

Router

Describe the bug

I'm just trying out Tanstack Router in an existing project using "virtual file routing". At the moment I'm just testing things with a dead-simple route config.

// routes.ts
import { rootRoute } from "@tanstack/virtual-file-routes";
export const routes = rootRoute("__root.tsx");
// __root.tsx
import { createRootRoute } from "@tanstack/react-router";
export const Route = createRootRoute();

When attempting to start the vite dev server, I keep seeing the following error.

Generating routes...
ZodError: [
  {
    "code": "invalid_literal",
    "expected": "root",
    "path": [
      "type"
    ],
    "message": "Invalid literal value, expected \"root\""
  },
  {
    "code": "invalid_type",
    "expected": "string",
    "received": "undefined",
    "path": [
      "file"
    ],
    "message": "Required"
  }
]
    at get error [as error] (/Users/john/code/comms/node_modules/zod/lib/types.js:55:31)
    at ZodObject.parse (/Users/john/code/comms/node_modules/zod/lib/types.js:131:22)
    at getVirtualRouteConfigFromFileExport (/Users/john/code/comms/node_modules/@tanstack/router-generator/dist/cjs/filesystem/virtual/getRouteNodes.cjs:71:40)
    at async Module.getRouteNodes (/Users/john/code/comms/node_modules/@tanstack/router-generator/dist/cjs/filesystem/virtual/getRouteNodes.cjs:32:26)
    at async Module.generator (/Users/john/code/comms/node_modules/@tanstack/router-generator/dist/cjs/generator.cjs:68:27)
    at async generate (/Users/john/code/comms/node_modules/@tanstack/router-plugin/dist/cjs/core/router-generator-plugin.cjs:46:7)
    at async run (/Users/john/code/comms/node_modules/@tanstack/router-plugin/dist/cjs/core/router-generator-plugin.cjs:70:7)
    at async configResolved (/Users/john/code/comms/node_modules/@tanstack/router-plugin/dist/cjs/core/router-generator-plugin.cjs:84:9)
    at async Promise.all (index 3)
    at async resolveConfig (file:///Users/john/code/comms/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:66404:3) {
  issues: [
    {
      received: undefined,
      code: 'invalid_literal',
      expected: 'root',
      path: [Array],
      message: 'Invalid literal value, expected "root"'
    },
    {
      code: 'invalid_type',
      expected: 'string',
      received: 'undefined',
      path: [Array],
      message: 'Required'
    }
  ],
  addIssue: [Function (anonymous)],
  addIssues: [Function (anonymous)],
  errors: [
    {
      received: undefined,
      code: 'invalid_literal',
      expected: 'root',
      path: [Array],
      message: 'Invalid literal value, expected "root"'
    },
    {
      code: 'invalid_type',
      expected: 'string',
      received: 'undefined',
      path: [Array],
      message: 'Required'
    }
  ]
}

It took me a while before I realized that the error was stating that routes.ts file's export was expected to be an object with type property equal to "root". Logging the value of the routes object, it had the shape { type: 'root', file: '__root.tsx', children: undefined }. So something else must have been going on.

Following the stack trace, I found that the internal getVirtualRouteConfigFromFileExport uses loadConfigFile.loadConfigFile to get an exports object that has the shape

{
  default: { routes: [Getter] },
  routes: { type: 'root', file: '__root.tsx', children: undefined }
}

Note that my routes.ts file doesn't have a default export. The presence of this strange default export is causing this

  // equal to `{ routes: [Getter] }`
  const virtualRouteConfig = "default" in exports2 ? exports2.default : exports2.routes;

  // which causes this to fail with the Zod error
  return config.virtualRootRouteSchema.parse(virtualRouteConfig);

Monkey patching the code like so fixes the error and the vite dev server starts correct

  return config.virtualRootRouteSchema.parse(virtualRouteConfig.routes);

Your Example Website or App

see explanation, above

Steps to Reproduce the Bug or Issue

See explanation above

Expected behavior

See explanation above

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 131

Additional context

No response

Activity

  1. changed the title [-]Internal `getVirtualRouteConfigFromFileExport` function appears to incorrectly load config file[/-] [+]bug: Internal `getVirtualRouteConfigFromFileExport` function appears to incorrectly load config file[/+] on Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions