Repository navigation
Fix predeploy hooks running for every codebase when --only lists several functions of one codebase - #11481
Conversation
…ral functions of one codebase
|
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. |
There was a problem hiding this comment.
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.
| } | ||
|
|
||
| function getReleventConfigs(target: string, options: Options) { | ||
| export function getReleventConfigs(target: string, options: Options) { |
There was a problem hiding this comment.
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
- Never use any or unknown as an escape hatch. Define proper interfaces/types or use type guards. (link)
| function optionsWith(only: string | undefined): Options { | ||
| return { | ||
| only, | ||
| config: { get: sinon.stub().withArgs("functions").returns([codebaseB, codebaseA]) }, | ||
| } as unknown as Options; | ||
| } |
There was a problem hiding this comment.
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.
| 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
- Never use any or unknown as an escape hatch. Define proper interfaces/types or use type guards. (link)
shettyvarun268
left a comment
There was a problem hiding this comment.
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?
| }); | ||
|
|
||
| it("returns only the codebase named by --only", () => { | ||
| expect(getReleventConfigs("functions", optionsWith("functions:a"))).to.deep.equal([codebaseA]); |
There was a problem hiding this comment.
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,
| } | ||
|
|
||
| function getReleventConfigs(target: string, options: Options) { | ||
| export function getReleventConfigs(target: string, options: Options) { |
There was a problem hiding this comment.
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:
| 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.
| function optionsWith(only: string | undefined): Options { | ||
| return { | ||
| only, | ||
| config: { get: sinon.stub().withArgs("functions").returns([codebaseB, codebaseA]) }, |
There was a problem hiding this comment.
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:
| 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"; |
There was a problem hiding this comment.
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", () => { |
There was a problem hiding this comment.
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]);
});|
Thanks for running it and for the detailed review. Pushed 178cc15:
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
| const codebaseC = { source: "c", codebase: "c", predeploy: ["echo c"] }; | ||
| const allCodebases = [codebaseB, codebaseA, codebaseC]; | ||
|
|
||
| function select(only: string | undefined): unknown[] { |
There was a problem hiding this comment.
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[] { |
There was a problem hiding this comment.
nit: I just noticed we have a typo in this function name. Can you rename this to getRelevantConfigs now that we're editing this?
| @@ -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) | |||
There was a problem hiding this comment.
This also covers postdeploy hooks.
| - 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) |
|
@googlebot I signed it! |
1 similar comment
|
@googlebot I signed it! |
Description
Fixes #11475.
getReleventConfigsinsrc/deploy/lifecycleHooks.tssplits--onlyinto one filter per function and marks a filter as matched withArray.find. For a codebase it only marks the first filter with that codebase prefix, sofirebase deploy --only "functions:a:fn1,functions:a:fn2"leavesa:fn2unmatched. The fallback then treats an unmatched filter as a function deployed without a codebase and runspredeployfor every codebase, including ones the deploy does not touch (for example theirnpm 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.getReleventConfigsis now exported so it can be unit tested.Scenarios Tested
I could not run
npm testor 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--onlyvalues with two codebasesbanda: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:afunctions: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.tscovers the same six cases (no--onlyincluded).Sample Commands
Before: runs
predeployfor every codebase. After: only codebasea.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.