vscode-rdbg: Attach mode does not work on version 0.2.1 using zsh

When using zsh as the login shell, attaching to a running instance of Rails using UNIX Socket fails with the following error: connect ENOENT 1337;RemoteHost=<user>@<machine>.local 1337;CurrentDir=/Users/<user>/<repoDir> 1337;ShellIntegrationVersion=13;shell=zsh/var/folders/9v/<TempDir>/ruby-debug-sock-501/ruby-debug-<user>-7957

macOS 13.4 VSCode 1.78.2 rdbg 1.7.1 & 1.8.0 Ruby 2.7.8 Rails 6.0.6

It work fine in version 0.1.0. It got broken in version 0.2.0

It is possible to attach to the process using TCP/IP but no breakpoint will be hit. It is however possible to break on exception when connected in this manner on version 0.2.1.

As reported in this issue, there is no issue when using bash as the login shell on with version 0.2.1

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 46 (1 by maintainers)

Commits related to this issue

Most upvoted comments

I apologize for any inconvenience, everyone. I will be discussing a solution to address these issues with the original author. Please bear with us a moment.

For anyone finding this thread, the issue for me was fzf.

A good way to avoid this problem is to define a special environment variable in launch.json:

"env": {
  "TERM_PROGRAM": "vscode",
}

Then, in .zshrc skip initialization of plugins that invoke zle

Hi guys, I have found a probably better way to fix this problem without having to touch the code of the extension or have to tell people to put their rbenv initialization routine in the login shell configuration file.

Wrap your shell integration initialization in the following condition:

if [[ "$TERM_PROGRAM" == "vscode" ]]; then
  test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh"
fi

This will prevent the shell integration from being loaded when the parent process is VSCode. This should probably also work with Oh My Zsh.

I encountered the same problem while playing with the TypeProf extension, so my guess is that this will be a very common occurence in the Ruby world.

I suggest you put something about this somewhere in the extension documentation so people can solve this easily.

HTH

What you say is true, but they mention this just after the ./zshrc setup in the pyenv documentation:

If you wish to get Pyenv in noninteractive login shells as well, also add the commands to ~/.zprofile or ~/.zlogin.

What I don’t understand is why they don’t suggest putting this in the config file that is common to all shells. They might have some reason for doing so.

@miharekar is correct: If you load the content of ~/.zshrc you will also load iTerm2 Shell Integration scripts. What could cause issues would be if rbenv requires something else from the ~/.zshrc to be loaded beforehand.

I am not quite sure to understand why rbenv decided to put its configuration in ~/.zshrc instead of ~/.zshenv. Clarifying these reasons might give you a better shot at fixing this issue long term. But I agree with @miharekar and what was said in the Oh My Zsh issue, I don’t see why it should require be run in either a login or interactive shell. Since I am new to Ruby, I may lack background information on the matter.

Ideally we’d not load the terminal in the interactive mode.

Instead of coupling with iTerm, you could technically couple with rbenv/chruby since they’re both ruby managers and this is a ruby plugin. So instead of relying on interactive terminal to load them, you’d try to load them in the way they should be loaded from their respective readmes. Silently fail if you can’t load and give a way in settings to override the “smart ruby manager detection” mode.

@plfoley if I try your makeShellCommand on 0.2.1 I get same error and this in the terminal output:

Error: Failed to activate Ruby environment using chruby. Error: Error: Command failed: export TERM=dumb; /bin/zsh -lic 'chruby-exec "ruby-3.2.2" -- ruby' -rjson -e "printf(%{RUBY_ENV_ACTIVATE%sRUBY_ENV_ACTIVATE}, JSON.dump(ENV.to_h))"
(eval):1: can't change option: zle
zsh:1: command not found: chruby

Make sure to install rdbg command (`gem install debug`).
If you are using bundler, write `gem 'debug'` in your Gemfile.
(eval):1: can't change option: zle

