vite: virtual file system is broken in v6
Describe the bug
import { createServer } from 'vite'
const server = await createServer({
plugins: [
{
name: 'virtual-fs',
resolveId (id) {
if (id === 'my-virtual-file') {
return id + '.js'
}
},
load (id) {
if (id === 'my-virtual-file.js') {
return 'export default "Hello, world!"'
}
}
}
]
})
console.log(await server.ssrLoadModule('my-virtual-file.js'))
await server.close()
This is working in v5, but broken in v6.
Reproduction
https://stackblitz.com/edit/stackblitz-starters-prwdrf?file=index.js
Steps to reproduce
No response
System Info
N/A
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 16 (1 by maintainers)
Probably your reproduction was misleading and the issue might be more like this scenario:
On Vite 6,
resolveIdis called differently and maybe that’s causing an issue.This seems like an odd case, but not sure. Let me re-open the issue for now.
@himself65 Other than switching to
virtual:, you might be able to workaround by removingrootfrom entries https://github.com/dai-shi/waku/blob/711be51c02eac6a767fe5651fae6d6417d0679f8/packages/waku/src/lib/plugins/vite-plugin-rsc-managed.ts#L55-L56 (for example, use/src/entriesinstead of/abs-path-to/src/entries).The issue (on Vite, fair to say) is that it stripes
rootas a part of normalization here, so your plugin’sresolveIddoesn’t consistently seeroot. https://github.com/vitejs/vite/blob/93b03b2692cc2617570f83e365606dae0a8ed859/packages/vite/src/module-runner/runner.ts#L223@himself65 I haven’t look into the issue, but I would probably try this to provide “optionally virtual entry” from plugin: https://stackblitz.com/edit/stackblitz-starters-t9hvyx?file=index.js
I don’t see why this even worked before, to be honest. The
my-virtual-file.jsin the reproduction is the resolved ID, not the user ID that would be used in an import. For example, this fails in Vite 5 and Vite 6:Reproduction: https://stackblitz.com/edit/stackblitz-starters-a9fxsa?file=actual-fs.js,index.js
This however works as intended (if you provide
resolveIdhook like in the reproduction):I’d say making the entry point work the same way as the
importis a bug fix.I suppose minimal repro is just
loadhook:On v5 https://stackblitz.com/edit/stackblitz-starters-cr4to5?file=index.js
On v6 https://stackblitz.com/edit/stackblitz-starters-asgtkf?file=index.js