Skip to content

Skip typegen for routes outside the app directory #12996

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

Merged

Conversation

wilcoxmd
Copy link

@wilcoxmd wilcoxmd commented Feb 11, 2025

This is a fix for #12993.

This PR limits typegen to only occur for route files in the current project's app directory. This behavior should be consistent with the current getTypesPath logic and generated Module typing, which also seem to assume that route files are located in the app directory.

Adding this check prevents type files from being generated outside .react-router/types (which can cause tsc failures) if a route file is located outside the current project's app directory, which is the case for route definitions imported from a library or monorepo workspace package. For now, route library/workspace packages can rely on their own separate typecheck processes to ensure routes are typesafe before they are imported to another application.

In the future it would be nice to figure out a way to include library route typings into a host project's typegen process. However, I imagine this approach would probably require more thought and input from the team to decide on where to generate those files and how they should be consumed. In the meantime, this PR's goal is just to help prevent unintended issues without changing the current typgen behavior too much.

Very open to feedback on this, particularly on the testing approach! I couldn't find a great pattern for testing workspace-style project structures, so I just created a new set of fixtures and a way to tweak their content as part of setting up the test in 74fed50. Let me know if you'd prefer a different strategy!

The primary source code change is in 6902a87

Copy link

changeset-bot bot commented Feb 11, 2025

🦋 Changeset detected

Latest commit: e771bf4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 11 packages
Name Type
@react-router/dev Patch
@react-router/fs-routes Patch
@react-router/remix-routes-option-adapter Patch
create-react-router Patch
react-router Patch
react-router-dom Patch
@react-router/architect Patch
@react-router/cloudflare Patch
@react-router/express Patch
@react-router/node Patch
@react-router/serve Patch

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

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Feb 11, 2025

Hi @wilcoxmd,

Welcome, and thank you for contributing to React Router!

Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once.

You may review the CLA and sign it by adding your name to contributors.yml.

Once the CLA is signed, the CLA Signed label will be added to the pull request.

If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at [email protected].

Thanks!

- The Remix team

@@ -1,5 +1,5 @@
packages:
- "integration"
- "integration/helpers/*"
- "integration/helpers/**"
Copy link
Author

@wilcoxmd wilcoxmd Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This update allows the workspace fixture to install node modules in nested directories

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Feb 12, 2025

Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳

@wilcoxmd wilcoxmd force-pushed the mwilcox/skip-imported-route-typegen branch 3 times, most recently from 725539b to 9d0dc64 Compare February 12, 2025 16:40
@@ -71,11 +72,21 @@ async function createContext({
};
}

function isRouteInAppDirectory(ctx: Context, route: RouteManifestEntry) {
const absoluteRoutePath = Path.resolve(ctx.config.appDirectory, route.file);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const root = path.resolve(__dirname, "../..");
const TMP_DIR = path.join(root, ".tmp/integration");

export async function writeTestFiles(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function, and the logic in createTestDirectory below were pulled from other files and just refactored into this file for reuse.

@wilcoxmd wilcoxmd marked this pull request as ready for review February 12, 2025 17:06
@pcattori pcattori self-assigned this Feb 12, 2025
@wilcoxmd
Copy link
Author

wilcoxmd commented Mar 19, 2025

Hi @pcattori! Do you have any thoughts on this PR? Would love to merge it to prevent issues in our monorepo at Square. Seems like a few other folks have run into the same problem as well based on this issue comment and discord.

@wilcoxmd
Copy link
Author

wilcoxmd commented Apr 3, 2025

Hi @pcattori, just wanted check again for any feedback on this? Or @brophdawg11 perhaps? I noticed you added some labels to the linked issue yesterday, so was hoping that maybe you were doing some planning around this and that it could be a good time to consider merging or discussing further.

@pcattori pcattori force-pushed the mwilcox/skip-imported-route-typegen branch from b577dde to e771bf4 Compare May 6, 2025 19:38
@pcattori pcattori force-pushed the mwilcox/skip-imported-route-typegen branch from e771bf4 to 3738344 Compare May 6, 2025 19:42
@pcattori pcattori merged commit b7b1876 into remix-run:dev May 6, 2025
10 of 11 checks passed
@pcattori
Copy link
Contributor

pcattori commented May 6, 2025

Thanks for your work on this @wilcoxmd !

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.

Typegen generates files outside of .react-router when using routes imported from a library
2 participants