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

fix(pnp): esm - handle parentURL without a file: protocol #5362

Merged
merged 7 commits into from
May 17, 2023
Merged

fix(pnp): esm - handle parentURL without a file: protocol #5362

merged 7 commits into from
May 17, 2023

Conversation

lizthegrey
Copy link
Contributor

@lizthegrey lizthegrey commented Mar 30, 2023

What's the problem this PR addresses?
When chaining Yarn PNP ESM loader with import-in-the-middle loader, INVALID_URL_SCHEME is thrown because fileURLToPath() is run on a parent URL of node:util?iitm=true generated by the IITM loader.

2023-03-30T21:40:58.280Z 3364766 U TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file
2023-03-30T21:40:58.280Z 3364766 U     at new NodeError (node:internal/errors:399:5)
2023-03-30T21:40:58.280Z 3364766 U     at fileURLToPath (node:internal/url:1212:11)
2023-03-30T21:40:58.280Z 3364766 U     at resolve$1 (file:///home/lizf/eve-roster/.pnp.loader.mjs:1993:77)
2023-03-30T21:40:58.280Z 3364766 U     at nextResolve (node:internal/modules/esm/hooks:654:28)
2023-03-30T21:40:58.280Z 3364766 U     at Hooks.resolve (node:internal/modules/esm/hooks:309:30)
2023-03-30T21:40:58.280Z 3364766 U     at ESMLoader.resolve (node:internal/modules/esm/loader:312:26)
2023-03-30T21:40:58.280Z 3364766 U     at ESMLoader.getModuleJob (node:internal/modules/esm/loader:172:38)
2023-03-30T21:40:58.281Z 3364766 U     at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
2023-03-30T21:40:58.281Z 3364766 U     at link (node:internal/modules/esm/module_job:75:36) {
2023-03-30T21:40:58.281Z 3364766 U   code: 'ERR_INVALID_URL_SCHEME'
2023-03-30T21:40:58.281Z 3364766 U }
2023-03-30T21:40:58.281Z 3364766 U 
2023-03-30T21:40:58.281Z 3364766 U Node.js v19.8.1

How did you fix it?
Ensure fileURLToPath() is only run on file URLs by explicitly checking the URL protocol; if it is not file, then default to CWD.

Checklist

  • I have read the Contributing Guide.
  • I have set the packages that need to be released for my changes to be effective.
  • I will check that all automated PR checks pass before the PR gets reviewed.

@lizthegrey
Copy link
Contributor Author

I have no idea how to write tests for this, please feel free to suggest.

@lizthegrey
Copy link
Contributor Author

Proof that hotpatching works: https://github.com/eve-val/eve-roster/blob/main/patches/pnp-loader.patch when loaded as a postinstall causes startup to work and ESM hotpatching to succeed, rather than the above error.

Copy link
Member

@merceyz merceyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I've added a test and updated the versions 👍.

@merceyz merceyz added the esm label May 17, 2023
@merceyz merceyz changed the title fix(esm-loader): allow parentURL to have node: scheme fix(pnp): esm - handle parentURL without a file: protocol May 17, 2023
@merceyz merceyz enabled auto-merge May 17, 2023 13:36
@merceyz merceyz added this pull request to the merge queue May 17, 2023
Merged via the queue into yarnpkg:master with commit 8b32064 May 17, 2023
@lizthegrey lizthegrey deleted the lizf.fix-esm-loader-chaining branch May 20, 2023 20:07
merceyz added a commit that referenced this pull request May 23, 2023
**What's the problem this PR addresses?**
When chaining Yarn PNP ESM loader with
[import-in-the-middle](https://github.com/DataDog/import-in-the-middle)
loader, INVALID_URL_SCHEME is thrown because `fileURLToPath()` is run on
a parent URL of `node:util?iitm=true` generated by the IITM loader.

```
2023-03-30T21:40:58.280Z 3364766 U TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file
2023-03-30T21:40:58.280Z 3364766 U     at new NodeError (node:internal/errors:399:5)
2023-03-30T21:40:58.280Z 3364766 U     at fileURLToPath (node:internal/url:1212:11)
2023-03-30T21:40:58.280Z 3364766 U     at resolve$1 (file:///home/lizf/eve-roster/.pnp.loader.mjs:1993:77)
2023-03-30T21:40:58.280Z 3364766 U     at nextResolve (node:internal/modules/esm/hooks:654:28)
2023-03-30T21:40:58.280Z 3364766 U     at Hooks.resolve (node:internal/modules/esm/hooks:309:30)
2023-03-30T21:40:58.280Z 3364766 U     at ESMLoader.resolve (node:internal/modules/esm/loader:312:26)
2023-03-30T21:40:58.280Z 3364766 U     at ESMLoader.getModuleJob (node:internal/modules/esm/loader:172:38)
2023-03-30T21:40:58.281Z 3364766 U     at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
2023-03-30T21:40:58.281Z 3364766 U     at link (node:internal/modules/esm/module_job:75:36) {
2023-03-30T21:40:58.281Z 3364766 U   code: 'ERR_INVALID_URL_SCHEME'
2023-03-30T21:40:58.281Z 3364766 U }
2023-03-30T21:40:58.281Z 3364766 U
2023-03-30T21:40:58.281Z 3364766 U Node.js v19.8.1
```

**How did you fix it?**
Ensure fileURLToPath() is only run on file URLs by explicitly checking
the URL protocol; if it is not `file`, then default to CWD.

**Checklist**
- [x] I have read the [Contributing
Guide](https://yarnpkg.com/advanced/contributing).
- [x] I have set the packages that need to be released for my changes to
be effective.
- [x] I will check that all automated PR checks pass before the PR gets
reviewed.

---------

Co-authored-by: merceyz <merceyz@users.noreply.github.com>
@merceyz merceyz mentioned this pull request May 24, 2023
3 tasks
arcanis pushed a commit that referenced this pull request Jun 1, 2023
**What's the problem this PR addresses?**

The test added in #5362 doesn't run
on Node.js v17.

Fixes
https://github.com/yarnpkg/berry/actions/runs/5060283241/jobs/9083017466

**How did you fix it?**

Updated the range to match Node.js versions containing
nodejs/node#42881

**Checklist**
- [x] I have read the [Contributing
Guide](https://yarnpkg.com/advanced/contributing).
- [x] I have set the packages that need to be released for my changes to
be effective.
- [x] I will check that all automated PR checks pass before the PR gets
reviewed.
merceyz added a commit that referenced this pull request Jun 1, 2023
**What's the problem this PR addresses?**

The test added in #5362 doesn't run
on Node.js v17.

Fixes
https://github.com/yarnpkg/berry/actions/runs/5060283241/jobs/9083017466

**How did you fix it?**

Updated the range to match Node.js versions containing
nodejs/node#42881

**Checklist**
- [x] I have read the [Contributing
Guide](https://yarnpkg.com/advanced/contributing).
- [x] I have set the packages that need to be released for my changes to
be effective.
- [x] I will check that all automated PR checks pass before the PR gets
reviewed.
arcanis pushed a commit to yarnpkg/example-repo-zipn that referenced this pull request Jul 3, 2023
**What's the problem this PR addresses?**

The test added in yarnpkg/berry#5362 doesn't run
on Node.js v17.

Fixes
https://github.com/yarnpkg/berry/actions/runs/5060283241/jobs/9083017466

**How did you fix it?**

Updated the range to match Node.js versions containing
nodejs/node#42881

**Checklist**
- [x] I have read the [Contributing
Guide](https://yarnpkg.com/advanced/contributing).
- [x] I have set the packages that need to be released for my changes to
be effective.
- [x] I will check that all automated PR checks pass before the PR gets
reviewed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants