pants icon indicating copy to clipboard operation
pants copied to clipboard

Bug Report: Pants-Ruff Import Order Bug

Open siggi84 opened this issue 1 year ago • 5 comments

Describe the bug The bug shows up when running ruff, with the isort (I) rules enabled, through pants. When running the ruff --fix ... command on a file with both a first and third party import (unsorted), the import order is changed as expected. However, when running pants fmt fix lint ::, which also runs ruff, the import order remains unchanged due to wrong distinction between first and third party dependencies. The expected behavior is for ruff to give the same result whether executed directly or through pants.

Pants version 2.16.0.dev7

OS Only tried this on linux (Ubuntu 22.10).

Additional info A minimal example that shows the issue, how to reproduce it.

The issue can be mitigated by adding the source root entries in pants.toml to ruff.toml. Something like

src = ["src/python"]

However, having to specify the source roots in two places is not desirable.

siggi84 avatar Mar 04 '23 19:03 siggi84

This is just https://github.com/pantsbuild/pants/issues/15069 mapped onto ruff lol :joy:

thejcannon avatar Mar 06 '23 15:03 thejcannon

Any news on this? This has been causing havoc with a new Pants-based monorepo implementation my company is slowly adopting 😬

Looks like #15069 was opened 2 years ago... is this something that is fixable? Or is there a workaround that works for ruff? Workarounds listed in #15069 don't seem to work for us.

We tried adding all the source folders to the src setting in a ruff.toml file (duplicating the source.root_patterns setting in pants.toml) to no avail.

olivercoleman-switchdin avatar Apr 08 '24 00:04 olivercoleman-switchdin

We also experienced a similar issue regarding:

  • src/ai/backend/manager/models/alembic vs. alembic

and workarounded this by adding such errorneously detetced packages to [tool.ruff.isort].known_third_party in pyproject.toml as in lablup/backend.ai#2005.

There was another case in the past:

  • src/ai/backend/common/redis vs. redis (from redis-py)

Here we have renamed ai.backend.common.redis to ai.backend.common.redis_helper.

achimnol avatar Apr 08 '24 07:04 achimnol

We just needed to add the known-first-party option to fix our issue, thanks @achimnol

olivercoleman-switchdin avatar Apr 11 '24 00:04 olivercoleman-switchdin

We were able to fix this by setting the ruff setting fix to true. The behaviour after running pants fmt lint :: is that ruff sorts the imports, and finishes successfully.

rajeshwar-nu avatar Apr 23 '24 12:04 rajeshwar-nu