mlly icon indicating copy to clipboard operation
mlly copied to clipboard

doesn't work with yarn pnp and esm-only modules with `import` condition

Open danieltroger opened this issue 1 year ago • 6 comments

Environment

Node 21.5.0, yarn 4.0.2, macOS 14.2.1 (23C71)

Reproduction

  1. git clone [email protected]:danieltroger/vinxi-repro.git
  2. cd vinxi-repro
  3. yarn
  4. yarn build

Describe the bug

Using yarn pnp (please see https://yarnpkg.com/features/pnp) modules have to be resolved very differently, since node_modules is a concept of the past there - instead, we have de-duplicated, compressed .zip files containing our dependencies.

It seems like mlly is a re-implementation of node-js' module resolution algorithm that has never heard about yarn berry. And because yarn berry doesn't patch mlly, it just explodes when being ran with yarn berry.

Additional context

I really need yarn berrys workspace, package deduplication, patching and linking features and I updated a project to the new solid-start to which mlly is a peerDependency. I'd suggest you to just re-export the working import.meta.resolve when detecting yarn berry since this project seems redundant to me today.

Logs

[vinxi 4:15:41 PM]  ERROR  Error: Error: Cannot find module vinxi/lib/invariant imported from file:///private/tmp/2/vinxi-repro/node_modules, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/nitropack/node_modules, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules, file:///private/tmp/2/vinxi-repro/, file:///private/tmp/2/vinxi-repro/node_modules/_index.js, file:///private/tmp/2/vinxi-repro/node_modules, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/nitropack/, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/nitropack/node_modules/_index.js, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/nitropack/node_modules, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/_index.js, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules


undefined


[4:15:41 PM]  ERROR  Error: Cannot find module vinxi/lib/invariant imported from file:///private/tmp/2/vinxi-repro/node_modules, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/nitropack/node_modules, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules, file:///private/tmp/2/vinxi-repro/, file:///private/tmp/2/vinxi-repro/node_modules/_index.js, file:///private/tmp/2/vinxi-repro/node_modules, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/nitropack/, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/nitropack/node_modules/_index.js, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/nitropack/node_modules, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/_index.js, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules

  at resolvePath (.yarn/cache/mlly-npm-1.4.2-75e10242a0-905e3a704c.zip/node_modules/mlly/dist/index.mjs:2074:10)
  at _resolve (.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/nitropack/dist/shared/nitro.4ea992bc.mjs:534:22)
  at Object.resolveId (.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/nitropack/dist/shared/nitro.4ea992bc.mjs:580:34)
  at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  at async PluginDriver.hookFirstAndGetPlugin (.yarn/cache/rollup-npm-4.9.2-c65ac1d4a3-c9cd2bd09c.zip/node_modules/rollup/dist/es/shared/node-entry.js:18542:28)
  at async resolveId (.yarn/cache/rollup-npm-4.9.2-c65ac1d4a3-c9cd2bd09c.zip/node_modules/rollup/dist/es/shared/node-entry.js:17211:26)
  at async ModuleLoader.resolveId (.yarn/cache/rollup-npm-4.9.2-c65ac1d4a3-c9cd2bd09c.zip/node_modules/rollup/dist/es/shared/node-entry.js:17625:15)
  at async Object.resolveId (.yarn/__virtual__/@rollup-plugin-commonjs-virtual-0deef3e5eb/0/cache/@rollup-plugin-commonjs-npm-25.0.7-f5b19139ea-d096af5aed.zip/node_modules/@rollup/plugin-commonjs/dist/es/index.js:588:10)
  at async PluginDriver.hookFirstAndGetPlugin (.yarn/cache/rollup-npm-4.9.2-c65ac1d4a3-c9cd2bd09c.zip/node_modules/rollup/dist/es/shared/node-entry.js:18542:28)
  at async resolveId (.yarn/cache/rollup-npm-4.9.2-c65ac1d4a3-c9cd2bd09c.zip/node_modules/rollup/dist/es/shared/node-entry.js:17211:26)
  at async ModuleLoader.resolveId (.yarn/cache/rollup-npm-4.9.2-c65ac1d4a3-c9cd2bd09c.zip/node_modules/rollup/dist/es/shared/node-entry.js:17625:15)
  at async .yarn/cache/rollup-npm-4.9.2-c65ac1d4a3-c9cd2bd09c.zip/node_modules/rollup/dist/es/shared/node-entry.js:17932:50 



[4:15:41 PM]  ERROR  Error: Cannot find module vinxi/lib/invariant imported from file:///private/tmp/2/vinxi-repro/node_modules, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/nitropack/node_modules, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules, file:///private/tmp/2/vinxi-repro/, file:///private/tmp/2/vinxi-repro/node_modules/_index.js, file:///private/tmp/2/vinxi-repro/node_modules, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/nitropack/, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/nitropack/node_modules/_index.js, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/nitropack/node_modules, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules/_index.js, file:///private/tmp/2/vinxi-repro/.yarn/__virtual__/nitropack-virtual-847b2d16a3/0/cache/nitropack-npm-2.8.1-662ce278d2-61738b56fd.zip/node_modules

danieltroger avatar Jan 16 '24 15:01 danieltroger

Investigating such issues with higher-order metaframeworks is time-consuming. I appreciate it if you could make a minimal reproduction (with mlly utils only). closing in the meantime.

pi0 avatar Jan 16 '24 16:01 pi0

Minimal reproduction:

  1. unzip mlly.zip
  2. yarn
  3. yarn start

Output should look like this:

Native resolution works: file:///Users/daniel/.yarn/berry/cache/vinxi-npm-0.1.4-662cb08e69-10c0.zip/node_modules/vinxi/lib/invariant.js
Observe mlly crash below



file:///Users/daniel/.yarn/berry/cache/mlly-npm-1.5.0-dc7923db5f-10c0.zip/node_modules/mlly/dist/index.mjs:2121
  return pcall(resolvePathSync, id, options);
         ^

Error: Error: Cannot find module vinxi/lib/invariant imported from file:///private/tmp/vinxi-repro/node_modules, file:///private/tmp/vinxi-repro/.yarn/unplugged/nitropack-virtual-847b2d16a3/node_modules/nitropack/node_modules, file:///private/tmp/vinxi-repro/.yarn/unplugged/nitropack-virtual-847b2d16a3/node_modules, file:///private/tmp/vinxi-repro/, file:///private/tmp/vinxi-repro/node_modules/_index.js, file:///private/tmp/vinxi-repro/node_modules, file:///private/tmp/vinxi-repro/.yarn/unplugged/nitropack-virtual-847b2d16a3/node_modules/nitropack/, file:///private/tmp/vinxi-repro/.yarn/unplugged/nitropack-virtual-847b2d16a3/node_modules/nitropack/node_modules/_index.js, file:///private/tmp/vinxi-repro/.yarn/unplugged/nitropack-virtual-847b2d16a3/node_modules/nitropack/node_modules, file:///private/tmp/vinxi-repro/.yarn/unplugged/nitropack-virtual-847b2d16a3/, file:///private/tmp/vinxi-repro/.yarn/unplugged/nitropack-virtual-847b2d16a3/node_modules/_index.js, file:///private/tmp/vinxi-repro/.yarn/unplugged/nitropack-virtual-847b2d16a3/node_modules
    at resolvePath (file:///Users/daniel/.yarn/berry/cache/mlly-npm-1.5.0-dc7923db5f-10c0.zip/node_modules/mlly/dist/index.mjs:2121:10)
    at file:///private/tmp/mlly/index.js:10:1
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:120:12) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v21.5.0

danieltroger avatar Jan 16 '24 18:01 danieltroger

Sorry I included some paths from where I reproduced the issue as part of solid-start in above, but the same error can be observed without them with just

resolvePath("vinxi/lib/invariant", {
  conditions: ["production", "node", "import", "default"]
});

danieltroger avatar Jan 16 '24 18:01 danieltroger

Thanks for the reproduction.

I think same as https://github.com/unjs/jiti/issues/204#issuecomment-1894357666 for jiti, for mlly we can leverage import.meta.resolve (that yarn pnp provides) if available but only have to make sure result is not promise (older implementations do this) and if is promise, discard it for sync methods.

Faster solution for module authors is using type: "module" + "default" condition.

pi0 avatar Jan 16 '24 19:01 pi0

Hey @pi0! Hope you're well :). I'm invested in making this work, so we can use yarn pnp with unplugin-icons (see also https://github.com/unplugin/unplugin-icons/issues/358).

I've made an early draft of a fix at https://github.com/unjs/mlly/pull/270. The code isn't well organized or anything; I'm looking for feedback on approach, where to put the safe resolve call, etc, before I adjust it more. Could you take a look?

I've tested the fix on both my own code and the minimal repro above; it fixes both. However, note that I tested only using a yarn patch -- I can do that once we've got the structure down, as final QA before merge.

bmulholland avatar Jul 26 '24 09:07 bmulholland

I'm experiencing the exact same issue with TanStack Start + Yarn PnP. Here's my setup and error:

Environment:

  • Node 22.13.1
  • Yarn 4.9.2 (PnP mode)
  • TanStack Start 1.122.1
  • macOS 23.1.0

Error:

Error: ENOTDIR: not a directory, open '/...../.yarn/unplugged/nitropack-virtual-388efa10d3/node_modules/nitropack/dist/core/index.mjs/package.json'

Would love to see mlly add proper Yarn PnP support or at least detect Yarn PnP and fall back to a compatible resolution strategy.

gnujoow avatar Jun 29 '25 09:06 gnujoow