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

Failed to load custom reporter if it's inside node_modules #2459

Closed
6 tasks done
segrey opened this issue Dec 8, 2022 · 14 comments · Fixed by #2461
Closed
6 tasks done

Failed to load custom reporter if it's inside node_modules #2459

segrey opened this issue Dec 8, 2022 · 14 comments · Fixed by #2461
Labels
duplicate This issue or pull request already exists

Comments

@segrey
Copy link
Contributor

segrey commented Dec 8, 2022

Describe the bug

If a custom reporter is located under node_modules and passed using a relative path, it cannot be loaded.
Reproduced with vitest 0.25.5
Not reproduced with vitest 0.25.4

Originally reported in WEB-58542. It's a simplified scenario of the initial issue.

Reproduction

{
  "devDependencies": {
    "vitest": "0.25.5"
  }
}
  1. Install dependencies
  2. touch ./node_modules/my-reporter.js (create an empty reporter inside node_modules)
  3. vitest --reporter ./node_modules/my-reporter.js fails
Error: Failed to load custom Reporter from ./node_modules/my-reporter.js
 ❯ loadCustomReporterModule node_modules/vitest/dist/chunk-vite-node-externalize.ca4784be.js:9194:11
 ❯ async file:/home/segrey/sandbox/vitest-example/node_modules/vitest/dist/chunk-vite-node-externalize.ca4784be.js:9207:32
 ❯ Vitest.setServer node_modules/vitest/dist/chunk-vite-node-externalize.ca4784be.js:10575:170
 ❯ configureServer node_modules/vitest/dist/chunk-vite-node-externalize.ca4784be.js:11395:11
 ❯ createServer node_modules/vite/dist/node/chunks/dep-5605cfa4.js:62117:24
 ❯ createVitest node_modules/vitest/dist/chunk-vite-node-externalize.ca4784be.js:11427:18
 ❯ startVitest node_modules/vitest/dist/chunk-vite-node-externalize.ca4784be.js:11543:15
 ❯ start node_modules/vitest/dist/cli.js:701:17

Caused by: Error: Cannot find module '/home/segrey/sandbox/vitest-example/node_modules/vitest/dist/node_modules/my-reporter.js' imported from /home/segrey/sandbox/vitest-example/node_modules/vitest/dist/chunk-vite-node-source-map.d0aa773d.js
Did you mean to import ../../../my-reporter.js?
 ❯ new NodeError node:internal/errors:393:5
 ❯ finalizeResolution node:internal/modules/esm/resolve:323:11
 ❯ moduleResolve node:internal/modules/esm/resolve:916:10
 ❯ defaultResolve node:internal/modules/esm/resolve:1124:11
 ❯ nextResolve node:internal/modules/esm/loader:163:28
 ❯ ESMLoader.resolve node:internal/modules/esm/loader:841:30
 ❯ ESMLoader.getModuleJob node:internal/modules/esm/loader:424:18
 ❯ ESMLoader.import node:internal/modules/esm/loader:525:22
 ❯ importModuleDynamically node:internal/modules/esm/translators:110:35
 ❯ importModuleDynamicallyCallback node:internal/process/esm_loader:35:14

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: {
  "code": "ERR_MODULE_NOT_FOUND",
}

System Info

System:
    OS: Linux 5.4 Linux Mint 20.3 (Una)
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 17.18 GB / 31.21 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 18.12.1 - ~/.nvm/versions/node/v18.12.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.12.1/bin/yarn
    npm: 8.19.2 - ~/.nvm/versions/node/v18.12.1/bin/npm
  Browsers:
    Chrome: 108.0.5359.98
  npmPackages:
    vitest: 0.25.5 => 0.25.5

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

Duplicate of #2455

@sheremet-va sheremet-va marked this as a duplicate of #2455 Dec 8, 2022
@sheremet-va sheremet-va added bug duplicate This issue or pull request already exists labels Dec 8, 2022
@sheremet-va
Copy link
Member

Should be fixed in 0.26.6.

@segrey
Copy link
Contributor Author

segrey commented Dec 8, 2022

@sheremet-va Thanks for the quick fix! Happy to confirm it's fixed in 0.26.6.

