VS Code 插件 puppet-vscode 的 IntelliSense 如何配置?

解读

面试官问的不是“装完插件能不能自动补全”,而是“怎么把补全、跳转、诊断、格式化等 IntelliSense 能力真正落地到国内开发机、CI 节点、新人电脑三套环境里,且保证离线也能用”。
国内常见坑点:

  1. 插件默认去 rubygems.org、forgeapi.puppet.com 拉取最新元数据,GFW 超时导致索引失败,补全列表为空。
  2. 公司自研 module 放在私有 GitLab,没有发布到 Forge,插件找不到 Puppetfile 里声明的依赖。
  3. 多人协作时各自用系统 Ruby,版本差异造成 parser 崩溃,IntelliSense 进程重启后依旧报错。
  4. 项目根目录缺少 metadata.json 或 .fixtures.yml,Language Server 无法判断环境边界,把整个 /etc/puppet 当成一个工程,提示漫天飞。
    因此,回答要覆盖“网络、依赖、解析、性能”四条线,给出可落地的文件模板与命令,让面试官一听就知道你踩过坑也填过坑。

知识点

  1. Language Server 启动参数:--stdio、--tcp、--debug=DEBUG 及对应的 VS Code settings.json 字段。
  2. Puppet 环境三层隔离:global Ruby + vendored Ruby + module-specific Ruby(.ruby-version)。
  3. 国内镜像源:Ruby 用 gems.ruby-china.com,Puppet Forge 用 forgeapi.puppet.com.cn 或 Nexus 自建缓存。
  4. 插件读取顺序:.puppet-lint.rc → metadata.json → .fixtures.yml → Puppetfile → environment.conf。
  5. IntelliSense 索引缓存路径:~/.puppetlabs/vscode/cache,可挂载到 SSD 并定期 warm-up。
  6. 诊断开关:puppet.languageserver.diagnostic.enable、puppet.languageserver.formatting.enable、editor.formatOnSave。
  7. Bolt 计划与 Puppet 类共存时,需要额外把 Bolt modulepath 追加到 --modulepath 启动参数。
  8. 离线场景:预生成 metadata-json-lint、parser-cache、yard 文档,打包成 docker-vscode 镜像,新人 pull 下来即可。

答案

“我按‘离线先行、镜像兜底、版本锁死、缓存提速’四步在国内团队落地过,步骤如下:
第一步,统一 Ruby 工具链。
在项目根建 .ruby-version 写 2.7.8,Gemfile 里锁死 puppet=7.24.0、pdk=2.7.0,source 换成 https://gems.ruby-china.com,执行 bundle install --deployment --path vendor/bundle,保证任何机器 bundle exec puppet --version 都一致。

第二步,让 Language Server 走内网。
在 .vscode/settings.json 里加:
"puppet.languageserver.address": "127.0.0.1",
"puppet.languageserver.port": 8081,
"puppet.languageserver.preferredDependency": "pdk",
"puppet.editorService.debugFile": "/tmp/puppet-ls.log",
"puppet.languageserver.modulePath": ["site", "modules", "nexus/forge"],
"puppet.languageserver.puppetVersion": "7.24.0"
启动命令用:
bundle exec ruby \
$(bundle info puppet-languageserver --path)/bin/puppet-languageserver \
--tcp --port=8081 --timeout=30 \
--modulepath=site:modules:nexus/forge \
--puppet-settings=--confdir,/tmp/puppet-conf \
--no-cache \
2>&1 | tee /tmp/puppet-ls.log &
这样 VS Code 通过 TCP 连本地 Server,既规避了 stdio 阻塞,又能在 Jenkins 里复用同一进程做语法检查。

第三步,私有 module 索引。
把 GitLab 里所有自研模块加到 Puppetfile,用 git 源:
mod 'profile_base',
git: 'https://gitlab.example.com/puppet/profile_base.git',
ref: 'v1.2.3'
然后 bundle exec r10k puppetfile install --moduledir nexus/forge,Language Server 启动时就能解析到 profile_base::users 的函数签名。

第四步,离线缓存与镜像。
在 Nexus 建 raw 仓库,预先把 forgeapi.puppet.com 的 v3/releases 接口响应抓下来放 Nexus;CI 每日跑定时任务,对比 etag 增量更新。开发机 hosts 把 forgeapi.puppet.com 指向 Nexus,实现零外网。
缓存目录 ~/.puppetlabs/vscode/cache 用 tmpfs 挂载,启动时加 --no-cache=false,首次索引 30 秒,之后秒开。

最后,把以上四步写成 docker-compose.yml,镜像推送到公司 Harbor,新人只需 code . 就能拿到完整的补全、跳转、诊断、格式化,无需再配。”

拓展思考

如果面试官继续追问“千人规模下如何做到秒级索引更新”,可答:

  1. 把 Language Server 拆成只读索引服务与增量通知服务,索引服务用 Rust 重写 parser,输出 protobuf 索引文件;
  2. GitLab MR 合并后通过 webhook 触发索引服务,生成 diff 级别的索引 patch,推送到 Redis 流;
  3. 各开发机的 VS Code 监听 Redis 流,本地 Language Server 热加载 patch,实现秒级更新;
  4. 同时接入公司统一身份中心,按 namespace 过滤索引,保证安全合规。