diff --git a/libmamba/include/mamba/util/environment.hpp b/libmamba/include/mamba/util/environment.hpp index f3fc75bced..0d6784d28c 100644 --- a/libmamba/include/mamba/util/environment.hpp +++ b/libmamba/include/mamba/util/environment.hpp @@ -62,6 +62,16 @@ namespace mamba::util */ [[nodiscard]] auto user_home_dir() -> std::string; +#ifndef _WIN32 + /** + * Return the directory to zsh dotfiles. + * + * This function checks for the `ZDOTDIR` environment variable and falls back to the user's + * home directory if it is not set. + */ + [[nodiscard]] auto zsh_home_dir() -> std::string; +#endif // _WIN32 + /** * Return the current user config directory. * diff --git a/libmamba/src/core/shell_init.cpp b/libmamba/src/core/shell_init.cpp index d823eed72a..768f0a8f10 100644 --- a/libmamba/src/core/shell_init.cpp +++ b/libmamba/src/core/shell_init.cpp @@ -1275,6 +1275,7 @@ namespace mamba } else if (shell == "zsh") { + home = util::zsh_home_dir(); fs::u8path zshrc_path = home / ".zshrc"; modify_rc_file(context, zshrc_path, conda_prefix, shell, mamba_exe); } @@ -1349,6 +1350,7 @@ namespace mamba } else if (shell == "zsh") { + home = util::zsh_home_dir(); fs::u8path zshrc_path = home / ".zshrc"; reset_rc_file(context, zshrc_path, shell, mamba_exe); } @@ -1415,6 +1417,7 @@ namespace mamba } else if (shell == "zsh") { + home = util::zsh_home_dir(); config_path = home / ".zshrc"; } else if (shell == "xonsh") diff --git a/libmamba/src/util/environment.cpp b/libmamba/src/util/environment.cpp index 9980b9a8ff..6e6f9d3c6e 100644 --- a/libmamba/src/util/environment.cpp +++ b/libmamba/src/util/environment.cpp @@ -290,6 +290,15 @@ namespace mamba::util throw std::runtime_error("HOME not set."); } + auto zsh_home_dir() -> std::string + { + if (auto maybe_zdotdir = get_env("ZDOTDIR").value_or(""); !maybe_zdotdir.empty()) + { + return maybe_zdotdir; + } + return user_home_dir(); + } + auto user_config_dir() -> std::string { if (auto maybe_dir = get_env("XDG_CONFIG_HOME").value_or(""); !maybe_dir.empty()) diff --git a/libmamba/tests/src/util/test_environment.cpp b/libmamba/tests/src/util/test_environment.cpp index 91c9010429..b9c9d84697 100644 --- a/libmamba/tests/src/util/test_environment.cpp +++ b/libmamba/tests/src/util/test_environment.cpp @@ -166,6 +166,29 @@ namespace } } + TEST_CASE("zsh_home_dir", "[mamba::util]") + { + if (on_win) + { + return; + } + + const auto restore = mambatests::EnvironmentCleaner(); + + SECTION("ZDOTDIR set") + { + set_env("ZDOTDIR", "/user/mamba/.zsh"); + REQUIRE(zsh_home_dir() == "/user/mamba/.zsh"); + } + + SECTION("ZDOTDIR not set") + { + unset_env("ZDOTDIR"); + set_env("HOME", "/user/mamba"); + REQUIRE(zsh_home_dir() == user_home_dir()); + } + } + TEST_CASE("user_xdg", "[mamba::util]") { const auto restore = mambatests::EnvironmentCleaner();