firebase-functions
firebase-functions copied to clipboard
vague error message on newer versions of CLI: "Failed to load function definition from source:..."
[REQUIRED] Environment info
firebase-tools: v10.9.0 to v11.8.0
Platform: macOS
[REQUIRED] Test case
On a project that requires functions.config(), remove the .runtimeconfig.json file from the project and run the functions emulator. Based on the official docs at https://firebase.google.com/docs/functions/local-emulator#web-version-9
[REQUIRED] Steps to reproduce
- Create a simple script
test.jswith the following contents:
const functions = require("firebase-functions");
const { region } = functions.config().functions;
console.log("my functions region: ", region);
...
// define some cloud functions
// then import test.js in your index.js file
'region' is a sample Functions environment variable whose firebase functions:config:get output looks like this:
{
"functions": {
"region": "us-central1"
}
- Run the Functions emulator with debug logging:
firebase emulators:start --debug
[REQUIRED] Expected behavior
The cloud functions should run as usual if we had a valid .runtimeconfig.json file.
However, if we legitimately don't have the file locally, the emulator should rightly fail with a helpful error message such as below (which versions 10.8.0 and below produce):
...
[warn] ⚠ It looks like you're trying to access functions.config().functions but there is no value there. You can learn more about setting up config here: https://firebase.google.com/docs/functions/local-emulator {"metadata":{"emulator":{"name":"functions"},"message":"It looks like you're trying to access functions.config().functions but there is no value there. You can learn more about setting up config here: https://firebase.google.com/docs/functions/local-emulator"}}
[warn] ⚠ TypeError: Cannot destructure property 'region' of 'functions.config(...).functions' as it is undefined.
at Object.<anonymous> (~/functions/test.js:2:9)
at Module._compile (node:internal/modules/cjs/loader:1095:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:816:12)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:93:18)
at Object.<anonymous> (~/functions/index.js:3:16)
at Module._compile (node:internal/modules/cjs/loader:1095:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10) {"metadata":{"emulator":{"name":"functions"},"message":"TypeError: Cannot destructure property 'region' of 'functions.config(...).functions' as it is undefined.\n at Object.<anonymous> (~/functions/test.js:2:9)\n at Module._compile (node:internal/modules/cjs/loader:1095:14)\n at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)\n at Module.load (node:internal/modules/cjs/loader:975:32)\n at Function.Module._load (node:internal/modules/cjs/loader:816:12)\n at Module.require (node:internal/modules/cjs/loader:999:19)\n at require (node:internal/modules/cjs/helpers:93:18)\n at Object.<anonymous> (~/functions/index.js:3:16)\n at Module._compile (node:internal/modules/cjs/loader:1095:14)\n at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)"}}
[warn] ⚠ We were unable to load your functions code. (see above) {"metadata":{"emulator":{"name":"functions"}}}
...
[REQUIRED] Actual behavior
An error occurs, but the message firebase-debug.log is not as helpful/detailed as in previous versions of the CLI:
...
[error] Error: Failed to load function definition from source: Failed to generate manifest from function source: TypeError: Cannot destructure property 'region' of 'functions.config(...).functions' as it is undefined.
...
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
The prior version of the Functions Emulator required monkey patching of the Functions SDK, which no longer works. Technically, we are trying to remove all monkey patching of library code as that ended up being unreliable and difficult to maintain.
I'm moving this FR to Functions SDK where I think it should be supported natively w/o relying on monkey patching.