Skip to content
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

Make react router as peer dep instead of dependency #3001

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

adgang
Copy link

@adgang adgang commented Dec 13, 2024

I came across this while debugging #3002

Some package managers/resolvers in certain cases, load different downstream packages for different imports.
For instance, in tanstack/start quick starter app, we import both @tanstack/start and @tanstack/router. @tanstack/router is imported in user application(See Getting Started) as well in @tanstack/start. In one place the resolver may resolve to a different module than in the other places.

deno, for instance resolves

  1. the import of @tanstack/react-router in user application to node_modules/.deno/@TanStack[email protected]_1 and
  2. the import of @tanstack/react-router in @tanstack/start to node_modules/.deno/@TanStack[email protected]

This causes a null router value to be returned by useRouterState() and the following logs:

Warning: useRouter must be used inside a <RouterProvider> component!

when the <Outlet /> code ultimately fetches context.

In other words, the module that hung the context up was in ssr.tsx via createStartHandler/StartServer was node_modules/.deno/@[email protected]_1(because resolver resolved to that module)
While, the code uses(and gets from resolver) node_modules/.deno/@[email protected]

pnpm/deno/bun/yarn all three can resolve to different modules this way.

Related issues:
#3002 - deno issue
#2594 - refers to bun and pnpm
#2540 - refers to pnpm and yarn
#1692 - doesn't mention the package manager used, but seems to indicate a similar issue.

I have fixed only the react-router issue for now. But if people used @tanstack/react-cross-context directly in their code, then they will see this issue there as well.

This means that potentially all the libraries in tanstack start/router should be appropriately set as peer dependencies instead of dependencies as a matter of best practice(because unlike a typical monorepo tanstack router packages should be independently usable as well). I will leave it for @tannerlinsley if/when he wants to do these changes, if he accepts this fix.

At the very least, @tanstack/react-cross-context should be declared as peer dependency in @tanstack/router as it is the container where we hang up the context(a singleton). Otherwise people will use it in their code and end up with two different contexts and will end up hitting their heads against the wall on why their context is empty in some part of the code while it is correctly obtained in other places(or worse they will have some 10 contexts).

The potential packages can be seen by dependency tree where this practice should apply:

deno info @tanstack/start | grep tanstack
├─┬ npm:/@tanstack/[email protected][email protected][email protected][email protected] (7.43KB)
├─┬ npm:/@tanstack/[email protected][email protected][email protected][email protected]_@[email protected] (1.63MB)
│ ├── npm:/@tanstack/[email protected] (71.85KB)
│ ├─┬ npm:/@tanstack/[email protected][email protected][email protected][email protected] (13.48KB)
│ │ ├── npm:/@tanstack/[email protected] (15.2KB)
│ ├─┬ npm:/@tanstack/[email protected] (353.33KB)
│ │ ├── npm:/@tanstack/[email protected] (23.26KB)
├── npm:/@tanstack/[email protected] (353.33KB) *
├─┬ npm:/@tanstack/[email protected][email protected]_@[email protected]_@[email protected] (280.35KB)
│ ├── npm:/@tanstack/[email protected] (353.33KB) *
│ ├── npm:/@tanstack/[email protected] (23.26KB)
├─┬ npm:/@tanstack/[email protected]_@[email protected] (134.55KB)

BTW this issue can show up in node as well if npm installs nested dependencies. I am not 100% sure on the module resolution logic of current npm but I believe at least older npm versions used to do this.

@adgang adgang marked this pull request as ready for review December 13, 2024 06:28
Copy link

nx-cloud bot commented Dec 13, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit e98f51c. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

@schiller-manuel
Copy link
Contributor

start cannot be used without router, so a peer dependency is not expressing the correct relationship.

Copy link

pkg-pr-new bot commented Dec 13, 2024

Open in Stackblitz

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/@tanstack/arktype-adapter@3001

@tanstack/history

npm i https://pkg.pr.new/@tanstack/history@3001

@tanstack/create-router

npm i https://pkg.pr.new/@tanstack/create-router@3001

@tanstack/react-cross-context

npm i https://pkg.pr.new/@tanstack/react-cross-context@3001

@tanstack/react-router

npm i https://pkg.pr.new/@tanstack/react-router@3001

@tanstack/react-router-with-query

npm i https://pkg.pr.new/@tanstack/react-router-with-query@3001

@tanstack/router-cli

npm i https://pkg.pr.new/@tanstack/router-cli@3001

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/@tanstack/eslint-plugin-router@3001

@tanstack/router-devtools

npm i https://pkg.pr.new/@tanstack/router-devtools@3001

@tanstack/router-generator

npm i https://pkg.pr.new/@tanstack/router-generator@3001

@tanstack/router-plugin

npm i https://pkg.pr.new/@tanstack/router-plugin@3001

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/@tanstack/router-vite-plugin@3001

@tanstack/start

npm i https://pkg.pr.new/@tanstack/start@3001

@tanstack/start-vite-plugin

npm i https://pkg.pr.new/@tanstack/start-vite-plugin@3001

@tanstack/valibot-adapter

npm i https://pkg.pr.new/@tanstack/valibot-adapter@3001

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/@tanstack/virtual-file-routes@3001

@tanstack/zod-adapter

npm i https://pkg.pr.new/@tanstack/zod-adapter@3001

commit: e98f51c

@adgang
Copy link
Author

adgang commented Dec 13, 2024

May be we can expose all functions like createRouter from start then? By reexporting I mean.

@schiller-manuel
Copy link
Contributor

this seems like a workaround for some bundler issues. what's the proper solution here?

Copy link
Member

@SeanCassiere SeanCassiere left a comment

Choose a reason for hiding this comment

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

Since we have it documented pretty much everywhere, that start is meant to be used with router, I think its fine to move it to peerDeps.

@@ -134,7 +134,6 @@
},
"dependencies": {
"@tanstack/react-cross-context": "workspace:^",
Copy link
Member

Choose a reason for hiding this comment

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

It may be worth checking out, whether we are actually using the react-cross-context package and possibly remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants