astral-sh / ruff-lsp

A Language Server Protocol implementation for Ruff.
Other
1.25k stars 45 forks source link

Format error in Github Action but not locally #397

Closed bedefrunner closed 6 months ago

bedefrunner commented 6 months ago

Hi!

In a repo I'm working with my team we have configured the Github Action ruff-action to run ruff. It's been working great, until a few hours ago when the github action started reporting that a file is not formatted (in all new PRs), but when running ruff locally it says that the file is well formatted.

Here's the ruff-action output in the Github Action:

Run chartboost/ruff-action@v1
  with:
    args: format --check src/
    src: .
Run if [ "$RUNNER_OS" == "Windows" ]; then
  if [ "$RUNNER_OS" == "Windows" ]; then
    python $GITHUB_ACTION_PATH/action/main.py
  else
    python3 $GITHUB_ACTION_PATH/action/main.py
  fi
  shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
  env:
    RUFF_FORMAT: github
    INPUT_ARGS: format --check src/
    INPUT_SRC: .
    INPUT_VERSION: 
    pythonioencoding: utf-8
creating virtual environment...
installing ruff...
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `src/pyproject.toml`:
  - 'extend-per-file-ignores' -> 'lint.extend-per-file-ignores'
Would reformat: src/manage.py
Would reformat: src/manage.py
2 files would be reformatted, 180 files already formatted
Error: Process completed with exit code 1.

Running it locally:

ruff format --check /src/
91 files already formatted

Also note that the Github Action output says "180 files already formatted" but when running it locally it says "91 files already formatted", the file count doesn't match.

Here's the content of the src/manage.py file:

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys

def main():
    """Run administrative tasks."""
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conf.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

if __name__ == "__main__":
    main()

Our src/project.toml:

[tool.poetry]
name = "new-core-banking"
version = "0.1.0"
description = "Fintual's core banking backend"
authors = ["Martín Ugarte <martin@fintual.com>"]

[tool.poetry.dependencies]
python = "^3.12"
Django = "^5.0.2"
django-ninja = "^1.1.0"
psycopg2 = "^2.9.9"
uvicorn = "^0.25.0"
django-pagination = "^1.0.7"
django-environ = "^0.11.2"
django-rq = "^2.10.1"
rq-scheduler = "^0.13.1"
polyfactory = "^2.13.0"
faker = "^22.2.0"
boto3 = "^1.34.20"
botocore = "^1.34.20"
django-structlog = "^7.1.0"
holidays = "^0.41"
makefile = "^1.1.0"

[tool.poetry.group.dev.dependencies]
ruff = "^0.1.11"
ipython = "^8.19.0"
ipdb = "^0.13.13"
django-extensions = "^3.2.3"

[tool.poetry.group.test.dependencies]
pytest = "^7.4.4"
pytest-django = "^4.7.0"
pytest-cov = "^4.1.0"
factory-boy = "^3.3.0"
parameterized = "^0.9.0"

[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "conf.settings"
python_files = "tests.py test_*.py *_tests.py"

[build-system]
requires = ["poetry-core>=1.8.0"]
build-backend = "poetry.core.masonry.api"

[tool.ruff]
target-version = "py312"
exclude = ['migrations']
line-length = 90

[tool.ruff.lint]
select = [
    "E", # pycodestyle
    "F", # Pyflakes
    "UP", # pyupgrade
    "B", # flake8-bugbear
    "SIM", # flake8-simplify
    "I", # isort
]

[tool.ruff.format]
quote-style = "double"
docstring-code-format = true

[tool.ruff.extend-per-file-ignores]
# Ignore `F401` in all `__init__.py` files.
"__init__.py" = ["F401"]

Local ruff version:

ruff --version
ruff 0.1.12

Thanks!

charliermarsh commented 6 months ago

"A few hours ago" makes me wonder if there are different Ruff versions running locally vs. in CI, and that it's only appearing now since we shipped some changes to the formatter style today? Is that possible?

charliermarsh commented 6 months ago

Do you get the same results if you specify the version: 0.1.12 in the GitHub Action?

bedefrunner commented 6 months ago

Hi Charlie, thank you for your quick reply.

It was a problem with the versions indeed! Locally we are using Ruff 0.1.12 and the ruff-action uses the latest Ruff version available by default (as stated in their readme). Yesterday Ruff 0.3.0 was shipped so our CI used that version and our PRs started to fail. I changed our local Ruff version to 0.3.0 and now I can see the same error that the CI is showing.

Thank you and sorry for this n00b issue!

MichaReiser commented 6 months ago

Sounds good. Thanks for reporting back.

I recommend you to use a fixed version in CI and bump it at the same time as you bump the local version to ensure CI/local stay in sync.

bedefrunner commented 6 months ago

That's a good idea Micha, will do, thanks!