Skip to content

Add pre-commit config with ruff and prettier#185

Merged
consideRatio merged 6 commits intosensmetry:mainfrom
consideRatio:pr/add-pre-commit
Mar 2, 2026
Merged

Add pre-commit config with ruff and prettier#185
consideRatio merged 6 commits intosensmetry:mainfrom
consideRatio:pr/add-pre-commit

Conversation

@consideRatio
Copy link
Copy Markdown
Collaborator

@consideRatio consideRatio commented Feb 27, 2026

pre-commit, and the rust replacement prek is a CLI that helps run autoformatters etc configured via a .pre-commit-config.yamlfile, and can be configured to run before yourgit commitsucceeds as a git hook by usingprek install` for example.

I have configured prek / pre-commit to use ruff and prettier, where prettier makes use of plugins for formatting xml, and java. Out of the box we get formatting for markdown, json, yaml, html, css, for example.

Meant to go hand in hand with activating pre-commit.ci via https://pre-commit.ci/ for automation integrated with GitHub to check the pre-commit hooks run correctly. We could also have a github workflow / job for this, but the pre-commit ci is very fast, no maintenance, and helps with both updates of the pre-commit hook versions and can push commits to PRs with fixes (can be opted out of).

Why .pre-commit-hooks.yaml?

I appreciate use of .pre-commit-hooks.yaml (with prek or pre-commit) because:

  • I am familiar to this already and have used them successfully for a long time
  • We would like this ensure the full repo is being autoformatted, and avoid needing to have multiple configs for this project which has various subprojects, and have each subproject lint
  • We get very good coverage of file types with few formatters involved (ruff + prettier)

Why pre-commit.ci to run the check?

I appreciate use of pre-commit.ci to run the automated checks as it offloads us from maintaining another Github workflow job, and has worked reliably and efficiently for a long time now. It also helps us bump the versions, and if we want, also push commit with fixes.

@consideRatio consideRatio changed the title Add pre-commit config to autoformat a lot of filetypes Add pre-commit config to autoformat with ruff and prettier Feb 27, 2026
@consideRatio consideRatio force-pushed the pr/add-pre-commit branch 4 times, most recently from 8142724 to f864ec9 Compare February 27, 2026 14:37
@andrius-puksta-sensmetry
Copy link
Copy Markdown
Collaborator

Just a note: prek is a faster alternative. It reads the same .pre-commit-config.yamland is mostly a drop-in replacement.

@consideRatio consideRatio changed the title Add pre-commit config to autoformat with ruff and prettier Add pre-commit config to autoformat with ruff and prettier, with prek or pre-commit Feb 27, 2026
@consideRatio consideRatio marked this pull request as ready for review February 27, 2026 15:00
@consideRatio consideRatio changed the title Add pre-commit config to autoformat with ruff and prettier, with prek or pre-commit Add pre-commit config with ruff and prettier Feb 27, 2026
@consideRatio
Copy link
Copy Markdown
Collaborator Author

Just a note: prek is a faster alternative. It reads the same .pre-commit-config.yamland is mostly a drop-in replacement.

Nice I hadn't heard about it before, tested it now and am happy about the outcome!

Comment thread docs/src/commands/partials/global_opts.md
Comment thread .pre-commit-config.yaml
Comment on lines +40 to +46
# pre-commit.ci is a free automation service that can both run a validation job
# and push commits with fixes to PRs, and keep the versions of configured
# pre-commit hooks up to date by creating version bumping PRs.
#
# pre-commit.ci config reference: https://pre-commit.ci/#configuration
ci:
autoupdate_schedule: monthly
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have/will you set this up on sysand repo side?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or does this need setup per-person?

Copy link
Copy Markdown
Collaborator Author

@consideRatio consideRatio Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember off the top of my head, but its a matter of a change in the github repo or github or org settings, adding pre-commit.ci as an integration with no further config needed etc.

I typically go to org settings in jupyterhub, enabling it for another specific repo as we add a project.

Edit: under github org settings, third party apps. However, it needs to be initially setup, so maybe via pre-commit.ci website i think

Copy link
Copy Markdown
Collaborator

@andrius-puksta-sensmetry andrius-puksta-sensmetry Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, maybe renovate can update deps here?

Copy link
Copy Markdown
Collaborator

@andrius-puksta-sensmetry andrius-puksta-sensmetry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Collaborator

@victor-linroth-sensmetry victor-linroth-sensmetry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just because something hasn't been included in the CI (yet) doesn't mean it's not used.

@victor-linroth-sensmetry
Copy link
Copy Markdown
Collaborator

Having thought about this a bit more and I still would prefer to handle the Java and pom.xml formatting through the Spotless Maven plugin for a two reasons:

  • Spotless together with Checkstyle can cover both formatting and linting based on the Google Java Style Guide (which seems to be the most dominant Java style a.t.m.). From what I can tell Prettier formats things differently and could potentially come to conflict with the linter.
  • Both have Maven plugins that integrates directly into the build process and so gets automatically enforced in CI.

Signed-off-by: Erik Sundell <erik.sundell@sensmetry.com>
consideRatio and others added 5 commits March 2, 2026 10:46
Signed-off-by: Erik Sundell <erik.sundell+2025@sensmetry.com>
Signed-off-by: Erik Sundell <erik.sundell@sensmetry.com>
Signed-off-by: Erik Sundell <erik.sundell+2025@sensmetry.com>
Signed-off-by: Erik Sundell <erik.sundell@sensmetry.com>
Signed-off-by: Erik Sundell <erik.sundell+2025@sensmetry.com>
Signed-off-by: Erik Sundell <erik.sundell@sensmetry.com>
Signed-off-by: Erik Sundell <erik.sundell+2025@sensmetry.com>
Signed-off-by: Erik Sundell <erik.sundell@sensmetry.com>
Signed-off-by: Erik Sundell <erik.sundell+2025@sensmetry.com>
@consideRatio
Copy link
Copy Markdown
Collaborator Author

consideRatio commented Mar 2, 2026

Having thought about this a bit more and I still would prefer to handle the Java and pom.xml formatting through the Spotless Maven plugin for a two reasons:

  • Spotless together with Checkstyle can cover both formatting and linting based on the Google Java Style Guide (which seems to be the most dominant Java style a.t.m.). From what I can tell Prettier formats things differently and could potentially come to conflict with the linter.
  • Both have Maven plugins that integrates directly into the build process and so gets automatically enforced in CI.

Thank you for reviewing this @andrius-puksta-sensmetry and @victor-linroth-sensmetry!!

For now I removed the addition related to XML and Java autoformatting. The XML was broken in how it formatted things, and probably caused issues by itself.

Findings about the the prettier-java plugin

Notes on re-activating the prettier-java plugin

If we want the Java formatter in the future from pre-commit's prettier system, we should add a .prettierrc.mjs file with the following content in order to activate the plugin once its available in the environment.

import { createRequire } from "module";

const require = createRequire(import.meta.url);

const javaPlugin = await import(require.resolve("prettier-plugin-java"));

export default {
  plugins: [javaPlugin.default],
  overrides: [
    {
      files: "*.java",
      options: {
        tabWidth: 4,
      },
    },
  ],
};

And in our .pre-commit-config.yaml, add the following to the prettier hook:

        additional_dependencies:
          # https://www.npmjs.com/package/prettier
          - "prettier@3.8.1"
          # https://www.npmjs.com/package/prettier-plugin-java
          - "prettier-plugin-java@2.8.1"

Copy link
Copy Markdown
Collaborator

@victor-linroth-sensmetry victor-linroth-sensmetry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I fully understand how pre-commits work, but seems worth trying out.

@consideRatio consideRatio merged commit d497d8c into sensmetry:main Mar 2, 2026
43 of 44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Set up formatters and linters in CI

3 participants