Skip to content

Always list all global poetry envs #158

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

Merged
merged 2 commits into from
Oct 1, 2024
Merged
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
16 changes: 13 additions & 3 deletions crates/pet-poetry/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ impl Config {
}

fn create_config(file: Option<PathBuf>, env: &EnvVariables) -> Option<Config> {
if let Some(file) = &file {
trace!("Parsing Poetry config file => {:?}", file);
}

let cfg = file.clone().and_then(|f| parse(&f));
let cache_dir = get_cache_dir(&cfg, env);
let virtualenvs_path_from_env_var = env
Expand All @@ -65,7 +69,6 @@ fn create_config(file: Option<PathBuf>, env: &EnvVariables) -> Option<Config> {
.map(|p| resolve_virtualenvs_path(&p, &cache_dir));

if let Some(virtualenvs_path) = &cfg.clone().and_then(|cfg| cfg.virtualenvs_path) {
trace!("Poetry virtualenvs path => {:?}", virtualenvs_path);
let virtualenvs_path = resolve_virtualenvs_path(&virtualenvs_path.clone(), &cache_dir);

return Some(Config::new(
Expand Down Expand Up @@ -113,24 +116,29 @@ fn resolve_virtualenvs_path(virtualenvs_path: &Path, cache_dir: &Option<PathBuf>
return virtualenvs_path;
}
}
trace!("Poetry virtualenvs path => {:?}", virtualenvs_path);
virtualenvs_path.to_path_buf()
}
/// Maps to DEFAULT_CACHE_DIR in poetry
fn get_cache_dir(cfg: &Option<ConfigToml>, env: &EnvVariables) -> Option<PathBuf> {
// Cache dir in env variables takes precedence
if let Some(cache_dir) = env.poetry_cache_dir.clone() {
if cache_dir.is_dir() {
trace!("Poetry cache dir from env variable: {:?}", cache_dir);
return Some(cache_dir);
}
}
// Check cache dir in config.
if let Some(cache_dir) = cfg.as_ref().and_then(|cfg| cfg.cache_dir.clone()) {
if cache_dir.is_dir() {
trace!("Poetry cache dir from config: {:?}", cache_dir);
return Some(cache_dir);
}
}

Platformdirs::new(_APP_NAME.into(), false).user_cache_path()
let default_cache_dir = Platformdirs::new(_APP_NAME.into(), false).user_cache_path();
trace!("Poetry cache (default): {:?}", default_cache_dir);
default_cache_dir
}

/// Maps to CONFIG_DIR in poetry
Expand Down Expand Up @@ -163,7 +171,9 @@ struct ConfigToml {

fn parse(file: &Path) -> Option<ConfigToml> {
let contents = fs::read_to_string(file).ok()?;
parse_contents(&contents)
let cfg = parse_contents(&contents);
trace!("Poetry config file for {:?} is {:?}", file, cfg);
cfg
}

fn parse_contents(contents: &str) -> Option<ConfigToml> {
Expand Down
9 changes: 4 additions & 5 deletions crates/pet-poetry/src/environment_locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ pub fn list_environments(
})
.collect::<Vec<_>>();

// We're only interested in directories that have a pyproject.toml
if workspace_dirs.is_empty() {
return None;
}

let mut envs = vec![];

let global_config = Config::find_global(env);
Expand All @@ -53,6 +48,10 @@ pub fn list_environments(
global_envs = list_all_environments_from_config(&config).unwrap_or_default();
}

if workspace_dirs.is_empty() {
trace!("pyproject.toml not found in any workspace directory");
}

for (workspace_dir, pyproject_toml) in workspace_dirs {
let virtualenv_prefix = generate_env_name(&pyproject_toml.name, workspace_dir);
trace!(
Expand Down
Loading