Skip to content

fix(functions): run the SDK bin entry when no .bin shim exists - #11387

Open
abhiramshaji-cyber wants to merge 16 commits into
firebase:mainfrom
abhiramshaji-cyber:fix-bun-windows-functions-binary
Open

abhiramshaji-cyber wants to merge 16 commits into
firebase:mainfrom
abhiramshaji-cyber:fix-bun-windows-functions-binary

Conversation

@abhiramshaji-cyber

@abhiramshaji-cyber abhiramshaji-cyber commented Oct 29, 2026 •

Copy link
Copy Markdown

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() in src/deploy/functions/runtimes/node/index.ts finds the program inside the firebase-functions package that reads a codebase and reports its functions. spawnFunctionsProcess() runs that program during firebase deploy and 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 install and runs firebase deploy --only functions, as @philemonkipngetich described in #11386.

  1. spawnFunctionsProcess() calls findFunctionsBinary().
  2. 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.
  3. The loop checks four node_modules folders for a file named exactly .bin/firebase-functions.
  4. Bun on Windows only wrote .bin/firebase-functions.exe and .bin/firebase-functions.bunx, so fileExistsSync() is false in all four folders.
  5. The CLI throws Failed to find location of Firebase Functions SDK and the deploy stops.

After this PR

Same developer, same command. Same as above through step 4.

  1. findSdkBinEntry(sdkPath) walks up from the resolved file to the package.json whose name is firebase-functions.
  2. It reads the bin field, { "firebase-functions": "lib/bin/firebase-functions.js" }, joins it to that folder, and checks the file is really there.
  3. findFunctionsBinary() returns [this.bin, "<sdk folder>/lib/bin/firebase-functions.js"], where this.bin is the node binary the CLI already resolved.
  4. spawnFunctionsProcess() runs that node with <that file> <sourceDir>, the same program the .bin file 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:#111827
Loading

The change

  • The fix: after the existing .bin loop finds nothing, a new function findSdkBinEntry(sdkPath) reads the bin field from the SDK's own package.json. It accepts both forms npm allows, a single path string or an object keyed by firebase-functions. If there is an entry and the file exists, findFunctionsBinary() returns [this.bin, entry]; otherwise the same FirebaseError as before is thrown, instead of a later MODULE_NOT_FOUND.
  • findFunctionsBinary() now returns the command and its leading arguments as an array, and spawnFunctionsProcess() spreads it into spawn(command, [...args, this.sourceDir]).
  • It finds the package.json by walking up from the resolved file and checking its name, because firebase-functions does not list ./package.json in its exports, so require.resolve("firebase-functions/package.json") throws.
  • I kept the .bin lookup first so setups that work today run exactly the same command. I would really value your view on the order: would you prefer the package.json entry to be tried first, or kept as the fallback like this?
  • node is this.bin, which comes from getNodeBinary() (the local node_modules/node or process.execPath), so it does not depend on node being on PATH. Reading this.bin also logs Using node@N from host. (or the version mismatch warning) during deploy, and only on this fallback path; setups that find the .bin file see no new log line.
  • A package.json on the way up that cannot be read or parsed is logged at debug level and skipped, so it never stops the deploy.
  • findSdkBinEntry is exported with @internal so the spec tests it directly.
  • Added a CHANGELOG.md line.

What changes for a user

  • Deploying and emulating functions works on Windows when Bun installs dependencies, including Bun's isolated node_modules/.bun store.
  • It also works for any other layout where the SDK resolves but no .bin/firebase-functions file exists in the four folders checked.
  • npm, Yarn and pnpm setups that already have the .bin file run the same command as before.
  • If the SDK cannot be found at all, the error message is unchanged.

Thank you @philemonkipngetich for the very clear diagnosis in #11386. Your note that the SDK resolves but only .exe and .bunx shims exist is exactly what this builds on, and your workaround of running the declared bin entry with node is the same idea moved into the CLI.

Scenarios Tested

  • Added tests in src/deploy/functions/runtimes/node/index.spec.ts that build real folders in a temp directory: an npm layout with .bin/firebase-functions, a Bun store with only .exe and .bunx, a pnpm layout where the root .bin shim is absent (only .cmd and .ps1, since on Windows pnpm normally also writes the extensionless shim), a bin declared as a single path, an SDK with no bin, a bin pointing to a missing file, and a malformed package.json both above the entry point and at the SDK root. One more test runs spawnFunctionsProcess() against a bin file that prints its arguments and checks it receives exactly the source directory. Without the fix, the Bun case in the findFunctionsBinary block fails with Failed to find location of Firebase Functions SDK, and the Bun, pnpm and single path cases in the findSdkBinEntry block fail because node.findSdkBinEntry does not exist.
  • On macOS, installed [email protected] with bun install --linker isolated, replaced the extensionless shim with .exe and .bunx files to match the Windows layout, and ran Delegate.execAdmin() from the built CLI. It exited 0 and wrote a functions.yaml listing the hello function. 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, and npm run mocha:fast (one failure in src/frameworks/utils.spec.ts that also happens without this change in my environment, since it runs npm root).

Sample Commands

firebase deploy --only functions

Closes #11386

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

@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 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-commenter

codecov-commenter commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.59259% with 2 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@bb49594). Learn more about missing BASE report.

Files with missing lines Patch % Lines
src/deploy/functions/runtimes/node/index.ts 92.59% 1 Missing and 1 partial ⚠️
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.
📢 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.

Use this.bin instead of the node on PATH, and ignore a bin entry that is not a string.
@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Pushed 6e136d7 addressing the review: the fallback now runs the SDK bin entry with this.bin (the same Node binary the emulator uses) instead of node from PATH, ignores a bin entry that is not a string, and the specs assert against the resolved binary. Node runtime specs, eslint, prettier and tsc all pass locally. The CHANGELOG entry is already included.

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

@abhiramshaji-cyber

Copy link
Copy Markdown
Author

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.

@joehan

joehan commented Nov 3, 2026

Copy link
Copy Markdown
Member

/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.
@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Hi @joehan, thanks again for the review! main moved past this branch, so I merged it back in 39af01c. The only conflict was CHANGELOG.md, where the entry now sits alongside the new ones from #11407, #11384 and #11417, and no code changed. Happy to adjust anything else you'd like.

@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Hi @joehan, hope you're doing well! main moved again, so I merged it back in e6a5ccf. The only conflict was CHANGELOG.md, where the 15.35.0 release had cleared the earlier entries, so it now holds the new #11413 entry plus ours, and no code in the fix changed. Happy to adjust anything else you'd like.

@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Hi @joehan, hope your week is going well! main moved again, so I merged it back in 98024ce. Same as before, the only conflict was CHANGELOG.md, and the fix itself is unchanged. Sorry for the repeated pings on this one. If it's easier, I'm happy to take the changelog entry out and let you add it at merge time. Whatever works best for you!

@abhiramshaji-cyber

Copy link
Copy Markdown
Author

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 CHANGELOG.md, where the new SQL Connect toolkit entry landed right next to ours, so I kept both entries. No code changes came with the merge, and the node runtime specs, lint and typecheck all still pass locally.

Happy to change anything else you'd like, just let me know.

@abhiramshaji-cyber

Copy link
Copy Markdown
Author

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 findFunctionsBinary tests that use bracket access for the parts that live on the Delegate (the .bin shim winning, pairing the entry with this.bin, and the error when nothing is found). Would you rather I drop those so there's no bracket access at all? Whenever you have a moment, would you mind taking another look? Happy to adjust.

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.

Functions SDK discovery fails on Windows with Bun-installed executable shims

3 participants