Search code examples
firebasegoogle-cloud-functionsvscode-debuggerfirebase-tools

Is it possible to debug multiple codebases running in Firebase functions emulator?


I'm trying to debug some Firebase Functions running locally with the Firebase Emulator and VS Code.

I can do that with the following command:

firebase emulators:start --inspect-functions

"Debugger is listening on ws://127.0.0.1:9229/{some-uuid}" then I can attach VS Code to it.

The problem is that I'm running 2 codebases in the same emulator, but the debugger can start only once... so I can only debug the first codebase defined in firebase.json.

Is it possible to change the 9229 port in order to run another debugger (in the same, or in another emulator)?


Solution

  • If you're encountering issues with debugging multiple Firebase functions codebases, here's a workaround that might help.

    First, locate your firebase-tools installation folder. This folder is typically installed globally within your Node.js installation. For those using NVM (Node Version Manager) on Windows, you can usually find it at C:\Progs\nvm\v20.10.0\node_modules\firebase-tools\.

    Modifications Needed:

    1. In firebase-tools\lib\emulator\controller.js:

      • Find the line containing codebase:.
      • After codebase: cfg.codebase,, insert a new line: inspectorPort: cfg.inspectorPort,.
      • The result should look like this:
        emulatableBackends.push({
            functionsDir,
            runtime,
            codebase: cfg.codebase,
            inspectorPort: cfg.inspectorPort,
            env: Object.assign({}, options.extDevEnv),
            secretEnv: [],
            predefinedTriggers: options.extDevTriggers,
        });
        
    2. In firebase-tools\lib\emulator\functionsEmulator.js:

      • Search for ${this.args.debugPort}.
      • Replace it with ${backend.inspectorPort || this.args.debugPort}.
      • This ensures that inspectorPort from the configuration is used if specified, otherwise it defaults to the port from command-line arguments or the default port.

    After these changes, your Firebase Tools installation should now support specifying separate ports for each codebase.

    Further Configuration:

    • In your firebase.json file, add "inspectorPort": "9230", to each codebase entry. Ensure you use unique ports for different codebases.
    • Modify your launch.json file to include a new debugger setting for each codebase, specifying the appropriate ports.

    With these steps completed, when you start the emulators, you should be able to attach debugger instances to the different functions codebases for effective debugging. Happy coding!