Skip to content

Commit

Permalink
fix: run hook-env on directory change (#3258)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored Nov 28, 2024
1 parent 11a785d commit e6b0754
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/cli/hook_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::env::{PATH_KEY, TERM_WIDTH, __MISE_DIFF};
use crate::env_diff::{EnvDiff, EnvDiffOperation};
use crate::shell::{get_shell, ShellType};
use crate::toolset::{Toolset, ToolsetBuilder};
use crate::{env, hook_env};
use crate::{dirs, env, hook_env};

/// [internal] called by activate hook to update env vars directory change
#[derive(Debug, clap::Args)]
Expand Down Expand Up @@ -59,6 +59,7 @@ impl HookEnv {
patches.extend(self.build_path_operations(&settings, &paths, &__MISE_DIFF.path)?);
patches.push(self.build_diff_operation(&diff)?);
patches.push(self.build_watch_operation(&watch_files)?);
patches.push(self.build_dir_operation()?);

let output = hook_env::build_env_commands(&*shell, &patches);
miseprint!("{output}")?;
Expand Down Expand Up @@ -167,6 +168,16 @@ impl HookEnv {
))
}

fn build_dir_operation(&self) -> Result<EnvDiffOperation> {
Ok(EnvDiffOperation::Add(
"__MISE_DIR".into(),
dirs::CWD
.as_ref()
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_default(),
))
}

fn build_watch_operation(
&self,
watch_files: impl IntoIterator<Item = impl AsRef<Path>>,
Expand Down
1 change: 1 addition & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub static MISE_TIMINGS: Lazy<u8> = Lazy::new(|| var_u8("MISE_TIMINGS"));
pub static MISE_PID: Lazy<String> = Lazy::new(|| process::id().to_string());
pub static __MISE_SCRIPT: Lazy<bool> = Lazy::new(|| var_is_true("__MISE_SCRIPT"));
pub static __MISE_DIFF: Lazy<EnvDiff> = Lazy::new(get_env_diff);
pub static __MISE_DIR: Lazy<PathBuf> = Lazy::new(|| var_path("__MISE_DIR").unwrap_or_default());
pub static __MISE_ORIG_PATH: Lazy<Option<String>> = Lazy::new(|| var("__MISE_ORIG_PATH").ok());
pub static __MISE_WATCH: Lazy<Option<HookEnvWatches>> = Lazy::new(|| match var("__MISE_WATCH") {
Ok(raw) => deserialize_watches(raw)
Expand Down
6 changes: 6 additions & 0 deletions src/hook_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ pub fn should_exit_early(watch_files: impl IntoIterator<Item = impl AsRef<Path>>
if args.len() < 2 || args[1] != "hook-env" {
return false;
}
if !dirs::CWD
.as_ref()
.is_some_and(|cwd| cwd == &*env::__MISE_DIR)
{
return false;
}
let watch_files = get_watch_files(watch_files);
match &*env::__MISE_WATCH {
Some(watches) => {
Expand Down

0 comments on commit e6b0754

Please sign in to comment.