Skip to content

Fix predeploy hooks running for every codebase when --only lists several functions of one codebase - #11481

Open
Shaurya55555 wants to merge 3 commits into
firebase:mainfrom
Shaurya55555:fix/predeploy-same-codebase-multiple-functions
Open

Shaurya55555 wants to merge 3 commits into
firebase:mainfrom
Shaurya55555:fix/predeploy-same-codebase-multiple-functions

Conversation

@Shaurya55555

Copy link
Copy Markdown

Description

Fixes #11475.

getReleventConfigs in src/deploy/lifecycleHooks.ts splits --only into one filter per function and marks a filter as matched with Array.find. For a codebase it only marks the first filter with that codebase prefix, so firebase deploy --only "functions:a:fn1,functions:a:fn2" leaves a:fn2 unmatched. The fallback then treats an unmatched filter as a function deployed without a codebase and runs predeploy for every codebase, including ones the deploy does not touch (for example their npm ci).

This marks every filter that belongs to a matching codebase, so only that codebase's hooks run. A function name without a codebase prefix (functions:fn1) still falls back to every codebase, as before. getReleventConfigs is now exported so it can be unit tested.

Scenarios Tested

I could not run npm test or mocha: installing the dependencies was stopped by low memory on my machine, so CI is the first full run of the new spec and the type check. What I did run is the matching logic copied into a standalone script, old and new side by side, for these --only values with two codebases b and a:

  • functions:a -> a (old and new)
  • functions:a:fn1 -> a (old and new)
  • functions:a:fn1,functions:a:fn2 -> old: b,a (the bug), new: a
  • functions:a:fn1,functions:a:fn2,functions:b:fn3 -> b,a (old and new)
  • functions:fn1 -> b,a, fallback unchanged (old and new)

The new src/deploy/lifecycleHooks.spec.ts covers the same six cases (no --only included).

Sample Commands

firebase deploy --only "functions:a:a-test1,functions:a:a-test2"

Before: runs predeploy for every codebase. After: only codebase a.

AI disclosure

I used Claude Code (Anthropic) to trace the matching code, write the change and the spec, and draft this description. I reviewed the diff myself. I have not signed the Google CLA yet and will do so when asked.

@google-cla

google-cla Bot commented Nov 10, 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 fixes an issue where firebase deploy --only runs predeploy hooks for all functions codebases when multiple functions from the same codebase are specified. The fix updates getReleventConfigs to correctly filter and match multiple targets, and adds comprehensive unit tests. The reviewer feedback recommends refactoring the options parameter in getReleventConfigs to a structural type to eliminate the use of as unknown as Options in the unit tests, aligning with the repository's style guide rule against using unknown or any as an escape hatch.

Comment thread src/deploy/lifecycleHooks.ts Outdated
}

