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

Use in projects that use Hatch project builder #665

Open
stevej2608 opened this issue Jan 9, 2025 · 13 comments
Open

Use in projects that use Hatch project builder #665

stevej2608 opened this issue Jan 9, 2025 · 13 comments
Labels
question Asking for support or clarification

Comments

@stevej2608
Copy link

The python Hatch builder command hatch fix uses an internal ruff rule configuration. This config is extremely verbose.

I'd like to configure ruff-vscode so that it mirrors the linting rules applied by hatch.

I there currently a way to do this?

@MichaReiser
Copy link
Member

@MichaReiser MichaReiser added the question Asking for support or clarification label Jan 10, 2025
@stevej2608
Copy link
Author

As I said in the original post 'the config is extremely verbose'. To quote the Hatch docs: "There are 525 selected stable rules and 129 selected preview rules... There are 175 unselected rules" What's worse the rule set changes with each release of Hatch.

Your right, I could cobble together a matching (v.large) Ruff config from the Hatch documentation. But with the Hatch project manager now running at 150k downloads per day I'd imagine there's an increasing need for the ruff-vscode plugin to recognise that Hatch is being used as the project builder and mirror the Hatch rule base automatically.

@MichaReiser
Copy link
Member

We wouldn't want to hard code the hatch config because it introduces a dependency on hatch itself. Do you know if the hatch default config exists as a toml file somehwere on disk?

@stevej2608
Copy link
Author

I've had a quick look already and found nothing obvious. I'll do some more investigations. Cheers.

@MichaReiser
Copy link
Member

Oh nice find. Yeah, we don't want to read that because it would require executing or at least parsing python code.

I don't see an obvious way on how this could be supported other than allowing other VS Code extensions to provide setting overrides (which seems complicated and I doubt that hatch wants to release a vs code extension). I suggest raising this issue on the hatch repository

@dhruvmanila
Copy link
Member

@stevej2608
Copy link
Author

https://github.com/pypa/hatch/blob/64031c1cf5d02d85203f68cb7a4fd5db2aa7a004/ruff_defaults.toml is the same but unfortunately it doesn't make it into the published hatch package.

@stevej2608
Copy link
Author

The hatch exe command:

hatch fmt --check

Conveniently emits the the ruff command and the location of the config being used.

cmd [1] | ruff check --config ~/.local/share/hatch/env/.internal/hatch-static-analysis/.config/lOvU-6Qw/pyproject.toml .
...

It would be possible for the ruff-vscode plugin to read this without parsing of executing python.

@MichaReiser
Copy link
Member

Thanks for the extra information. I don't think this is something we want to read because it introduces quiet some complexity:

  • We have to detect that a project is usinghatch fmt (this isn't the same as knowing a project uses hatch)
  • We have to parse the hatch output to find the config call
  • We'd have to do this recursively if sub folders use other hatch configurations
  • ...

A workaround for you is to specify this very path in your own vscode settings as the ruff configuration.

@stevej2608
Copy link
Author

I tried to set ruff.configuration in settings.json to the hatch generated pyproject.toml to no effect.

When I set ruff.configuration to non-existent file (eg: "$workspaceFolder/pyproject-undefined.toml") no errors are reported in
the ruff terminal window.

Is ruff.configuration ignored if a pyproject.toml is present in the project root?

@MichaReiser
Copy link
Member

Hmm, this might be the case. @dhruvmanila will know better than I.

@dhruvmanila
Copy link
Member

I tried to set ruff.configuration in settings.json to the hatch generated pyproject.toml to no effect.

I think this depends on the Ruff version that you have. Hatch will use the following Ruff version:

https://github.com/pypa/hatch/blob/64031c1cf5d02d85203f68cb7a4fd5db2aa7a004/src/hatch/env/internal/static_analysis.py#L20

On the latest Ruff version (0.9.1 as of writing this), the config throws an error:

ruff failed
  Cause: Failed to parse /Users/dhruv/Library/Application Support/hatch/env/.internal/hatch-static-analysis/.config/hYftQvxZ/ruff_defaults.toml
  Cause: TOML parse error at line 7, column 1
  |
7 | [lint]
  | ^^^^^^
Unknown rule selector: `ASYNC101`

When I set ruff.configuration to non-existent file (eg: "$workspaceFolder/pyproject-undefined.toml") no errors are reported in
the ruff terminal window.

It should report an error in the server logs:

   0.000271500s ERROR main ruff_server::session::index::ruff_settings: Unable to find editor-specified configuration file: Failed to read /private/tmp/ruff.toml

There were some observability issues in the past which has been fixed in the latest version of the extension (2025.0.0) and Ruff (0.9.0). I'd recommend to upgrade them if possible.

Is ruff.configuration ignored if a pyproject.toml is present in the project root?

No, the settings are merged as per the strategy specified in ruff.configurationPreference.

dhruvmanila added a commit to astral-sh/ruff that referenced this issue Jan 13, 2025
## Summary

I noticed this while trying out
astral-sh/ruff-vscode#665 that we use the
`Display` implementation to show the error which hides the context. This
PR changes it to use the `Debug` implementation and adds the message as
a context.

## Test Plan

**Before:**

```
   0.001228084s ERROR main ruff_server::session::index::ruff_settings: Unable to find editor-specified configuration file: Failed to parse /private/tmp/hatch-test/ruff.toml
```

**After:**

```
   0.002348750s ERROR main ruff_server::session::index::ruff_settings: Unable to load editor-specified configuration file

Caused by:
    0: Failed to parse /private/tmp/hatch-test/ruff.toml
    1: TOML parse error at line 2, column 18
         |
       2 | extend-select = ["ASYNC101"]
         |                  ^^^^^^^^^^
       Unknown rule selector: `ASYNC101`
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking for support or clarification
Projects
None yet
Development

No branches or pull requests

3 participants