Skip to content

Glob import does not refresh in SSR - minimatch vs micromatch #5511

@frandiox

Description

@frandiox

Describe the bug

The observed bug is that import.meta.glob('...') does not detect newly added files under some circumstances.
I've only seen this during SSR and using a pattern like ..../*.(js|ts). It works when using ..../*.[jt]s) instead.

I think the reason is that import.meta.glob uses fast-glob to match files, which internally uses micromatch.
However, the watcher uses minimatch instead to track new files. Apparently, the syntax of those two is different.

See this example:

const minimatch = require("minimatch")
const micromatch = require("micromatch").isMatch

const newFile = 'pages/name.server.jsx'
const pattern = 'pages/**/*.server.(jsx|tsx)'

console.log(
  minimatch(newFile, pattern), // false
  micromatch(newFile, pattern) // true
)

Related issue: #2734

Reproduction

https://stackblitz.com/edit/vitejs-vite-5mh3tc?file=entry-server.js

  1. Run npm run dev
  2. Add new JS or TS files to files directory. The view won't be refreshed automatically.
  3. Toggle the glob comments in entry-server.js.
  4. Add files again. The view should now refresh.

System Info

System:
    OS: macOS 12.0.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 3.98 GB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.5.0 - /opt/dev/sh/nvm/versions/node/v16.5.0/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 7.19.1 - /opt/dev/sh/nvm/versions/node/v16.5.0/bin/npm
    Watchman: 2021.10.18.00 - /usr/local/bin/watchman
  Browsers:
    Chrome: 95.0.4638.54
    Safari: 15.1
  npmPackages:
    vite: ^2.6.0 => 2.6.13

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions