-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Pre-Bundled dependencies used over locally linked dependency when built before linking #6718
Comments
I have experienced this before too. This should technically not happen, since Vite's vite/packages/vite/src/node/optimizer/index.ts Lines 84 to 104 in 6409747
So when linking after prebundling. The next time Vite's dev server starts it should have a different |
Hit this issue again today. I dug into the cause and it seems like it's because the hash used for cache bust is derived from the Vite config and lock file only. The fix I'd imagine is to manually resolve each dependencies and include the paths in the hash too, but now that incurs extra work for everyone who doesn't link local dependencies. I'm not really sure what's the best way here. |
The linking information is global, so as you said @bluwy, we would need to change the hash, or leave it as is, but add new fields to each optimized dep (maybe I don't know if the extra complexity is worthy though. @MadLittleMods you can run vite with |
Those are fine ways to workaround the problem once you figure out the problem. But you still have to run into the pitfall in the first place and figure out why your sub-dependency isn't changing in order to utilize them. Feels like we need some way to resolve automagically or warn and explain about what's happening. |
I was going to write we could at least add a warning in https://vitejs.dev/guide/dep-pre-bundling.html#monorepos-and-linked-dependencies, but we have already this:
It could be upgraded to a warning box maybe. A real warning may be as expensive as properly supporting the reload |
That text is for local deps that are explicitly/manually added to I think this is a tricky situation too and hard to find a balance, unless there's another trick I've not thought of. |
I'm having a similar issue I believe is related to this. I have a module published on npm and a bunch of components having it in their dependencies. Now, I discover some bug in one component but related to that module, so I link them both locally, make the change to that first module (that all the other components also depend on), and expect to see the change (because now they're linked locally). But as long as there is one component that still has the npm version of the module in its dependencies (ie not linked), then vite will pick up and use that one and completely ignore the one I changed locally. I use the vite dev server, not bundling anything at this point. Using |
This is the code i used to solve this in a generic way, in case it's useful to anyone. It traverses all linked packages and finds all module specifiers and puts them in const visitedPackages = new Set()
const excludeLinks = (root: string) => {
if (visitedPackages.has(root)) return
visitedPackages.add(root)
try {
const json = fs.readFileSync(path.resolve(path.join(root, 'package.json')), 'utf-8')
const pkg = JSON.parse(json)
for (
const [name, version] of [
...Object.entries(pkg.dependencies),
...Object.entries(pkg.devDependencies),
] as [string, string][]
) {
if (version.startsWith('file:')) {
if (!optimizeDeps.exclude!.includes(name)) {
optimizeDeps.exclude!.push(name)
const target = path.resolve(root, version.replace('file:', ''))
excludeLinks(target)
}
}
}
} catch {}
}
excludeLinks(root) |
That's sounds great. |
@AntoineParent You need to pass the |
Describe the bug
Reproduction instructions
my-test-project
directory that has a boilerplatepackage.json
to use)turbo
under the hood for installing packages and I can't figure out how to do the local linking which is essential the reproduction.yarn link
->ERR!: Unknown command 'link'
yarn
yarn add my-test-dependency
by runningcp -R my-test-dependency node_modules/my-test-dependency
yarn dev
(this will create pre-bundled dependencies formy-test-dependency
and populatenode_modules/.vite
)my-test-dependency asdf
log in the devtools consolemy-test-dependency
ready for linking:cd my-test-dependency && yarn link && cd ..
yarn link my-test-dependency
yarn dev
my-test-dependency asdf
log in the devtools console./my-test-dependency/index.js
to a differentconsole.log('other message')
just to show that Vite is pulling from the wrong spotThis use-case happens when you're doing some development on a project, find a problem in a sub-dependency, then link it locally to dive deeper.
Expected result
Vite would see that the dependency/package is now linked locally and will bust it's own pre-bundle cache.
Workaround
rm -rf ./node_modules/.vite
Dev notes
Relevant docs:
Reproduction
StackBlitz uses
turbo
under the hood for installing packages and I can't figure out how to do the local linking which is essential the reproduction.yarn link
->ERR!: Unknown command 'link'
. See the reproduction steps above instead ^System Info
vite@2.7.13
Used Package Manager
yarn
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: