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

tweak ruff scripts #294

Merged
merged 2 commits into from
Dec 9, 2024
Merged

tweak ruff scripts #294

merged 2 commits into from
Dec 9, 2024

Conversation

chris48s
Copy link
Member

@chris48s chris48s commented Dec 3, 2024

I quickly pressed approve on #277 the other day, but I realised afterwards that I slightly disagree with the scripts we made and I'd like to fiddle with it before we copy this pattern elsewhere.

I think what we actually want is 2 scripts:

  1. We want a script that runs both the formatting and lint checks. I've called it lint.sh, but we could call it check.sh or ruff-check.sh if you prefer. This script doesn't write anything, but exists with not-zero if any errors are found. This is the script we want to run in CI (but we can run it locally too if we want).
  2. We want a script that applies formatting and lint fixes. I've called it format.sh but we could call it autofix.sh or ruff-autofix.sh if you want. This script writes files and this is the one we will mostly run locally. We don't want to run this in CI.

@chris48s chris48s requested a review from symroe December 3, 2024 16:05
@symroe
Copy link
Member

symroe commented Dec 3, 2024

I see what you're getting at. I think I'd like to talk over some of the implications of this approach as a group, to make sure we're aware of what we're aiming for.

For some name bike shedding:

I don't think it's obvious enough to me form the outside what the difference between "lint" and "format" is. Maybe this is my interpretation, but I don't think I could answer that if this were a technical interview.

Or at least I'd not know that one modified code and the other didn't.

I can see they they're trying to do different things though, so the two concepts seem reasonable to split into two scripts.

Also, I assume the e.g djhtml should be added to these scripts too, as they're not simply about wrapping one tool.

On that point: I think I can firmly reject the idea of ruff-*.sh: if the point of these scripts is to be the same across all projects, then we're going to end up calling our scripts ruff-lint.sh in the widget, or whatever. That's not a good idea. Also, what if someone comes up with an even more cool linting (or formatting?) Zig based program?

I think the script names shouldn't be tied to the invoked command: they should describe the concept or output.

Last point for now: I wonder if this is really one script with an argument. We could have scripts/lint.sh --modify with the default just checking. I reckon all linting tools have some version of this flag that we could set in the script.

@chris48s
Copy link
Member Author

chris48s commented Dec 4, 2024

Also, I assume the e.g djhtml should be added to these scripts too, as they're not simply about wrapping one tool.

Yep. So in a project where we are using djhtml, these would be:

format:

uv run ruff format .
uv run ruff check . --fix
git ls-files '*.html' | xargs djhtml

lint:

uv run ruff format . --check
uv run ruff check .
git ls-files '*.html' | xargs djhtml --check

On that point: I think I can firmly reject the idea of ruff-*.sh

Agreed 👍 using "ruff" in the name does not make sense.

I don't think it's obvious enough to me form the outside what the difference between "lint" and "format" is

Happy to go with different names. format.sh and check.sh ? Whatever makes sense to you. Core point is:

  • There is one that runs all your linters/code style tools and tells you about problems, but doesn't write anything
  • There is one that runs all your linters/code style tools and writes autofixes

Last point for now: I wonder if this is really one script with an argument.

I think there's 2 reasons why I would say "no"

Firstly, every line of this script will be different based on that flag. If your code is

if [condition]
    uv run ruff format .
    uv run ruff check . --fix
    git ls-files '*.html' | xargs djhtml
else
    uv run ruff format . --check
    uv run ruff check .
    git ls-files '*.html' | xargs djhtml --check

that feels like 2 scripts.

Secondly, doing argument parsing of any complexity in bash/shell is kind of a pain, or we have to invent it from first principles. I'd tend to optimise away from doing that unless we reeeeeeealy need it. In this case, I'd certainly prefer 2 scripts to

WRITE=false

# Parse arguments
while [[ $# -gt 0 ]]; do
  case "$1" in
    --write)
      WRITE=true
      shift
      ;;
    *)
      echo "Unknown option: $1"
      exit 1
      ;;
  esac
done

or whatever.

@chris48s
Copy link
Member Author

chris48s commented Dec 5, 2024

Decision: code-format.sh and code-check.sh

@chris48s chris48s merged commit cde8c43 into master Dec 9, 2024
4 checks passed
@chris48s chris48s deleted the ruff-bikeshed branch December 9, 2024 09:11
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.

2 participants