Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(build): honor GOOS as a Make variable when naming build outputs (#7309)

Bohan Jiang1ea40db1c4a4
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 80a80da9e5..a12a424691 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -102,6 +102,9 @@ jobs:
- 'scripts/helm-config.test.sh'
- 'scripts/test-go.sh'
- 'scripts/test-go.test.sh'
+ # makefile-build.test.sh asserts how `make build` names its
+ # output binaries, so a change to either side must run it.
+ - 'scripts/makefile-build.test.sh'
- 'Makefile'
- '.github/workflows/ci.yml'
sqlc:
@@ -499,6 +502,10 @@ jobs:
if: ${{ needs.changes.outputs.backend == 'true' }}
run: bash scripts/helm-config.test.sh
+ - name: Test build output naming
+ if: ${{ needs.changes.outputs.backend == 'true' }}
+ run: bash scripts/makefile-build.test.sh
+
- name: Build
if: ${{ needs.changes.outputs.backend == 'true' }}
run: cd server && go build ./...
diff --git a/Makefile b/Makefile
index c492d2a567..5c6dec1a50 100644
--- a/Makefile
+++ b/Makefile
@@ -278,8 +278,18 @@ multica: ## Run the multica CLI entrypoint directly from the Go source tree
VERSION ?= $(shell git describe --tags --match 'v[0-9]*' --always --dirty 2>/dev/null || echo dev)
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
DATE ?= $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
-EXE := $(if $(filter windows,$(shell go env GOOS)),.exe,)
-
+# Windows will not execute an extensionless binary, so a source build there has
+# to name its outputs the way the target platform expects — otherwise the CLI
+# builds fine and then fails to re-exec itself as a daemon (#7255). GOOS reaches
+# a build two ways: as an environment variable (`GOOS=windows make build`) and
+# as a Make variable (`make build GOOS=windows`). The top-level `export` sends
+# both forms to the recipe, so `go build` honors both and the suffix has to as
+# well; `$(GOOS)` covers the Make-variable form, which a parse-time
+# `go env GOOS` cannot see. Target-specific so only `build` pays for the probe:
+# a global assignment runs `go env` on every target — `export` expands even a
+# recursive one — which prints `go: Command not found` on frontend-only
+# checkouts with no Go toolchain installed.
+build: EXE = $(if $(filter windows,$(or $(GOOS),$(shell go env GOOS))),.exe,)
build: ## Build the server, CLI, and migrate binaries into server/bin
cd server && go build -ldflags "-X main.version=$(VERSION) -X main.commit=$(COMMIT)" -o bin/server$(EXE) ./cmd/server
cd server && go build -ldflags "-X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(DATE)" -o bin/multica$(EXE) ./cmd/multica
diff --git a/scripts/makefile-build.test.sh b/scripts/makefile-build.test.sh
new file mode 100755
index 0000000000..195c7f5616
--- /dev/null
+++ b/scripts/makefile-build.test.sh
@@ -0,0 +1,85 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+# `make build` has to name its outputs the way the *target* platform expects.
+# Windows refuses to execute an extensionless file, so a Windows source build
+# whose artifacts are named `multica` produces a CLI that cannot re-exec itself
+# as a daemon (#7255) — the build succeeds and the failure surfaces later as a
+# misleading "not found" at startup.
+#
+# The suffix is derived from GOOS, which reaches a build two ways: as an
+# environment variable and as a Make variable on the command line. `go build`
+# honors both, so a suffix that honors only one silently rebuilds the original
+# bug. Nothing else covers this: the Go test suite never runs the Makefile, and
+# CI's own build steps call `go build` directly.
+
+ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
+cd "$ROOT_DIR"
+
+fail() {
+ echo "$1" >&2
+ exit 1
+}
+
+# The recipe reads `-o bin/server$(EXE) ./cmd/server`, so the trailing space is
+# what keeps an expected `bin/server` from matching an emitted `bin/server.exe`.
+require_outputs() {
+ local label=$1 suffix=$2 output=$3 binary
+
+ for binary in server multica migrate; do
+ grep -Fq -- "-o bin/${binary}${suffix} " <<<"$output" ||
+ fail "$label: expected 'go build ... -o bin/${binary}${suffix}', got:
+$output"
+ done
+}
+
+# A `go` shim that records every invocation, so the assertions below can tell
+# "the Makefile probed the toolchain" from "the Makefile did not" without
+# depending on how the host PATH is laid out.
+probe_dir="$(mktemp -d)"
+trap 'rm -rf "$probe_dir"' EXIT
+real_go="$(command -v go || true)"
+cat >"$probe_dir/go" <<EOF
+#!/usr/bin/env bash
+echo "\$@" >>"$probe_dir/invocations"
+[ -n "$real_go" ] || exit 1
+exec "$real_go" "\$@"
+EOF
+chmod +x "$probe_dir/go"
+
+probe_count() {
+ [ -f "$probe_dir/invocations" ] || { echo 0; return; }
+ wc -l <"$probe_dir/invocations" | tr -d ' '
+}
+
+require_outputs "GOOS=windows in the environment" .exe \
+ "$(GOOS=windows make -n build)"
+require_outputs "GOOS=windows as a Make variable" .exe \
+ "$(make -n build GOOS=windows)"
+require_outputs "GOOS=linux in the environment" "" \
+ "$(GOOS=linux make -n build)"
+require_outputs "GOOS=darwin as a Make variable" "" \
+ "$(make -n build GOOS=darwin)"
+
+# Non-build targets must not reach for a Go toolchain: `make help` and
+# `make clean` are the first thing a frontend-only contributor runs, and a
+# global suffix assignment makes every one of them print `go: Command not
+# found` on a checkout with no Go installed.
+PATH="$probe_dir:$PATH" make -n clean >/dev/null
+PATH="$probe_dir:$PATH" make help >/dev/null
+[ "$(probe_count)" = 0 ] ||
+ fail "non-build targets invoked go $(probe_count) time(s): $(cat "$probe_dir/invocations")"
+
+# With no GOOS given, the suffix has to follow the toolchain's own default.
+if [ -n "$real_go" ]; then
+ host_suffix=""
+ [ "$("$real_go" env GOOS)" = windows ] && host_suffix=.exe
+ require_outputs "no GOOS set" "$host_suffix" \
+ "$(PATH="$probe_dir:$PATH" make -n build)"
+ [ "$(probe_count)" != 0 ] ||
+ fail "no GOOS set: expected the build target to resolve GOOS via go env"
+else
+ echo "skipping the host-default case: no go toolchain on PATH"
+fi
+
+echo "✓ make build names its outputs for the target platform"