Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/vite-node/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ export class ViteNodeRunner {

const modulePath = cleanUrl(moduleId)
// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
const href = pathToFileURL(modulePath).href
const [moduleUrl] = await this.resolveUrl(modulePath)
const href = pathToFileURL(moduleUrl).href
Comment on lines +292 to +293
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When an aliased modules is loaded the fsPath doesn't contain extension at all. @sheremet-va
should I make this change to fsPath instead so that everywhere it's used would be affected, e.g. stacktraces I think?

With alias, without PR changes:

import { addThreeNumbers } from "src/logic/utils";

{
  __filename: '/x/y/repro/src/logic/utils',
  id: 'src/logic/utils',
  fsPath: 'src/logic/utils'
}

Without alias, without PR changes, works OK:

import { addThreeNumbers } from "../../logic/utils";

{
  __filename: '/x/y/repro/src/logic/utils.ts',
  id: '/src/logic/utils.ts',
  fsPath: '/x/y/repro/src/logic/utils.ts'
}

With alias, with PR changes:

import { addThreeNumbers } from "src/logic/utils";

{
  __filename: '/x/y/repro/src/logic/utils.ts',
  id: 'src/logic/utils',
  fsPath: 'src/logic/utils'
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not even be allowed. Vitest should fail if the module is not resolved. I am coming up with a fix for this. Some people rely on tsconfig resolution when in practice we don't support it.

Copy link
Member Author

@AriPerkkio AriPerkkio May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, the reproduction case is not using alias in vitest.config.ts. By adding following the coverage works OK:

    alias: {
      src: fileURLToPath(new URL("./src", import.meta.url)),
    },

So I guess in your fix Vitest will fail to load module (or warn?) if only tsconfig.json has alias but vitest.config.ts does not?

Copy link
Member

@sheremet-va sheremet-va May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can finally stop supporting this weird edge case: #3307

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I guess in your fix Vitest will fail to load module (or warn?) if only tsconfig.json has alias but vitest.config.ts does not?

People rely on the fact that their modules are relative to the baseDir which is never true in Vite. Because of this, we have a lot of issues when the file is not resolved correctly, but it can still fetch module code.

const meta = { url: href }
const exports = Object.create(null)
Object.defineProperty(exports, Symbol.toStringTag, {
Expand Down