Repository navigation
Conversation
When the functions source directory sits inside a detected monorepo workspace (pnpm, npm/yarn/bun workspaces, or Rush), isolate the source with isolate-package before packaging, so internal workspace dependencies are included and the lockfile is pruned to match. Detection is file-based via detect-monorepo; standalone projects are unaffected.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request introduces monorepo support for functions deployment by automatically isolating internal workspace dependencies and pruning lockfiles when the source directory is within a monorepo (such as pnpm, npm/yarn/bun, or Rush). Feedback on the implementation highlights three main areas of improvement: avoiding workspace clutter by isolating into a temporary directory, resolving CWD dependency issues by using absolute paths, and wrapping expected errors in a FirebaseError with a non-zero exit code as per the repository style guide. Corresponding updates to the caller in prepare.ts and the unit tests in prepareFunctionsUpload.spec.ts are also suggested to align with these changes.
`targetPackagePath` was resolved with `path.join(process.cwd(), targetPackagePath)` and the workspace root pinned to `process.cwd()`, so callers had to guarantee the process runs at the workspace root. An absolute path produced a broken join. An absolute `targetPackagePath` now targets that directory directly, and the workspace root is resolved the same way as the no-`targetPackagePath` case: from the `workspaceRoot` setting, or auto-detected by walking upward from the target package directory. Relative paths keep their exact existing semantics, so this is purely additive — absolute paths were previously a broken input nobody could depend on. Motivation: the firebase-tools PR upstreaming the isolate integration (firebase/firebase-tools#10996) got review feedback that its isolate call breaks when the CLI runs from a subdirectory of the project. With this change firebase-tools can pass its already-resolved absolute source directory and be correct from any cwd, without a `process.chdir` workaround. Decisions: - Gated the new behavior on `path.isAbsolute` rather than changing relative-path semantics, to stay non-breaking for existing configs. - With an absolute path, `workspaceRoot` is honored instead of ignored (it was only ignored before because cwd was assumed to be the root).
Address review feedback: runIsolate now receives the already-resolved absolute source directory and passes it straight to isolate-package, which resolves the workspace root by walking upward from it (added in isolate-package 1.36.0), so isolation behaves the same regardless of the directory the CLI is invoked from. Isolation failures are wrapped in a FirebaseError with a non-zero exit code, matching the convention used elsewhere in this file.
|
Firebase team please considering adding this! |
Description
Deploying functions from a monorepo workspace has been broken for years (#653, open since 2018; #5911 for pnpm specifically). Internal workspace dependencies are linked rather than published, so the packaged source cannot be built on GCF, and users resort to predeploy copy scripts, bundling hacks, or private registries.
This PR makes
firebase deployhandle it automatically. During functions prepare, the source directory is checked for an enclosing workspace root using detect-monorepo, which looks for apnpm-workspace.yaml, a parentpackage.jsonwith aworkspacesfield, or arush.json. When one is found, isolate-package rewrites the source into a self-contained directory. Internal workspace dependencies are packed and included, manifests are rewritten, and the lockfile is pruned to match. That directory is uploaded instead of the original source. When no workspace is detected, the deploy takes exactly the code path it takes today, and a unit test pins that guarantee.The integration is small: two functions in
prepareFunctionsUpload.ts, a three-line gate inprepare.ts, and the two dependencies. It mirrors the firebase-tools-with-isolate fork, which has shipped it since early 2023, tracking upstream releases daily, in production monorepos across pnpm, npm, yarn, and Rush setups. Review feedback hardened it beyond the fork. Isolation is now independent of the CLI invocation directory, sinceisolate()receives the absolute source directory (supported as of isolate-package 1.36.0), and isolation failures are wrapped inFirebaseErrorwith a non-zero exit code. I did not adopt the reviewer's suggestion to isolate into a temp directory: the in-packageisolate/output is isolate-package's documented design, its API has no external-output option, and the directory is consumed as the deploy source for the remainder of the flow. A tmp-dir output option is a possible follow-up on the isolate-package side. Upstreaming this change would let fork users return to the official CLI and close #653.Two things reviewers will want called out:
isolate-packagebrings a transitive tree dominated by@npmcli/arboristand pnpm lockfile tooling. That tooling performs the per-package-manager lockfile pruning, which is the hard part of this problem and where the maintenance effort has gone. The package is ESM-only, hence thedynamicImportusage.isolate-packageand/ordetect-monorepoto the Firebase org, or adding Firebase maintainers.If the team prefers a different shape, I am happy to adapt this PR:
firebase.jsonfunctions option such as"isolate": true, leaving existing monorepo workarounds untouched, with automatic detection as a possible later step. The fork started with a flag and moved to automatic detection without complaints, which is why this PR proposes automatic.detect-monorepois a small file-sniffing walk and could be reimplemented in-tree, dropping one dependency.Scenarios Tested
prepareFunctionsUpload.spec.ts: workspace detection fixtures for pnpm, npm/yarn/bun workspaces, and Rush; a standalone project asserting detection returns false so behavior is unchanged; andrunIsolatetests proving the absolute source directory is passed through and failures surface asFirebaseError.mocha 'src/deploy/functions/**/*.spec.ts'passes 982 specs.npm run test:compileandeslint --quietare clean on the touched files.Sample Commands
No new commands or flags. From a monorepo, with
firebase.jsonat the workspace root:{ "functions": [ { "source": "services/api", "codebase": "api", "runtime": "nodejs22" }, { "source": "services/fns", "codebase": "fns", "runtime": "nodejs22" } ] }Isolation runs automatically for each codebase before packaging. Standalone projects see no change.