@shardy-lbar
Copy link

I may be missing something here, but how do I update to 0.26.6? It isnt in npm (latest there is 0.25.6) and it doesnt seem to be the name of a branch or tag here? I tried updating to 0.25.6 and that did not fix the issue.

@sheremet-va
Copy link
Member

I may be missing something here, but how do I update to 0.26.6? It isnt in npm (latest there is 0.25.6) and it doesnt seem to be the name of a branch or tag here? I tried updating to 0.25.6 and that did not fix the issue.

Sorry, I meant 0.25.6. If it doesn't work for you, please provide reproduction. The one in this issue works for me.

@shardy-lbar
Copy link

Okay thanks. I'm currently investigating another lead.

I'm trying to use vitest with the latest Webstorm, and its there reporter that its trying to load. However I just tried to cat the file (vitest-intellij-reporter-safe.js) that its using, and it doesnt appear to exist... so might be a Webstorm issue after all.

@shardy-lbar
Copy link

shardy-lbar commented Dec 8, 2022

I may be missing something here, but how do I update to 0.26.6? It isnt in npm (latest there is 0.25.6) and it doesnt seem to be the name of a branch or tag here? I tried updating to 0.25.6 and that did not fix the issue.

Sorry, I meant 0.25.6. If it doesn't work for you, please provide reproduction. The one in this issue works for me.

Okay, the issue appears to be the setting of a "root" in the vite config.

I had the root set as the "src" subfolder (in the main vite config, not the test section). When webstorm starts vitest, it passes the path to its reporter as a relative path. It seems like vitest was using the path passed in as relative to the configured root, rather than the working directory, causing it not to find the reporter.

Simple reproduction

package.json:
{ "devDependencies": { "vitest": "0.25.6" } }

Then:

  1. Install dependencies
  2. touch ./node_modules/my-reporter.js (create an empty reporter inside node_modules)
  3. vitest --reporter ./node_modules/my-reporter.js --root src fails

interestingly, providing a path relative to the "root" directory (i.e. vitest --reporter ../node_modules/my-reporter.js --root src) also fails. Using an absolute path works fine (i.e. npx vitest --reporter "$(pwd)/node_modules/my-reporter.js" --root src)

@sheremet-va
Copy link
Member

vitest --reporter ./node_modules/my-reporter.js --root src fails

I think this is expected behaviour, because every provided path is relative to root.

interestingly, providing a path relative to the "root" directory (i.e. vitest --reporter ../node_modules/my-reporter.js --root src) also fails.

This shouldn't fail, and it doesn't fail for me with your reproduction steps.

@AlmeroSteyn
Copy link

This is happening for me in 0.28.4

@tinynumbers
Copy link

I'm also seeing this in vitest version 0.28.5. Should this issue be reopened?

@sheremet-va
Copy link
Member

I'm also seeing this in vitest version 0.28.5. Should this issue be reopened?

Open a separate issue with reproduction.

@baptooo
Copy link

baptooo commented Feb 20, 2023

Hello there,

I was getting the same issue with vitest@0.28.5 and figure out the error was caused by vite-plugin-eslint, excluding the plugin for test run did the trick, if it can help 🤷

import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
import eslint from "vite-plugin-eslint";

// https://vitejs.dev/config/
export default defineConfig({
- plugins: [react(), eslint()],
+ plugins: [react(), !process.env.VITEST ? eslint() : null], 
  test: {
    globals: true,
    environment: "jsdom",
    setupFiles: "./src/tests/setup.ts",
  },
});

@alextompkins
Copy link

I believe this issue may need to be re-opened; with Vitest 0.28.1 the WebStorm custom reporter will not work if your test.root is set to anything other than __dirname of vite.config.ts.

@sheremet-va
Copy link
Member

sheremet-va commented Mar 13, 2023

I believe this issue may need to be re-opened; with Vitest 0.28.1 the WebStorm custom reporter will not work if your test.root is set to anything other than __dirname of vite.config.ts.

Create a new issue with a reproduction, please. I cannot confirm that there is an issue. Make sure your reporter path is set relative to the config root or has the correct absolute path.

@vitest-dev vitest-dev locked as resolved and limited conversation to collaborators Mar 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants