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
The gems needed for debugging were added to the Gemfile and were already installed
gem "ruby-debug-ide"
gem "debase"
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>'
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
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.
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
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