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 statements within vitest.workspace can't import .ts files using the .js extension #6519

Closed
6 tasks done
sxxov opened this issue Sep 17, 2024 · 7 comments · Fixed by #6584
Closed
6 tasks done
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@sxxov
Copy link

sxxov commented Sep 17, 2024

Describe the bug

thank you vitest team for this awesome project! i can't be happier leaving jest in the dust.

when using an import statment in vitest.workspace.ts, i can't import .ts files using the .js extension.
changing the extension to use .ts or removing the extension completely, makes the import succeed.

perculiarly, this seems to only fail if the import is inside the vitest.workspace.ts file, any further .js imports that exist within the imported modules will import successfully.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-t6tqec?file=vitest.workspace.ts

// vite.workspace.ts

import { foo } from './foo.js';
//       ^ Error: Failed to load url ./foo.js (resolved id: ./foo.js). Does the file exist?
//             at loadAndTransform (./node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:51857:17)
//             at async ViteNodeServer._transformRequest (./node_modules/vite-node/dist/server.mjs:524:16)
//             at async ViteNodeServer._fetchModule (./node_modules/vite-node/dist/server.mjs:486:17)
//             at async ViteNodeRunner.directRequest (./node_modules/vite-node/dist/client.mjs:277:46)
//             at async ViteNodeRunner.cachedRequest (./node_modules/vite-node/dist/client.mjs:206:14)
//             at async ViteNodeRunner.dependencyRequest (./node_modules/vite-node/dist/client.mjs:259:12)
//             at async C:\__Projects\Code\pawe\vitest.workspace.ts:4:31
//             at async ViteNodeRunner.runModule (./node_modules/vite-node/dist/client.mjs:399:5)
//             at async ViteNodeRunner.directRequest (./node_modules/vite-node/dist/client.mjs:381:5)
//             at async ViteNodeRunner.cachedRequest (./node_modules/vite-node/dist/client.mjs:206:14) {
//           code: 'ERR_LOAD_URL'

void foo;
// foo.ts

export foo = 'bar';

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (16) x64 AMD Ryzen 9 4900HS with Radeon Graphics
    Memory: 4.02 GB / 15.42 GB
  Binaries:
    Node: 20.11.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.21 - C:\Program Files\nodejs\yarn.CMD
    npm: 10.8.1 - ~\AppData\Roaming\npm\npm.CMD
    pnpm: 8.7.4 - ~\AppData\Roaming\npm\pnpm.CMD
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    @vitejs/plugin-react: ^4.3.1 => 4.3.1
    @vitest/browser: ^2.1.1 => 2.1.1
    vite: ^5.4.6 => 5.4.6
    vitest: ^2.1.1 => 2.1.1

Used Package Manager

npm

Validations

@hi-ogawa
Copy link
Contributor

I thought this is same as #5999, but it seems a bit different. Can you provide a reproduction? Maybe related to your tsconfig.json?

Copy link

Hello @sxxov. Please provide a minimal reproduction using a GitHub repository or StackBlitz (you can also use examples). Issues marked with needs reproduction will be closed if they have no activity within 3 days.

@sxxov
Copy link
Author

sxxov commented Sep 17, 2024

I thought this is same as #5999, but it seems a bit different. Can you provide a reproduction? Maybe related to your tsconfig.json?

@hi-ogawa

this issue is specific to vitest.workspace.ts. it's only that file, from what i can see.

& notably it's also .ts (vitest.workspace.ts) -importing-> .ts (foo.ts),
whilst #5999 is for .js -importing-> .ts.

as requested, i've also edited the issue to include the stackblitz link forked from the default vitest template, with the two files above (without touching anything else, i.e. using the provided tsconfig.json).

hope this helps!

@hi-ogawa
Copy link
Contributor

Thanks for the repro. I thought the issue might be tsconfig include not covering vitest.workspace.ts, but changing it doesn't seem to matter.

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Sep 17, 2024

It looks like this inconsistency is happening because Vitest is processing vitest.workspace.ts during Vite's configureServer plugin hook, which is technically before the complete server initialization, so probably Vite is not able to handle resolution properly.

Here is a repro with just Vite showing the difference of transformRequest during and after configureServer hook https://github.com/hi-ogawa/reproductions/tree/main/vitest-6519-transformRequest-during-configureServer

Not sure if this is something we can fix, but putting a bug label for now.

@hi-ogawa hi-ogawa added p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Sep 17, 2024
@hi-ogawa
Copy link
Contributor

hi-ogawa commented Sep 18, 2024

Digging this further, I found that Vite's import analysis (which replaces import of .js with .ts) is not happening since they also need configureServer hook to be called https://github.com/vitejs/vite/blob/f9691767ad763720065cc7c5c7f369f97b4e7ea8/packages/vite/src/node/plugins/importAnalysis.ts#L226

Interestingly this is not the case in Vite 6 since it can grab this.environment during transform hook without waiting for configureServer https://github.com/vitejs/vite/blob/ba56cf43b5480f8519349f7d7fe60718e9af5f1a/packages/vite/src/node/plugins/importAnalysis.ts#L258 and the same reproduction actually works with Vite 6 overrides https://stackblitz.com/edit/vitest-dev-vitest-oh5jyw?file=package.json

@sheremet-va
Copy link
Member

Digging this further, I found that Vite's import analysis (which replaces import of .js with .ts) is not happening since they also need configureServer hook to be called vitejs/vite@f969176/packages/vite/src/node/plugins/importAnalysis.ts#L226

Interestingly this is not the case in Vite 6 since it can grab this.environment during transform hook without waiting for configureServer vitejs/vite@ba56cf4/packages/vite/src/node/plugins/importAnalysis.ts#L258 and the same reproduction actually works with Vite 6 overrides stackblitz.com/edit/vitest-dev-vitest-oh5jyw?file=package.json

Can't we just move Vitest's configureServer to order: 'post'? 🤔

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
3 participants