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

Buildkite执行Go代码覆盖率检查时遇语法错误求助

问题

我在Buildkite步骤中执行以下Make目标:

cover:
    go test ./... -coverprofile=coverage.out 
    ccover=$$(go tool cover -func cover.out | grep total | grep -Eo '[0-9]+\.[0-9]+');
    if [ $$(bc <<< "$$ccover < 20.0") -eq 1 ]; then\
        exit 1;\
    fi

我的Buildkite YAML配置如下:

- label: ":coverage: check code coverage"
    key: "cvg"
    command:
      - make cover

执行时出现如下错误:

/bin/sh: syntax error: unexpected redirection
make: *** [Makefile:20: analysecover] Error 2
Error: The command exited with status 1

请问可能是什么原因导致的问题?

原因分析与解决办法
  • 问题根源:Make默认使用/bin/sh作为shell解释器,而<<<是Bash独有的here-string语法,POSIX标准的/bin/sh不支持该语法,因此触发了"unexpected redirection"错误。
  • 解决方式1:强制Make使用Bash
    在Makefile开头添加一行配置,指定用Bash执行所有命令:
    SHELL := /bin/bash
    
  • 解决方式2:替换为POSIX兼容的管道语法
    如果不想依赖Bash,可以将bc <<< "$$ccover < 20.0"替换为echo管道的写法,兼容所有标准shell:
    cover:
        go test ./... -coverprofile=coverage.out 
        ccover=$$(go tool cover -func cover.out | grep total | grep -Eo '[0-9]+\.[0-9]+');
        if [ $$(echo "$$ccover < 20.0" | bc) -eq 1 ]; then\
            exit 1;\
        fi
    
  • 额外提示:Makefile中每一行命令默认是独立的shell进程,若要在多行之间传递变量,要么用分号将命令连为一行,要么用\换行保持在同一个shell进程内(你的现有写法已满足此要求)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 02:45:35