@@ -63,46 +63,14 @@ runs: |
63 | 63 | PRE_COMMIT_BIN=$(command -v pre-commit || true) |
64 | 64 | echo "PRE_COMMIT_BIN=$PRE_COMMIT_BIN" >> $GITHUB_ENV |
65 | 65 |
|
66 | | - # pre-commit 4.6.1 rewrote `language: node` hook installation to run |
67 | | - # `npm install --allow-git=root -g git+file://<hook checkout> <deps>` |
68 | | - # instead of packing a tarball. npm then refuses that git+file:// |
69 | | - # package ("npm error code EALLOWGIT") for any node hook that also |
70 | | - # declares additional_dependencies, which covers mirrors-prettier and |
71 | | - # mirrors-eslint. Repos only notice once something invalidates their |
72 | | - # hook cache and forces a rebuild, which then fails lint and blocks |
73 | | - # release pipelines gated on it. |
74 | | - # |
75 | | - # Runner images can pre-bake an affected pre-commit, so check the |
76 | | - # version of whatever is on PATH rather than only whether one exists. |
77 | | - # Drop this pin once upstream ships a working node installer. |
78 | | - PRE_COMMIT_BAD_FROM_VERSION="4.6.1" |
79 | | - PRE_COMMIT_PIN="pre-commit<${PRE_COMMIT_BAD_FROM_VERSION}" |
80 | | - # Default to installing our own, which is also the right answer when no |
81 | | - # pre-commit is present or its version cannot be determined. |
82 | | - PRE_COMMIT_NEEDS_PIN="true" |
83 | | - if [[ -n "$PRE_COMMIT_BIN" ]]; then |
84 | | - PRE_COMMIT_CURRENT_VERSION=$("$PRE_COMMIT_BIN" --version | awk '{print $2}') || PRE_COMMIT_CURRENT_VERSION="" |
85 | | - if [[ ! "$PRE_COMMIT_CURRENT_VERSION" =~ ^[0-9]+(\.[0-9]+)+$ ]]; then |
86 | | - echo "::warning::could not read a version from '$PRE_COMMIT_BIN --version'; installing $PRE_COMMIT_PIN" |
87 | | - # Already sorted means current >= the first affected version. |
88 | | - elif printf '%s\n%s\n' "$PRE_COMMIT_BAD_FROM_VERSION" "$PRE_COMMIT_CURRENT_VERSION" | sort -V -C; then |
89 | | - echo "pre-commit $PRE_COMMIT_CURRENT_VERSION at $PRE_COMMIT_BIN is affected; installing $PRE_COMMIT_PIN" |
90 | | - else |
91 | | - PRE_COMMIT_NEEDS_PIN="false" |
92 | | - echo "pre-commit $PRE_COMMIT_CURRENT_VERSION at $PRE_COMMIT_BIN predates the issue; using it as-is" |
93 | | - fi |
94 | | - fi |
95 | | - echo "PRE_COMMIT_PIN=$PRE_COMMIT_PIN" >> $GITHUB_ENV |
96 | | - echo "PRE_COMMIT_NEEDS_PIN=$PRE_COMMIT_NEEDS_PIN" >> $GITHUB_ENV |
97 | | -
|
98 | 66 | # Finding python |
99 | 67 | # This will set an environment variable we can use to conditionally |
100 | 68 | # install python. |
101 | 69 | PYTHON_BIN=$(command -v python || true) |
102 | 70 | echo "PYTHON_BIN=$PYTHON_BIN" >> $GITHUB_ENV |
103 | 71 | - name: Setup python |
104 | | - # Needed only when we have to install our own pinned pre-commit |
105 | | - if: env.PYTHON_BIN == null && env.PRE_COMMIT_NEEDS_PIN == 'true' && steps.pre-commit-config.outputs.exists != 'false' |
| 72 | + # Only run this if we don't already have a pre-commit on the PATH |
| 73 | + if: env.PYTHON_BIN == null && env.PRE_COMMIT_BIN == null && steps.pre-commit-config.outputs.exists != 'false' |
106 | 74 | uses: actions/setup-python@v7 |
107 | 75 | - name: Determine node version |
108 | 76 | id: node-version |
@@ -123,6 +91,49 @@ runs: |
123 | 91 | uses: actions/setup-node@v7 |
124 | 92 | with: |
125 | 93 | node-version: ${{ steps.node-version.outputs.version }} |
| 94 | + - name: Ensure a verified-safe npm outside $HOME |
| 95 | + shell: bash |
| 96 | + run: | |
| 97 | + # npm 11.9.0-11.12.x has a bug (npm/cli#9189, fixed in 11.13.0 via |
| 98 | + # npm/cli#9206/#9237): --allow-git=root incorrectly rejects |
| 99 | + # legitimate root-level git installs, breaking every `language: |
| 100 | + # node` pre-commit hook with additional_dependencies (mirrors- |
| 101 | + # prettier, mirrors-eslint) with EALLOWGIT. |
| 102 | + # |
| 103 | + # pre-commit only reuses an already-installed node/npm when it |
| 104 | + # resolves outside $HOME (pre_commit/lang_base.py::exe_exists). Our |
| 105 | + # self-hosted runners install node under $HOME (RUNNER_TOOL_CACHE), |
| 106 | + # so without this, pre-commit ignores the npm we just set up and |
| 107 | + # downloads its own via nodeenv instead -- whose bundled npm can |
| 108 | + # land in the same affected range. |
| 109 | + # |
| 110 | + # Pinned, not "latest": to bump, re-verify against the new version |
| 111 | + # first (see PR description), the same way a hook rev bump gets |
| 112 | + # verified. An open range would silently drift back into a broken |
| 113 | + # npm the same way this bug shipped in the first place. |
| 114 | + |
| 115 | +
|
| 116 | + node_bin="$(command -v node || true)" |
| 117 | + npm_bin="$(command -v npm || true)" |
| 118 | + if [[ -z "$node_bin" || -z "$npm_bin" ]]; then |
| 119 | + echo "node/npm not on PATH; leaving pre-commit to manage its own" |
| 120 | + exit 0 |
| 121 | + fi |
| 122 | +
|
| 123 | + # Deliberately not named "shims": pre-commit rejects those paths. |
| 124 | + dir="${RUNNER_TEMP:-/tmp}/action-pre-commit-node" |
| 125 | + if [[ "$dir" == "$HOME"* ]]; then |
| 126 | + dir=/tmp/action-pre-commit-node |
| 127 | + fi |
| 128 | + mkdir -p "$dir" |
| 129 | + ln -sf "$node_bin" "$dir/node" |
| 130 | + ln -sf "$npm_bin" "$dir/npm" |
| 131 | + echo "$dir" >> "$GITHUB_PATH" |
| 132 | + export PATH="$dir:$PATH" |
| 133 | +
|
| 134 | + npm install -g "$PRE_COMMIT_NPM_PIN" |
| 135 | + hash -r |
| 136 | + echo "npm pinned to $(npm --version) (was $("$npm_bin" --version))" |
126 | 137 | - name: Commitlint |
127 | 138 | # This version needs to be sync'd with the repo HEAD if a major version is cut |
128 | 139 | if: inputs.disable-commitlint != 'true' |
@@ -185,22 +196,10 @@ runs: |
185 | 196 | AWS_REGION: ${{ inputs.s3-bucket-region }} |
186 | 197 | cache-name: ${{ github.event.repository.name }}/cache-pre-commit |
187 | 198 | - name: Install Pre-commit |
188 | | - # Runs when PATH has no pre-commit, or one affected by the node-hook |
189 | | - # install bug described in "Version files". |
190 | | - if: env.PRE_COMMIT_NEEDS_PIN == 'true' && steps.pre-commit-config.outputs.exists != 'false' |
191 | | - env: |
192 | | - RUNNER_TEMP: ${{ runner.temp }} |
| 199 | + if: env.PRE_COMMIT_BIN == null && steps.pre-commit-config.outputs.exists != 'false' |
193 | 200 | run: | |
194 | | - # Install into a dedicated venv and put it first on PATH. A plain |
195 | | - # `pip install` cannot be relied on to shadow a pre-commit pre-baked |
196 | | - # into the runner image (e.g. /usr/local/bin/pre-commit), which can sit |
197 | | - # outside pip's install prefix or need elevated permissions to replace. |
198 | | - python="$(command -v python3 || command -v python)" |
199 | | - venv="$RUNNER_TEMP/action-pre-commit-venv" |
200 | | - "$python" -m venv "$venv" |
201 | | - "$venv/bin/pip" install --quiet "$PRE_COMMIT_PIN" |
202 | | - echo "$venv/bin" >> "$GITHUB_PATH" |
203 | | - echo "installed $("$venv/bin/pre-commit" --version) into $venv" |
| 201 | + pip install pre-commit |
| 202 | + pip freeze --local |
204 | 203 | shell: bash |
205 | 204 | - name: Pre-commit (from PATH) |
206 | 205 | shell: bash |
|