aspect-build / aspect-build/rules_lint

[Bug]: lint_ruff_aspect does not fully support sorting imports

Open
#303 6 comments 1 reaction 0 assignees View on GitHub
bug help wanted
Dominant language
Starlark
Stars
154
Forks
125
Avg merge
3d 21h
Merged PRs (30d)
20

Description

### What happened?

When enabling sorting imports in e.g. pyproject.toml, there are cases ruff cannot categorize an import to be StandardLibrary, FirstParty, or ThirdParty, because it does not see a full source tree.

### Version

Development (host) and target OS/architectures:

Output of `bazel --version`: bazel 7.2.1

Version of the Aspect rules, or other relevant rules from your
`WORKSPACE` or `MODULE.bazel` file: v1.0.0-rc4

Language(s) and/or frameworks involved: Python

### How to reproduce

Consider a simple source tree as the following:

```
src
|_foo.py
|_foo_test.py
```
where `foo_test.py` has imports like:

```
import known_third_party
import foo
```

`pyproject.toml`:

```toml
[tool.ruff]
src = ["src"]

[tool.ruff.lint]
extend-select = ["I"]
```

And the following bazel stuff:

```bazel
py_binary(
name = "foo",
srcs = ["foo.py"],
)

py_test(
name = "foo_test",
srcs = ["foo_test.py"],
deps = [":foo"]
)

ruff = lint_ruff_aspect(
binary = "@@//path/to:ruff",
configs = ["@@//:pyproject.toml"],
)

ruff_test = lint_test(aspect = ruff)

ruff_test(
name = "ruff_test",
srcs = [":foo_test"],
)
```

`bazel test //path/to:ruff_test` would fail because ruff cannot find `foo.py` so it categorized it as a ThirdParty, which means it wants this instead:

```python
import foo
import known_third_party
```

### Any other information?

After a closer look at this, it seems `lint_ruff_aspect` only populates `srcs` to be in the sandbox source tree, which means only `src/foo_test.py` is in the source tree, not deps.

It also doesn't help having:
```bazel
ruff_test(
name = "ruff_test",
srcs = [":foo", ":foo_test"],
)
```
because it would just be two separated ruff invocations, one with `foo.py` in source tree and the other one with `foo_test.py` in source tree.

Another thing to point out is, `lint_ruff_aspect` does not support having `pyproject.toml` not in project root. I left a comment [here](https://github.com/aspect-build/rules_lint/pull/60/files#r1658418689).

Contributor guide

Open the contributing guide

Research direction

Start with the lint_ruff_aspect configuration and reproduce the case using the provided src/foo.py, src/foo_test.py, pyproject.toml, and Bazel targets. Run bazel test //path/to:ruff_test and inspect the sandbox contents and dependency handling described in the issue. Done means Ruff can correctly classify imports using the relevant source tree and the documented pyproject.toml location is supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.