Facet
DeutschInstall
Integrations & setup

Catch broken FACET records before you push, with a pre-commit hook

Install a git HookA hook is a script a tool runs automatically at a fixed point, such as at the start of a session or Git before a commit. FACET's hooks bring the matching record into the session and stop commits that break records or anchors.In the glossary that runs the same FACET check as your pull requests, which confirms feature descriptions and code still point at each other, only on the change you are committing, so broken links never reach the shared branch.

  • 5 min read
  • 5 steps
  • Gitnpm
  • Your coding agent

You tidy up a folder name, commit, push and switch to the next task. Ten minutes later CI is red: a FACET recordA FACET record is a short, checkable description of one feature, kept as a YAML file next to the code. It states why the feature exists, the behaviour it promises as scenarios, and the tests that check it.In the glossary no longer matches its path, and the code still points at the old id. Now you context-switch back, fix it, push again, and wait for the pipeline a second time.

FACET keeps a short description of each feature, called a record, in a file next to your code, and a comment in the code, called an AnchorAn anchor is a comment in the source code, starting with @facet:, that names the record the code belongs to. FACET reads these comments to know where each feature is implemented.In the glossary, names the record it belongs to. Rename a folder, and the two can stop matching. By the end of this guide that commit never leaves your laptop. facet setup precommit installs a git pre-commit hook that runs facet lint --staged: it checks the change you are about to commit and refuses it when it introduces a broken record or anchor. Errors that were already there before your change do not stop you.

The commands below use a small example shop written in TypeScript, set up as in One answer instead of three. They work the same in any Git repository with FACET records.

Step 1Put facet on your PATH

The hook calls facet by name. A plain git commit does not see the project's node_modules/.bin, so the development dependency from “One answer instead of three” is not enough. Install the same version globally:

sh
  • npm install --global facet-framework

Check that your shell finds it outside npx:

sh
  • facet version

The first line starts with facet core. Keep the global version the same as the one in package.json, so the hook and CI judge with the same rules.

Step 2Install the pre-commit hook

Preview what will happen. Without options nothing is written:

sh
  • npx facet setup precommit
console
  • npx facet setup precommit
  • Dry run: would install ~/demo-shop/.git/hooks/pre-commit, which runs 'facet lint --staged' and fails the commit when 'facet' is not on PATH.
  • Run with --write to do it.

Then install it:

sh
  • npx facet setup precommit --write
console
  • npx facet setup precommit --write
  • Wrote ~/demo-shop/.git/hooks/pre-commit.

The hook is a short shell script in .git/hooks/pre-commit. Git does not version that folder, so each person on the team runs this step once in their clone.

Step 3Try the commit that would break CI

Rename the record folder, as a quick tidy-up might:

sh
  • git mv docs/facets/features/checkout/discounts docs/facets/features/checkout/discount-codes
  • git commit -m "Rename the discounts folder"
Terminal output of git commit: two errors, the @facet: tag in src/checkout/discount.ts references checkout/discounts/redeem-code which has no record, and the record in discount-codes declares an id its path does not imply; 2 errors; the commit is not created.Terminal output of git commit: two errors, the @facet: tag in src/checkout/discount.ts references checkout/discounts/redeem-code which has no record, and the record in discount-codes declares an id its path does not imply; 2 errors; the commit is not created.
The hook refuses the commit and names both broken links.

No commit was created. The record's id still says checkout/discounts/redeem-code, its new path implies checkout/discount-codes/redeem-code, and the anchor in the code points at an id that no longer has a record at the matching path. These are the same errors the pull request check would have reported ten minutes later.

To see the same result without committing, run the check the hook runs:

sh
  • npx facet lint --staged

Step 4Fix it and commit

A rename of a record is three changes that belong together: the folder, the id in the record and every @facet: anchor that names it. Here, the quick fix is to undo the rename:

sh
  • git mv docs/facets/features/checkout/discount-codes docs/facets/features/checkout/discounts

Then commit something that does not break anything, for example a line added to README.md:

sh
  • git add README.md
  • git commit -m "Mention free shipping in the README"
Terminal output of git commit: the hook prints 1 record, 1 anchor checked, 0 errors, 0 warnings, then git creates the commit Mention free shipping in the README.Terminal output of git commit: the hook prints 1 record, 1 anchor checked, 0 errors, 0 warnings, then git creates the commit Mention free shipping in the README.
A clean change passes the hook and is committed.

The hook only judges what the commit introduces. If HEAD already contains an error, for example because someone committed with --no-verify, an unrelated commit still goes through, and npx facet lint on the whole repository keeps reporting the old error.

To remove the hook again:

sh
  • npx facet setup precommit --remove

It deletes the file only if FACET wrote it.

Step 5If the hook says something else

  • facet pre-commit: 'facet' is not on PATH, so the staged records and anchors were NOT checked.: the commit was refused because the hook could not run the check. Install facet globally as in step 1, or check that the global npm folder is on your PATH. A GUI git client may use a different PATH than your terminal.
  • npm error code EACCES during the global install: your npm installs global packages into a system folder you cannot write to. Follow npm's guide to change the default directory, or use a Node version manager; do not use sudo.
  • husky is present, so no git hook file is written. Add this line to .husky/pre-commit:: your project manages hooks with husky. Add the printed line facet lint --staged to .husky/pre-commit yourself.
  • … already exists and was not written by FACET, so it is left alone.: you already have a pre-commit hook. FACET prints the lines to add to it and changes nothing.

For more on catching broken changes before they reach others, read “That line can go.” Are you sure?

Sources