Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sage-monorepo): configure pre-commit hooks to format and lint files #2801

Merged

Conversation

tschaffter
Copy link
Member

@tschaffter tschaffter commented Sep 11, 2024

Closes #2799

Changelog

  • Update Husky from v8 to v9
  • Install and configure lint-staged
  • Format and lint files during pre-commit (except Python files)
  • Remove deprecated lines at the top of Husky git hook scripts
  • Remove unused google-java-format

Preview

$ git commit -m 'format YAML files'
✔ Preparing lint-staged...
✔ Running tasks for staged files...
✔ Applying modifications from tasks...
✔ Cleaning up temporary files...
[sage-monorepo/pre-commit-format 855b9791] format YAML files
 2 files changed, 15 insertions(+), 10 deletions(-)
 rewrite lint-staged.config.js (91%)

Formatting with nx format Vs. prettier

nx format is a wrapper around Prettier that needs a patch to format Java files. This patch could be removed if we end up not using nx format. The two instructions shown below were specified in lint-staged JS configuration.

prettier --write ${filenames.join(' ')}:

$ hyperfine --runs 10 'git commit -m ""' --ignore-failure
Benchmark 1: git commit -m ""
  Time (mean ± σ):      6.067 s ±  0.374 s    [User: 4.883 s, System: 1.434 s]
  Range (min … max):    5.498 s …  6.699 s    10 runs
 
  Warning: Ignoring non-zero exit code.

nx format:write --files=${filenames.join(',')}:

$ hyperfine --runs 10 'git commit -m ""' --ignore-failure
Benchmark 1: git commit -m ""
  Time (mean ± σ):     10.928 s ±  0.975 s    [User: 8.541 s, System: 2.483 s]
  Range (min … max):    9.097 s … 12.069 s    10 runs
 
  Warning: Ignoring non-zero exit code.

Running Prettier directly is faster

Deprecated lines at the top of Husky files

lint-staged showed this message, which led to the removal of the lines. The Husky documentation site also no longer includes these lines.

$ git commit -m 'add lint-staged'
husky - DEPRECATED

Please remove the following two lines from .husky/pre-commit:

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

They WILL FAIL in v10.0.0

@tschaffter tschaffter self-assigned this Sep 11, 2024
@tschaffter tschaffter marked this pull request as ready for review September 11, 2024 16:44
@tschaffter tschaffter merged commit 5c6b163 into Sage-Bionetworks:main Sep 11, 2024
8 of 9 checks passed
@tschaffter tschaffter deleted the sage-monorepo/pre-commit-format branch September 11, 2024 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Configure pre-commit hooks to format and lint files
1 participant