function getReleventConfigs(target: string, options: Options) {
export function getReleventConfigs(target: string, options: Options) {

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.

medium

To avoid using as unknown as Options in the unit tests (which violates the repository style guide's rule against using unknown or any as an escape hatch), we can narrow the type of the options parameter to a structural type containing only the properties that getReleventConfigs actually uses.

export function getReleventConfigs(
  target: string,
  options: { only?: string; config: { get: (key: string) => unknown } },
) {
References
  1. Never use any or unknown as an escape hatch. Define proper interfaces/types or use type guards. (link)

Comment thread src/deploy/lifecycleHooks.spec.ts Outdated
Comment on lines +12 to +17
function optionsWith(only: string | undefined): Options {
return {
only,
config: { get: sinon.stub().withArgs("functions").returns([codebaseB, codebaseA]) },
} as unknown as Options;
}

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.

medium

By updating getReleventConfigs to accept a structural type, we can completely eliminate the as unknown as Options type assertion here, adhering to the repository style guide's rule against using unknown as an escape hatch. You can also remove the unused Options import on line 5.

Suggested change
function optionsWith(only: string | undefined): Options {
return {
only,
config: { get: sinon.stub().withArgs("functions").returns([codebaseB, codebaseA]) },
} as unknown as Options;
}
function optionsWith(only: string | undefined) {
return {
only,
config: { get: sinon.stub().withArgs("functions").returns([codebaseB, codebaseA]) },
};
}
References
  1. Never use any or unknown as an escape hatch. Define proper interfaces/types or use type guards. (link)

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

Thanks for the fix and the detailed write-up! The change looks right to me. I ran the spec locally and it passes, and with the fix reverted the same-codebase test fails, as it should.

A few comments inline. The prettier one will fail CI so that one needs a fix, and it'd be good to fix the multi-codebase test so it actually covers the bug. The rest are small.

Also looks like the CLA check is still pending, could you sign it when you get a chance?

Comment thread src/deploy/lifecycleHooks.spec.ts Outdated
});

it("returns only the codebase named by --only", () => {
expect(getReleventConfigs("functions", optionsWith("functions:a"))).to.deep.equal([codebaseA]);

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.

eslint reports prettier/prettier as an error on this line, so npm run lint (and npm test) will fail in CI. Running npm run format fixes it,

Comment thread src/deploy/lifecycleHooks.ts Outdated
}

function getReleventConfigs(target: string, options: Options) {
export function getReleventConfigs(target: string, options: Options) {

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.

Could we add a return type and a short JSDoc here? Exporting it trips explicit-function-return-type and require-jsdoc. Deployable from firebaseConfig.ts works without adding an any:

Suggested change
export function getReleventConfigs(target: string, options: Options) {
/** Exported for testing. */
export function getReleventConfigs(target: string, options: Options): Deployable[] {

plus import { Deployable } from "../firebaseConfig";. Non-blocking.

Comment thread src/deploy/lifecycleHooks.spec.ts Outdated
function optionsWith(only: string | undefined): Options {
return {
only,
config: { get: sinon.stub().withArgs("functions").returns([codebaseB, codebaseA]) },

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.

nit: withArgs(...).returns(...) hands back the same fake, so get returns the configs for any key, not just "functions". A plain function is clearer and we can drop the sinon import:

Suggested change
config: { get: sinon.stub().withArgs("functions").returns([codebaseB, codebaseA]) },
config: { get: (key: string) => (key === "functions" ? [codebaseB, codebaseA] : undefined) },

});

it("returns each codebase once for functions in different codebases", () => {
const only = "functions:a:fn1,functions:a:fn2,functions:b:fn3";

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.

This one passes even without the fix: both codebases are selected, so the old fallback also returns [b, a]. Could we add a third codebase that isn't in --only so it actually catches the bug? e.g. configs [codebaseB, codebaseA, codebaseC] → expected [codebaseB, codebaseA] (the old code returns all three).

]);
});

it("falls back to every config for a function without a codebase prefix", () => {

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.

Could we also add a case for functions:a,functions:a:fn1? It hit the same fallback before and this PR fixes it too. Non-blocking.

it("returns only the codebase when it and one of its functions are listed", () => {
  const only = "functions:a,functions:a:fn1";
  expect(getReleventConfigs("functions", optionsWith(only))).to.deep.equal([codebaseA]);
});

@Shaurya55555

Copy link
Copy Markdown
Author

Thanks for running it and for the detailed review. Pushed 178cc15:

  • The multi-codebase test now has a third codebase (c) that is not in --only, so it fails on the old code (the old fallback returns all three).
  • Added the functions:a,functions:a:fn1 case.
  • config.get is now a plain function that only answers for "functions", and the sinon import is gone.
  • Added the Deployable[] return type and a short JSDoc on getReleventConfigs.
  • Shortened the spec lines to fit prettier's 100 columns.

I could not run prettier or the full lint locally (the dependency install did not finish on my machine), so I formatted by hand and CI is the check for the prettier error. I will sign the CLA.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@87c3636). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #11481   +/-   ##
=======================================
  Coverage        ?   60.77%           
=======================================
  Files           ?      864           
  Lines           ?    50623           
  Branches        ?    10168           
=======================================
  Hits            ?    30765           
  Misses          ?    18766           
  Partials        ?     1092           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

const codebaseC = { source: "c", codebase: "c", predeploy: ["echo c"] };
const allCodebases = [codebaseB, codebaseA, codebaseC];

function select(only: string | undefined): unknown[] {

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.

Per repository TypeScript conventions, we should avoid unknown types as an escape hatch. I suggest either updating the return type to Deployable[] or omit the explicit return type and let TypeScript it from getRelevantConfigs.


function getReleventConfigs(target: string, options: Options) {
/** Exported for testing. */
export function getReleventConfigs(target: string, options: Options): Deployable[] {

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.

nit: I just noticed we have a typo in this function name. Can you rename this to getRelevantConfigs now that we're editing this?

Comment thread CHANGELOG.md
@@ -1,3 +1,4 @@
- Fixed `firebase deploy --only` running the `predeploy` hooks of every functions codebase when several functions of the same codebase were listed. (#11475)

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.

This also covers postdeploy hooks.

Suggested change
- Fixed `firebase deploy --only` running the `predeploy` hooks of every functions codebase when several functions of the same codebase were listed. (#11475)
- Fixed `firebase deploy --only` running `predeploy` and `postdeploy` hooks for all functions codebases when deploying multiple functions from the same codebase. (#11475)

@Shaurya55555

Copy link
Copy Markdown
Author

@googlebot I signed it!

1 similar comment
@Shaurya55555

Copy link
Copy Markdown
Author

@googlebot I signed it!

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.

predeploy hooks for every codebase run when --only lists multiple functions in the same codebase

4 participants