Skip to content

Commit

Permalink
fix: use config_root for env._.source
Browse files Browse the repository at this point in the history
Fixes #2335
  • Loading branch information
jdx committed Dec 11, 2024
1 parent 960fbd5 commit d9d6f04
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/backend/asdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ impl AsdfBackend {
sm.prepend_path(p);
}
let script = sm.get_script_path(&ExecEnv);
let ed = EnvDiff::from_bash_script(&script, &sm.env)?;
let dir = dirs::CWD.clone().unwrap_or_default();
let ed = EnvDiff::from_bash_script(&script, &dir, &sm.env)?;
let env = ed
.to_patches()
.into_iter()
Expand Down
20 changes: 14 additions & 6 deletions src/config/config_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use tool_versions::ToolVersions;
use crate::cli::args::{BackendArg, ToolArg};
use crate::config::config_file::mise_toml::MiseToml;
use crate::config::env_directive::EnvDirective;
use crate::config::{settings, AliasMap, Settings, SETTINGS};
use crate::config::{is_global_config, settings, AliasMap, Settings, SETTINGS};
use crate::errors::Error::UntrustedConfig;
use crate::file::display_path;
use crate::hash::hash_to_str;
Expand Down Expand Up @@ -232,9 +232,9 @@ pub fn parse(path: &Path) -> Result<Box<dyn ConfigFile>> {
}
}

pub fn config_trust_root(path: &Path) -> PathBuf {
if settings::is_loaded() && SETTINGS.paranoid {
return path.to_path_buf();
pub fn config_root(path: &Path) -> PathBuf {
if is_global_config(path) {
return env::HOME.to_path_buf();
}
let path = path
.absolutize()
Expand All @@ -245,7 +245,7 @@ pub fn config_trust_root(path: &Path) -> PathBuf {
.map(|c| c.as_os_str().to_string_lossy().to_string())
.collect::<Vec<_>>();
const EMPTY: &str = "";
// let filename = parts.last().map(|p| p.as_str()).unwrap_or(EMPTY);
let filename = parts.last().map(|p| p.as_str()).unwrap_or(EMPTY);
let parent = parts
.get(parts.len() - 2)
.map(|p| p.as_str())
Expand All @@ -260,7 +260,7 @@ pub fn config_trust_root(path: &Path) -> PathBuf {
let is_mise_dir = |d: &str| d == "mise" || d == ".mise";
if parent == "conf.d" && is_mise_dir(grandparent) {
grandparent_path()
} else if is_mise_dir(parent) {
} else if is_mise_dir(parent) && filename == "config.toml" {
if grandparent == ".config" {
grandparent_path()
} else {
Expand All @@ -271,6 +271,14 @@ pub fn config_trust_root(path: &Path) -> PathBuf {
}
}

pub fn config_trust_root(path: &Path) -> PathBuf {
if settings::is_loaded() && SETTINGS.paranoid {
path.to_path_buf()
} else {
config_root(path)
}
}

pub fn trust_check(path: &Path) -> eyre::Result<()> {
static MUTEX: Mutex<()> = Mutex::new(());
let _lock = MUTEX.lock().unwrap(); // Prevent multiple checks at once so we don't prompt multiple times for the same path
Expand Down
11 changes: 4 additions & 7 deletions src/config/env_directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use indexmap::IndexMap;
use serde::{Deserialize, Deserializer};

use crate::cmd::CmdLineRunner;
use crate::config::config_file::trust_check;
use crate::config::config_file::{config_root, trust_check};
use crate::config::{Config, SETTINGS};
use crate::env::PATH_KEY;
use crate::env_diff::{EnvDiff, EnvDiffOperation};
Expand Down Expand Up @@ -164,11 +164,7 @@ impl EnvResults {
// &directive,
// &source
// );
let config_root = source
.parent()
.map(Path::to_path_buf)
.or_else(|| dirs::CWD.clone())
.unwrap_or_default();
let config_root = config_root(&source);
ctx.insert("cwd", &*dirs::CWD);
ctx.insert("config_root", &config_root);
let env_vars = env
Expand Down Expand Up @@ -237,7 +233,8 @@ impl EnvResults {
{
r.env_scripts.push(p.clone());
let env_diff =
EnvDiff::from_bash_script(&p, env_vars.clone()).unwrap_or_default();
EnvDiff::from_bash_script(&p, &config_root, env_vars.clone())
.unwrap_or_default();
for p in env_diff.to_patches() {
match p {
EnvDiffOperation::Add(k, v)
Expand Down
3 changes: 2 additions & 1 deletion src/env_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl EnvDiff {
diff
}

pub fn from_bash_script<T, U, V>(script: &Path, env: T) -> Result<Self>
pub fn from_bash_script<T, U, V>(script: &Path, dir: &Path, env: T) -> Result<Self>
where
T: IntoIterator<Item = (U, V)>,
U: Into<OsString>,
Expand All @@ -75,6 +75,7 @@ impl EnvDiff {
export -p
", script = script.display()}
)
.dir(dir)
.full_env(&env)
.read()?;
let env: HashMap<String, String> = env
Expand Down

0 comments on commit d9d6f04

Please sign in to comment.