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 existing ruff in active python environment #141

Closed
buhrmann opened this issue Feb 23, 2023 · 10 comments · Fixed by #144
Closed

Use existing ruff in active python environment #141

buhrmann opened this issue Feb 23, 2023 · 10 comments · Fixed by #144
Labels
question Asking for support or clarification

Comments

@buhrmann
Copy link

Hi, unless I'm missing something, it's not currently possible to configure the Ruff extension to use an already existing Ruff install in the currently active Python environment. There are options to manually set a fixed path to the interpreter and Ruff binary, but that's no use if you habitually switch between different python environments in VS Code.

The problem is that this leads to possibly incongruent linting via the extension and any configured pre-commit linting with Ruff. If the extension updates and introduces new rules, e.g., but the pre-commit Ruff has a fixed prior version, the result of the two may differ.

Not sure if it's possible to add the option to use the same Ruff already installed in the environment, or used by pre-commit, or if there is an alternative way to ensure consistency?

@charliermarsh
Copy link
Member

If you use importStrategy: fromEnvironment, it should behave this way -- it'll pick up the Ruff from the current workspace environment, using the same logic as all other VS Code Python extensions (it's picked up from Microsoft's template -- nothing specific to Ruff).

@charliermarsh charliermarsh added the question Asking for support or clarification label Feb 23, 2023
@buhrmann
Copy link
Author

buhrmann commented Mar 1, 2023

Uff, totally missed that setting, and there are only nine hahaha. Thanks!

@sjdemartini
Copy link
Contributor

@charliermarsh Instead of requiring a setting to be changed, is it possible to have the Ruff extension prefer the workspace environment by default if it can find that ruff is installed there, and only fall back to the bundled version of ruff if not? This is for instance how the Pylint and Flake8 extensions from Microsoft work—e.g.

The bundled pylint is only used if there is no installed version of pylint found in the selected python environment.

We have a shared repo for our workplace, and we want to ensure that everyone using it with VSCode has a consistent dev experience out of the box. So we'd like ruff to behave the same for everyone based on the version we have pinned in our environment. As a workaround, I tried putting "ruff.importStrategy": "fromEnvironment", in our .vscode/settings.json file, but it seems that setting doesn't take effect within workspaces and so must be applied manually by each user to their own user or other settings if they want to use the locally-installed environment version of ruff:
image

Thanks for considering!

@charliermarsh
Copy link
Member

Yeah I think it makes sense to change the default.

sjdemartini added a commit to sjdemartini/ruff-vscode that referenced this issue Mar 1, 2023
Closes astral-sh#141

The docs claimed that `useBundled` was the default import strategy, but
it seems that's in fact not the case, based on
https://github.com/charliermarsh/ruff-vscode/blob/f57ae4f1cad2e7ea3a9caa307898fdfacbd180f1/src/common/settings.ts#L51

Per discussion here
astral-sh#141 (comment),
it was agreed to that `fromEnvironment` was the right default, so this
updates docs to reflect that existing default.
@sjdemartini
Copy link
Contributor

Okay, nice, it looks like the import strategy default was actually already being set as fromEnvironment based on:

https://github.com/charliermarsh/ruff-vscode/blob/f57ae4f1cad2e7ea3a9caa307898fdfacbd180f1/src/common/settings.ts#L51

It's just that the docs didn't reflect that. I made a PR to update them: #144

@buhrmann
Copy link
Author

Hm, I'm still having problems. Even if I use the fromEnvironment options it doesn't always seem to use the correct version of Ruff:

image

As you can see in the image, Ruff says "Using environment executable: /Users/thomas/mambaforge/bin/ruff", which is the base environment, while VS Code's active environment is not the base environment, but one named "grapy" (see lower right corner of the screenshot).

I'm pretty sure at some point I saw Ruff pointing at the correct executable in the active environment. Not sure what changed.

@buhrmann
Copy link
Author

FWIW, if I uninstall Ruff from the base environment it then finds the correct binary in the active environment. Is it possible that the resolution logic is "backwards"?

@charliermarsh
Copy link
Member

Lemme check real quick...

@charliermarsh
Copy link
Member

Yeah I think it's wrong. It should be using current environment, then global environment, but it's inverted AFAICT.

@buhrmann
Copy link
Author

Ok. No problem, I've got a workaround for now. Just thought it better to report. Thanks for looking into it!

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

Successfully merging a pull request may close this issue.

3 participants