From 1cfeb8bceeec4f6c66651c21ae42e952ca52ff16 Mon Sep 17 00:00:00 2001 From: Jonathan Ringer Date: Tue, 24 Feb 2026 11:56:23 -0800 Subject: [PATCH] init: don't resolve shadowenv in shell hooks --- build.rs | 2 +- sh/shadowenv.bash.in | 2 +- sh/shadowenv.fish.in | 2 +- sh/shadowenv.nushell.in | 2 +- sh/shadowenv.zsh.in | 2 +- src/init.rs | 31 ++++++++----------------------- 6 files changed, 13 insertions(+), 28 deletions(-) diff --git a/build.rs b/build.rs index 1893298..24cfda0 100644 --- a/build.rs +++ b/build.rs @@ -1,6 +1,6 @@ extern crate clap; -use clap::{command, CommandFactory, ValueEnum}; +use clap::{CommandFactory, ValueEnum}; use clap_complete::{generate_to, Shell}; include!("src/cli.rs"); diff --git a/sh/shadowenv.bash.in b/sh/shadowenv.bash.in index 4b47960..9913794 100644 --- a/sh/shadowenv.bash.in +++ b/sh/shadowenv.bash.in @@ -9,7 +9,7 @@ __shadowenv_hook() { fi # We can't do the nice `x | source /dev/stdin` trick in old versions of bash, # meaning we need a subshell, so we can't just let shadowenv look up its ppid. - eval "$("@SELF@" hook "${flags[@]}")" + eval "$(shadowenv hook "${flags[@]}")" } @HOOKBOOK@ __shadowenv_force_run=1 diff --git a/sh/shadowenv.fish.in b/sh/shadowenv.fish.in index ffd966a..4d3a451 100644 --- a/sh/shadowenv.fish.in +++ b/sh/shadowenv.fish.in @@ -4,7 +4,7 @@ function __shadowenv_hook --on-event fish_prompt --on-variable PWD set -a flags --force set -eg __shadowenv_force_run end - @SELF@ hook $flags | source 2>/dev/null + shadowenv hook $flags | source 2>/dev/null end set -g __shadowenv_force_run 1 diff --git a/sh/shadowenv.nushell.in b/sh/shadowenv.nushell.in index 8ed279b..37888ee 100644 --- a/sh/shadowenv.nushell.in +++ b/sh/shadowenv.nushell.in @@ -8,7 +8,7 @@ $env.config = ($env.config | upsert hooks.env_change.PWD { |config| $flags = ($flags | append "--force") } - let result = @SELF@ hook ...$flags | complete + let result = shadowenv hook ...$flags | complete if $result.exit_code != 0 { return } diff --git a/sh/shadowenv.zsh.in b/sh/shadowenv.zsh.in index 634a4fe..b76a3f3 100644 --- a/sh/shadowenv.zsh.in +++ b/sh/shadowenv.zsh.in @@ -7,7 +7,7 @@ __shadowenv_hook() { flags+=(--force) unset __shadowenv_force_run fi - "@SELF@" hook "${flags[@]}" | source /dev/stdin + shadowenv hook "${flags[@]}" | source /dev/stdin } @HOOKBOOK@ __shadowenv_force_run=1 diff --git a/src/init.rs b/src/init.rs index b088d0a..a0cfeeb 100644 --- a/src/init.rs +++ b/src/init.rs @@ -7,28 +7,15 @@ use std::process::Command; /// print a script that can be sourced into the provided shell, and sets up the shadowenv shell /// hooks. pub fn run(cmd: InitCmd) -> Result<()> { - let pb = std::env::current_exe().unwrap(); // this would be... an unusual failure. match cmd { - Bash(opts) => print_script( - pb, - include_bytes!("../sh/shadowenv.bash.in"), - opts.no_hookbook, - ), - Zsh(opts) => print_script( - pb, - include_bytes!("../sh/shadowenv.zsh.in"), - opts.no_hookbook, - ), - Fish => print_script( - pb, - include_bytes!("../sh/shadowenv.fish.in"), - true, // Fish doesn't use hookbook - ), - Nushell => install_nushell_hook(pb), + Bash(opts) => print_script(include_bytes!("../sh/shadowenv.bash.in"), opts.no_hookbook), + Zsh(opts) => print_script(include_bytes!("../sh/shadowenv.zsh.in"), opts.no_hookbook), + Fish => print_script(include_bytes!("../sh/shadowenv.fish.in"), true), + Nushell => install_nushell_hook(), } } -fn install_nushell_hook(selfpath: PathBuf) -> Result<()> { +fn install_nushell_hook() -> Result<()> { let output = Command::new("nu") .args(["-c", "$nu.user-autoload-dirs | first"]) .output() @@ -54,19 +41,17 @@ fn install_nushell_hook(selfpath: PathBuf) -> Result<()> { .with_context(|| format!("Failed to create autoload directory '{}'", autoload_dir))?; let script_path = autoload_path.join("shadowenv.nu"); - let script = String::from_utf8_lossy(include_bytes!("../sh/shadowenv.nushell.in")); - let script = script.replace("@SELF@", selfpath.to_str().unwrap()); + let script = include_bytes!("../sh/shadowenv.nushell.in"); - fs::write(&script_path, script.as_bytes()) + fs::write(&script_path, script) .with_context(|| format!("Failed to write '{}'", script_path.display()))?; println!("Wrote shadowenv hook to {}", script_path.display()); Ok(()) } -fn print_script(selfpath: PathBuf, bytes: &[u8], no_hookbook: bool) -> Result<()> { +fn print_script(bytes: &[u8], no_hookbook: bool) -> Result<()> { let script = String::from_utf8_lossy(bytes); - let script = script.replace("@SELF@", selfpath.into_os_string().to_str().unwrap()); if no_hookbook { // If no_hookbook is true, replace @HOOKBOOK@ with an empty string