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

ssr: Nested built-in dependencies of ESM dependencies can't be resolved #4700

Closed
7 tasks done
jplhomer opened this issue Aug 23, 2021 · 2 comments · Fixed by #4904 or #5017
Closed
7 tasks done

ssr: Nested built-in dependencies of ESM dependencies can't be resolved #4700

jplhomer opened this issue Aug 23, 2021 · 2 comments · Fixed by #4904 or #5017

Comments

@jplhomer
Copy link
Contributor

jplhomer commented Aug 23, 2021

Describe the bug

When I attempt to load an entrypoint with ssrLoadModule which references an ESM dependency e.g. https://github.com/jplhomer/repro-ssr-pkg, there is an esbuild error indicating the built-in dependency cannot be loaded.

This only happens during the optimize missing deps phase of the dev server.

Potential similar issues, but slightly different:

Reproduction

https://github.com/jplhomer/repro-ssr-modules

System Info

System:
    OS: macOS 11.5.2
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 254.77 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.9.0 - ~/.nvm/versions/node/v15.9.0/bin/node
    Yarn: 1.22.11 - ~/.nvm/versions/node/v15.9.0/bin/yarn
    npm: 7.5.3 - ~/.nvm/versions/node/v15.9.0/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Browsers:
    Chrome: 92.0.4515.159
    Firefox: 86.0
    Safari: 14.1.2
  npmPackages:
    vite: ^2.5.0 => 2.5.0

Used Package Manager

yarn

Logs

2:26:32 PM [vite] new dependencies found: repro-ssr-pkg, updating...
 > node_modules/react-dom/cjs/react-dom-server.node.development.js:18:21: error: Could not read from file: /Users/joshlarson/src/github.com/jplhomer/repro-ssr-modules/stream
    18 │ var stream = require('stream');~~~~~~~~

2:26:32 PM [vite] error while updating dependencies:
Error: Build failed with 1 error:
node_modules/react-dom/cjs/react-dom-server.node.development.js:18:21: error: Could not read from file: /Users/joshlarson/src/github.com/jplhomer/repro-ssr-modules/stream
    at failureErrorWithLog (/Users/joshlarson/src/github.com/jplhomer/repro-ssr-modules/node_modules/esbuild/lib/main.js:1449:15)
    at /Users/joshlarson/src/github.com/jplhomer/repro-ssr-modules/node_modules/esbuild/lib/main.js:1131:28
    at runOnEndCallbacks (/Users/joshlarson/src/github.com/jplhomer/repro-ssr-modules/node_modules/esbuild/lib/main.js:921:63)
    at buildResponseToResult (/Users/joshlarson/src/github.com/jplhomer/repro-ssr-modules/node_modules/esbuild/lib/main.js:1129:7)
    at /Users/joshlarson/src/github.com/jplhomer/repro-ssr-modules/node_modules/esbuild/lib/main.js:1236:14
    at /Users/joshlarson/src/github.com/jplhomer/repro-ssr-modules/node_modules/esbuild/lib/main.js:609:9
    at handleIncomingPacket (/Users/joshlarson/src/github.com/jplhomer/repro-ssr-modules/node_modules/esbuild/lib/main.js:706:9)
    at Socket.readFromStdout (/Users/joshlarson/src/github.com/jplhomer/repro-ssr-modules/node_modules/esbuild/lib/main.js:576:7)
    at Socket.emit (node:events:378:20)
    at Socket.EventEmitter.emit (node:domain:470:12)

Validations

@ygj6
Copy link
Member

ygj6 commented Sep 11, 2021

This error occurs because esbuildDepPlugin incorrectly resolve the built-in module:

// use vite's own resolver
const resolved = await resolve(id, importer, kind)
if (resolved) {
if (resolved.startsWith(browserExternalId)) {
return {
path: id,
namespace: 'browser-external'
}
}
if (isExternalUrl(resolved)) {
return {
path: resolved,
external: true
}
}
return {
path: path.resolve(resolved)
}
}

Vite's own resolver will return the built-in module id, the id will be resolved by path.resolve(resolve), and the result is:
/project/path/built-in-module-id

The resolved content is wrong, we need to externalize the built-in module during SSR, modify the source code:

  if (isExternalUrl(resolved) || (ssr && isBuiltin(id))) {
    return {
      path: resolved,
      external: true
    }
  }

So the esbuild will resolve the correct content. I will submit a pull request later.

@jplhomer
Copy link
Contributor Author

Thanks, @ygj6. You're a hero!

@github-actions github-actions bot locked and limited conversation to collaborators Sep 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
2 participants