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

[Bug?]: Dynamic import does not use the PnP API when importing a ESM from CommonJS #4045

Closed
1 task done
flying-sheep opened this issue Jan 31, 2022 · 5 comments
Closed
1 task done
Labels
bug Something isn't working esm

Comments

@flying-sheep
Copy link
Contributor

flying-sheep commented Jan 31, 2022

Self-service

  • I'd be willing to implement a fix

Describe the bug

Trying to use the https://github.com/xojs/vscode-linter-xo extension with yarn 2, I ran into problems in the past, see #3708. With nodejs 17.4, one blocker is gone, but it doesn’t work yet:

The yarn PNP loader doesn’t seem to be used when a dynamic import from a commonjs module imports a ESM like in this dynamic import:

const { Files } = require('vscode-languageserver/node')
const { URI } = require('vscode-uri')

async function main() {
	const xoFilePath = await Files.resolve('xo', undefined, '.')
	const xoUri = URI.file(xoFilePath).toString()
	await import(xoUri)
}

main()

To reproduce

Sadly I don’t think reproducing using sherlock is possible, as this requires running node with NODE_OPTIONS set, and sherlock doesn’t seem to support that. I made a reproducer in https://github.com/flying-sheep/repro-yarn-xo

  1. git clone https://github.com/flying-sheep/repro-yarn-xo.git && cd repro-yarn-xo

  2. yarn start (i.e. bash .yarn/sdks/xo/node.sh import-xo.js)

  3. See the following error:

    node:fs:1538
      handleErrorFromBinding(ctx);
      ^
    
    Error: ENOTDIR: not a directory, stat '/home/phil/Dev/Web/repro-yarn-xo/.yarn/cache/xo-npm-0.47.0-3e655742d0-5bd4d72322.zip/node_modules/xo/index.js'
        at statSync (node:fs:1538:3)
        at tryStatSync (node:internal/modules/esm/resolve:160:13)
        at finalizeResolution (node:internal/modules/esm/resolve:387:17)
        at moduleResolve (node:internal/modules/esm/resolve:944:10)
        at defaultResolve (node:internal/modules/esm/resolve:1041:11)
        at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
        at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
        at ESMLoader.import (node:internal/modules/esm/loader:332:22)
        at importModuleDynamically (node:internal/modules/cjs/loader:1036:29)
        at importModuleDynamicallyWrapper (node:internal/vm/module:437:21) {
      errno: -20,
      syscall: 'stat',
      code: 'ENOTDIR',
      path: '/home/phil/Dev/Web/repro-yarn-xo/.yarn/cache/xo-npm-0.47.0-3e655742d0-5bd4d72322.zip/node_modules/xo/index.js'
    }
    
    Node.js v17.4.0
    

Environment

System:
    OS: Linux 5.16 Arch Linux
    CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor
  Binaries:
    Node: 17.4.0 - /tmp/xfs-b43fe39f/node
    Yarn: 3.1.1 / 3.2.0-rc2 - /tmp/xfs-b43fe39f/yarn
    npm: 8.4.0 - /usr/bin/npm

Additional context

No response

@flying-sheep flying-sheep added the bug Something isn't working label Jan 31, 2022
@flying-sheep flying-sheep changed the title [Bug?]: import() doesn’t use the PNP loader when called from a VS Code extension [Bug?]: import() doesn’t use the PNP loader when called from CommonJS Jan 31, 2022
@merceyz merceyz changed the title [Bug?]: import() doesn’t use the PNP loader when called from CommonJS [Bug?]: The SDK folder can't use the PnP API Jan 31, 2022
@merceyz
Copy link
Member

merceyz commented Jan 31, 2022

It's because the PnP linker explicitly lists .yarn/sdks/** as a path it shouldn't control

const ignorePattern = miscUtils.buildIgnorePattern([`.yarn/sdks/**`, ...this.opts.project.configuration.get(`pnpIgnorePatterns`)]);

The solution would be to remove it but maybe @arcanis remembers why its there

@flying-sheep
Copy link
Contributor Author

flying-sheep commented Jan 31, 2022

That’s not true, the script within .yarn/sdks is a bash script, there’s no JS code in there. I just tried it out by moving the bash script out of .yarn/sdks: It doesn’t prevent the error.

I think the problem really has to do with node’s import behavior, as the error only happens when trying to dynamically import xo (a ESM) from commonjs. Using await import(xoUri) from another ESM works fine.

Since the SDK folder thing is demonstrably unrelated to this, I took the liberty to change the title back.

@flying-sheep flying-sheep changed the title [Bug?]: The SDK folder can't use the PnP API [Bug?]: Dynamic import does not use the PnP API when importing a ESM from CommonJS Jan 31, 2022
@arcanis
Copy link
Member

arcanis commented Jan 31, 2022

The solution would be to remove it but maybe @arcanis remembers why its there

Was added in #646; I don't find a reference to NODE_PATH anymore, so perhaps this isn't needed anymore?

@merceyz
Copy link
Member

merceyz commented Jan 31, 2022

Ah, I was looking at the repro from #3708 sorry about that.

This was fixed in #3667 so this is a duplicate of #3687

yarn set version canary && yarn

@merceyz merceyz closed this as completed Jan 31, 2022
@merceyz merceyz added the esm label Jan 31, 2022
@flying-sheep
Copy link
Contributor Author

flying-sheep commented Jan 31, 2022

Thanks! I actually did yarn set version canary but forgot to do yarn after before trying.

And forgot to search for CJS in the issues in addition to commonjs 🤦

PhilippBaschke added a commit to PhilippBaschke/cv that referenced this issue Mar 4, 2022
The XO VSCode doesn't work with yarn PNP out of the box. Someone found
a solution by createing a patched node runtime that can be used to run
XO.
xojs/vscode-linter-xo#71
flying-sheep/react-color-scheme-switch#3
yarnpkg/berry#4045

I added the XO plugin to the recommended VSCode extensions and added
a workspace config to automatically enable it for the project.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working esm
Projects
None yet
Development

No branches or pull requests

3 participants