Skip to content

feat(functions): support monorepo deploys via automatic source isolation - #10996

Open
0x80 wants to merge 2 commits into
firebase:mainfrom
0x80:functions-monorepo-isolation
Open

0x80 wants to merge 2 commits into
firebase:mainfrom
0x80:functions-monorepo-isolation

Conversation

@0x80

@0x80 0x80 commented Aug 28, 2026 •

Copy link
Copy Markdown

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 deploy handle it automatically. During functions prepare, the source directory is checked for an enclosing workspace root using detect-monorepo, which looks for a pnpm-workspace.yaml, a parent package.json with a workspaces field, or a rush.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 in prepare.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, since isolate() receives the absolute source directory (supported as of isolate-package 1.36.0), and isolation failures are wrapped in FirebaseError with a non-zero exit code. I did not adopt the reviewer's suggestion to isolate into a temp directory: the in-package isolate/ 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:

  • Dependency footprint. isolate-package brings a transitive tree dominated by @npmcli/arborist and 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 the dynamicImport usage.
  • Ownership. Both packages are maintained by me. If that is a concern, I am open to transferring ownership of isolate-package and/or detect-monorepo to the Firebase org, or adding Firebase maintainers.

If the team prefers a different shape, I am happy to adapt this PR:

  • Opt-in instead of automatic. Gate isolation behind a firebase.json functions 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.
  • Inline the detection. detect-monorepo is a small file-sniffing walk and could be reimplemented in-tree, dropping one dependency.

Scenarios Tested

  • New unit tests in 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; and runIsolate tests proving the absolute source directory is passed through and failures surface as FirebaseError.
  • mocha 'src/deploy/functions/**/*.spec.ts' passes 982 specs. npm run test:compile and eslint --quiet are clean on the touched files.
  • Users of the fork exercise the same integration in real deploys across pnpm, npm, yarn, and Rush monorepos, including multi-codebase setups deploying from the workspace root.

Sample Commands

No new commands or flags. From a monorepo, with firebase.json at the workspace root:

{
  "functions": [
    { "source": "services/api", "codebase": "api", "runtime": "nodejs22" },
    { "source": "services/fns", "codebase": "fns", "runtime": "nodejs22" }
  ]
}
firebase deploy --only functions

Isolation runs automatically for each codebase before packaging. Standalone projects see no change.

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.
@google-cla

google-cla Bot commented Aug 28, 2026

Copy link
Copy Markdown

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/deploy/functions/prepare.ts Resolved
0x80 added a commit to 0x80/isolate-package that referenced this pull request Aug 28, 2026
`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.
@mat4n

mat4n commented Aug 29, 2026

Copy link
Copy Markdown

Firebase team please considering adding this!

@joehan
joehan requested review from ajperel and joehan September 8, 2026 17:49

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support mono-repos in deployment

2 participants