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

import.meta.glob from node_modules caches project files incorrectly #19632

Closed
7 tasks done
SystemParadox opened this issue Mar 12, 2025 · 4 comments
Closed
7 tasks done

Comments

@SystemParadox
Copy link

SystemParadox commented Mar 12, 2025

Describe the bug

If a library in node_modules uses import.meta.glob, any files loaded by the glob are loaded using the same caching policy as the library.

For example, in [email protected]:SystemParadox/vite-cache-issue.git, the network tab of devtools shows that mylib.js and foo.svelte both use ?v=2061e277 and are both cached aggressively.

This means that if for example foo.svelte is loaded initially but then something causes a dependency reoptimisation reload, then the browser will use its cached version of foo.svelte which is incorrect.

Combined with sveltejs/vite-plugin-svelte#1032 this also results in svelte styles randomly disappearing in our application.

Reproduction

[email protected]:SystemParadox/vite-cache-issue.git

Steps to reproduce

  • Clone the linked repository
  • pnpm i
  • pnpm start
  • Visit http://localhost:5173 and observe the devtools network tab shows aggressive caching for foo.svelte

System Info

System:
    OS: Linux 6.8 Linux Mint 22 (Wilma)
    CPU: (12) x64 AMD Ryzen 5 2600 Six-Core Processor
    Memory: 33.78 GB / 46.96 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 20.18.0 - ~/.nvm/versions/node/v20.18.0/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v20.18.0/bin/npm
    pnpm: 7.33.7 - ~/.local/share/pnpm/pnpm
  Browsers:
    Chrome: 133.0.6943.126
    Chromium: 133.0.6943.126
  npmPackages:
    vite: ^6.2.0 => 6.2.1

Used Package Manager

pnpm

Logs

No response

Validations

@dominikg
Copy link
Contributor

the reproduction does not work, the mylib import is not recognized. https://stackblitz.com/github/SystemParadox/vite-cache-issue?file=src%2FApp.svelte

Please provide a reproduction that depicts the exact problem you describe and also why you believe this to be a bug in vite.

Is this in a monorepo setup or do you intend to publish a package where import.meta.glob is part of the published js output?

My initial thought on this is that import.meta.glob shouldn't work at all in node_modules, imports in dependencies shouldn't depend on a bundler and even if it did work, it should only be possible to import from declared dependencies or the packages import map, not the apps /src.

To make the lib not depend on the app, you could change the setup so that the app does the globing and passes the result into the library.

@SystemParadox
Copy link
Author

the reproduction does not work, the mylib import is not recognized. https://stackblitz.com/github/SystemParadox/vite-cache-issue?file=src%2FApp.svelte

See the start script, it copies mylib into node_modules. You need to clone this locally, it's unlikely to work in stackblitz. I couldn't think how else to demonstrate an installed library.

Please provide a reproduction that depicts the exact problem you describe and also why you believe this to be a bug in vite.

In summary, if a library imports files from your project like this:

export async function loadView(path) {
    const views = import.meta.glob('/src/views/**/*.svelte');
    const module = await views[`/src/views/${path}.svelte`]();
    return module.default;
}

And you use it like this:

<script lang="ts">
    import { loadView } from 'mylib';
</script>
{#await loadView('foo')}
    Loading...
{:then Component}
    <svelte:component this={Component} />
{/await}

(and have src/views/foo.svelte with anything in it)

Then it caches src/views/foo.svelte aggressively using the same caching as mylib which leads to problems with things not updating properly or CSS not loading correctly.

Is this in a monorepo setup or do you intend to publish a package where import.meta.glob is part of the published js output?

It's a published package but currently to a private repository for internal use only.

My initial thought on this is that import.meta.glob shouldn't work at all in node_modules, imports in dependencies shouldn't depend on a bundler and even if it did work, it should only be possible to import from declared dependencies or the packages import map, not the apps /src.

This all works and we've been using it for some time - except for the caching issue which we've only just managed to figure out.

To make the lib not depend on the app, you could change the setup so that the app does the globing and passes the result into the library.

This is less a library and more a "framework" - the framework is actually in control and calls down to the project rather than the other way around.

However, I don't mind if you want to say this isn't supported - we've already got tricks to generate boilerplate files in the project to invert the control and make it look more "normal" to vite, so we could do similar to avoid this issue.

@dominikg
Copy link
Contributor

personally i think its odd to import app code into a library/framework that way as it creates an unexpected implicit tight coupling in the wrong direction.

The optimizer for one does not expect this to happen and as such can't know if a local file matched by a glob has changed since it was last optimized. You might be able to work around this by putting mylib into optimizeDeps.exclude in vite config.

@SystemParadox
Copy link
Author

We're already using optimizeDeps.exclude so that doesn't seem to help.

I will close as unsupported.

@SystemParadox SystemParadox closed this as not planned Won't fix, can't repro, duplicate, stale Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants