Skip to content

Commit d816819

Browse files
fix(fs-routes): throw error if routes directory is missing (#12407)
Co-authored-by: Mark Dalgleish <[email protected]>
1 parent e1fd999 commit d816819

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

.changeset/honest-dots-deliver.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-router/fs-routes": patch
3+
---
4+
5+
Throw error in `flatRoutes` if routes directory is missing

packages/react-router-fs-routes/__tests__/flatRoutes-test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import path from "node:path";
2+
import { mkdirSync, rmdirSync, createFileSync, rmSync } from "fs-extra";
23

34
import type { RouteManifestEntry } from "../manifest";
45

56
import {
7+
flatRoutes,
68
flatRoutesUniversal,
79
getRoutePathConflictErrorMessage,
810
getRouteIdConflictErrorMessage,
@@ -877,4 +879,28 @@ describe("flatRoutes", () => {
877879
expect(routes).toHaveLength(3);
878880
});
879881
});
882+
883+
describe("throw correct error", () => {
884+
beforeEach(() => {
885+
mkdirSync(APP_DIR, { recursive: true });
886+
});
887+
afterEach(() => {
888+
rmdirSync(APP_DIR);
889+
});
890+
891+
test("root route is not found", () => {
892+
expect(() => flatRoutes(APP_DIR)).toThrow(
893+
`Could not find a root route module in the app directory: test/root/app`
894+
);
895+
});
896+
897+
test("routes dir is not found", () => {
898+
const rootRoute = path.join(APP_DIR, "root.tsx");
899+
createFileSync(rootRoute);
900+
expect(() => flatRoutes(APP_DIR)).toThrow(
901+
`Could not find the routes directory: test/root/app/routes. Did you forget to create it?`
902+
);
903+
rmSync(rootRoute);
904+
});
905+
});
880906
});

packages/react-router-fs-routes/flatRoutes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export function flatRoutes(
8989
);
9090
}
9191

92-
if (!fs.existsSync(rootRoute)) {
92+
if (!fs.existsSync(routesDir)) {
9393
throw new Error(
9494
`Could not find the routes directory: ${routesDir}. Did you forget to create it?`
9595
);

0 commit comments

Comments
 (0)