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 4f3e4b0d6d..2e4d30b523 100644 --- a/libmamba/src/core/shell_init.cpp +++ b/libmamba/src/core/shell_init.cpp @@ -1284,6 +1284,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); } @@ -1358,6 +1359,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); } @@ -1424,6 +1426,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 29ef25c30f..e34a1f6835 100644 --- a/libmamba/src/util/environment.cpp +++ b/libmamba/src/util/environment.cpp @@ -292,6 +292,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 d3903a3e1c..d43a4825d7 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();