ruby / vscode-rdbg

VSCode Ruby rdbg Debugger
MIT License
176 stars 50 forks source link

Corrupted filename when using socat to expose a remote unix socket via tcp #482

Open vitobotta opened 3 months ago

vitobotta commented 3 months ago

Hi! I'll try to explain what my problem is.

I just switched to Solid Queue for background job, and I couldn't get rdbg to run on a TCP port because of the way Solid Queue handles child processes, so I ended up using a socket instead.

However Solid Queue is running in a remote container in a Kubernetes cluster, and I am trying to debug remotely since I use Okteto to work with the cluster.

Since it's a remote container, I am using socat in the container to expose the unix socket via a TCP port which I can then port-forward to my local machine. This works great and VsCode can connect to the debugger as expected.

However with this configuration VsCode has trouble setting the breakpoints, because a newline character is appended to the filename so it cannot find the file locally, event though the path - minus the newline character at the end - does exist and is valid.

My launch configuration:

   {
      "type": "rdbg",
      "name": "Attach with rdbg (worker)",
      "request": "attach",
      "debugPort": "0.0.0.0:32002",
      "localfsMap": "/home/app/backend:${workspaceFolder}",
      "showProtocolLog": true
    },

Where /home/app/backend is the root of the Rails app in the container, and 32002 is the port at which socat exposes the unix socket in the remote container, and then that port is forwarded to my local machine at 0.0.0.0.

Start script in the container that exposes the unix socket via tcp:

  SOCKET_FILE=/var/run/rdbg.socket

  RUBY_DEBUG_FORK_MODE=child bundle exec rdbg --sock_path $SOCKET_FILE --open --nonstop -c -- bundle exec rake solid_queue:work &

  while [ ! -S $SOCKET_FILE ]; do
    echo "Waiting for debugger socket to be available..."
    sleep 1
  done

  sleep 3

  socat TCP-LISTEN:32002,fork,bind=0.0.0.0 UNIX-CONNECT:$SOCKET_FILE &

  wait -n

In the debugger output in VsCode I see this when I set a breakpoint:

[DA->VSCode] {"type":"event","event":"output","body":{"category":"console","output":"No such file or directory @ rb_check_realpath_internal - /Users/vito/Cloud/Vito/Code/brella/brella-backend-1/app/jobs/approaching_meeting_notification_job.rb\n"},"seq":12}

Notice the \n at the end of the path. I believe this is why it cannot find the file and therefore the breakpoint doesn't work. But like I said the path, without the newline character at the end, does exist and points to the expected file.

Are there any workarounds? Is this an actual bug or am I missing something in my configuration?

Perhaps also worth mentioning that with the same lunch configuration for other processes that can use rdbg with a tcp port directly, all works great. So the problem occurs only with the unix socket config.

Thanks in advance for any help!

iccole commented 1 month ago

@vitobotta Did you ever solve this issue? I'm experiencing something similar

Platform: MacOS

I'm running an app using docker and expose the port bundle exec rdbg -n --open --host 0.0.0.0 --port 12345 -c -- bin/rails s

ports:
      - "12345:12345"

My launch configuration:

 {
            "type": "rdbg",
            "name": "Attach rdbg at :12345",
            "request": "attach",
            "debugPort": "localhost:12345",
            "localfsMap": "/opt/app-root:${workspaceFolder}"
}

My workspace root maps to /opt/app-root dir inside the running container, but when I attach the debugger it connects and complains that not such file exists

app-1        | DEBUGGER: Connected.
app-1        | DEBUGGER: ReaderThreadError: No such file or directory @ rb_check_realpath_internal - {workspacePath}/config/application.rb

Any help anyone can provide would be greatly appreciated!

vitobotta commented 1 month ago

Hi, if I remember correctly the only major change I made to make it work was to activate the debugger from my ruby code instead of using the rdbg binary. I can't remember if there was something else.