Sublime Text 中 Bundler 找不到的根本原因是 GUI 启动时未加载 shell 的 PATH,需在 Build System 的 path 字段显式添加 ~/.rbenv/shims(rbenv 用户)等绝对路径,并完全重启 Sublime 验证;推荐用 zsh -l -c 或 cmd /c 包裹 bundle exec 命令。

Sublime Text 里 Bundler 找不到,不是没装,是 Build System 没看到它——bundler 和 ruby 一样,得在 Sublime 的进程环境里显式暴露路径。
为什么 bundle exec 在 Sublime 里报 command not found
终端能跑 bundle exec rails s,但 Sublime 的构建系统一调用就报 command not found: bundle,根本原因和 ruby 一样:GUI 启动的 Sublime 不加载 shell 的 PATH,也就找不到 bundler 可执行文件(通常位于 ~/.rbenv/shims/bundle 或 /opt/homebrew/bin/bundle)。更隐蔽的是,即使 ruby 路径配对了,bundler 仍可能不在同一目录下——尤其用 rbenv 时,bundle 是 shims 生成的代理脚本,必须确保 path 字段包含 shims 目录,而非仅 Ruby 的 bin 目录。
- 先验证:按
Ctrl+`打开控制台,运行import os; print(os.environ.get('PATH')),确认输出里有bundler所在路径(比如/Users/xxx/.rbenv/shims) - 如果没出现,改
~/.zprofile(macOS)或系统环境变量(Windows),补全该路径,然后**完全退出 Sublime 再重开** - 别依赖
rbenv init输出的export PATH=...放进~/.zshrc——GUI 进程不读它
path 字段必须包含 shims 目录(rbenv 用户重点看)
Build System 的 path 字段不是可选配置,它是 Sublime 查找命令的唯一依据。对 rbenv 用户,bundler 不在 ruby 的安装目录里,而在 ~/.rbenv/shims;这个路径必须写进 path,且不能用 ~,得展开为绝对路径。
- 错误写法:
"path": "/Users/xxx/.rbenv/bin"(这里只有rbenv命令,没有bundle) - 正确写法:
"path": "/Users/xxx/.rbenv/shims:/opt/homebrew/bin"(shims 在前,确保优先命中) - Windows 用户若用 RubyInstaller,
bundler一般和ruby.exe同在C:\Ruby32-x64\bin,但需确认该目录下真有bundler.bat或bundler.exe
需要 bundle exec 的场景,别硬塞进 cmd
想让 Sublime 直接运行 bundle exec ruby $file?不推荐。Build System 的 cmd 字段不支持 shell 解析(比如管道、&&、exec),直接写 ["bundle", "exec", "ruby", "$file"] 会失败——bundle 命令本身需要 shell 环境才能加载 shims 和 Gemfile 上下文。
- 可行方案:用
zsh -l -c(macOS/Linux)包裹完整命令,例如:"cmd": ["zsh", "-l", "-c", "bundle exec ruby '$file'"] - Windows 用户可用
cmd /c:"cmd": ["cmd", "/c", "bundle exec ruby %1", "$file"],但需确保bundle.bat在 PATH 中且能被 cmd 正确识别 - 更稳妥的做法:把
bundler路径配进path,然后只用ruby $file——前提是当前项目已bundle install,且require 'bundler/setup'在脚本开头
验证 bundler 是否真被识别的最快方式
别等写完 Rails 项目再试。建一个空 test.rb,内容只有一行:puts `which bundle`,然后 Ctrl+B 运行。如果输出为空或报错,说明 Sublime 进程根本看不到 bundler;如果输出类似 /Users/xxx/.rbenv/shims/bundle,说明路径已通。
这个小技巧比反复改 .sublime-build 文件快得多——which bundle 的结果,就是 Sublime 当前实际使用的 PATH 下能找到的 bundler 位置。所有配置都该以它为基准校准,而不是靠猜。











