genkit icon indicating copy to clipboard operation
genkit copied to clipboard

Allow easy debugging (setting breakpoints) of a Genkit app in VSCode

Open pavelgj opened this issue 1 year ago • 2 comments

Right now it's not possible to debug when running the app via genkit start because it Genkit CLI starts a sub-process and doesn't pass along debug settings (ex. can't pass --inspect flag to be able to attach a debugger).

Today you need to run your actual app in debug mode (F5). You'll need to set the env vars in the launch config... You'll be able to set breakpoints, but you won't have the dev UI... To get the dev ui you you need to manually set "GENKIT_ENV": "dev" and then attach the dev UI to the running process -- genkit start -a http://localhost:3100.

Would be great to have a "Debug Genkit App" VSCode option. This might require a Genkit VSCode extension.

pavelgj avatar Jul 16 '24 14:07 pavelgj

When using it with the firebase emulators together you can use this launch configuration:

{
      "type": "node",
      "request": "attach",
      "name": "Attach Functions",
      "port": 9229,
      "restart": true,
      "skipFiles": [
        "<node_internals>/**"
      ]
    },

But it does not work always. It seems like you need to call some function calls before the process attaches.

dario-digregorio avatar Jul 25 '24 22:07 dario-digregorio

When using it with the firebase emulators together you can use this launch configuration:

{
      "type": "node",
      "request": "attach",
      "name": "Attach Functions",
      "port": 9229,
      "restart": true,
      "skipFiles": [
        "<node_internals>/**"
      ]
    },

But it does not work always. It seems like you need to call some function calls before the process attaches.

You can specify --inspect-functions option which will initialize the functions without needing to manually invoke then first:

https://firebase.google.com/docs/genkit/firebase#developing_using_firebase_local_emulator_suite

pavelgj avatar Jul 26 '24 01:07 pavelgj

Hello everyone,

I (with a help of chatGPT) created this sh-script to address the problem from topic (unsuccessfully)

#!/bin/bash
set -e

APP_PATTERN="./cmd/document-classifier"
genkit start --non-interactive -- go run -gcflags='all=-N -l' -ldflags='' $APP_PATTERN &
GENKIT_PID=$!

echo "Genkit started (PID: $GENKIT_PID). Waiting for Go process..."

#Wait until server responds on port 8080
while ! curl -s http://localhost:$API_PORT/ >/dev/null; do
    sleep 1
done

# Find all matching PIDs (Genkit + child)
ALL_PIDS=$(pgrep -f "$APP_PATTERN")

# Convert to array, get the **highest** PID (child is usually the last one)
APP_PID=$(echo "$ALL_PIDS" | tail -n 1)

echo "Go app is running (PID: $APP_PID, PORT: $API_PORT)"

# Attach Delve debugger
echo "Attaching dlv to PID $APP_PID..."
dlv attach "$APP_PID" --headless=true --listen=:$DEBUG_PORT --api-version=2 --accept-multiclient &

wait $GENKIT_PID

Eventually, it is starting dev-ui (flows are available) then attaching delve-process into child genkit process, which is running app.

However, delve produces following warning: Warning: no debug info found, some functionality will be missing such as stack traces and variable evaluation.

Apparently, resulting binary doesn't contain gcflags='all=-N -l' flags, even despite they were provided.

Anyone knows workaround for this?

I've spent already so much time trying to combine delve and genkit dev-ui 😅

Thank you!

KhairullinAR avatar Oct 14 '25 11:10 KhairullinAR