Skip to content

Commit

Permalink
fix(pnp): resolve the virtual path of the pnpapi instead of the issuer
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Sep 26, 2020
1 parent 290c685 commit a432101
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 41 deletions.
31 changes: 9 additions & 22 deletions .pnp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .yarn/versions/109cba4d.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/plugin-pnp": patch
"@yarnpkg/pnp": patch
"@yarnpkg/pnpify": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-node-modules"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

32 changes: 14 additions & 18 deletions packages/yarnpkg-pnp/sources/loader/makeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ export function makeManager(pnpapi: PnpApi, opts: MakeManagerOptions) {

const findApiPathCache = new Map<PortablePath, PortablePath | null>();

function addToCache(start: PortablePath, end: PortablePath, target: PortablePath | null) {
function addToCacheAndReturn(start: PortablePath, end: PortablePath, target: PortablePath | null) {
if (target !== null)
target = VirtualFS.resolveVirtual(target);

let curr: PortablePath;
let next = start;

Expand All @@ -88,12 +91,12 @@ export function makeManager(pnpapi: PnpApi, opts: MakeManagerOptions) {
findApiPathCache.set(curr, target);
next = ppath.dirname(curr);
} while (curr !== end);

return target;
}

function findApiPathFor(modulePath: NativePath) {
const start = VirtualFS.resolveVirtual(
ppath.resolve(npath.toPortablePath(modulePath)),
);
const start = ppath.resolve(npath.toPortablePath(modulePath));

let curr: PortablePath;
let next = start;
Expand All @@ -102,28 +105,21 @@ export function makeManager(pnpapi: PnpApi, opts: MakeManagerOptions) {
curr = next;

const cached = findApiPathCache.get(curr);
if (cached !== undefined) {
addToCache(start, curr, cached);
return cached;
}
if (cached !== undefined)
return addToCacheAndReturn(start, curr, cached);

const candidate = ppath.join(curr, `.pnp.js` as Filename);
if (xfs.existsSync(candidate) && xfs.statSync(candidate).isFile()) {
addToCache(start, curr, candidate);
return candidate;
}
if (xfs.existsSync(candidate) && xfs.statSync(candidate).isFile())
return addToCacheAndReturn(start, curr, candidate);

const cjsCandidate = ppath.join(curr, `.pnp.cjs` as Filename);
if (xfs.existsSync(cjsCandidate) && xfs.statSync(cjsCandidate).isFile()) {
addToCache(start, curr, cjsCandidate);
return cjsCandidate;
}
if (xfs.existsSync(cjsCandidate) && xfs.statSync(cjsCandidate).isFile())
return addToCacheAndReturn(start, curr, cjsCandidate);

next = ppath.dirname(curr);
} while (curr !== PortablePath.root);

addToCache(start, curr, null);
return null;
return addToCacheAndReturn(start, curr, null);
}

function getApiPathFromParent(parent: Module | null | undefined): PortablePath | null {
Expand Down

0 comments on commit a432101

Please sign in to comment.