Skip to content

Latest commit

 

History

History
159 lines (126 loc) · 4.02 KB

File metadata and controls

159 lines (126 loc) · 4.02 KB

code-helper 安装脚本修复方案

问题描述

当前 src/cli/menus/install.ts 第 49 行使用管道执行:

curl -fsSL https://claude.ai/install.sh | bash

这在 Ubuntu/Debian 上会失败,因为 /bin/sh 链接到 dash,不支持 pipefail

修复方案

修改位置 1:INSTALL_METHODS 定义(第 49 行)

修改前:

{
  name: '官方脚本安装',
  command: 'curl -fsSL https://claude.ai/install.sh | bash',
  description: '需要代理或外网访问',
  platforms: ['darwin', 'linux'],
  recommended: true,
},

修改后:

{
  name: '官方脚本安装',
  command: 'temp=$(mktemp) && trap "rm -f $temp" EXIT && curl -fsSL https://claude.ai/install.sh -o "$temp" && bash "$temp"',
  description: '需要代理或外网访问',
  platforms: ['darwin', 'linux'],
  recommended: true,
},

修改位置 2:代理处理逻辑(第 394-405 行)

修改前:

if (proxyUrl) {
  // 如果是 curl 命令,直接在命令中添加代理参数
  if (finalCommand.includes('curl')) {
    finalCommand = finalCommand.replace('curl ', `curl --proxy ${proxyUrl} `);
  } else {
    // 其他命令使用环境变量
    envVars = {
      http_proxy: proxyUrl,
      https_proxy: proxyUrl,
      HTTP_PROXY: proxyUrl,
      HTTPS_PROXY: proxyUrl,
    };
  }
  showInfo(`使用代理: ${proxyUrl}`);
}

修改后:

if (proxyUrl) {
  // 如果是 curl 命令,直接在命令中添加代理参数
  if (finalCommand.includes('curl')) {
    // 在所有 curl 后面添加代理参数(支持多个 curl)
    finalCommand = finalCommand.replace(/curl\s+/g, `curl --proxy ${proxyUrl} `);
  } else {
    // 其他命令使用环境变量
    envVars = {
      http_proxy: proxyUrl,
      https_proxy: proxyUrl,
      HTTP_PROXY: proxyUrl,
      HTTPS_PROXY: proxyUrl,
    };
  }
  showInfo(`使用代理: ${proxyUrl}`);
}

修复说明

新命令工作流程

# 1. 创建临时文件
temp=$(mktemp)

# 2. 设置自动清理(无论如何退出都会清理)
trap "rm -f $temp" EXIT

# 3. 下载安装脚本
curl -fsSL https://claude.ai/install.sh -o "$temp"

# 4. 执行脚本
bash "$temp"

# 5. trap 自动清理临时文件

关键改进点

  1. 避免管道执行 - 先下载再执行,避免 pipefail 问题
  2. 自动清理 - 使用 trap 确保临时文件总是被清理
  3. 错误传播 - 使用 && 连接,任何步骤失败都会停止
  4. 兼容性 - 适用于所有 POSIX 兼容的 shell(dash/bash/zsh)

代理处理

使用正则表达式 /curl\s+/g 替换所有 curl 命令,确保:

  • 原始命令:curl -fsSL
  • 带代理的命令:curl --proxy http://127.0.0.1:7890 -fsSL
  • 临时文件中的 curl:也会被替换

测试验证

在 Ubuntu/Debian 上测试

# 测试修复后的命令
temp=$(mktemp) && trap "rm -f $temp" EXIT && curl -fsSL https://claude.ai/install.sh -o "$temp" && bash "$temp"

# 应该成功执行,不会报 pipefail 错误

在 macOS 上测试

# 同样应该正常工作
temp=$(mktemp) && trap "rm -f $temp" EXIT && curl -fsSL https://claude.ai/install.sh -o "$temp" && bash "$temp"

为什么简单的修复方案有问题

你同事提到的修复:

temp=$(mktemp)
curl --proxy $proxy -fsSL https://claude.ai/install.sh -o "$temp"
bash "$temp"
rm -f "$temp"

存在的问题

  1. ❌ curl 失败时,bash 仍会执行(可能执行空文件)
  2. ❌ mktemp 失败未处理
  3. ❌ 退出码丢失(无法判断安装是否成功)
  4. ❌ 中途失败时,临时文件可能残留

我们的方案优势

  1. ✅ 使用 && 连接,任何步骤失败都会停止
  2. ✅ 使用 trap 自动清理,即使失败也会清理
  3. ✅ 退出码正确传播
  4. ✅ 简洁明了,易于维护

实施步骤

  1. 修改 src/cli/menus/install.ts 第 49 行
  2. 修改第 394 行的正则替换
  3. 运行 npm run build 构建
  4. 在 Ubuntu/Debian 上测试安装功能
  5. 在 macOS 上测试确保不受影响