You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让VS Code中的Sorbet使用正确的Ruby及Bundler版本?

解决Sorbet强制使用指定Ruby版本的问题

先看你遇到的错误:

/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems.rb:283:in `find_spec_for_exe': Could not find 'bundler' (2.3.25) required by your /Users/amirsharif/Projects/sendapostcardonline/Gemfile.lock. (Gem::GemNotFoundException)
To update to the latest version installed on your system, run `bundle update --bundler`.
To install the missing version, run `gem install bundler:2.3.25`
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems.rb:302:in `activate_bin_path'
    from /usr/bin/bundle:23:in `<main>'
[Error - 10:42:32 AM] Connection to server got closed. Server will not be restarted.
Running Sorbet LSP with:
    bundle exec srb typecheck --lsp --disable-watchman

问题核心是系统默认Ruby(2.6)未安装对应版本的bundler,且Sorbet未调用你需要的Ruby版本。按以下方法强制指定:

  • 先确认正确Ruby版本的路径
    如果你用rbenv、rvm这类版本管理器,先获取目标Ruby的绝对路径:

    # rbenv用户
    rbenv which ruby
    # rvm用户
    rvm which ruby
    # 直接查看当前正确的ruby路径
    which ruby
    

    输出类似/Users/amirsharif/.rbenv/versions/3.2.0/bin/ruby,记下来备用。

  • 直接用指定Ruby路径执行Sorbet命令
    把原来的启动命令改成用正确Ruby调用bundle,示例:

    /Users/amirsharif/.rbenv/versions/3.2.0/bin/ruby /Users/amirsharif/.rbenv/versions/3.2.0/bin/bundle exec srb typecheck --lsp --disable-watchman
    
  • 给编辑器/IDE配置正确的环境
    如果是VS Code这类工具,找到Sorbet插件的配置项,将Sorbet: Command修改为带版本管理器的命令:

    rbenv exec bundle exec srb typecheck --lsp --disable-watchman
    

    或者在项目根目录创建.env文件,写入版本管理器的环境变量:

    RBENV_VERSION=3.2.0
    # rvm用户可写:
    # rvm use 3.2.0
    

    编辑器启动时会自动读取该文件的环境配置。

  • 先修复bundler依赖
    切换到正确Ruby版本后,先安装指定版本的bundler:

    gem install bundler:2.3.25
    bundle install
    

    确保依赖安装完成后再运行Sorbet。

内容的提问来源于stack exchange,提问作者Overload119

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 06:17:27