Repository navigation
fix(functions): resolve default regions for parameterized event triggers - #11026
Conversation
There was a problem hiding this comment.
Code Review
This pull request fixes an issue where second-generation functions with parameterized trigger event filters failed default region resolution. It unbundles parameter resolution and backend generation in prepare.ts so that parameter values can be ephemerally resolved in resolveRegionForTrigger before querying GCP APIs. The feedback suggests refactoring resolveRegionForTrigger to use an early return if the endpoint is not event-triggered, which simplifies the logic and aligns with the repository's style guide on reducing nesting.
|
I like this approach more than the alternative you suggested - the alternative of partially resolving some fields before others seems complex and ripe for future bugs , and I think it is reasonable for 'resolveDefaultRegions' to require param values. As a slight twist - Should we keep resolveBackend and just move resolveDefaultRegionForBuild inside of it? Or is there some other call site that needs resolveBackend to not include resolveDefaultRegionForBuild? |
Yeah. I investigated this path with Gemini cause it also sounded nice to me and I didn't like recreating resolveBackend. But with a little more proding maybe we can make it work. The two issues to solve:
I do think that could be a bit better? |
### Description
In 2nd-gen Cloud Functions, default region resolution for event-triggered functions (e.g., Firestore or Storage) queries GCP APIs to discover the location of the trigger resource. When these triggers use parameters for event filters (such as `database: defineString("DATABASE")` or `bucket: defineString("BUCKET")`), default region resolution previously failed because parameter evaluation occurred after region resolution. The CLI passed raw CEL expressions (e.g., `"{{ params.DATABASE }}"`) to GCP APIs, causing invalid resource ID errors and silently falling back to deploying in `us-central1`.
This regression occurred after PR #10471 moved region resolution ahead of `build.resolveBackend` to support VPC connector formatting. Because `build.resolveBackend` bundled both parameter evaluation and backend conversion, parameter resolution was inadvertently deferred.
To fix this:
1. **Lifecycle Hook**: Added an optional `resolveRegions` hook to `build.resolveBackend`. This allows parameter evaluation, default region discovery, and Backend AST generation to execute sequentially in a single pass while keeping `build.ts` hermetic and preserving a clean interface in `prepare.ts`.
2. **Ephemeral Filter Resolution**: During region discovery, `resolveRegionForTrigger` ephemerally resolves filter expressions for the GCP API query without mutating the underlying `Build` AST.
Fixes #11020
### Scenarios Tested
- Unit tests in `prepare.spec.ts` for parameterized Firestore and Storage triggers resolving to mapped regions (`europe-west1`) while preserving raw expressions on the `Build` object.
- Fallback unit test verifying graceful default to `us-central1` if parameter resolution fails.
- Unit test in `build.spec.ts` verifying `build.resolveBackend` executes the `resolveRegions` hook before generating VPC connector paths.
- `npm run test`
### Sample Commands
- `firebase deploy --only functions`
1659b1f to
f66e914
Compare
Fixes #11020.
Description
When 2nd-gen Cloud Functions declare parameterized event trigger filters (such as
database: defineString("DATABASE")orbucket: defineString("BUCKET")), default region resolution failed because parameter evaluation occurred after region resolution. As a result, the CLI passed raw CEL expressions (e.g.,"{{ params.DATABASE }}") to GCP Firestore and Storage REST APIs. The APIs rejected these with invalid ID errors, causing the CLI to silently fall back tous-central1for functions whose event sources lived in other regions (e.g.,eur3->europe-west1).This regression was introduced by PR #10471, which moved region resolution before
build.resolveBackendto ensure VPC connectors could be built with concrete region paths. However, becausebuild.resolveBackendbundled both parameter evaluation (params.resolveParams) and backend conversion (build.toBackend), parameter resolution was inadvertently moved after region resolution.Fix
We unbundle parameter evaluation from backend conversion so region resolution can sit between them:
Buildendpoint in-place.Buildendpoint retains its original parameter expressions untilbuild.toBackendperforms the definitive transformation into aBackendobject.src/deploy/functions/prepare.spec.tscovering parameterized Firestore and Storage triggers, asserting that GCP API stubs receive the concrete resolved resource names, the region resolves to the mapped location, and theBuildendpoint's trigger filters remain completely unmutated.Alternative
An alternative design is to introduce a dedicated
build.resolveEventFilters(wantBuild, paramValues)step that mutateseventFiltersdirectly on theBuildobject before region resolution runs.resolveDefaultRegionsForBuildto read plain strings without acceptingparamValues, it introduces an asymmetry where trigger filters are partially resolved onBuildahead of time while all other expression fields (serviceAccount,cpu,memory,vpc,schedule) remain unresolved untiltoBackend. Additionally,toBackendmust retain filter resolution logic anyway for standalone callers like the emulator.Buildstate and keeptoBackendas the sole authority for expression evaluation.Test Scenarios
database: "{{ params.DATABASE }}"withDATABASE=custom-dbineur3resolves the endpoint region toeurope-west1, passes"custom-db"togetDatabase(), and verifieswant.endpoints["onDocumentCreate"].eventTrigger.eventFiltersremains unmutated ({ database: "{{ params.DATABASE }}" }).bucket: "{{ params.BUCKET }}"withBUCKET=custom-bucketineuresolves the endpoint region toeurope-west1, passes"custom-bucket"togetBucket(), and verifieswant.endpoints["onArchive"].eventTrigger.eventFiltersremains unmutated ({ bucket: "{{ params.BUCKET }}" }).us-central1and leave the trigger filter expressions intact.npm run testSample Commands
firebase deploy --only functions