fix(initconfig): 必须带 cgo 构建,且失败不再静默

cmd/initconfig 通过 database/sql 使用 mattn/go-sqlite3,而 build.sh 一直用
CGO_ENABLED=0 构建它:该库在非 cgo 下退化成 static_mock.go 里的桩,sql.Open
是惰性的所以不报错、第一次 Exec 才失败,而 main.go 丢掉了所有返回值。合起来
是一个完全静默的空操作——打印凭据、退出码 0、config.db 里一个字节都没写。
安装脚本把这份凭据写进 credentials.txt,用户照着登录必然失败,全程无报错。

- build.sh: initconfig 改 CGO_ENABLED=1,并写明为何不能图省事去掉 cgo
- main.go: 每个 Exec 都检查;写完**回读比对**(不看返回码,看真实落盘内容),
  不一致即非零退出

反向验证:仍用 CGO_ENABLED=0 构建时,现在 stderr 报
"Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work"
且 exit 1(此前是 exit 0 并把凭据照打印出来)。
This commit is contained in:
JianFeeeee
2026-09-12 08:10:26 +08:00
parent ba0b5a1fe0
commit 6a3439a49c
2 changed files with 76 additions and 18 deletions

View File

@ -149,18 +149,23 @@ build_waiter() {
echo " OK ($(du -h "$out" | cut -f1))"
}
# ---- initconfig (CGO-free 配置初始化器) ----
# ---- initconfig(必须 cgo写 config.db 用的是 go-sqlite3----
#
# NSIS 安装包installer.nsi:220 File "..\build\initconfig.exe")与
# package-linux.sh 的 stage_variant 都引用它,但此前 build.sh 从不构建它——
# Windows 安装包构建会直接失败在缺文件上。
# 这里**必须** CGO_ENABLED=1。此前写的是 CGO_ENABLED=0而 cmd/initconfig 通过
# database/sql 使用 mattn/go-sqlite3CGO_ENABLED=0 时该库退化成 static_mock.go
# 里的桩sql.Open 是懒的所以不报错、第一次 Exec 才失败;而 main.go 当时忽略
# 了所有错误——于是 initconfig 打印凭据、退出码 0、一个字节都没写进 config.db。
# 安装脚本把这份凭据写进 credentials.txt用户照它登录必然失败全程无报错。
#
# NSIS 安装包installer.nsi与 package-linux.sh 的 stage_variant 都引用它,
# 但此前 build.sh 从不构建它——Windows 安装包构建会直接失败在缺文件上。
build_initconfig() {
local plat="${GOOS:-linux}/${GOARCH:-amd64}"
local out="$BUILD_DIR/initconfig${SUFFIX:+_$SUFFIX}"
if [ "$GOOS" = "windows" ]; then out="${out}.exe"; fi
echo "[BUILD] initconfig ${plat}$out"
CGO_ENABLED=0 "$GO" build -trimpath -installsuffix dynlink \
CGO_ENABLED=1 "$GO" build -trimpath -installsuffix dynlink \
-ldflags "$LDFLAGS" -o "$out" ./cmd/initconfig/
echo " OK ($(du -h "$out" | cut -f1))"
}