必须在终端执行code .启动VSCode以继承rbenv/rvm环境;调试需用fxn.vscode-ruby-debug插件和debug gem,Rails 7+须设autoloader为classic并清除tmp缓存。

code . 必须在终端里执行,否则 VSCode 根本看不到你的 rbenv 或 rvm 环境——双击图标打开时,GEM_HOME、GEM_PATH 全丢,bundle install 成功但 rails server 直接报 “Could not find gem”。
为什么 which ruby 在终端里是对的,VSCode 里却错?
VSCode 启动方式决定它继承哪个 shell 上下文。直接双击图标启动,走的是系统默认 shell(常为 /bin/zsh),但没加载 ~/.zshrc 里的 rbenv init,所以 ruby 指向系统自带版本或根本找不到 shim。
- 永远用终端进入项目目录后,运行
code .(不是open -a "Visual Studio Code" .) - 在 VSCode 集成终端里执行
echo $SHELL和which ruby,结果必须跟你在外部终端里运行的一致 - 别在
.vscode/settings.json里硬写"ruby.interpreterPath"指向~/.rbenv/versions/3.2.2/bin/ruby—— 这会绕过 shim,bundle exec行为异常,gem 加载失败
断点进 controller 却跳不过 model 怎么办?
Rails 7+ 默认用 :zeitwerk 自动加载,但 debug gem 对它的路径监听不稳定,缓存一残留,断点就跳到错误文件或直接失效。
- 在
config/environments/development.rb中设config.autoloader = :classic - 改完立刻在终端执行
bin/rails tmp:clear,不然 zeitwerk 缓存还在生效 - 如果仍不进
app/models,在launch.json的env字段加"RUBY_DEBUG_NO_RAILS": "1"临时禁用 Rails 集成,确认是不是 autoload 干扰
调试器该选哪个?byebug 还是 debug?
官方 rebornix.Ruby 插件已停更,misogi.ruby-debug-ide 在 Ruby 3.1+ 上崩溃率高。目前唯一稳定组合是 fxn.vscode-ruby-debug + debug gem。
-
Gemfile里加gem "debug", group: :development, require: false,然后bundle install - 删干净所有
binding.pry、byebug、pry-byebug—— 它们跟debug冲突,会导致断点卡死或进程假死 -
launch.json中type必须是"ruby",request是"launch",program设为"${workspaceFolder}/bin/rails",args填["server", "-p", "3000"]
Solargraph 补全失效、跳转不到 routes 或 helper 方法?
Solargraph 不读 Gemfile 就没法识别 Rails 方法签名,尤其 link_to、belongs_to 这类动态定义的方法。
- 确保已执行
gem install solargraph,且在项目根目录运行solargraph bundle(不是bundle exec solargraph bundle) -
.vscode/settings.json中启用"solargraph.useBundler": true - 若仍不识别
routes.rb,装bung87.rails扩展,并在设置中开启rails.enableRouteHighlighting -
.html.erb文件默认用 HTML 模式,需在 settings 里加"files.associations": {"*.html.erb": "erb"},再装ERB Helper插件补全标签
code . 启动后集成终端里的 ruby -v 和 bundle exec rails runner 'puts Rails.version' 是否真能跑通。


