"(eval):1: can't change option: zle\n"
["\u001b]1337;RemoteHost=miharekar@iMac-Pro.local\u0007\u001b]1337;CurrentDir=/Users/miharekar/Development/personal/decent-visualizer\u0007\u001b]1337;ShellIntegrationVersion=6;shell=zsh\u0007/var/folders/x1/h2b59yq14cs6pv9r_x1_39lw0000gn/T/ruby-debug-sock-501/ruby-debug-miharekar-66153"]
[Start session]
{"d":{},"f":"cfcbb8ab-f30d-4e4c-a53e-4655fa0856a8","g":"rdbg","h":"Attach with rdbg","i":{"uri":{"$mid":1,"fsPath":"/Users/miharekar/Development/personal/decent-visualizer","external":"file:///Users/miharekar/Development/personal/decent-visualizer","path":"/Users/miharekar/Development/personal/decent-visualizer","scheme":"file"},"name":"decent-visualizer","index":0},"j":{"type":"rdbg","name":"Attach with rdbg","request":"attach","rdbgPath":"PATH=/Users/miharekar/.rubies/ruby-3.2.2/bin:${PATH} /Users/miharekar/.gem/ruby/3.2.2/bin/rdbg","__configurationTarget":6,"rdbgExtensions":["traceInspector"],"rdbgInitialScripts":[]}}

But if I remove test -e ${HOME}/.iterm2_shell_integration.zsh && source ${HOME}/.iterm2_shell_integration.zsh from .zshrc it does indeed fix the issue!

Is there a way to add an if rdbg around this iterm2 sourcing? That would fix the issue and can go into the readme of rdbg that it’s required for iterm2 users.

Edit: This fixed it for me inside zshrc 🥳

if [ -z "$VSCODE_CLI" ]; then
  test -e ${HOME}/.iterm2_shell_integration.zsh && source ${HOME}/.iterm2_shell_integration.zsh
fi

All yours! Happy to see this fixed. One thing you could do to improve on my solution would be to test if the file ~/.iterm2_shell_integration.zsh exists. That would be a a sure-fire way of telling if iTerm2 Shell Integration was deployed but that would increase the coupling with their implementation a bit as well. It is your call at this point.

Have you folks tried configuring the new "rdbg.rubyVersionManager" VS Code setting? That fixed a similar error on one of my machines.

@miharekar @plfoley

Could you check the following steps?

  1. Run $ rdbg target.rb --O

I got the following results in my environment:

$ rdbg target.rb -O
DEBUGGER: Debugger can attach via UNIX domain socket (/var/folders/5j/z2c9zm7124q81f_py4xmd3scp7w9j7/T/ruby-debug-sock-746464807/ruby-debug-s15236-21955)
DEBUGGER: wait for debugger connection...
  1. Run $ rdbg --util=list-socks in another window

I got the following results in my environment:

$ rdbg --util=list-socks
/var/folders/5j/z2c9zm7124q81f_py4xmd3scp7w9j7/T/ruby-debug-sock-746464807/ruby-debug-s15236-21955
  1. Run rdbg -A --sock-path=<socket path>

I got the following results in my environment:

$ rdbg -A --sock-path=/var/folders/5j/z2c9zm7124q81f_py4xmd3scp7w9j7/T/ruby-debug-sock-746464807/ruby-debug-s15236-21955
/var/folders/5j/z2c9zm7124q81f_py4xmd3scp7w9j7/T/ruby-debug-sock-746464807/ruby-debug-s15236-21955

DEBUGGER (client): Connected. PID:21955, $0:target.rb

[1, 10] in target.rb
=>   1| def foo(a, b)
     2|   [a, b]
     3| end
     4| def bar(c, d)
     5|   foo(1, 2);foo(3, 4)
     6| end
     7| 
     8| here = "--- set a breakpoint here ---"
     9| bar 10, 20 # trace "bar" method here.
    10| here = "--- set a breakpoint here ---"
=>#0    <main> at target.rb:1