-
Notifications
You must be signed in to change notification settings - Fork 255
Auto-detect .venv interpreter when configured path is invalid #2083
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
base: main
Are you sure you want to change the base?
Conversation
|
Hi @HamzaYslmn! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Hey @HamzaYslmn, thanks for making this! I'm not sure I fully understand the use case you're trying to address though. Can you walk me through an example setup where you would expect to use this? Have you run into this problem before? Right now, I'm kind of leaning toward the idea that explicitly configured interpreters are a source of truth, and we generally shouldn't be attempting to fall back to something else automatically, so I think trying to understand your reasoning behind this will help me to decide if this is the right approach, or if something else might work (maybe something like #1662).
I just want to clarify, there are a lot of cases where this isn't required and should just work 'by default'. Are you seeing issues with this, or do you have a setup incompatible with auto-configuration? |
|
My use case is: when I create projects with But Pyrefly can’t automatically detect the interpreter inside that [tool.pyrefly]
python-interpreter-path = ".venv/Scripts/python.exe"What my PR does is not to override anything. If an interpreter path is explicitly configured, it stays the source of truth. The change only applies when there is no configured path and auto-discovery fails , then it falls back to checking for a local So it’s a safe fallback, not a replacement. Pyrefly cannot handle such situations. Have to manually configure .venv for every project, which is really tedious. Tools like uv automatically detect pyproject.toml and use the adjacent .venv, but with Pyrefly I still need to add an extra config each time. |
|
I want to echo that I’m hitting the same problems outlined in this PR. The current behavior has caused confusion and extra overhead in my own workflow, particularly in cases similar to the ones referenced here. I’ve spent a fair amount of time trying to understand whether the issues were coming from my setup or my code, only to realize they align very closely with what’s being discussed in this change. From my perspective, this PR targets a real and practical limitation rather than a theoretical edge case. Having this fixed would remove a lot of unnecessary friction when using Pyrefly on larger or more complex projects. Really appreciate the work going into this. |

Summary
This PR improves the interpreter discovery logic to automatically fall back to a
.venvdirectory in the project root if the configuredpython-interpreter-pathis invalid or refers to a non-existent file.Motivation
Modern Python workflows (using tools like
uv,poetry, or standardpip) often create a local.venvdirectory in the project root by default. Currently, users are often required to explicitly configure the full path to this interpreter inpyproject.tomlor [pyrefly.toml]This change aims to reduce configuration boilerplate by attempting to auto-discover the
.venvdirectory if the initially configured path fails validation. This makes the "out-of-the-box" experience much smoother for standard project layouts.Changes
ConfigFile::configurein [crates/pyrefly_config/src/config.rs]interpreter.exists()returnsfalse, the config now attempts to find a valid environment usingcrate::environment::venv::find(root)..venvis found, [pyrefly] automatically switches to using it instead of throwing an error or falling back to a system interpreter.