必须在终端执行code .启动vscode以继承rbenv/rvm环境;rails 7+需设config.autoloader = :classic并执行bin/rails tmp:clear;调试仅支持fxn.vscode-ruby-debug+debug gem组合;solargraph须bundle exec solargraph bundle且配置usebundler和require rails上下文。

code . 必须在终端里执行,否则 Ruby 环境全丢
VSCode 本身不加载你的 ~/.zshrc 或 rbenv 初始化逻辑,双击图标或用 open -a 启动时,GEM_HOME、GEM_PATH、PATH 全部失效。结果就是:bundle install 成功,但 rails server 直接报 Could not find gem。
- 永远在项目根目录终端运行
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行为异常
Rails 7+ 调试前必须切 classic autoloader 并清缓存
Rails 7 默认用 :zeitwerk,但 debug gem 对它的路径监听不稳定,断点常跳到错误文件、卡在 controller 不进 model。
- 在
config/environments/development.rb中加一行:config.autoloader = :classic - 改完立刻在终端执行
bin/rails tmp:clear,否则 zeitwerk 缓存还在生效 - 如果仍不进
app/models,在launch.json的env字段加"RUBY_DEBUG_NO_RAILS": "1",临时禁用 Rails 集成验证是否是 autoload 干扰
调试器只认 fxn.vscode-ruby-debug + debug gem
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 补全失效?它根本没读你的 Gemfile.lock
Solargraph 不解析 Gemfile.lock 就不知道你用了哪些 gems,form_with、belongs_to 这类动态方法就无法补全或跳转。
- 必须在项目根目录运行
bundle exec solargraph bundle(不是solargraph bundle) - 确保
.vscode/settings.json中启用了"solargraph.useBundler": true -
.solargraph.yml里建议加require: ["./config/environment"],让 Solargraph 加载 Rails 上下文
code . 启动方式不对、autoloader 模式没切、solargraph bundle 没走 bundle exec —— 这三处漏掉任意一个,补全跳转就断,断点就飘,环境就“看起来装了但啥都不对”。











