Skip to content

fix(functions): normalize migrated extension memory to valid MemoryOption - #11056

Open
wandamora wants to merge 2 commits into
mainfrom
morawand/fix-missing-b
Open

fix(functions): normalize migrated extension memory to valid MemoryOption#11056
wandamora wants to merge 2 commits into
mainfrom
morawand/fix-missing-b

Conversation

@wandamora

@wandamora wandamora commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Description

When exporting Firebase Extensions for migration to Cloud Functions kits (ext:export --mode functions), memory settings are mapped to the EXT_MIGRATED_SYSTEM_MEMORY environment variable. However:

  1. Incompatible Memory Format / Missing Unit Suffix: Extensions v2 system memory parameters are formatted without the trailing B (e.g. "256Mi", "512Mi", "1Gi"), and v1 parameters are integers representing megabytes (e.g. "256", "1024"). Directly assigning these values produces invalid MemoryOption values for firebase-functions/v2's setGlobalOptions, which expects "MiB" / "GiB" suffixes (e.g. "256MiB", "1GiB").
  2. Conflicting V1 and V2 System Parameters: When an extension contains both v1 and v2 memory parameters (or when spec defaults and live parameters co-exist), memory parameters could overwrite each other arbitrarily in functionsEnvFromInstance.

Changes:

  • Added memoryToMb and resolveMigratedMemory in src/extensions/export.ts to convert memory values to MB and safely resolve multiple memory parameters by selecting the higher memory allocation (preventing runtime OOMs when migrating multi-function extensions to a shared global kit configuration).
  • Added parseMemory in src/extensions/export.ts to normalize raw memory values into valid Cloud Functions v2 MemoryOption strings ("256MiB", "512MiB", "1GiB").
  • Updated functionsEnvFromInstance to normalize EXT_MIGRATED_SYSTEM_MEMORY upon export, ensuring the generated .env.<projectId> file contains standard, unambiguous units and keeping user-facing kit migration templates free of parsing boilerplate.
  • Added comprehensive unit tests in src/extensions/export.spec.ts covering memory conversion, normalization permutations, conflict resolution, and environment generation.

Scenarios Tested

  • Ran npx mocha src/extensions/export.spec.ts (all 62 tests passing):
    • Verified memoryToMb correctly parses plain megabytes ("256", "1024"), Mi/MiB ("256Mi", "512MiB"), Gi/GiB ("1Gi", "2GiB"), decimals ("0.5Gi"), negative numbers, and invalid inputs.
    • Verified parseMemory correctly maps all standard memory options from 128MiB to 32GiB, normalizes 1024Mi -> 1GiB and 2048Mi -> 2GiB, accurately parses 1GB, 1G, and 0.5Gi, and returns undefined for non-standard/invalid inputs.
    • Verified resolveMigratedMemory handles missing params, single v1/v2 params, selects the higher memory allocation when both are present, and falls back to spec defaults.
    • Verified functionsEnvFromInstance normalizes EXT_MIGRATED_SYSTEM_MEMORY to valid MemoryOption values for v1-only, v2-only, equal memory, v1-higher, and spec-default fallback cases.
  • Manually verified ext:export -> kits:install flow to ensure parameters apply cleanly within .env and functions.yaml.

Sample Flow

firebase functions:kits:install --package @firebase-function-kits/firestore-bigquery-export@0.0.2-rc.9 --template migration --no-configure
firebase ext:export --mode functions -k inst1 --instance firestore-bigquery-export
firebase deploy --only functions:inst1

@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 adds memory resolution logic for migrated Extension instances, ensuring that the highest memory value is selected when both V1 and V2 parameters are present, and normalizes memory strings for global options. The reviewer provided valuable feedback pointing out critical bugs in the parseMemory implementation regarding decimal values and standard units, as well as suggesting a more robust regex in memoryToMb to handle optional whitespace.

Comment thread templates/init/functions/typescript/index-kit-migration.ts Outdated
Comment thread src/extensions/export.ts Outdated
@wandamora
wandamora force-pushed the morawand/fix-missing-b branch from b41ea15 to 2b94934 Compare September 9, 2026 00:38
@wandamora
wandamora force-pushed the morawand/fix-missing-b branch from 2b94934 to 239517c Compare September 9, 2026 00:48
@wandamora
wandamora requested review from Berlioz and ajperel September 9, 2026 00:49

@ajperel ajperel 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.

Please get Victor to review, he's the expert, but the overall approach seems good (I'm trusting you on what formats come out of extensions and are accepted by setGlobalOptions). Leaving some feedback from Joe's skill that seem ok.

Comment thread src/extensions/export.ts Outdated
Comment thread src/extensions/export.ts Outdated
Comment thread src/extensions/export.ts
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.

3 participants