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

Makefile校验Git Tag与package.json版本一致性时无论匹配与否均报错的解决方法

Makefile enforce-versions Target Always Exits with Code 1 (Even When Versions Match)

Let’s break down why your current setup fails, and fix it step by step:

Root Cause

Your issue stems from two key problems:

  1. Quoted version string from jq: By default, jq .version package.json outputs the version wrapped in double quotes (e.g., "1.1.0"). When you prepend a v, your PKG_VERSION variable becomes v"1.1.0" (with embedded quotes), while LATEST_TAG from git describe is a clean string like v1.1.0 (no quotes). These values never match, so Make’s ifeq condition always evaluates to false, triggering exit 1.
  2. Misuse of Make’s ifeq in rule commands: Make processes ifeq conditions during the rule-parsing phase, not at runtime when shell commands execute. This leads to unexpected behavior when working with dynamically generated values like git tags or package.json versions.

Fixed Makefile (String Comparison)

First, fix the jq call to get an unquoted version string with the -r (raw output) flag. Then replace Make’s ifeq with a shell if statement for runtime evaluation:

# Get package version without quotes, prepend 'v'
PKG_VERSION=v$(shell jq -r .version package.json)
# Get latest git tag
LATEST_TAG=$(shell git describe --tags --abbrev=0 $$(git rev-list --tags --max-count=1))

enforce-versions:
	@echo "Comparing \"$(PKG_VERSION)\" & \"$(LATEST_TAG)\""
	@if [ "$(PKG_VERSION)" = "$(LATEST_TAG)" ]; then \
		echo "Versions match - proceeding with CI"; \
		exit 0; \
	else \
		echo "ERROR: Versions do NOT match - aborting CI"; \
		exit 1; \
	fi

Semantic Version Comparison (More Robust)

If you need to handle semantic versioning correctly (e.g., avoiding string comparison bugs like v1.10.0 being treated as "less than" v1.9.0), use a dedicated semver tool. For example, with the Node.js semver package:

  1. Install it in your CI environment (if not already present):

    npm install -g semver
    
  2. Update the enforce-versions target:

    enforce-versions:
     @echo "Comparing \"$(PKG_VERSION)\" & \"$(LATEST_TAG)\""
     @if semver eq $(PKG_VERSION) $(LATEST_TAG); then \
     	echo "Versions match - proceeding with CI"; \
     	exit 0; \
     else \
     	echo "ERROR: Versions do NOT match - aborting CI"; \
     	exit 1; \
     fi
    

How This Works

  • The jq -r flag ensures we get a raw, unquoted version string from package.json, so PKG_VERSION matches the format of your git tags exactly.
  • Using a shell if statement ensures the comparison happens at runtime, right when you run make enforce-versions, avoiding pre-parsing surprises from Make.
  • The semver-based check handles edge cases in version numbering that simple string comparison can’t.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 23:47:32