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

instanceof RegExp check fails when testing vite-node runner in Jest with Vite 6.x #19245

Closed
7 tasks done
askoufis opened this issue Jan 21, 2025 · 3 comments · Fixed by #19247
Closed
7 tasks done

instanceof RegExp check fails when testing vite-node runner in Jest with Vite 6.x #19245

askoufis opened this issue Jan 21, 2025 · 3 comments · Fixed by #19247

Comments

@askoufis
Copy link
Contributor

askoufis commented Jan 21, 2025

Describe the bug

I'm a maintainer of Vanilla Extract. We use vite-node to execute files for the purpose of generating classnames and eventually CSS. With the release of vitest v3, I was keen to upgrade both vite-node to v3 and our vite peer dep to include v6. However, while attempting to update I noticed that some of our tests began failing.

The failing tests are related to our vite-node-based compiler. We test the compiler in both vitest and jest, mainly to be thorough, and our tests are only failing in jest. vitest tests are passing.

After some debugging, my understanding of what's causing the error in the reproduction is as follows:

  1. Vite creates some default build options, specifically this /node_modules/ regex literal specified in dynamicImportVarsOptions
  2. These default options are eventually merged with user-provided options using vite's deepClone implementation. The regex literal is cloned via the native structuredClone (I don't think structuredClone is being polyfilled, but happy to be proven wrong).
  3. This cloned config eventually makes its way to @rollup/plugin-dynamic-import-vars. This plugin calls this createFilter function which contains an instanceof RegExp check.
  4. The instanceof RegExp check fails against the cloned /node_modules/ regex literal, resulting in an incorrect code branch being taken, eventually resulting in this isAbsolute call being passed a regex literal, causing the error.

As mentioned below in the reproduction, this error does not occur on vite 5.x. At this point I'm unsure if the issue is prototype pollution, or maybe some kind of multiple realm-related issue.

Another theory I had is that this is somehow related to the new environments feature because the code that accesses dynamicImportVarsOptions and calls createFilter was changed as part of that feature implementation, but that's really just speculation on my end.

Workaround

Passing in my own /node_modules/ regex literal when calling createServer results in the test passing. E.g.:

const server = await createServer({
  optimizeDeps: {
    noDiscovery: true,
    include: undefined,
  },
  build: {
    dynamicImportVarsOptions: {
      exclude: [/node_modules/],
    },
  },
});

Impact

AFAIK most (if not all) consumers of Vanilla Extract would not be testing this compiler directly, nor would they likely test any bundler integrations that depend on the compiler, so there's likely no impact to consumers.

I appreciate that this is a bit of a niche/strange use case (testing vite-node in jest), so I'd completely understand if the ultimate decision is that this issue will not be fixed.

Reproduction

https://github.com/askoufis/vite-node-issue

Steps to reproduce

git clone [email protected]:askoufis/vite-node-issue.git
cd vite-node-issue
pnpm install
pnpm test

Test should fail with the error:

TypeError: The "path" argument must be of type string. Received an instance of RegExp

Downgrading vite to a 5.x version (e.g. 5.4.13) makes the test pass. Downgrading vite-node has no effect.

System Info

System:
    OS: macOS 15.1.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 2.95 GB / 32.00 GB
    Shell: 3.7.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 20.9.0 - ~/.local/share/mise/installs/node/20.9.0/bin/node
    npm: 10.1.0 - ~/.local/share/mise/installs/node/20.9.0/bin/npm
    pnpm: 9.15.4 - ~/.local/share/mise/installs/node/20.9.0/bin/pnpm
    bun: 1.1.43 - /opt/homebrew/bin/bun
    Watchman: 2024.12.02.00 - /opt/homebrew/bin/watchman
  Browsers:
    Safari: 18.1.1
  npmPackages:
    vite: ^6.0.10 => 6.0.10

Used Package Manager

pnpm

Logs

No response

Validations

@sapphi-red
Copy link
Member

This was reported in #18875. See #18875 (comment).
I'm still open to changing the structuredClone line, feel free to open a PR 🙂Probably new RegExp(regexp) would work.

@askoufis
Copy link
Contributor Author

This was reported in #18875. See #18875 (comment). I'm still open to changing the structuredClone line, feel free to open a PR 🙂Probably new RegExp(regexp) would work.

Oh my bad, I completely forgot to search closed issues as well as open issues 🤦. new RegExp does indeed fix the issue. I'll make a PR.

@askoufis
Copy link
Contributor Author

@sapphi-red Made a PR #19247.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants