|
In our repo, we are generating server types from an OpenAPI spec and then checking if a file was generated by using [tasks."spec:build"]
run = "openapi-ts"
[tasks."spec:check"]
run = '''
mise run src:spec:build
if git diff --quiet --exit-code ./routes; then
echo "All good, no need to regenerate the openapi spec."
else
echo "Change detected. Re-run mise run src:spec:build and commit the results." >&2
exit 1
fi
'''This is my hk file amends "package://github.com/jdx/hk/releases/download/v1.52.0/hk@1.52.0#/Config.pkl"
import "package://github.com/jdx/hk/releases/download/v1.52.0/hk@1.52.0#/Builtins.pkl"
local linters = new Mapping<String, Step> {
["src:spec:check"] {
check = "mise run src:spec:check"
}
}
hooks {
["pre-commit"] {
stash = "git"
steps = linters
}
}and this is my package.json (minimal ofc) {
"private": true,
"type": "module",
"module": "index.ts",
"dependencies": {
"fastify": "^5.10.0",
"fastify-openapi-glue": "^4.11.3"
},
"devDependencies": {
"@hey-api/openapi-ts": "^0.99.0",
"typescript": "5.9.3"
},
}I can workaround the issue by unsetting the GIT_ env vars hk adds. [tasks."spec:check"]
run = '''
mise run src:spec:build
if env -u GIT_DIR -u GIT_INDEX_FILE -u GIT_WORK_TREE git diff --quiet --exit-code ./routes; then
echo "All good, no need to regenerate the openapi spec."
else
echo "Change detected. Re-run mise run src:spec:build and commit the results." >&2
exit 1
fi
'''But I would like to not do this. I use Git worktrees if that has an impact. Removing the stash does not fix the problem. |
Replies: 1 comment 8 replies
|
Thanks for the detailed report. I tried reproducing this with the same basic pattern (a pre-commit check that modifies a tracked generated file and then runs One important detail is that these variables are supplied by Git to hooks, rather than created by hk. hk preserves them because they identify the repository and, importantly, the index that is actually being committed. Could you share the exact commit command or Git client you use, along with the output of the following from inside the failing task? printf 'pwd=%s\n' "$PWD"
git --version
env | sort | grep '^GIT_'
git rev-parse --show-toplevel --git-dir --git-path index
git status --short
git diff --name-status -- ./routes
env -u GIT_DIR -u GIT_INDEX_FILE -u GIT_WORK_TREE \
git diff --name-status -- ./routesThe two AI-assisted — Tool: Codex; model: openai/gpt-5; version: unavailable. |
That diagnosis is correct. Git starts a non-bare hook at the worktree root, where
GIT_DIRwithoutGIT_WORK_TREEis valid. hk then runs the scoped step fromsrc/, so nested Git commands treat that directory as the worktree root and misalign the index paths.I opened #1200 to fix this in hk. It makes the resolved worktree root explicit as
GIT_WORK_TREEwhile preservingGIT_INDEX_FILE, since that can identify the temporary index actually being committed. The regression test exercises a real commit from a linked worktree with the step scoped to a subdirectory, in both libgit2 modes.Thanks for tracking down the full-hook diagnostic, and sorry for the earlier misdiagnoses.
AI-assisted — Tool: …