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

在Bitbucket CI中使用Madge检测循环依赖的问题排查

问题分析与修复

为什么你的grep匹配不到输出?

你遇到的问题主要源于两个核心原因:

  1. 输出流捕获不全:Madge可能会将结果输出到stderr(标准错误流),但你只捕获了stdout(标准输出流)。不管是用$(...)还是>重定向,默认都只处理stdout,导致你拿不到实际的输出内容。
  2. 特殊字符编码兼容问题:输出里的✔是UTF-8特殊符号,Bitbucket Pipeline的环境可能存在编码差异,导致grep无法精准匹配该符号。

修复现有脚本的方案

修改重定向命令,同时捕获stdout和stderr到文件,并且简化匹配逻辑(避开特殊符号,只匹配文本核心内容):

- step:
    name: Check Circular Dependencies
    caches:
      - node
    image: node:20.10.0
    script:
      - cd dependency-check
      - npm install
      - cd ..
      # 同时捕获标准输出和标准错误到文件
      - npx madge --circular harmonee-ui/src/App.tsx > dependency_check.txt 2>&1
      - cat dependency_check.txt
      # 仅匹配文本内容,避免特殊符号干扰
      - if grep -q "No circular dependency found!" dependency_check.txt; then echo "No circular dependencies found"; else echo "Circular dependencies found!" && exit 1; fi

CI中检测循环依赖的更佳方案

其实完全不需要通过匹配输出来判断结果——Madge本身会根据检测结果返回对应的退出码:

  • 未检测到循环依赖:返回退出码0
  • 检测到循环依赖:返回非0退出码

Bitbucket Pipeline会自动识别命令的退出码,只要命令返回非0,就会标记步骤失败。所以可以大幅简化脚本,直接运行命令即可:

- step:
    name: Check Circular Dependencies
    caches:
      - node
    image: node:20.10.0
    script:
      - cd dependency-check
      - npm install
      - cd ..
      # 直接运行Madge命令,Pipeline自动根据退出码判定结果
      - npx madge --circular harmonee-ui/src/App.tsx

这种方式既简洁又可靠,完全规避了输出匹配的各种潜在问题。

如果需要自定义提示信息,也可以手动判断退出码:

- step:
    name: Check Circular Dependencies
    caches:
      - node
    image: node:20.10.0
    script:
      - cd dependency-check
      - npm install
      - cd ..
      - echo "Checking for circular dependencies..."
      - npx madge --circular harmonee-ui/src/App.tsx
      - echo "✅ No circular dependencies detected"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 03:43:13