Skip to content

fix Upward Find for venv #759#2791

Open
asukaminato0721 wants to merge 3 commits intofacebook:mainfrom
asukaminato0721:759
Open

fix Upward Find for venv #759#2791
asukaminato0721 wants to merge 3 commits intofacebook:mainfrom
asukaminato0721:759

Conversation

@asukaminato0721
Copy link
Contributor

Summary

Fixes #759

keeping the existing per-root scan and applying it across project_path plus its ancestors, so Pyrefly now finds the nearest ancestor .venv instead of only searching downward from the current root.

Test Plan

add test

@meta-cla meta-cla bot added the cla signed label Mar 13, 2026
@asukaminato0721 asukaminato0721 marked this pull request as ready for review March 13, 2026 11:40
Copilot AI review requested due to automatic review settings March 13, 2026 11:40
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates Pyrefly’s virtual environment discovery to search upward through ancestor directories so a project can use a .venv located in a parent directory (addresses #759).

Changes:

  • Refactors venv discovery into a per-root helper (find_in_root) and applies it across project_path ancestors.
  • Adds unit tests ensuring ancestor search works and prefers the nearest matching ancestor venv.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +51 to +53
pub fn find(project_path: &Path) -> Option<PathBuf> {
project_path.ancestors().find_map(find_in_root)
}
Copy link
Contributor

@connernilsen connernilsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @asukaminato0721, thanks for doing this. This has been on the todo list for a while, so it's awesome that you picked it up.

There are a few requests below, but I don't think they should be too difficult to fix. Let me know if you have further questions or want to discuss anything.

Comment on lines +64 to +71
fn search_roots(project_path: &Path) -> impl Iterator<Item = &Path> {
project_path
.ancestors()
.take_while(|path| !path.as_os_str().is_empty())
}

pub fn find(project_path: &Path) -> Option<PathBuf> {
search_roots(project_path).find_map(find_in_root)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing an upward filesystem walk to then do a recursive downward search can be very expensive, which is why I originally designed this to be very limited in scope. I have two suggestions for how to improve this, but I'm curious about your/others' thoughts.

  1. instead of looking at all project_path.ancestors(), only look at ancestors where a candidate dir exists (something like path.ancestors().flatten(|a| CANDIDATE_DIRS.iter().map(|c| a.join(c))).filter(|p| p.exists())). This I think would be necessary at minimum.
  2. stop the upward search at the first config file or marker file (path.ancestors().take_while(|p| !ConfigFile::CONFIG_FILE_NAMES.chain(ConfigFile::ADDITIONAL_ROOT_FILE_NAMES).any(|c| p.join(c).exists())))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Upward Find for venv

3 participants