-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(functions): add full support for Yarn 2+ (PnP and pnpm nodeLinkers) #10846
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
Open
ajperel
wants to merge
1
commit into
main
Choose a base branch
from
ajp/fix-10813
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+204
−17
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import * as fs from "fs"; | ||
| import * as path from "path"; | ||
| import { logger } from "../../../../logger"; | ||
|
|
||
| /** | ||
| * Encapsulates the logic of dynamically interrogating a strict Yarn Plug'n'Play environment | ||
| * to discover the absolute physical path of a given module. | ||
| * | ||
| * In strict Yarn PnP environments (where node_modules does not exist), Yarn downloads | ||
| * dependencies as zipped archives deeply tucked into `.yarn/cache/`. To run Node scripts, Yarn | ||
| * automatically injects `NODE_OPTIONS=--require .pnp.cjs` into the NodeJS boot sequence, which | ||
| * natively monkey-patches Node's `fs` and `require` modules to intercept and resolve absolute paths | ||
| * (even paths ending in `.zip/`) entirely from memory. | ||
| * @param sourceDir the user's source code directory. | ||
| * @param moduleName the package to resolve (e.g. "firebase-functions"). | ||
| */ | ||
| function resolvePnpModulePath( | ||
| sourceDir: string, | ||
| projectDir: string, | ||
| moduleName: string, | ||
| ): string | undefined { | ||
| try { | ||
| const searchDirs = sourceDir === projectDir ? [sourceDir] : [sourceDir, projectDir]; | ||
| for (const searchDir of searchDirs) { | ||
| const pnpHookPath = path.join(searchDir, ".pnp.cjs"); | ||
| if (!fs.existsSync(pnpHookPath)) { | ||
| continue; | ||
| } | ||
| // Inline the API types to satisfy TypeScipt and duck-type the PnP Hook. | ||
| interface PnpApi { | ||
| setup?(): void; | ||
| resolveToUnqualified(item: string, dir: string): string | null; | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const pnpapi = require(pnpHookPath) as PnpApi; | ||
|
|
||
| // Dynamically invoke the setup API if present. This monkeypatches the native fs module | ||
| // in the CLI process to seamlessly support zipped boundaries if the CLI was booted | ||
| // without yarn (e.g., executing `node ../firebase-tools/lib/bin/firebase.js`). | ||
| if (typeof pnpapi.setup === "function") { | ||
| pnpapi.setup(); | ||
| } | ||
| const pkgPath = pnpapi.resolveToUnqualified(moduleName, path.join(sourceDir, "package.json")); | ||
| if (pkgPath) { | ||
| return pkgPath; | ||
| } | ||
| } | ||
| } catch (e) { | ||
| logger.debug( | ||
| `resolvePnpModulePath encountered error querying Yarn PnP API for ${moduleName}:`, | ||
| e, | ||
| ); | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| export interface PackageJson { | ||
| name: string; | ||
| version: string; | ||
| bin?: Record<string, string>; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the path to the PnP resolved package and its parsed package.json if using Yarn PnP with a local `.pnp.cjs` hook and undefined otherwise. | ||
| * @param sourceDir the user's source code directory. | ||
| * @param moduleName the package to resolve (e.g. "firebase-functions"). | ||
| */ | ||
| export function resolvePnpModulePackageJson( | ||
| sourceDir: string, | ||
| projectDir: string, | ||
| moduleName: string, | ||
| ): { pkgPath: string; packageJson: PackageJson } | undefined { | ||
| // Query the local `.pnp.cjs` hook API to discover the absolute (often zipped) path mapped to the library. | ||
| const pkgPath = resolvePnpModulePath(sourceDir, projectDir, moduleName); | ||
| if (!pkgPath) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const pkgJsonPath = path.join(pkgPath, "package.json"); | ||
| // Even if `pkgJsonPath` is technically a `.zip/` path on the OS, these synchronous file lookups | ||
| // succeed natively because the hosting Node process has been monkey-patched by Yarn! | ||
| if (!fs.existsSync(pkgJsonPath)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| try { | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const packageJson = require(pkgJsonPath) as PackageJson; | ||
|
|
||
| // We self-defined PackageJson interface above, so if the API changes | ||
| // this interface could be wrong. Validate it lightly here. | ||
| if ( | ||
| typeof packageJson.name !== "string" || | ||
| typeof packageJson.version !== "string" || | ||
| (packageJson.bin && typeof packageJson.bin !== "object") | ||
| ) { | ||
| throw new Error("invalid PackageJson object"); | ||
| } | ||
|
|
||
| return { pkgPath, packageJson }; | ||
| } catch (e) { | ||
| logger.debug(`Error reading package.json for ${moduleName}:`, e); | ||
| return undefined; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can reduce nesting by flattening the conditional checks and combining the path resolution and existence check. This adheres to the repository style guide's rule to reduce nesting as much as possible.
References