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
相关产品推荐
相关产品推荐

