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

ruff runs on standard library and typeshed files

Open DetachHead opened this issue 1 year ago • 19 comments

it seems like ruff tries to avoid standard library files:

2023-06-19 10:11:58.875 [info] [Warn  - 10:11:58 AM] Skipping standard library file: c:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\project-zbC6VfKB-py3.11\Lib\site-packages\playwright\sync_api\_generated.py

but it only seems to work on third party packages. it still runs on standard library and typeshed files:

2023-06-19 10:12:14.871 [info] Running Ruff with: c:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\project-zbC6VfKB-py3.11\Scripts\ruff.exe ['--force-exclude', '--no-cache', '--no-fix', '--quiet', '--format', 'json', '-', '--stdin-filename', 'c:\\Users\\user\\AppData\\Local\\Programs\\Python\\Lib\\typing.py']
2023-06-19 10:13:47.096 [info] Running Ruff with: c:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\project-zbC6VfKB-py3.11\Scripts\ruff.exe ['--force-exclude', '--no-cache', '--no-fix', '--quiet', '--format', 'json', '-', '--stdin-filename', 'c:\\Users\\user\\.vscode\\extensions\\ms-python.vscode-pylance-2023.6.20\\dist\\typeshed-fallback\\stdlib\\typing.pyi']

DetachHead avatar Jun 19 '23 00:06 DetachHead

I think it just depends on whether the packages are in the same environment as currently-running interpreter (notice your first example vs. the next two). I would assume that the isort and all other Python extensions have the same behavior. I'm not sure there's really a good way to avoid this.

charliermarsh avatar Jun 19 '23 01:06 charliermarsh

Respecting ignorePatterns would help with this but still wouldn't catch your typing.py example. (See: https://github.com/astral-sh/ruff-vscode/issues/175.)

charliermarsh avatar Jun 19 '23 02:06 charliermarsh

I recommend adding something like this to your configuration:

{
  "ruff.args": [
    "--extend-exclude=.vscode/**/*.py",
    "--extend-exclude=Python/lib/**/*.py",
  ]
}

charliermarsh avatar Jun 19 '23 02:06 charliermarsh

unfortunately that doesn't seem to work due to https://github.com/microsoft/vscode-black-formatter/issues/251#issuecomment-1595019087 :(

DetachHead avatar Jun 19 '23 04:06 DetachHead

@DetachHead Adding ruff.args to the global User settings.json file worked for me:

This is what I'm using right now:

"ruff.args": [
        "--extend-ignore=E501",
        "--extend-exclude=/**/3rdparty_repositories/**/*.py",
        "--extend-exclude=/**/site-packages/**/*.py",
    ],

dufferzafar avatar Jun 20 '23 19:06 dufferzafar

Only tangentially related, but I also use this to quickly toggle python analysis & type checking:

https://github.com/Diogo-Rossi/vscode-settings-switcher

"settingsSwitcher.lists": {
        "Python analysis & linting": {
            "Full": {
                "description": "Pylance + Pyright + ruff",
                "python.analysis.typeCheckingMode": "strict",
                "python.languageServer": "Pylance",
            },
            "Minimal": {
                "description": "Only ruff stays on",
                "python.analysis.typeCheckingMode": "off",
                "python.languageServer": "None",
            },
        },
    },

Being able to set these up depending on file patterns would be great as well!

dufferzafar avatar Jun 20 '23 19:06 dufferzafar

@charliermarsh What prevents ruff-vscode to ship these as standard settings?

TheRealBecks avatar Oct 09 '23 18:10 TheRealBecks

@TheRealBecks - Are you referring to settings like --extend-exclude? Exposing those in the VS Code UI?

charliermarsh avatar Oct 09 '23 19:10 charliermarsh

The ruff-vscode extension is trying to lint files like:

  • /usr/lib64/python3.10
  • /home/becks/.local/share/virtualenvs/my_project-Gchh4Lgq/lib/python3.10/site-packages/django/urls/base.py (site-packages)

That only happens with the extension, but the command line tool ruff works just fine. So it seems that the extensions settings need to exclude additional files by default?

TheRealBecks avatar Oct 09 '23 20:10 TheRealBecks

@DetachHead I've been dealing with this issue as well, and the extension has been linting standard library modules even when they are excluded with --extend-exclude. However, I discovered today that I could get the directories to be properly excluded by making the c drive in my --extend-exclude lowercase instead of uppercase.

Simply changing from:

"ruff.lint.args": [
    "--extend-exclude=C:/Users/username/AppData/**/*",
    "--extend-exclude=C:/Users/username/.vscode/**/*",
],

to

"ruff.lint.args": [
    "--extend-exclude=c:/Users/username/AppData/**/*",
    "--extend-exclude=c:/Users/username/.vscode/**/*",
],

fixes the issue.

harrymconner avatar Oct 17 '23 20:10 harrymconner

Would it be possible to exclude the standard library within the vscode extension, independent to the current environment per default? The problem with using something like --extend-exclude= is that it breaks as soon the environment changes though using devcontainers, multi device/OS etc.

SRv6d avatar Nov 09 '23 11:11 SRv6d

yeah, ideally you'd be able to commit this config but you can't because those paths will be different for each user

DetachHead avatar Nov 09 '23 11:11 DetachHead

Why does ruff even run outside of the ./ folder? When I execute ruff check ./ in my repository the output is fine. With the VS Code extension seems to be using another folder for ruff. Do we know which command will be used by VS Code?

TheRealBecks avatar Nov 09 '23 12:11 TheRealBecks

This is extremely annoying. I get output about files I do not even have open nor do I want to know about their issues. I think shipping a default to ignore */site-packages/* and others would be good.

Tatsh avatar Dec 18 '23 15:12 Tatsh

@Tatsh -- That does seem reasonable to me, although it won't necessarily cover opening files from Python standard library.

charliermarsh avatar Dec 18 '23 15:12 charliermarsh

@Tatsh -- Added in the next Ruff release: https://github.com/astral-sh/ruff/pull/9188

charliermarsh avatar Dec 18 '23 16:12 charliermarsh

This seems to be partially fixed with Ruff 0.1.9 but the defaults in Ruff (as far as I can tell) still allow for stdlib to be checked unnecessarily.

Tatsh avatar Dec 21 '23 20:12 Tatsh

I'd love to improve the situation here, though it's not clear to me how we can definitively detect that a file is a stdlib file.

charliermarsh avatar Dec 21 '23 20:12 charliermarsh

Ruff would be the best place to detect this rather than trying to make exclude filters. The best way is to inspect where some library like sys is located. If it comes back as an invalid parh that most likely means the stdlib is in a zip file.

For now to cover system stdlib:

For Linux and macOS: /usr/**/*.py and /opt/**/*.py

The latter is to cover the standard location for MacPorts.

On Windows it's a bit more complicated. First is escaping \ but also that the stdlib that python.org ships is in a zip file to hopefully speed up IO. I think this means Ruff is not analysing it at all (and sadly VS Code cannot open these libs either for examination with ctrl+click). Assuming we can use / on Windows:

  • c:/msys/**/*.py
  • c:/program*/**/*.py

And you may need to cover the virtualenv path stdlib which is where inspection is needed.

Tatsh avatar Dec 21 '23 21:12 Tatsh