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?]: cannot import xo from a module using PNP #3708

Closed
1 task
flying-sheep opened this issue Nov 8, 2021 · 10 comments
Closed
1 task

[Bug?]: cannot import xo from a module using PNP #3708

flying-sheep opened this issue Nov 8, 2021 · 10 comments
Labels
bug Something isn't working esm upholded Real issues without formal reproduction

Comments

@flying-sheep
Copy link
Contributor

flying-sheep commented Nov 8, 2021

Self-service

  • I'd be willing to implement a fix

Describe the bug

I’m trying to create a SDK package for VS Code’s xo linter https://github.com/xojs/vscode-linter-xo

But PnP doesn’t seem to work with import(). Am I doing something wrong?

[Error - 20:01:44] Cannot find package 'xo' imported from ~/dev/<myproject>/.yarn/sdks/xo/index.js

I did specify pnpEnableEsmLoader: true in my yarnrc.yml.

To reproduce

Create the SDK and try to import it

mkdir -p .yarn/sdks/xo
echo '{"type": "module"}' >.yarn/sdks/xo/package.json
cat >.yarn/sdks/xo/index.js <<EOF
import {existsSync} from 'fs'

const relPnpApiPath = '../../../.pnp.cjs'

const { pathname: absPnpApiPath } = new URL(relPnpApiPath, import.meta.url)

if (existsSync(absPnpApiPath)) {
	if (!process.versions.pnp) {
		// Setup the environment to be able to import xo
		const { default: pnp } = await import(absPnpApiPath)
		pnp.setup()
	}
}

// Defer to the real xo your application uses
const { getErrorResults, getFormatter, outputFixes, lintText, lintFiles } = await import('xo')
export { getErrorResults, getFormatter, outputFixes, lintText, lintFiles }
EOF

Environment

System:
    OS: Linux 5.14 Arch Linux
    CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor
  Binaries:
    Node: 16.11.1 - /tmp/xfs-4e63bbf9/node
    Yarn: 3.1.0 - /tmp/xfs-4e63bbf9/yarn
    npm: 8.1.3 - /usr/bin/npm

Additional context

No response

@flying-sheep
Copy link
Contributor Author

Any help with this? I can’t seem to figure out if import('xo') is supposed to call into your monkeypatched system or if what I’m trying can’t work.

@merceyz
Copy link
Member

merceyz commented Nov 12, 2021

Using the experimental ESM support requires starting Node with the --experimental-loader flag https://nodejs.org/docs/latest-v16.x/api/esm.html#loaders

@flying-sheep
Copy link
Contributor Author

I see. With that, I get the server to listen, but it still doesn’t seemt to work 🤔

https://github.com/flying-sheep/react-color-scheme-switch/pull/3/files

[Info  - 16:29:21] XO Server Starting in Node v16.11.1

But “Language Client is not ready yet” if I want to trigger a code action.

@yarnbot

This comment has been minimized.

@yarnbot yarnbot added the stale Issues that didn't get attention label Dec 12, 2021
@flying-sheep
Copy link
Contributor Author

flying-sheep commented Dec 12, 2021

C’mon, I thought someone had finally time to check this out, instead I get a bot …

No idea how I can reproduce this in sherlock.

@merceyz
Copy link
Member

merceyz commented Dec 12, 2021

I looked into it and It looks like you're running into nodejs/node#39140

@merceyz merceyz added the upholded Real issues without formal reproduction label Dec 12, 2021
@yarnbot yarnbot removed the stale Issues that didn't get attention label Dec 12, 2021
@flying-sheep
Copy link
Contributor Author

flying-sheep commented Dec 12, 2021

Thank you so much for helping! xo is an ESM-only package, so I think I have to write the SDK as ESM. I wonder where the bug is. I don’t assume it’s in vscode-linter-xo, as it seems to properly await promises everywhere. https://github.com/xojs/vscode-linter-xo/blob/ddc03f4cbcf47a1e5332dc15bc7daa70c838aeda/server/server.js#L487-L493

I think VS Code handles the actual subprocess spawning and message passing, so maybe this is a race condition in VS Code, that PNP is slow enough to trigger?

@merceyz
Copy link
Member

merceyz commented Dec 12, 2021

that PNP is slow enough to trigger?

It's not PnP, it's because of ESM (see the issue I linked)

@flying-sheep
Copy link
Contributor Author

flying-sheep commented Dec 13, 2021

I see! It could still be a race condition (if xo-without-pnp loads fast enough, it won’t trigger the bug), but I should probably just wait for nodejs/node#41134 to be fixed for this, as there’s only 2 reliable fixes:

  1. that one
  2. make VS code do the handshake that was mentioned in your linked issue.

Thank you! I’ll close this, and if it turns out there’s another issue that looks to be on yarn’s side, I’ll reopen it.

@flying-sheep
Copy link
Contributor Author

flying-sheep commented Jan 31, 2022

With node 17.4 we can make progress! See #4045

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 upholded Real issues without formal reproduction
Projects
None yet
Development

No branches or pull requests

3 participants