Skip to content

fix(firestore-bigquery-change-tracker): resolve project ID from GOOGLE_CLOUD_PROJECT for Gen2 functions#2779

Open
davestimpert wants to merge 2 commits intofirebase:nextfrom
davestimpert:fix/bq-change-tracker-gen2-project-id
Open

fix(firestore-bigquery-change-tracker): resolve project ID from GOOGLE_CLOUD_PROJECT for Gen2 functions#2779
davestimpert wants to merge 2 commits intofirebase:nextfrom
davestimpert:fix/bq-change-tracker-gen2-project-id

Conversation

@davestimpert
Copy link
Copy Markdown

@davestimpert davestimpert commented Apr 19, 2026

Summary

Fixes project ID resolving to undefined in Gen2 / Cloud Run environments, producing broken SQL like Table undefined:dataset.table.

Root cause — two compounding issues in Gen2:

  1. firebase-functions/params's projectID.value() returns "" when FIREBASE_CONFIG lacks projectId. Since "" is falsy, config.bqProjectId || process.env.PROJECT_ID falls through even when a project ID was explicitly passed.
  2. PROJECT_ID is not set in Gen2 / Cloud Run — the standard env var there is GOOGLE_CLOUD_PROJECT, which was never checked.

Fix: add GOOGLE_CLOUD_PROJECT to the fallback chain, matching the pattern already used in this repo (e.g. delete-user-data):

preferred || process.env.GOOGLE_CLOUD_PROJECT || process.env.PROJECT_ID

Fixes #2778

Changes

  • src/bigquery/gcpProject.ts — new utility resolveGcpProjectIdForBigQuery(preferred?)
  • src/bigquery/index.ts — use utility when setting bq.projectId
  • src/bigquery/snapshot.ts — use utility in buildLatestSnapshotViewQuery
  • src/__tests__/bigquery/gcpProject.test.ts — unit tests covering the fallback priority order

Test plan

  • resolveGcpProjectIdForBigQuery unit tests pass (npm test -- gcpProject)
  • Existing snapshot unit tests pass (npm test -- snapshot)
  • Manual verification in a Gen2 Cloud Function environment

🤖 Generated with Claude Code

…E_CLOUD_PROJECT for Gen2 functions

PROJECT_ID is only set in Gen1 Cloud Functions. Gen2 functions running on
Cloud Run expose GOOGLE_CLOUD_PROJECT instead, causing project ID to resolve
to undefined and producing broken SQL (undefined.dataset.table).

Adds a small gcpProject utility that mirrors the GOOGLE_CLOUD_PROJECT ||
PROJECT_ID fallback pattern already used in other extensions (delete-user-data).

Fixes firebase#2778

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 19, 2026

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.

Copy link
Copy Markdown
Contributor

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

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 a utility function resolveGcpProjectIdForBigQuery to standardize Google Cloud Project ID resolution, prioritizing explicit configuration over environment variables. The changes update the FirestoreBigQueryEventHistoryTracker and snapshot query builder to use this utility. Feedback recommends adding error handling for unresolved project IDs to prevent invalid SQL generation and removing redundant logic in the resolution function.

@cabljac
Copy link
Copy Markdown
Contributor

cabljac commented Apr 20, 2026

Hi @davestimpert do you mind signing the Google CLA agreement? you can click on the CI run it should link you to the form

…edback

- Remove redundant `|| undefined` in resolveGcpProjectIdForBigQuery
- Throw a descriptive error in buildLatestSnapshotViewQuery when project ID
  cannot be resolved, preventing silent generation of invalid SQL

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

🐛 [firestore-bigquery-export] Project ID resolves to undefined in Gen2 / Cloud Run functions

2 participants