diff --git a/.agents/skills/tool_usage/SKILL.md b/.agents/skills/tool_usage/SKILL.md index cc28cd1b1170..bac2c0ec4e6c 100644 --- a/.agents/skills/tool_usage/SKILL.md +++ b/.agents/skills/tool_usage/SKILL.md @@ -19,18 +19,18 @@ We use `bazelisk` for build and test. **IMPORTANT**: AI assistants use `bazelisk` instead of `bazel`. -## Pre-commit +## Prek -Running `pre-commit` is mandatory. To run it on all files: +Running `prek` is mandatory. To run it on all files: ```bash -pre-commit run -a +prek run -a ``` To validate a specific list of files: ```bash -pre-commit run --files +prek run --files ``` ## Command line tools restrictions diff --git a/.github/workflows/README.md b/.github/workflows/README.md index 1345883b62f2..28e74fbd78da 100644 --- a/.github/workflows/README.md +++ b/.github/workflows/README.md @@ -19,7 +19,7 @@ Most jobs only have a few endpoints, but due to tools which do downloads, a few have significantly more. These are: - clangd_tidy.yaml (Bazel) -- pre_commit.yaml (Bazel, pre-commit) +- prek.yaml (Bazel, prek) - nightly_release.yaml (Bazel) - tests.yaml (Bazel) diff --git a/.github/workflows/pre_commit.yaml b/.github/workflows/prek.yaml similarity index 83% rename from .github/workflows/pre_commit.yaml rename to .github/workflows/prek.yaml index f268f2085fe6..689b32206b98 100644 --- a/.github/workflows/pre_commit.yaml +++ b/.github/workflows/prek.yaml @@ -2,7 +2,7 @@ # Exceptions. See /LICENSE for license information. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -name: pre-commit +name: prek on: pull_request: @@ -14,7 +14,7 @@ permissions: contents: read # For actions/checkout. jobs: - pre-commit: + prek: runs-on: ubuntu-22.04 steps: - name: Harden Runner @@ -57,22 +57,22 @@ jobs: matrix_runner: ubuntu-22.04 remote_cache_upload: '--remote_upload_local_results=false' - - uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1 + - uses: j178/prek-action@01345c78b7de7d79edf368729212760396ba9345 # v2 - # We want to automatically create github suggestions for pre-commit file + # We want to automatically create github suggestions for prek file # changes for a pull request. But `pull_request` actions never have write # permissions to the repository, so we create the suggestions in a separate - # privileged `workflow_run` action in pre_commit_suggestions.yaml. Here, + # privileged `workflow_run` action in prek_suggestions.yaml. Here, # we upload the diffs and event configuration to an artifact for use by # that action. - - name: Collect pre-commit output + - name: Collect prek output if: failure() run: | - mkdir -p pre-commit-output - git diff > pre-commit-output/diff - cp $GITHUB_EVENT_PATH pre-commit-output/event + mkdir -p prek-output + git diff > prek-output/diff + cp $GITHUB_EVENT_PATH prek-output/event - uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0 if: failure() with: - name: pre-commit output - path: pre-commit-output/* + name: prek output + path: prek-output/* diff --git a/.github/workflows/pre_commit_suggestions.yaml b/.github/workflows/prek_suggestions.yaml similarity index 81% rename from .github/workflows/pre_commit_suggestions.yaml rename to .github/workflows/prek_suggestions.yaml index 82fb173499b2..aa68150cb4a2 100644 --- a/.github/workflows/pre_commit_suggestions.yaml +++ b/.github/workflows/prek_suggestions.yaml @@ -2,11 +2,11 @@ # Exceptions. See /LICENSE for license information. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -# Create PR suggestions based on problems found by pre-commit action. -name: 'Add pre-commit suggestions' +# Create PR suggestions based on problems found by prek action. +name: 'Add prek suggestions' -# This action is run whenever the `pre-commit` action finishes. Because the -# `pre-commit` action is an unprivileged action running on (for example) the +# This action is run whenever the `prek` action finishes. Because the +# `prek` action is an unprivileged action running on (for example) the # `pull_request` event, it's run without write permissions to the repository, so # we use a separate privileged `workflow_run` action here to pick up its results # and convert them into suggestion comments. @@ -15,7 +15,7 @@ name: 'Add pre-commit suggestions' # this file will not take effect until they are merged to trunk. on: workflow_run: - workflows: [pre-commit] + workflows: [prek] types: - completed @@ -25,7 +25,7 @@ permissions: jobs: pull-request-suggestions: - # Only generate suggestions if pre-commit for a PR failed. + # Only generate suggestions if prek for a PR failed. if: | github.event.workflow_run.conclusion == 'failure' && github.event.workflow_run.event == 'pull_request' @@ -49,15 +49,15 @@ jobs: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - - name: Download pre-commit output + - name: Download prek output uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0 with: - name: pre-commit output + name: prek output github-token: ${{ secrets.GITHUB_TOKEN }} run-id: ${{ github.event.workflow_run.id }} # Use https://github.com/reviewdog/reviewdog to create PR suggestions - # matching the diff that pre-commit created. + # matching the diff that prek created. - name: Create suggestions env: REVIEWDOG_GITHUB_API_TOKEN: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7ad4441d4e36..72fc6ba80850 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,8 +11,7 @@ default_language_version: python: python3 # Defaults to python2, so override it. repos: - - repo: https://github.com/pre-commit/pre-commit-hooks - rev: 3e8a8703264a2f4a69428a0aa4dcb512790b2c8c # frozen: v6.0.0 + - repo: builtin hooks: - id: check-added-large-files - id: check-case-conflict diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e43ae049a0ad..ad7d03ed5f52 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -227,7 +227,7 @@ trying to write proposals, both types of contributor access will help. Please see our [contribution tool](/docs/project/contribution_tools.md) documentation for information on setting up a git client for Carbon development, as well as helpful tooling that will ease the contribution process. For example, -[pre-commit](https://pre-commit.com) is used to simplify +[prek](https://github.com/j178/prek) is used to simplify [code review](/docs/project/code_review.md). #### Using AI-based contribution tools @@ -407,7 +407,7 @@ Changes to Carbon documentation follow the Markdown files should additionally use [Prettier](https://prettier.io) for formatting, which we automate with -[pre-commit](/docs/project/contribution_tools.md#main-tools). +[prek](/docs/project/contribution_tools.md#running-prek). Other style points to be aware of are: diff --git a/docs/project/contribution_tools.md b/docs/project/contribution_tools.md index 8deea62e3a5a..a9c95e76c911 100644 --- a/docs/project/contribution_tools.md +++ b/docs/project/contribution_tools.md @@ -20,7 +20,7 @@ contributions. - [macOS](#macos) - [Tools](#tools) - [Main tools](#main-tools) - - [Running pre-commit](#running-pre-commit) + - [Running prek](#running-prek) - [Optional tools](#optional-tools) - [Jujutsu (`jj`)](#jujutsu-jj) - [AI assistants](#ai-assistants) @@ -58,17 +58,19 @@ sudo apt install \ libc++-dev \ libc++abi-dev \ lld \ - lldb \ - pre-commit + lldb # Install `uv` for Python scripts. curl -LsSf https://astral.sh/uv/install.sh | sh +# Install `prek` for Git hooks. +cargo install --locked prek + # Set up git. # If you don't already have a fork: gh repo fork --clone carbon-language/carbon-lang cd carbon-lang -pre-commit install +prek install # Run tests. ./scripts/run_bazelisk.py test //...:all @@ -123,14 +125,14 @@ brew install \ gh \ llvm \ uv \ - pre-commit + prek # IMPORTANT: Make sure `llvm` is added to the PATH! It's separate from `brew`. # Set up git. gh repo fork --clone carbon-language/carbon-lang cd carbon-lang -pre-commit install +prek install # Run tests. Note homebrew makes `bazel` an alias to `bazelisk`. bazel test //...:all @@ -179,31 +181,36 @@ These tools are essential for work on Carbon. issues, see [troubleshooting build issues](#troubleshooting-build-issues). - [gh CLI](https://github.com/cli/cli): Helps with GitHub. - - [pre-commit](https://pre-commit.com): Validates and cleans up git - commits. + - [prek](https://github.com/j178/prek): Validates and cleans up commits. - `autoupdate_testdata.py`: Updates expected output for tests. - Usage: `./toolchain/autoupdate_testdata.py [files...]` - This is essential when changes affect compiler output (diagnostics, SemIR, etc.). -#### Running pre-commit +#### Running prek -[pre-commit](https://pre-commit.com) is typically set up using -`pre-commit install`. When set up in this mode, it will check for issues when -`git commit` is run. A typical commit workflow looks like: +[prek](https://github.com/j178/prek) runs linters and formatters. It's a drop-in +replacement for [pre-commit](https://pre-commit.com/). -1. `git commit` to try committing files. This automatically executes - `pre-commit run`, which may fail and leave files modified for cleanup. -2. `git add .` to add the automatically modifications done by `pre-commit`. -3. `git commit` again. +To use it: -You can also use `pre-commit run` to check pending changes without `git commit`, -or `pre-commit run -a` to run on all files in the repository. +1. Install it by way of `cargo install --locked prek`. +2. Run `prek install` to set up the git hooks. -> NOTE: Some developers prefer to run `pre-commit` on `git push` instead of +A typical commit workflow looks like: + +1. `git commit` to try committing files. This automatically executes `prek run`, + which may fail and leave files modified for cleanup. +2. `git add .` to add the automatic modifications done by hooks. +3. `git commit` again. + +You can also use `prek run` to check pending changes without `git commit`, or +`prek run -a` to run on all files in the repository. + +> NOTE: Some developers prefer to run `prek` on `git push` instead of > `git commit` because they want to commit files as originally authored instead -> of with pre-commit modifications. To switch, run -> `pre-commit uninstall && pre-commit install -t pre-push`. +> of with automatic modifications. To switch, run +> `prek uninstall && prek install --hook-type pre-push`. ### Optional tools @@ -215,7 +222,7 @@ considering if they fit your workflow. - `rs-git-fsmonitor` and Watchman: Helps make `git` run faster on large repositories. - **WARNING**: Bugs in `rs-git-fsmonitor` and/or Watchman can result in - `pre-commit` deleting files. If you see files being deleted, disable + `prek` deleting files. If you see files being deleted, disable `rs-git-fsmonitor` with `git config --unset core.fsmonitor`. - [vim-prettier](https://github.com/prettier/vim-prettier): A vim integration for [Prettier](https://prettier.io/), which we use for formatting. @@ -309,8 +316,7 @@ allowlisting) are: bazelisk build bazelisk test bazelisk run //toolchain/testing:file_test -- -clang-format -pre-commit run +prek run ./toolchain/autoupdate_testdata.py # Shell commands. Note that these allow reading arbitrary files on your local diff --git a/proposals/scripts/new_proposal.py b/proposals/scripts/new_proposal.py index 396a21b7df74..f582bd344725 100755 --- a/proposals/scripts/new_proposal.py +++ b/proposals/scripts/new_proposal.py @@ -171,7 +171,7 @@ def main() -> None: else: git_bin = _find_tool("git") jj_bin = None - precommit_bin = _find_tool("pre-commit") + prek_bin = _find_tool("prek") # Verify there are no uncommitted changes (jj has no equivalent). if git_bin: @@ -263,11 +263,11 @@ def main() -> None: with open(final_path, "w") as final_file: final_file.write(content) - # Run pre-commit for a ToC update, then push the PR update. + # Run prek for a ToC update, then push the PR update. final_desc = "Filling out template with PR %d" % pr_num if git_bin: _run([git_bin, "add", temp_path, final_path]) - _run([precommit_bin, "run"], check=False) + _run([prek_bin, "run"], check=False) _run([git_bin, "commit", "--amend", "-m", final_desc]) _run([git_bin, "push", "--force-with-lease"]) @@ -275,7 +275,7 @@ def main() -> None: assert jj_bin # For mypy. _run( - [precommit_bin, "run", "--files", "$(jj diff --name-only)"], + [prek_bin, "run", "--files", "$(jj diff --name-only)"], check=False, ) _run([jj_bin, "describe", "-m", final_desc]) diff --git a/scripts/bazel_mod_deps.py b/scripts/bazel_mod_deps.py index 515994df249c..d59f21b820f5 100755 --- a/scripts/bazel_mod_deps.py +++ b/scripts/bazel_mod_deps.py @@ -5,7 +5,7 @@ # /// -"""Run `bazel mod deps` for pre-commit. Lets pre-commit handle modifications.""" +"""Run `bazel mod deps` for prek. Lets prek handle modifications.""" __copyright__ = """ Part of the Carbon Language project, under the Apache License v2.0 with LLVM diff --git a/scripts/check_build_graph.py b/scripts/check_build_graph.py index d5e9e30f2b47..5b91830f1b4a 100755 --- a/scripts/check_build_graph.py +++ b/scripts/check_build_graph.py @@ -5,7 +5,7 @@ # /// -"""Verify that the bazel build graph is in a valid state, for pre-commit.""" +"""Verify that the bazel build graph is in a valid state, for prek.""" __copyright__ = """ Part of the Carbon Language project, under the Apache License v2.0 with LLVM diff --git a/scripts/run_buildifier.py b/scripts/run_buildifier.py index 51400b2baa14..6bf4cedf9946 100755 --- a/scripts/run_buildifier.py +++ b/scripts/run_buildifier.py @@ -5,7 +5,7 @@ # /// -"""Runs buildifier on passed-in BUILD files, mainly for pre-commit.""" +"""Runs buildifier on passed-in BUILD files, mainly for prek.""" __copyright__ = """ Part of the Carbon Language project, under the Apache License v2.0 with LLVM diff --git a/scripts/scripts_utils.py b/scripts/scripts_utils.py index a874286cff30..6d797732a054 100644 --- a/scripts/scripts_utils.py +++ b/scripts/scripts_utils.py @@ -129,7 +129,7 @@ def _get_cached_binary(name: str, url: str, want_hash: str) -> str: cache_dir.mkdir(parents=True, exist_ok=True) # Hold a lock while checksumming and downloading the path. Otherwise, - # parallel runs by pre-commit may conflict with one another with + # parallel runs by prek may conflict with one another with # simultaneous downloads. with open(cache_dir.joinpath(f"{name}.lock"), "w") as lock_file: fcntl.lockf(lock_file.fileno(), fcntl.LOCK_EX)