Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion crates/pyrefly_config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ use crate::base::ConfigBase;
use crate::base::UntypedDefBehavior;
use crate::environment::environment::PythonEnvironment;
use crate::environment::interpreters::Interpreters;
use crate::environment::venv;
use crate::error::ErrorConfig;
use crate::error::ErrorDisplayConfig;
use crate::finder::ConfigError;
use crate::module_wildcard::Match;
use crate::pyproject::PyProject;
use crate::util::ConfigOrigin;

pub static GENERATED_FILE_CONFIG_OVERRIDE: LazyLock<
RwLock<SmallMap<ModulePathBuf, ArcId<ConfigFile>>>,
Expand Down Expand Up @@ -1005,7 +1007,14 @@ impl ConfigFile {
));
}
match self.interpreters.find_interpreter(self.source.root()) {
Ok(interpreter) => {
Ok(mut interpreter) => {
if !interpreter.exists() && let Some(root) = self.source.root() {
// Attempt to auto-discover .venv if the configured path is invalid
if let Some(found) = venv::find(root) {
interpreter = ConfigOrigin::fallback(found);
}
}

let (env, error) = PythonEnvironment::get_interpreter_env(&interpreter);
self.python_environment.override_empty(env);
self.interpreters.python_interpreter_path = Some(interpreter);
Expand Down
15 changes: 15 additions & 0 deletions crates/pyrefly_config/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ pub(crate) enum ConfigOrigin<T> {
#[serde(skip)]
Auto(T),

/// This value was auto-discovered as a fallback when configured path was invalid.
#[serde(skip)]
Fallback(T),

/// This value was explicitly provided by the IDE using "workspace.configuration" using LSP protocol.
#[serde(skip)]
Lsp(T),
Expand Down Expand Up @@ -88,6 +92,7 @@ impl<T> Deref for ConfigOrigin<T> {
Self::CommandLine(value)
| Self::ConfigFile(value)
| Self::Auto(value)
| Self::Fallback(value)
| Self::Lsp(value) => value,
}
}
Expand All @@ -99,6 +104,7 @@ impl<T> DerefMut for ConfigOrigin<T> {
Self::CommandLine(value)
| Self::ConfigFile(value)
| Self::Auto(value)
| Self::Fallback(value)
| Self::Lsp(value) => value,
}
}
Expand Down Expand Up @@ -126,6 +132,11 @@ impl<T> ConfigOrigin<T> {
Self::Auto(value)
}

/// Construct a new [`ConfigOrigin::Fallback`] with the given value.
pub(crate) fn fallback(value: T) -> Self {
Self::Fallback(value)
}

/// Construct a new [`ConfigOrigin::Lsp`] with the given value.
pub(crate) fn lsp(value: T) -> Self {
Self::Lsp(value)
Expand All @@ -142,6 +153,7 @@ impl<T> ConfigOrigin<T> {
ConfigOrigin::ConfigFile(value) => ConfigOrigin::ConfigFile(f(value)),
ConfigOrigin::CommandLine(value) => ConfigOrigin::CommandLine(f(value)),
ConfigOrigin::Auto(value) => ConfigOrigin::Auto(f(value)),
ConfigOrigin::Fallback(value) => ConfigOrigin::Fallback(f(value)),
ConfigOrigin::Lsp(value) => ConfigOrigin::Lsp(f(value)),
}
}
Expand All @@ -151,6 +163,7 @@ impl<T> ConfigOrigin<T> {
ConfigOrigin::ConfigFile(ref value) => ConfigOrigin::ConfigFile(value),
ConfigOrigin::CommandLine(ref value) => ConfigOrigin::CommandLine(value),
ConfigOrigin::Auto(ref value) => ConfigOrigin::Auto(value),
ConfigOrigin::Fallback(ref value) => ConfigOrigin::Fallback(value),
ConfigOrigin::Lsp(ref value) => ConfigOrigin::Lsp(value),
}
}
Expand All @@ -171,10 +184,12 @@ impl<T, E> ConfigOrigin<Result<T, E>> {
ConfigOrigin::ConfigFile(Ok(value)) => Ok(ConfigOrigin::ConfigFile(value)),
ConfigOrigin::CommandLine(Ok(value)) => Ok(ConfigOrigin::CommandLine(value)),
ConfigOrigin::Auto(Ok(value)) => Ok(ConfigOrigin::Auto(value)),
ConfigOrigin::Fallback(Ok(value)) => Ok(ConfigOrigin::Fallback(value)),
ConfigOrigin::Lsp(Ok(value)) => Ok(ConfigOrigin::Lsp(value)),
ConfigOrigin::ConfigFile(Err(err))
| ConfigOrigin::CommandLine(Err(err))
| ConfigOrigin::Auto(Err(err))
| ConfigOrigin::Fallback(Err(err))
| ConfigOrigin::Lsp(Err(err)) => Err(err),
}
}
Expand Down