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 9f8d213 commit 390706b
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 33 deletions.
2 changes: 1 addition & 1 deletion docs/tasks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ I don't want to turn all file tasks into tera templates just for this feature.
The following environment variables are passed to the task:

- `MISE_ORIGINAL_CWD`: The original working directory from where the task was run.
- `MISE_CONFIG_ROOT`: The directory containing the `mise.toml` file where the task was defined.
- `MISE_CONFIG_ROOT`: The directory containing the `mise.toml` file where the task was defined or if the config path is something like `~/src/myproj/.config/mise.toml`, it will be `~/src/myproj`.
- `MISE_PROJECT_ROOT` or `root`: The root of the project.
- `MISE_TASK_NAME`: The name of the task being run.
- `MISE_TASK_DIR`: The directory containing the task script.
Expand Down
3 changes: 1 addition & 2 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ These variables offer key information about the current environment:
- `env: HashMap<String, String>` – Accesses current environment variables as
a key-value map.
- `cwd: PathBuf` – Points to the current working directory.
- `config_root: PathBuf` – Locates the directory containing your `mise.toml` file
or the `.mise` configuration folder.
- `config_root: PathBuf` – Locates the directory containing your `mise.toml` file, or in the case of something like `~/src/myproj/.config/mise.toml`, it will point to `~/src/myproj`.
- `mise_bin: String` - Points to the path to the current mise executable
- `mise_pid: String` - Points to the pid of the current mise process
- `xdg_cache_home: PathBuf` - Points to the directory of XDG cache home
Expand Down
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
88 changes: 72 additions & 16 deletions src/config/config_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use indexmap::IndexMap;
use once_cell::sync::Lazy;
use path_absolutize::Absolutize;
use serde_derive::Deserialize;
use versions::Versioning;

use tool_versions::ToolVersions;
use versions::Versioning;
use xx::regex;

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,29 +245,54 @@ 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)
.iter()
.nth_back(1)
.map(|p| p.as_str())
.unwrap_or(EMPTY);
let grandparent = parts
.get(parts.len() - 3)
.iter()
.nth_back(2)
.map(|p| p.as_str())
.unwrap_or(EMPTY);
let great_grandparent = parts
.iter()
.nth_back(3)
.map(|p| p.as_str())
.unwrap_or(EMPTY);
let cur_path = || path.parent().unwrap().to_path_buf();
let parent_path = || cur_path().parent().unwrap().to_path_buf();
let parent_path = || path.parent().unwrap().to_path_buf();
let grandparent_path = || parent_path().parent().unwrap().to_path_buf();
let great_grandparent_path = || grandparent_path().parent().unwrap().to_path_buf();
let great_great_grandparent_path = || great_grandparent_path().parent().unwrap().to_path_buf();
let is_mise_dir = |d: &str| d == "mise" || d == ".mise";
let is_config_filename = |f: &str| {
f == "config.toml" || f == "config.local.toml" || regex!(r"config\..+\.toml").is_match(f)
};
if parent == "conf.d" && is_mise_dir(grandparent) {
grandparent_path()
} else if is_mise_dir(parent) {
if great_grandparent == ".config" {
great_great_grandparent_path()
} else {
great_grandparent_path()
}
} else if is_mise_dir(parent) && is_config_filename(filename) {
if grandparent == ".config" {
grandparent_path()
great_grandparent_path()
} else {
parent_path()
grandparent_path()
}
} else if parent == ".config" {
grandparent_path()
} else {
parent_path()
}
}

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

Expand Down Expand Up @@ -544,4 +569,35 @@ mod tests {
Some(ConfigFileType::MiseToml)
);
}

#[test]
fn test_config_root() {
for p in &[
"/foo/bar/.config/mise/conf.d/config.toml",
"/foo/bar/.config/mise/conf.d/foo.toml",
"/foo/bar/.config/mise/config.local.toml",
"/foo/bar/.config/mise/config.toml",
"/foo/bar/.config/mise.local.toml",
"/foo/bar/.config/mise.toml",
"/foo/bar/.mise.env.toml",
"/foo/bar/.mise.local.toml",
"/foo/bar/.mise.toml",
"/foo/bar/.mise/conf.d/config.toml",
"/foo/bar/.mise/config.local.toml",
"/foo/bar/.mise/config.toml",
"/foo/bar/.tool-versions",
"/foo/bar/mise.env.toml",
"/foo/bar/mise.local.toml",
"/foo/bar/mise.toml",
"/foo/bar/mise/config.local.toml",
"/foo/bar/mise/config.toml",
"/foo/bar/.config/mise/config.env.toml",
"/foo/bar/.config/mise.env.toml",
"/foo/bar/.mise/config.env.toml",
"/foo/bar/.mise.env.toml",
] {
println!("{}", p);
assert_eq!(config_root(Path::new(p)), PathBuf::from("/foo/bar"));
}
}
}
13 changes: 5 additions & 8 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 Expand Up @@ -481,7 +478,7 @@ mod tests {
results.env_paths.into_iter().map(|p| replace_path(&p.display().to_string())).collect::<Vec<_>>(),
@r#"
[
"~/cwd/bin",
"~/bin",
"/bin",
]
"#
Expand Down
1 change: 0 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,6 @@ static LOCAL_CONFIG_FILENAMES: Lazy<IndexSet<&'static str>> = Lazy::new(|| {
".config/mise.toml",
".mise/config.toml",
"mise/config.toml",
".mise/config.toml",
".rtx.toml",
"mise.toml",
&*env::MISE_DEFAULT_CONFIG_FILENAME, // mise.toml
Expand Down
10 changes: 6 additions & 4 deletions 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 Expand Up @@ -246,12 +247,12 @@ fn normalize_escape_sequences(input: &str) -> String {

#[cfg(test)]
mod tests {
use super::*;

use insta::assert_debug_snapshot;
use pretty_assertions::assert_str_eq;
use test_log::test;

use super::*;

#[test]
fn test_diff() {
let diff = EnvDiff::new(&new_from_hashmap(), new_to_hashmap());
Expand Down Expand Up @@ -347,7 +348,8 @@ mod tests {
.into_iter()
.map(|(k, v)| (k.into(), v.into()))
.collect::<Vec<(String, String)>>();
let ed = EnvDiff::from_bash_script(path.as_path(), orig).unwrap();
let cwd = dirs::CWD.clone().unwrap();
let ed = EnvDiff::from_bash_script(path.as_path(), &cwd, orig).unwrap();
assert_debug_snapshot!(ed);
}

Expand Down

0 comments on commit 390706b

Please sign in to comment.