[Visual Studio Code] I get a syntax error when debugging when using rbenv

environment

item version
OS Mac Catalina 10.15.6
Visual Studio Code 1.49.0
Ruby 2.7.1

I was using rbenv for Ruby version control

Premise

The gems needed for debugging were added to the Gemfile and were already installed

gem "ruby-debug-ide"
gem "debase"

error

Select Run on the left side of Visual Studio Code and select Add Config-> RSpec --active spec file only

When I debugged with the above settings, the status bar at the bottom of the screen remained orange indicating that it was running, and nothing progressed.

When rerun, the following error is output to the [OUTPUT] view as shown below.

Uncaught exception: /Users/pldb/.rbenv/shims/rspec:3: syntax error, unexpected tSTRING_BEG, expecting do or '{' or '('
[ -n "$RBENV_DEBUG" ] && set -x
     ^
/Users/pldb/.rbenv/shims/rspec:3: syntax error, unexpected ']', expecting end-of-input
[ -n "$RBENV_DEBUG" ] && set -x
                    ^

/Users/pldb/.rbenv/versions/2.6.6/bin/rdebug-ide:23:in `load': /Users/pldb/.rbenv/shims/rspec:3: syntax error, unexpected tSTRING_BEG, expecting do or '{' or '(' (SyntaxError)
[ -n "$RBENV_DEBUG" ] && set -x
     ^
/Users/pldb/.rbenv/shims/rspec:3: syntax error, unexpected ']', expecting end-of-input
[ -n "$RBENV_DEBUG" ] && set -x
                    ^
	from /Users/pldb/.rbenv/versions/2.6.6/bin/rdebug-ide:23:in `<main>'

result

Changed .vscode / launch.json as follows

Change before

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "RSpec - active spec file only",
      "type": "Ruby",
      "request": "launch",
      "program": "/Users/pldb/.rbenv/shims/rspec",
      "args": [
        "-I",
        "${workspaceRoot}",
        "${file}"
      ]
    }
  ]
}

** After change **

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "RSpec - active spec file only",
      "type": "Ruby",
      "request": "launch",
      "program": "${workspaceRoot}/bin/rspec",
      "args": [
        "-I",
        "${workspaceRoot}",
        "${file}"
      ]
    }
  ]
}

This is the default setting in Visual Studio Code However, I used binstub

Cause

The " program " at the time launch.json was generated was " $ {workspaceRoot} / bin / rspec "

However, I thought that the path of rspec used was correct and replaced it with the following path

% which rspec
/Users/pldb/.rbenv/shims/rspec

The title error was caused by this

Reference: rspec doesn't execute as a program, it is parsed as if it's a code file.

This is the content of rspec under shims that I was actually referring to

#!/usr/bin/env bash
set -e
[ -n "$RBENV_DEBUG" ] && set -x

program="${0##*/}"
if [ "$program" = "ruby" ]; then
  for arg; do
    case "$arg" in
    -e* | -- ) break ;;
    */* )
      if [ -f "$arg" ]; then
        export RBENV_DIR="${arg%/*}"
        break
      fi
      ;;
    esac
  done
fi

export RBENV_ROOT="/Users/pldb/.rbenv"
exec "/usr/local/Cellar/rbenv/1.1.2/libexec/rbenv" exec "$program" "$@"

This is a bash file generated by rbenv (for referencing rspec), and reading it as it is resulted in an error. In other words, it was because I didn't fully understand rspec.

Countermeasures

Use binstub

Reference: [Translation + Explanation] Understand binstub: Behavior of RubyGems, rbenv, bundler

Generate $ {workspaceRoot} / bin / rspec in binstub to make it the rspec to use in your project

$ {workspaceRoot} points to a folder in the directory extracted with Visual Studio Code

Go to this directory and then do the following

% bundle binstubs rspec-core
% ./bin/rspec -v
RSpec 3.9
  - rspec-core 3.9.2
  - rspec-expectations 3.9.2
  - rspec-mocks 3.9.1
  - rspec-support 3.9.3

Now you can run with the default settings

Avoid bin folders (so as not to affect other contributors) if you are creating a gem

% bundle binstubs rspec-core --path exe
% ./exe/rspec -v
RSpec 3.9
  - rspec-core 3.9.2
  - rspec-expectations 3.9.2
  - rspec-mocks 3.9.1
  - rspec-support 3.9.3

In this case, the path of " program " in launch.json will be $ {workspaceRoot} / exe / rspec

Remaining

If you don't care about the version of rspec in your project, it works even if you specify / usr / local / bin / rsepc as below.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "RSpec - active spec file only",
      "type": "Ruby",
      "request": "launch",
      "program": "/usr/local/bin/rspec",
      "args": [
        "-I",
        "${workspaceRoot}",
        "${file}"
      ]
    }
  ]
}

And this is another matter, but I'll cover another thing about setting launch.json. Encoding error in gemspec

Uncaught exception: 
[!] There was an error parsing `Gemfile`: 
[!] There was an error while loading `xxx.gemspec`: invalid byte sequence in US-ASCII. Bundler cannot continue.

As you can see, it's due to the encoding settings

Reference: Encoding issue when using gem'xcodeproj'

The launch.json when debugging during gem development finally looks like this:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "RSpec - active spec file only",
      "type": "Ruby",
      "request": "launch",
      "program": "${workspaceRoot}/exe/rspec",
      "args": [
        "-I",
        "${workspaceRoot}",
        "${file}"
      ],
      "env": {
        "LANG": "en_US.UTF-8",
        "LC_COLLATE": "en_US.UTF-8",
        "LC_CTYPE": "en_US.UTF-8",
        "LC_MESSAGES": "en_US.UTF-8",
        "LC_MONETARY": "en_US.UTF-8",
        "LC_NUMERIC": "en_US.UTF-8",
        "LC_TIME": "en_US.UTF-8",
        "LC_ALL": "en_US.UTF-8"
      }
    }
  ]
}

Recommended Posts

[Visual Studio Code] I get a syntax error when debugging when using rbenv
I get an error when adding a dependency
A memo when "I do not get a certificate error with a self-signed certificate using Java's Keytool"
I get a 404 error when testing forms authentication with Spring Security
I get a Ruby version error when I try to start Rails.
I have a question. I get an error when playing a video in Listview on android.
Getting started with Java programs using Visual Studio Code
I couldn't type Japanese in Ubuntu20.04 + Visual Studio Code
Why can I develop Java with Visual Studio Code?
Error when using SnapKit
I found no way to get the error code when I received an exception on Android
When starting Eclipse debug, I get a `ERROR: JDWP Transport dt_socket failed` error and cannot start.
I got an error when using nextInt, nextLine and substring.
I got the error The java.home variable defined in Visual Studio Code settings points to a missing or inaccessible folder (C: avadk-13.0.1)
Java in Visual Studio Code
Error when using rails capybara
I got a Permission Denied error when I put Laravel in Docker
What I learned from doing Java work with Visual Studio Code
Avoid character code error in java when using VScode extension RUN-CODE