Skip to content

Commit

Permalink
Adds back part of the legacy fallback (yarnpkg#1063)
Browse files Browse the repository at this point in the history
* Adds back part of the legacy fallback

* Adds versions

* Slight refactorings
  • Loading branch information
arcanis authored Mar 12, 2020
1 parent bb4fdeb commit 1941c72
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 33 deletions.
47 changes: 31 additions & 16 deletions .pnp.js

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

22 changes: 22 additions & 0 deletions .yarn/versions/bf0e75aa.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
releases:
"@yarnpkg/cli": prerelease
"@yarnpkg/plugin-pnp": prerelease
"@yarnpkg/pnp": prerelease

declined:
- "@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"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
- "@yarnpkg/pnpify"
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

57 changes: 41 additions & 16 deletions packages/yarnpkg-pnp/sources/loader/makeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export type ResolveRequestOptions =
ResolveUnqualifiedOptions;

export function makeApi(runtimeState: RuntimeState, opts: MakeApiOptions): PnpApi {
const alwaysWarnOnFallback = Number(process.env.PNP_ALWAYS_WARN_ON_FALLBACK) > 0;
const debugLevel = Number(process.env.PNP_DEBUG_LEVEL);

// @ts-ignore
const builtinModules = new Set(Module.builtinModules || Object.keys(process.binding('natives')));

Expand All @@ -45,6 +48,9 @@ export function makeApi(runtimeState: RuntimeState, opts: MakeApiOptions): PnpAp
// Used for compatibility purposes - cf setupCompatibilityLayer
const fallbackLocators: Array<PackageLocator> = [];

// To avoid emitting the same warning multiple times
const emittedWarnings = new Set<string>();

if (runtimeState.enableTopLevelFallback === true)
fallbackLocators.push(topLevelLocator);

Expand Down Expand Up @@ -104,10 +110,8 @@ export function makeApi(runtimeState: RuntimeState, opts: MakeApiOptions): PnpAp
if (opts.allowDebug === false)
return fn;

const level = Number(process.env.PNP_DEBUG_LEVEL);

if (Number.isFinite(level)) {
if (level >= 2) {
if (Number.isFinite(debugLevel)) {
if (debugLevel >= 2) {
return (...args: Array<any>) => {
const logEntry = makeLogEntry(name, args);

Expand All @@ -119,7 +123,7 @@ export function makeApi(runtimeState: RuntimeState, opts: MakeApiOptions): PnpAp
console.trace(logEntry);
}
};
} else if (level >= 1) {
} else if (debugLevel >= 1) {
return (...args: Array<any>) => {
try {
return fn(...args);
Expand Down Expand Up @@ -526,10 +530,26 @@ export function makeApi(runtimeState: RuntimeState, opts: MakeApiOptions): PnpAp
const canUseFallbacks = !exclusionEntry || !exclusionEntry.has(issuerLocator.reference);

if (canUseFallbacks) {
const reference = runtimeState.fallbackPool.get(dependencyName);
for (let t = 0, T = fallbackLocators.length; t < T; ++t) {
const fallbackInformation = getPackageInformationSafe(fallbackLocators[t]);
const reference = fallbackInformation.packageDependencies.get(dependencyName);

if (reference == null)
continue;

if (alwaysWarnOnFallback)
fallbackReference = reference;
else
dependencyReference = reference;

break;
}

if (reference != null) {
fallbackReference = reference;
if (typeof dependencyReference === `undefined` && fallbackReference === null) {
const reference = runtimeState.fallbackPool.get(dependencyName);
if (reference != null) {
fallbackReference = reference;
}
}
}
}
Expand Down Expand Up @@ -569,16 +589,21 @@ export function makeApi(runtimeState: RuntimeState, opts: MakeApiOptions): PnpAp
}
}

if (dependencyReference == null) {
if (error === null)
throw new Error(`Assertion failed: Expected an error to have been set`);
if (dependencyReference === null)
throw error || new Error(`Assertion failed: Expected an error to have been set`);

if (typeof dependencyReference === `undefined`) {
if (fallbackReference === null || error === null)
throw error || new Error(`Assertion failed: Expected an error to have been set`);

dependencyReference = fallbackReference;

const message = error.message.replace(/\n.*/g, ``);
error.message = message;

if (typeof dependencyReference === `undefined` && fallbackReference !== null) {
dependencyReference = fallbackReference;
error.message = error.message.replace(/\n.*/g, ``);
if (!emittedWarnings.has(message)) {
emittedWarnings.add(message);
process.emitWarning(error);
} else {
throw error;
}
}

Expand Down

0 comments on commit 1941c72

Please sign in to comment.