Skip to content

webpack/rspack: virtual modules fail with ENOENT when the build context goes through a symlink #640

Open

Description

@AronSoto

Environment

  • unplugin 3.7.0
  • webpack 5.112.2, @rspack/core 2.2.9
  • Node.js 22
  • Any OS: reproduced on Linux, macOS (any project under /var, which links to /private/var) and Windows (through a junction)

Reproduction

repro.mjs, after npm i unplugin@3.7.0 webpack@5:

import { mkdirSync, symlinkSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { createUnplugin } from 'unplugin'
import webpack from 'webpack'

const real = join(import.meta.dirname, 'real')
const link = join(import.meta.dirname, 'link')
mkdirSync(real, { recursive: true })
try { symlinkSync(real, link, 'junction') } catch {}
writeFileSync(join(real, 'entry.js'), `import msg from 'virtual-msg'\nconsole.log(msg)\n`)

const plugin = createUnplugin(() => ({
  name: 'repro',
  resolveId: id => id === 'virtual-msg' ? '\0virtual-msg' : undefined,
  load: id => id === '\0virtual-msg' ? `export default 'hello'` : undefined,
}))

// `real` builds fine, `link` (the same folder through a symlink) fails with ENOENT
const context = process.argv[2] === 'link' ? link : real
webpack({
  mode: 'development',
  context,
  entry: './entry.js',
  output: { path: join(real, 'dist') },
  plugins: [plugin.webpack()],
}, (error, stats) => {
  console.log(error ?? (stats.toString({ all: false, errors: true }) || 'build ok'))
})
node repro.mjs real   # build ok
node repro.mjs link   # ENOENT

The same failure on the repo's own CI, on Ubuntu, Windows and macOS, with a test that builds through a symlinked context: https://github.com/AronSoto/unplugin/actions/runs/41587601440

Describe the bug

When the context of a webpack or rspack build goes through a symlink, every virtual module fails to build with ENOENT. This happens to any project under macOS's tmpdir (/var is a link to /private/var), and to a project in a linked folder (or a Windows junction) on any OS.

The virtual module prefix is built on compiler.options.context as given, so the virtual module is written under the link. Both resolvers follow symlinks (resolve.symlinks is on by default), so the module is then read from the real path, where nothing was written.

I have a PR ready with a fix and tests: it builds the prefix on the real path of the context in both adapters.

Additional context

The suite already works around this: virtual-cache.test.ts and escaped-path.test.ts run realpathSync on their temp dirs because webpack resolves to /private/var.

rspack fails the same way, on <real>/node_modules/.virtual/....

Logs

ERROR in ../real/_virtual_%00virtual-msg
Module build failed (from ../node_modules/unplugin/dist/webpack/loaders/load.mjs):
Error: ENOENT: no such file or directory, open '<repro>/real/_virtual_%00virtual-msg'

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions