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

Unable to import commonjs file with absolute path #2218

Closed
6 tasks done
danez opened this issue Oct 27, 2022 · 5 comments
Closed
6 tasks done

Unable to import commonjs file with absolute path #2218

danez opened this issue Oct 27, 2022 · 5 comments

Comments

@danez
Copy link
Contributor

danez commented Oct 27, 2022

Describe the bug

When I try to import a commonjs file with an absolute path (e.g. /tmp/something.js) vitest fails to resolve the relative dependencies of this just-imported file.

So for example if I have

// /tmp/function.js
module.exports = require('./function2.js')
// /tmp/function2.js
module.exports  = 2

and in my test I try to do

import('/tmp/function.js')

I get

Error: Cannot find module './fixture2.js'
Require stack:
- /Users/username/Projects/tests/vitest-import-absolute/tmp/fixture.js

Which is also not a real path.

When I try the same thing with ESM and mjs files it works.

Reproduction

https://github.com/danez-labs/vitest-absolute-import/

Clone and run npm test.

There are two tests, one that fails, although it should work, the other uses /@fs/ as a workaround and works, but it is not a real solution.

System Info

System:
    OS: macOS 12.6.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 329.33 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.10.0 - ~/.asdf/installs/nodejs/18.10.0/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.19.2 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Safari: 16.1
  npmPackages:
    vite: ^3.2.0 => 3.2.0 
    vitest: ^0.24.3 => 0.24.3

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

Cannot open reproduction:

The 'danez-labs/vitest-absolute-import' repository doesn't contain the 'tests/main.test.ts' path in 'main'

@danez
Copy link
Contributor Author

danez commented Oct 27, 2022

Cannot open reproduction:

Oopps, yes, I renamed the test afterwards. Here is the link to the repo: https://github.com/danez-labs/vitest-absolute-import/

@sheremet-va
Copy link
Member

sheremet-va commented Oct 28, 2022

This issue is cause by the same problem as #1864 (root is added before actual absolute path)

@sheremet-va
Copy link
Member

sheremet-va commented Nov 1, 2022

Fixed with #1868, released in 0.24.4

@danez
Copy link
Contributor Author

danez commented Nov 2, 2022

Thanks for the quick fix. I can confirm this works now.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants