astral-sh / ruff-vscode

A Visual Studio Code extension with support for the Ruff linter.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Organize imports on save with custom config file location

toinbis opened this issue · comments

I have issues with import formats on save not respect the ruff config file.

    "ruff.lint.args": [
        "--config=${workspaceFolder}/src/shared_core/static_configs/ruff.toml",
        "check",
        "--unsafe-fixes",
        "--fix"
    ],


    "ruff.format.args": [
        "--config=${workspaceFolder}/src/shared_core/static_configs/ruff.toml",
        "--preview"
    ],

    
    "[python]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
          "source.fixAll": "always",
          "source.organizeImports": "always"
        },
        "editor.defaultFormatter": "charliermarsh.ruff"
      },

This is my vscode settings.json. I get consistent behaviour (=reading and using custom config file location) for:

  • ruff.lint.args & ruff format.args; VSCode highlights errors based on the config file;
  • The suggested fix dropdown:
    Screenshot 2024-02-20 at 14 41 12
    It also fixes according to my config file.
  • The same command run from CLI also behaves identically.

I.e. All as expected!

Until I want to do organize imports automatically on save. So these settings come into effect:

"[python]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
          "source.fixAll": "always",
          "source.organizeImports": "always"
        },
        "editor.defaultFormatter": "charliermarsh.ruff"
      }

And they seem to just use default ruff without any custom config. And it just formats differentely!

"ruff.lint.run": "onType|onSave", seemed like a setting which could help, but this settings, as shown by VSCode, is invalid.

How do I sort imports on save when using custom ruff config file location?

Thanks

Thank you for opening the issue! Can you provide the content in your config file? And can you expand on how "it formats differently"? Like, what is the expected behavior and what are you actually getting?