-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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] PnP API cannot be consumed if file located outside workspace #693
Comments
Related issue in IntelliJ: https://youtrack.jetbrains.com/issue/WEB-43298 |
I think the regression occurred while I was working on the multi-tree improvement (#630). I think I'll fix that by making const api = require(`pnpapi`).findApiPathFor(process.cwd()); |
@arcanis Sure, that will work. |
@arcanis Could you please clarify these off-topic questions:
|
Yep, we'll keep supporting non-PnP accesses to the API. The
Never thought about it 🤔 I think you should be able to download any release via GitHub:
I'll add support for this in |
#696 will make it possible for non-PnP modules to access the PnP API based on their path. That being said, if |
Should be fixed in the latest release (minus the |
@arcanis Great, thanks! 👍 I've replaced
Not sure Regarding
Output:
|
I'm not 100% sure myself to be honest 😄 I think there is a stronger semantic meaning with Btw, off topic but is ESLint supported out of the box with Yarn 2 and IntelliJ? Some engineers here have reported me that they had to use our SDK + generate an extra shim for |
Thanks for the explanation.
@arcanis Is it possible to support again requiring by full path like that? |
tldr: You need to go through I've thought some more about your question on First, consider that Yarn supports a mode where all projects on the disk load packages from the same global cache. Because of this, a problem appears when doing the following: require('/home/segrey/WebstormProjects/untitled17/.yarn/cache/eslint-npm-6.8.0-d27045f313-1.zip/node_modules/eslint/lib/options'); Which dependency tree is this ESLint file part of? Since the cache may be shared, multiple projects on the disk may depend on this file - but we don't know which one is the right one if we only have the path (and we need to know it in order to give ESLint access to its own dependencies). We could maybe default to the global PnP hook, but even then it might not be the right one ... and generally speaking, if something cannot be guaranteed to be true, we should assume it can't be relied. So a followup question is: how is Yarn able to make such So one last question remains: what's the difference between By contrast, To make things maybe clearer, consider what happens if you run this code from a PnP project when the global cache is enabled?: const pathToMyESLint = require.resolve('eslint');
const pathToAnotherESLint = require.resolve('eslint', {
paths: [pathToAnotherProject],
});
// Since we have a global cache, pathToMyESLint === pathToAnotherESLint. So
// what should happen when we do this?
const eslint = require(pathToAnotherESLint); By contrast, if you use const requireForOtherProject = createRequire(pathToAnotherProject + '/package.json');
const pathToMyESLint = require.resolve('eslint');
const pathToAnotherESLint = requireForOtherProject.resolve('eslint');
// pathToMyESLint and pathToAnotherESLint are still equal, but this time we are
// able to load them through their own unique `require` contexts:
const myEslint = require(pathToMyESLint);
const anotherESlint = requireForOtherProject(pathToAnotherESLint); I hope that makes sense - it's fairly complex, so please feel free to ask me any question. I will try not to make my answers as long as this post 😅 |
@arcanis Thank you very much for the great explanation! It's clear now. |
Yep, I plan to tag stable and make a release post next Friday! :)
Note that I recently implemented transparent TS support (the difference with native being that Yarn is silently patching the TS package to merge the minimal change set required for TS to have PnP support). It seems to work with WebStorm just fine, the only thing is that it currently requires to manually set the TypeScript path: |
Do you mean that WebStorm could detect |
No, I meant that maybe you could load it the same way as you load ESLint without needing an explicit shim (by doing the |
Ah, I see, we'll take a look, thanks! |
yarnpkg/berry#693 (comment) GitOrigin-RevId: 0f16a5968cc4f631be104938f568f0ac1a2c969c
…B-43396) yarnpkg/berry#693 (comment) (cherry picked from commit 0f16a5968cc4f631be104938f568f0ac1a2c969c) GitOrigin-RevId: 86c01dc91d5c7064470ded20d32eaef85f4f2d09
I have just encountered this, with the same repro as in the original issue (or even using This is a big issue because i am unable to use a local package that tries to use |
@arcanis i made a reproduction at https://github.com/remorses/yarn-reproduction-pnpapi-from-file-located-outside-workspace Should i open another issue or can we reopen this one? |
Your repro behaves as expected, the file isn't controlled by a pnpapi so there is no pnpapi to resolve back to it |
Depending on the context, you may explicitly require the const {createRequire} = require(`module`);
const otherModuleRequire = createRequire(`/path/to/another/file`);
const pnpApi = otherModuleRequire(`pnpapi`); |
How can I run a local binary (outside the workspace) that makes use of pnpapi inside a yarn workspace? I already tried yarn link and link: protocol without success |
I managed to use my local package publishing the package to a local registry with verdaccio and installing with |
Describe the bug
require('pnpapi')
fails if it's run in a file located outside Yarn workspace.For example, IntelliJ consumes PnP API in this way (
intellij-yarn-pnp-deps-tree-loader.js
is located in IDE installation folder) and after updating Yarn to 2.0.0-rc.22 it fails. This disables IntelliJ Yarn 2 integration unfortunately.To Reproduce
{}
).yarn policies set-version berry
, it will install Yarn 2.0.0-rc.22yarn install
node --require ./.pnp.js api-client.js
outputsOK
api-client.js
outside of Yarn workspace withmv api-client.js ..
node --require ./.pnp.js ../api-client.js
fails withError: Cannot find module 'pnpapi'
Environment if relevant (please complete the following information):
The text was updated successfully, but these errors were encountered: