vscode-ruff icon indicating copy to clipboard operation
vscode-ruff copied to clipboard

Support Ruff configuration settings as VS Code extension options

Open charliermarsh opened this issue 2 years ago • 26 comments

E.g., you should be able to set your line-length in VS Code directly if desired.

charliermarsh avatar Nov 09 '22 18:11 charliermarsh

@charliermarsh - how can you currently set line length / disable that error? I've tried editing pyproject.toml and failed. Thanks

AronDurkinSSS avatar Jan 04 '23 17:01 AronDurkinSSS

You can provide a command-line argument directly through the extension settings:

Screen Shot 2023-01-04 at 4 43 02 PM

Screen Shot 2023-01-04 at 4 42 55 PM

Although better would be to put a pyproject.toml in the same path as the files you're linting, and do:

[tool.ruff]
line-length = 10

charliermarsh avatar Jan 04 '23 21:01 charliermarsh

Hi, have you ever consider allow passing more complex configurations to ruff from command line arguments (e.g., a JSON string or similar)? For example, like clang-format (docs):

When specifying configuration in the -style= option, the same configuration is applied for all input files. The format of the configuration is:

-style='{key1: value1, key2: value2, ...}'

This might need changes in ruff upstream. But I think we can discuss it here first.

For C/C++ projects, I'm using microsoft/vscode-cpptools and I have:

{
    //* C/C++ */
    "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
    "C_Cpp.autocomplete": "Default",
    "C_Cpp.formatting": "clangFormat",
    "C_Cpp.clang_format_sortIncludes": false,
    "C_Cpp.clang_format_style": "file",
    "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, AccessModifierOffset: -3, ColumnLimit: 100, BinPackArguments: false, BinPackParameters: false }"
}

It provides a fallback style if the .clang-format file does not exist in the project. I think we can provide a similar thing like:

{
    //* Ruff */
    "ruff.configuration": "file",
    "ruff.fallbackConfiguration": "{ ling-length: 100, target-version: 'py38', show-source: true, isort.lines-after-imports: 2, flake8-quotes.inline-quotes: 'single' }"
}

when pyproject.toml or ruff.toml does not exist in the project. Currently, we cannot pass options for sub-checker from the command line. This would be a limitation for vscode configuration "ruff.args".

[tool.ruff.isort]
lines-after-imports = 2

[tool.ruff.flake8-quotes]
inline-quotes = "single"

XuehaiPan avatar Apr 17 '23 09:04 XuehaiPan

Would being able to customise ruff as described in the above way be worked on? If not how else can we do this without having to create a pyproject.toml or ruff.toml?

udeepam avatar Oct 03 '23 13:10 udeepam

@udeepam - Right now, it does require that you use a pyproject.toml or ruff.toml file. That being said, the pyproject.toml or ruff.toml file doesn't have to line in your project. You can either hardcode a path to your file (by setting --config=/path/to/file.toml in the VS Code extension's args field) or use a global, user-specific configuration file for Ruff. See https://docs.astral.sh/ruff/faq/#how-can-i-change-ruffs-default-configuration for more, but e.g. on macOS, you'd create the file at /Users/Alice/Library/Application Support/ruff/ruff.toml or similar.

charliermarsh avatar Oct 03 '23 16:10 charliermarsh

@charliermarsh

I think ruff format has a bug in vscode. I preferred to comment here instead of creating new issue. If this is the real bug and not my fault, let me know and I will create bug issue.

I've installed ruff vscode extension and then I've installed ruff via pip pip install ruff in my conda environment.

I've been using black formatter and as you know vscode offers command to format any document via command palette and I'm using that all the time.

Now, after installing ruff and ruff extension, I removed black formatter extension and everything related to black. I've set ruff as a default formatter. After that, I've altered two setting in GUI as well as in setting.json file

image

After this changes ruff formatter stoped working. It does not formats Python files anymore and also reducing line length to 88 did not have any effect. However, if I remove line length arguments then formatter works and cuts the line at 88 character. I guess this is the default.

Additionally, I've created ruff.toml file in /Users/my_user_here/Library/Application Support/ruff/ directory and adjusted vscode settings accordingly. This did not have effect as well.

Okroshiashvili avatar Oct 27 '23 21:10 Okroshiashvili

Hey @Okroshiashvili it looks like you're encountering this because the --line-length option is not supported in ruff format https://github.com/astral-sh/ruff/pull/8131

I'm not sure why it's not working in another location though, @charliermarsh may have insight there. Can you provide some logs from the Ruff extension when formatting?

zanieb avatar Oct 27 '23 22:10 zanieb

Hi @zanieb

Thanks for a quick replay ;)

I did not know about --line-length removal and it seems pretty new though. Then I understand why my effort was in vain :)

Let me retry the things with ruff.toml file and will update the thread

Okroshiashvili avatar Oct 27 '23 22:10 Okroshiashvili

Hello @charliermarsh , I'm encountering an error when trying to use VSCode settings to define an ignore for ruff. Specifically I have this in my settings.json:

    "ruff.lint.args": [
        "--ignore E501"
    ],

But I'm seeing this error, and of course it doesn't work. image

How should I specify my exception? Thank you!!

CarlaFernandez avatar Jan 11 '24 08:01 CarlaFernandez

@CarlaFernandez - Can you try "--ignore=E501"?

charliermarsh avatar Jan 11 '24 18:01 charliermarsh

That appears to be working, thank you!

CarlaFernandez avatar Jan 12 '24 10:01 CarlaFernandez

For reference, the current solution with ruff v0.2.2 in VS Code is to set: "ruff.format.args": ["--config", "line-length=79"]

pipme avatar Feb 20 '24 18:02 pipme

For reference, the current solution with ruff v0.2.2 in VS Code is to set: "ruff.format.args": ["--config", "line-length=79"]

I'm having trouble getting this to work; on the VSCode Ruff extension page, what exactly do I need to put in the input box? Is it everything in the code formatted part of your comment? Or just "args":["--config", "line-length=79"]

pnulty avatar Feb 20 '24 19:02 pnulty

@pnulty Please check the figure below. You can also directly specify the setting "ruff.format.args": ["--config", "line-length=79"] in settings.json. image

pipme avatar Feb 20 '24 19:02 pipme

@pnulty - can you confirm that you’re using Ruff v0.2.2 (latest)? Very new feature.

charliermarsh avatar Feb 20 '24 20:02 charliermarsh

@pnulty - can you confirm that you’re using Ruff v0.2.2 (latest)? Very new feature.

Thanks, yes I just happened to install an hour ago, can confirm it is v0.2.2. New user so maybe I'm missing context, the --config part causes an error for me, seems like its looking for a toml file. For just the line length, what should be in user settings in settings.json? The below doesn't seem to be having an effect, but very possible I'm just missing something.

image

pnulty avatar Feb 20 '24 20:02 pnulty

Sorry, can you try --line-length=100, instead of line-length=100?

charliermarsh avatar Feb 20 '24 20:02 charliermarsh

Or, better yet: ["--config", "line-length=100"] (this is the newer syntax)

charliermarsh avatar Feb 20 '24 20:02 charliermarsh

["--line-length=100"] works! ["--config", "line-length=100"] gives the error below

image

pnulty avatar Feb 20 '24 20:02 pnulty

I think you might be using a version of Ruff other than v0.2.2. The extension will prioritize a Ruff installed in your environment / on your machine over the version that ships with the extension itself (which is viewed as a fallback).

charliermarsh avatar Feb 20 '24 20:02 charliermarsh

But I'd have to test it myself later to be certain. That's just the error I'd expect to see if you were accidentally using v0.2.1.

charliermarsh avatar Feb 20 '24 20:02 charliermarsh

Apologies, I was just reading "this extension ships with ruff==0.2.2" instead of actually checking what ruff I had installed on my system. I've upgraded ruff and can confirm that it works the second way now. Thanks for the very quick help!

pnulty avatar Feb 20 '24 20:02 pnulty

how can i set quote-style to preserve in vscode?

tolni avatar Mar 02 '24 20:03 tolni

Try this in your settings.json:

{
    "ruff.format.args": ["--config", "format.quote-style='preserve'"]
}

charliermarsh avatar Mar 02 '24 22:03 charliermarsh

Fyi native server now supports use config file inside settings: https://docs.astral.sh/ruff/editors/settings/#configuration

T-256 avatar Aug 15 '24 21:08 T-256

We also support a subset of configuration directly in the editor:

  • exclude
  • lineLength
  • Linter specific: https://docs.astral.sh/ruff/editors/settings/#lint
  • Formatter specific: https://docs.astral.sh/ruff/editors/settings/#format

dhruvmanila avatar Aug 16 '24 04:08 dhruvmanila