-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
fix(react-router/fs-routes): correctly apply ignoredFilePatterns to files and folders in flatRoutes #14195
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
base: dev
Are you sure you want to change the base?
Conversation
…iles and folders in flatRoutes
🦋 Changeset detectedLatest commit: c5e7159 The changes in this PR will be included in the next version bump. This PR includes changesets to release 11 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
While working on this issue I discovered another potential problem in a specific case. function findRouteModuleForFolder(
appDirectory: string,
routesDir: string,
filepath: string,
ignoredFileRegex: RegExp[],
): string | null {
- let relativePath = path.relative(routesDir, filepath);
+ let relativePath = normalizeSlashes(path.relative(routesDir, filepath));
- let isIgnored = ignoredFileRegex.some((regex) => regex.test(relativePath));
+ let isIgnored = ignoredFileRegex.some((regex) => regex.test(relativePath) || regex.test(relativePath + "/*"));
if (isIgnored) return null; |
I acknowledge this is an incomplete solution, but note that React Router usage disallows nested folders as a constraint. |
Hi @brophdawg11, My proposal to fix that is outlined in the comment section. I've left the PR in draft status to get your feedback on this proposed approach first. Could you please take a look and let me know if this is the right direction for the project? If so, I'll implement the fix and mark the PR as ready for review. |
Thanks for the PR! I think you're right that the current behaviour is not ideal and your proposed behaviour of matching relative to the routes directory is much better. However, this would be a breaking change for anyone already using this feature. We could potentially add a flag to change this behaviour, or deprecate the existing I think the main problem to be fixed is that the documentation is both wrong and incomplete. We should be documenting that patterns are relative to the app directory, and giving better examples of patterns that can be used (potentially linking out to minimatch since that's what we use internally). For example, to your point about directories, you can ignore them like this: If you'd like, you could update this PR with the following changes and I'd be happy to merge it:
|
Sorry for the late reply, and thanks for your response! |
I re-examined the issue, but there is one problem. react-router/packages/react-router-fs-routes/flatRoutes.ts Lines 310 to 338 in 2df250e
In other words, even if there is a file like routes/home/route.tsx , the string used to verify whether it matches the ignoredFilePatterns is routes/home . There is no process to recheck whether the file within the home folder matches the ignoredFilePatterns .Therefore, in the current implementation, if you want to match routes/home.tsx , you must write routes/home.tsx , and if you want to match routes/home/route.tsx , you must also write routes/home.tsx , which is a bit cumbersome.
For example, this test will always fail. test("should ignore files in a folder", () => {
let routeOne = path.join(routesDir, "one.tsx");
let routeTwoDir = path.join(routesDir, "two");
let routeTwo = path.join(routeTwoDir, "route.tsx");
writeFileSync(routeOne, "");
mkdirSync(routeTwoDir, { recursive: true });
writeFileSync(routeTwo, "");
// OK
// let ignoredFilePatterns = ["routes/two"];
// FAIL
let ignoredFilePatterns = ["routes/two/*.tsx"];
let routeManifest = flatRoutes(tempDir, ignoredFilePatterns);
let routeIds = Object.keys(routeManifest);
expect(routeIds).not.toContain("routes/two");
expect(routeIds.length).toBe(1);
}); |
fixes: #14157
The current implementation checks
ignoredFilePatterns
against file paths relative to theapp
directory (e.g."routes/mainPage.tsx"
) instead of theroutes
directory as documented (e.g."mainPage.tsx"
). As a result, patterns like"mainPage.tsx"
do not match and the behavior is surprising for users.This PR fixes that by using paths relative to the routes directory when evaluating
ignoredFilePatterns
, restoring the documented behavior.