-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Bad heuristic used to differentiate between in-repo and dependency code #16571
Comments
Bazel ecosystem maintainer here, +1 for a more principled way to distinguish external packages from those in the monorepo. |
In this specific case, I think using This might not be an ultimate solution for Bazel / Vite story, but I thought it's still worth mentioning. I verified that the following config works. // pkgs/foo-app/vite.config.js
export default {
optimizeDeps: {
exclude: ["@monorepo/foo-lib"]
}
}; In your reproduction, |
What would be the suggested solution to distinguish between them? Different package managers can have different ways of linking. Checking |
Thanks for taking a look at this so quickly! @hi-ogawa I think the issue we're having is that if we manually exclude our in-repo packages, Vite doesn't scan and process their transitive dependencies. I pushed a new
I think we would end up needing to manually add all of these transitive dependencies to @bluwy I was imagining something like letting the caller specify an optional callback in |
I understand that the point you're making, but just in case, to provide more contexts, the intended usage is to write something like this https://vitejs.dev/config/dep-optimization-options.html#optimizedeps-exclude optimizeDeps: {
exclude: ["@monorepo/foo-lib"],
include: ["@monorepo/foo-lib > react"],
} Also there is a related issue / suggestion #16293 (comment) |
+1 to this, particularly with things like https://github.com/tiktok/pnpm-sync or Rush's subspaces on the horizon it would be excellent if we could finally make workspace-enabled monorepos that make use of peer dependencies viable without significant hackery around vite. The optimizeDeps include/exclude combo is something that I tried repeatedly to get correct but found significant issues with package deduplication, commonjs transpilation etc. Would it be substantial overhead to enable plugins to augment this heuristic as a first step? |
I think it's unlikely for us to add a new option specifically to detect if a dependency is linked or not. Using The pnpm docs also clarified this:
Configuring |
Describe the bug
Vite uses
isInNodeModules()
as a heuristic to differentiate between in-repo code and dependency code during dependency pre-bundling:PNPM's
dependenciesMeta.*.injected
feature violates this heuristic by resolving both in-repo and NPM dependencies to paths that includenode_modules
. When this feautre is used,@monorepo/foo-lib
from the linked repro resolves tonode_modules/.pnpm/file+pkgs+foo-lib/node_modules/@monorepo/foo-lib/dist/index.js
, which causesisInNodeModules()
to betrue
and triggers failures.The sandbox environment setup by https://github.com/aspect-build/rules_js to run Vite with Bazel behaves very similarly. For example in the linked repro,
@monorepo/foo-lib
ultimately resolves tobazel-bin/node_modules/.aspect_rules_js/@[email protected]/node_modules/@monorepo/foo-lib/dist/index.js
, which meansisInNodeModules()
will betrue
.I originally assumed that this would affect Plug 'n Play environments as well since they resolve dependencies straight out of
.zip
files, but it looks like we're getting lucky that the ids in that case still includenode_modules
. For example:~/wburgin-anduril/vite-repro/.yarn/cache/chalk-npm-5.3.0-d181999efb-8297d436b2.zip/node_modules/chalk/source/index.js
. Maybe that's as a result of issues with heuristics like this 🤔 🤷♂️.We haven't had any luck using
optimizeDeps.include
+optimizeDeps.exclude
to work around this, so for now we're using a janky PNPM patch 🙈. Is it possible to make the determination between in-repo and dependency code configurable?Related
Reproduction
https://github.com/wburgin-anduril/vite-repro
Steps to reproduce
On the
master
branch:pnpm build
bazel-bin/pkgs/foo-app/foo-app/dist/index.html
and observe that the page workspnpm dev
and observe the errors in the consoleOn the
patched
branch:pnpm dev
and observe that there are no errors in the console and that the page works againpatches/[email protected]
to see how Vite was patched on this branchOn the
pnpm-dependencies-injected
branch:pnpm
to install dependenciespnpm dev
and observe the errors in the console@monorepo/foo-lib
into@monorepo/foo-app
in this configuration:System Info
Used Package Manager
pnpm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: