Commit Graph
8 Commits
Author SHA1 Message Date
Steven Masley 9a33a25f14 perf: scope local git hook checks (#32867)
## Why

Running full checks on every local iteration made development too slow.
Each AI change meant another wait before the next iteration could start;
one pre-push run alone took 27 minutes. The full suite should validate
the PR as a whole, not every intermediate change.

> [!IMPORTANT]
> **This is a stopgap.** My local hooks were taking over 20 minutes and
preventing me from iterating. If you’re reading this, please weigh in on
[ENG-3583](https://linear.app/codercom/issue/ENG-3583/design-fast-pre-commit-and-pre-push-checks-for-codercoder):
what should block a commit or push, what can wait for CI, and how fast
do the local checks need to be? A short RFC may be the right way to
agree on those answers.

## How

- Only packages with Go changes run tests and vet in the default
pre-push check. Pre-commit checks staged files. On the three Go files in
#32868, pre-commit took **5.4 to 6.8 seconds** across three runs, down
from **10 minutes 58 seconds** on the same change.
- Set `CODERDEV_HOOK_SKIP_PACKAGE_TESTS=1` to compile and vet changed
packages without running their tests.
- CI runs the full suite for the PR. Set `CODERDEV_HOOK_RUN_ALL=1` to
run full checks locally.

<details>
<summary>Implementation plan and decision</summary>

# Make local hooks fast without weakening CI

## Evidence
- Go changes invoked `make pre-commit`, which ran repository-wide
generation/formatting, all lint, and a slim binary build
(`scripts/githooks/pre-commit`, `Makefile:1043-1058`). Our observed run
took 658 seconds; `lint/go` alone took 500 seconds, and TypeScript lint
137 seconds.
- Go changes also invoked `make pre-push`, which ran the entire Go suite
(`./...`), JavaScript unit tests, and the site build
(`scripts/githooks/pre-push`, `Makefile:1080-1090`). Our
bounded-parallelism run took 1620 seconds; it failed in an unrelated
`agent` package goleak check, even though the changed `coderd` packages
passed.
- Pull-request CI already runs full lint, formatting/generation checks,
the Go suite on Linux/macOS/Windows, and JavaScript tests
(`.github/workflows/ci.yaml`). The hooks are opt-in; we explicitly
enabled both on this workspace.

## Decision
Keep exhaustive lint, generation, site build, and full repository test
suites in PR CI and available locally through `make pre-commit`, `make
pre-push`, or `CODERDEV_HOOK_RUN_ALL=1`. By default, pre-commit formats
and checks staged files, and pre-push runs tests and vet on changed Go
packages. Set `CODERDEV_HOOK_SKIP_PACKAGE_TESTS=1` to compile and vet
those packages without running their tests. `CODERDEV_HOOK_RUN_ALL=1`
takes precedence (the old `CODER_HOOK_RUN_ALL=1` remains supported for
compatibility); `go.mod`, `go.sum`, or an unknown diff base still
trigger the exhaustive pre-push target.

## Implementation
1. Keep the full Makefile targets unchanged. Pre-commit formats staged
Go, TypeScript, shell, Terraform, and Markdown files, runs generation
for SQL/proto inputs, and checks whitespace, shell scripts, and unstaged
output.
2. Pre-push selects Go packages from the pushed diff and runs their test
suites and `go vet`. `CODERDEV_HOOK_SKIP_PACKAGE_TESTS=1` retains
compile-and-vet-only for developers who opt out. JavaScript tests and
the site build stay in CI. Developers can run the full target locally
with `CODERDEV_HOOK_RUN_ALL=1`.
3. Add shell-level tests for staged-file routing, pushed-diff selection,
new branches, the full-check override, and fallback for shared
dependencies. Run these tests as part of `make lint` so CI covers the
hook behavior.
4. Update development workflow guidance to explain the new defaults.
Keep the build-secret PR separate; this hook change does not modify its
diff.

Review follow-up: untracked files warn rather than blocking a commit,
and Biome skips files its configuration ignores. Moving a Go file checks
both packages if they still exist. Pushing a commit other than the one
checked out prints a warning without blocking the push. After a rebase,
local package selection remains incremental; PR CI validates the full
change. Hook tests isolate their settings from the developer’s
environment.

</details>

Created by Coder Agents on behalf of @Emyrk.
2026-12-04 11:48:19 -06:00
Michael Suchacz 805b47aa00 chore: make pre-commit hook checks opt-in (#28895)
The pre-commit hook currently runs `make pre-commit` or `make
pre-commit-light` on every commit for everyone who has the repository
hooks installed. This makes it opt-in, mirroring how
`scripts/githooks/pre-push` already works: checks run only when `git
config coder.pre-commit true` is set or the workspace owner is on the
hook's allowlist, and `git config coder.pre-commit false` opts out
explicitly. By default the hook prints a one-line skip message and exits
0.

Hook installation (`core.hooksPath`) and the linked-worktree hooksPath
shield are unchanged, so `post-checkout` and `pre-push` keep working as
before. `.claude/docs/WORKFLOWS.md` and
`docs/about/contributing/CONTRIBUTING.md` describe the new opt-in.

> Xum (an AI coding agent) authored this PR on behalf of @ibetitsmike.
2026-09-02 11:16:11 +02:00
Ethan be686a8d0d fix(scripts/githooks): clear all repo-local Git env vars in hooks (#24138)
## Problem

In linked worktrees, Git hooks inherit multiple repo-local environment
variables: `GIT_DIR`, `GIT_COMMON_DIR`, `GIT_INDEX_FILE`, and others.
The
pre-commit and pre-push hooks only unset `GIT_DIR`, leaving the rest in
place.

When `make pre-commit` runs `go build`, Go tries to stamp VCS info by
shelling
out to `git`. With the leftover partial Git environment, `git` exits 128
and
the build fails:

```
error obtaining VCS status: exit status 128
    Use -buildvcs=false to disable VCS stamping.
```

This only happens inside hooks in a linked worktree — running `make
pre-commit`
directly from the terminal works fine because the repo-local vars are
not set.

## Fix

Replace the bare `unset GIT_DIR` in both hooks with a loop that clears
every
variable reported by `git rev-parse --local-env-vars`:

```sh
while IFS= read -r var; do
    unset "$var"
done < <(git rev-parse --local-env-vars)
```

This covers all 15 repo-local variables Git may inject (`GIT_DIR`,
`GIT_COMMON_DIR`, `GIT_INDEX_FILE`, `GIT_OBJECT_DIRECTORY`, etc.) and is
forward-compatible — if Git adds new local vars in the future, the loop
picks
them up automatically.
2026-04-09 01:06:12 +10:00
Mathias Fredriksson 23542cb6af feat: smart file-based target selection for scripts/githooks (#23358)
Pre-commit classifies staged files and runs make pre-commit-light
when no Go, TypeScript, or Makefile changes are present. This
skips gen, lint/go, lint/ts, fmt/go, fmt/ts, and the binary
build. A markdown-only commit takes seconds instead of minutes.

Pre-push uses the same heuristic: if only light files changed
(docs, shell, terraform, etc.), tests are skipped entirely.
Falls back to the full make targets when Go/TS/Makefile changes
are detected, CODER_HOOK_RUN_ALL=1 is set, or the diff range
can't be determined.

Also adds test-storybook to make pre-push (vitest with the
storybook project in Playwright browser mode).
2026-03-20 17:05:44 +02:00
Mathias FredrikssonandCian Johnston 660a3dad21 feat(scripts/githooks): restore pre-push hook with allowlist (#22980)
The pre-push hook was removed in #22956. This restores it with a
reduced scope (tests + site build) and an allowlist so it only runs
for developers who opt in.

Two opt-in mechanisms:

- git config coder.pre-push true (local, not committed)
- CODER_WORKSPACE_OWNER_NAME allowlist in the hook script

git config takes priority and also supports explicit opt-out for
allowlisted users (git config coder.pre-push false).

Refs #22956

---------

Co-authored-by: Cian Johnston <cian@coder.com>
2026-03-12 12:13:55 +02:00
Thomas Kosiewski e96cd5cbb2 chore(githooks): remove pre-push hook (#22956)
## Summary
- remove the `pre-push` git hook script from the repository
- remove the `make pre-push` target and related Makefile documentation
- update contributor and agent docs so they only describe the remaining
`pre-commit` hook

## Validation
- `make pre-commit`
- `git diff --check`

---
_Generated with [`mux`](https://github.com/coder/mux) • Model:
`openai:gpt-5.4` • Thinking: `high`_
2026-03-11 17:44:19 +01:00
Mathias Fredriksson dd34e3d3c2 fix(scripts/githooks): prevent agents from bypassing git hooks (#22825)
Agents hit short shell timeouts on `git commit` (~13s) before
`make pre-commit` finishes (~20s warm), then disable hooks via
`git config core.hooksPath /dev/null`. This bypasses all local checks
and, because it writes to shared `.git/config`, silently disables hooks
for every other worktree too.

Add explicit timing guidance to AGENTS.md, and write worktree-scoped
`core.hooksPath` in post-checkout, pre-commit, and pre-push hooks to
make the bypass ineffective.
2026-03-09 12:51:44 +02:00
Mathias Fredriksson 752e6ecc16 build: add pre-commit/push hooks mirroring CI checks (#22705)
This change adds git hooks and Makefile targets that mirror CI required
checks locally, catching issues before they reach CI.

This is for use by AI agents (documented in AGENTS.md).

- **pre-commit** (every commit): gen, fmt, lint, typos, slim binary
  build. Fast checks without Docker or Playwright.
- **pre-push** (before push): full CI suite including site build, tests,
  sqlc-vet, offlinedocs.
  
To use:

```sh
git config core.hooksPath scripts/githooks
```

Works in worktrees (where `.git` is a file). Bypass with `--no-verify`.
2026-03-06 16:56:11 +02:00