Repository navigation
fix(functions): run the SDK bin entry when no .bin shim exists - #11387
abhiramshaji-cyber wants to merge 16 commits into
Conversation
Deploy looked for an extensionless node_modules/.bin/firebase-functions file and gave up when it was missing. Bun on Windows only writes firebase-functions.exe and firebase-functions.bunx, so every deploy stopped with "Failed to find location of Firebase Functions SDK" even though the SDK itself resolved fine. The .bin lookup stays first so working setups are unchanged. When it finds nothing, the CLI now reads the bin entry the SDK declares in its own package.json and runs it with node, which is what the shim would have done. This does not depend on how a package manager names its shims or where it stores packages. Fixes firebase#11386
There was a problem hiding this comment.
Code Review
This pull request addresses an issue where firebase deploy fails to locate the Cloud Functions SDK under certain package managers (like Bun on Windows or PNPM) that do not create a standard node_modules/.bin/firebase-functions shim. It introduces a helper to locate the SDK's binary entry point via its package.json and updates the process spawning logic to execute it with Node. The feedback recommends enhancing robustness by wrapping the package.json parsing in a try-catch block and using the class's existing this.bin property instead of hardcoding the global 'node' binary, along with corresponding updates to the test assertions.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #11387 +/- ##
=======================================
Coverage ? 59.32%
=======================================
Files ? 859
Lines ? 50155
Branches ? 10027
=======================================
Hits ? 29753
Misses ? 18116
Partials ? 2286 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Use this.bin instead of the node on PATH, and ignore a bin entry that is not a string.
|
Pushed 6e136d7 addressing the review: the fallback now runs the SDK bin entry with @IzaakGough thank you for reproducing this on Windows. Whenever you have a moment, I would really appreciate it if you could try this branch there, since I could only exercise the Windows layouts through the specs. |
|
Hi there, hope things are going well on your end! Thanks so much for your time. When someone gets a chance, could the CI Tests workflow be approved here and on #11385? Happy to change anything you'd prefer. |
|
/joe-review |
…n entry to exist Export findSdkBinEntry and test it directly, covering a malformed package.json and a bin entry that points to a missing file.
…ctions-binary # Conflicts: # CHANGELOG.md
…ctions-binary # Conflicts: # CHANGELOG.md
|
Hi @joehan, hope you're doing well! |
…ctions-binary # Conflicts: # CHANGELOG.md
|
Hi @joehan, hope your week is going well! |
…ctions-binary # Conflicts: # CHANGELOG.md
…ctions-binary # Conflicts: # CHANGELOG.md
…ctions-binary # Conflicts: # CHANGELOG.md
# Conflicts: # CHANGELOG.md
|
Hey @joehan, hope you're doing well! Thanks again for the review on this one. I merged the latest main in so the PR is mergeable again. The only conflict was in Happy to change anything else you'd like, just let me know. |
|
Hey @joehan, hope you're doing well! All three of your review points are in 27cf57e, so I resolved those threads, but please reopen any you'd like to revisit. One question from them is still open: I kept three small |
…ctions-binary # Conflicts: # CHANGELOG.md
…ctions-binary # Conflicts: # CHANGELOG.md
Description
Deploying functions on Windows with Bun stopped because the CLI could not find the Functions SDK, and now it runs the SDK's own declared entry file instead.
What this code is for
findFunctionsBinary()insrc/deploy/functions/runtimes/node/index.tsfinds the program inside thefirebase-functionspackage that reads a codebase and reports its functions.spawnFunctionsProcess()runs that program duringfirebase deployand in the emulator, and the CLI builds the deploy plan from what it reports.Before this PR
A developer on Windows installs dependencies with
bun installand runsfirebase deploy --only functions, as @philemonkipngetich described in #11386.spawnFunctionsProcess()callsfindFunctionsBinary().require.resolve("firebase-functions", { paths: [this.sourceDir] })succeeds and points into Bun's store,node_modules/.bun/[email protected]+<hash>/node_modules/firebase-functions/lib/v2/index.js.node_modulesfolders for a file named exactly.bin/firebase-functions..bin/firebase-functions.exeand.bin/firebase-functions.bunx, sofileExistsSync()is false in all four folders.Failed to find location of Firebase Functions SDKand the deploy stops.After this PR
Same developer, same command. Same as above through step 4.
findSdkBinEntry(sdkPath)walks up from the resolved file to thepackage.jsonwhosenameisfirebase-functions.binfield,{ "firebase-functions": "lib/bin/firebase-functions.js" }, joins it to that folder, and checks the file is really there.findFunctionsBinary()returns[this.bin, "<sdk folder>/lib/bin/firebase-functions.js"], wherethis.binis the node binary the CLI already resolved.spawnFunctionsProcess()runs that node with<that file> <sourceDir>, the same program the.binfile would have run, and the deploy continues.A developer using npm still has
.bin/firebase-functions, so step 3 finds it and nothing else runs.The diagram
flowchart LR classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827 classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px subgraph BEFORE["BEFORE: deploy stops with an error"] direction TB B1["firebase deploy on Windows with Bun"] --> B2["findFunctionsBinary()"] B2 --> B3["check 4 node_modules/.bin folders"] B3 --> B4{"file named .bin/firebase-functions?"} B4 -->|"only .exe and .bunx exist"| B5["Failed to find location of Firebase Functions SDK"] B4 -->|"npm, file found"| B6["spawn the .bin file"] end subgraph AFTER["AFTER: deploy continues"] direction TB A1["firebase deploy on Windows with Bun"] --> A2["findFunctionsBinary()"] A2 --> A3["check 4 node_modules/.bin folders"] A3 --> A4{"THE CHANGE: no file, read bin from SDK package.json"} A4 -->|"only .exe and .bunx exist"| A5["spawn node lib/bin/firebase-functions.js"] A4 -->|"npm, file found"| A6["spawn the .bin file, as today"] end B1 ~~~ A1 class B1,B2,B3,B6 box class A1,A2,A3,A6 box class B4,B5 bad class A4,A5 good linkStyle 3 stroke:#cc0000,stroke-width:5px linkStyle 8 stroke:#008800,stroke-width:5px style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827 style AFTER fill:#f3fff3,stroke:#008800,color:#111827The change
.binloop finds nothing, a new functionfindSdkBinEntry(sdkPath)reads thebinfield from the SDK's ownpackage.json. It accepts both forms npm allows, a single path string or an object keyed byfirebase-functions. If there is an entry and the file exists,findFunctionsBinary()returns[this.bin, entry]; otherwise the sameFirebaseErroras before is thrown, instead of a laterMODULE_NOT_FOUND.findFunctionsBinary()now returns the command and its leading arguments as an array, andspawnFunctionsProcess()spreads it intospawn(command, [...args, this.sourceDir]).package.jsonby walking up from the resolved file and checking itsname, becausefirebase-functionsdoes not list./package.jsonin itsexports, sorequire.resolve("firebase-functions/package.json")throws..binlookup first so setups that work today run exactly the same command. I would really value your view on the order: would you prefer thepackage.jsonentry to be tried first, or kept as the fallback like this?nodeisthis.bin, which comes fromgetNodeBinary()(the localnode_modules/nodeorprocess.execPath), so it does not depend onnodebeing onPATH. Readingthis.binalso logsUsing node@N from host.(or the version mismatch warning) during deploy, and only on this fallback path; setups that find the.binfile see no new log line.package.jsonon the way up that cannot be read or parsed is logged at debug level and skipped, so it never stops the deploy.findSdkBinEntryis exported with@internalso the spec tests it directly.CHANGELOG.mdline.What changes for a user
node_modules/.bunstore..bin/firebase-functionsfile exists in the four folders checked..binfile run the same command as before.Thank you @philemonkipngetich for the very clear diagnosis in #11386. Your note that the SDK resolves but only
.exeand.bunxshims exist is exactly what this builds on, and your workaround of running the declaredbinentry withnodeis the same idea moved into the CLI.Scenarios Tested
src/deploy/functions/runtimes/node/index.spec.tsthat build real folders in a temp directory: an npm layout with.bin/firebase-functions, a Bun store with only.exeand.bunx, a pnpm layout where the root.binshim is absent (only.cmdand.ps1, since on Windows pnpm normally also writes the extensionless shim), abindeclared as a single path, an SDK with nobin, abinpointing to a missing file, and a malformedpackage.jsonboth above the entry point and at the SDK root. One more test runsspawnFunctionsProcess()against a bin file that prints its arguments and checks it receives exactly the source directory. Without the fix, the Bun case in thefindFunctionsBinaryblock fails withFailed to find location of Firebase Functions SDK, and the Bun, pnpm and single path cases in thefindSdkBinEntryblock fail becausenode.findSdkBinEntrydoes not exist.[email protected]withbun install --linker isolated, replaced the extensionless shim with.exeand.bunxfiles to match the Windows layout, and ranDelegate.execAdmin()from the built CLI. It exited 0 and wrote afunctions.yamllisting thehellofunction. I do not have a Windows machine, so a run there would be very welcome.npm run build,npx tsc --noEmit, eslint and prettier on the changed files, andnpm run mocha:fast(one failure insrc/frameworks/utils.spec.tsthat also happens without this change in my environment, since it runsnpm root).Sample Commands
firebase deploy --only functionsCloses #11386