question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pre-Bundled dependencies used over locally linked dependency when built before linking

See original GitHub issue

Describe the bug

Reproduction instructions

  1. Download https://stackblitz.com/edit/vitejs-vite-9ibgrz (just a base Vite project with a my-test-project directory that has a boilerplate package.json to use)
    • 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'
  2. yarn
  3. Fake a yarn add my-test-dependency by running cp -R my-test-dependency node_modules/my-test-dependency
  4. yarn dev (this will create pre-bundled dependencies for my-test-dependency and populate node_modules/.vite)
  5. Notice my-test-dependency asdf log in the devtools console
  6. Get my-test-dependency ready for linking: cd my-test-dependency && yarn link && cd ..
  7. Locally link it in your project: yarn link my-test-dependency
  8. yarn dev
  9. Notice my-test-dependency asdf log in the devtools console
  10. Edit ./my-test-dependency/index.js to a different console.log('other message') just to show that Vite is pulling from the wrong spot
  11. Notice how the log message never changes to what you set

This 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

  1. Clear your pre-bundled dependency build cache rm -rf ./node_modules/.vite
  2. Notice how Vite is correctly bundling your locally linked package now and has the updated message

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
npx envinfo --system --npmPackages '{vite,@vitejs/*}' --binaries --browsers
npx: installed 1 in 2.131s

  System:
    OS: Windows 10 10.0.19044
  Binaries:
    Node: 14.16.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.17 - C:\Program Files\nodejs\yarn.CMD
    npm: 6.14.11 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 97.0.4692.99

Used Package Manager

yarn

Logs

No response

Validations

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
stagascommented, May 9, 2022

That’s sounds great. I experience same issue… If it works, you definitely save my day ! Where can i add this code ?

@AntoineParent You need to pass the root from your config and have an optimizeDeps = { exclude: [] } object defined earlier so that it is populated. Then you pass them to your Vite config however you run it. See the Vite config docs for more details. Note that it might not solve for every case, it currently solves mine which is symlinking packages i own (using npm link pkg-name --save so that they get a file: url in the dependencies) and then having also dependencies that use the npm versions of those packages, work side-by-side, as they’ll both use their own version when they’re excluded from optimizeDeps.

1reaction
stagascommented, May 8, 2022

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 optimizeDeps.exclude:

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)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency Pre-Bundling - Vite
Vite automatically detects dependencies that are not resolved from node_modules and treats the linked dep as source code. It will not attempt to...
Read more >
Using Vite with linked dependencies - DEV Community ‍ ‍
I start by linking my fork of Svelte and run it in dev mode so it will automatically update. # svelte-fork npm link...
Read more >
How to use local Node packages as project dependencies
Test packages that have not been published to a registry, without getting caught in the pitfalls of npm and yarn's built-in solutions.
Read more >
Index · Dependency scanning · Application security · User · Help ...
To identify pre-bundled dependencies, enable Container Scanning language ... To run dependency scanning jobs, by default, you need GitLab Runner with the ...
Read more >
Learning the Basics - Gradle User Manual
Gradle has built-in support for dependency management and lives up to the task ... For the use case of overriding remote artifacts with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found