Skip to content

Failed to load function definition from source - functions emulator fails to start or deploy fails #6285

Closed

Description

@james-dellow

[REQUIRED] Environment info

firebase-tools: 12.4.8

Platform: Windows 11 (all updates applied), Node v16.13.0

[REQUIRED] Test case

Sorry, I'm unable to provide an MCVE.

[REQUIRED] Steps to reproduce

Note: This issue is occurring intermittently over the last few days.

Running either firebase deploy or firebase emulators:start results in either the deploy failing or the functions emulators doesn't start, or sometime not all functions are available. Error reported by the CLI is:

!! functions: Failed to load function definition from source: FirebaseError: User code failed to load. Cannot determine backend specification

However, re-running the command again and it usually works ok on the second attempt.

[REQUIRED] Expected behavior

All functions run in the emulator or the deploy doesn't fail on first attempt.

[REQUIRED] Actual behavior

I've attached the full log for a failed emulator startup, as that's the only one I've been able to capture today, but this may be the relevant section:

[2023-08-21T12:33:42.975Z] > [functions] package.json contents: {
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "16"
},
"main": "index.js",
"dependencies": {
"atlassian-jwt": "^2.0.2",
"dayjs": "^1.11.7",
"express-validator": "^6.15.0",
"firebase-admin": "^11.10.1",
"firebase-functions": "^4.4.1",
"nanoid": "^3.3.4",
"node-forge": "^1.3.1"
},
"devDependencies": {
"eslint": "^8.36.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-promise": "^6.1.1"
},
"private": true
}
[2023-08-21T12:33:42.977Z] Building nodejs source
[2023-08-21T12:33:42.978Z] Failed to find version of module node: reached end of search path C:\Users\MYUSERNAME\documents\my-app\functions\node_modules

  • functions: Using node@16 from host.
    [2023-08-21T12:33:42.986Z] Could not find functions.yaml. Must use http discovery
    [2023-08-21T12:33:43.011Z] Found firebase-functions binary at 'C:\Users\MYUSERNAME\documents\my-app\functions\node_modules.bin\firebase-functions'
    Serving at port 8683

shutdown requested via /__/quitquitquit

!! functions: Failed to load function definition from source: FirebaseError: User code failed to load. Cannot determine backend specification {"metadata":{"emulator":{"name":"functions"},"message":"Failed to load function definition from source: FirebaseError: User code failed to load. Cannot determine backend specification"}}

debug.txt

Activity

  1. james-dellow commented on Aug 21, 2023

    @james-dellow
    Author

    One other thing I noticed - the process is slow to start when it fails, but when it is successful, both the emulator and deploy start more quickly.

  2. james-dellow commented on Aug 26, 2023

    @james-dellow
    Author

    While running the emulator today, I encountered the same error:

    !! functions: Failed to load function definition from source: FirebaseError: User code failed to load. Cannot determine backend specification

    I immediately re-ran the command and the functions emulator started correctly.

  3. aalej commented on Aug 29, 2023

    @aalej
    Contributor

    Hi @james-dellow, taking a look into this, it looks like the cause is a network issue based of this log checkFunctionsSDKVersion was unable to fetch information from NPM Error: spawnSync C:\Windows\system32\cmd.exe ETIMEDOUT from the debug.txt you provided. We will work on better surfacing this type of error to make it more visible.

  4. james-dellow commented on Sep 3, 2023

    @james-dellow
    Author

    Thanks @aalej - is there anything you can suggest that I can do in my environment to mitigate this?

  5. ErlikC commented on Oct 11, 2024

    @ErlikC

    I wonder if this has anything to do with #5888.

    I'm working with firebase-tools: 13.22.0

    In my case I get !! functions: Failed to start functions in C:\Users\MYUSERNAME\documents\my-app: FirebaseError: Failed to load function.

    There's an advice to set the environment variable FUNCTIONS_DISCOVERY_TIMEOUT=50, but it only worked for the User code failed to load. Cannot determine backend specification message.

    I found that modifying firebase-tools\lib\emulator\functionsRuntimeWorker.js in function waitForSocketReady to something like this, stopped the message. I guess it's taking longer for the socket to be ready.

    const timeout = new Promise((resolve, reject) => {
                setTimeout(() => {
                    reject(new error_1.FirebaseError("Failed to load function."));
                }, 60000);
    

    Previously it had 30 secs.

    It would be great if variable FUNCTIONS_DISCOVERY_TIMEOUT could be added to this spot (of course if that makes sense).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions