From 394faa6850b02fc0bafd9f0664ab952cac28b8d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= Date: Fri, 3 Oct 2025 11:57:28 +0100 Subject: [PATCH 1/4] Allow libgit2 to guess sysdir homedir successfully This prevents the generic error: _pygit2.GitError: error loading known_hosts: which is happening in certain pygit2/libgit2 versions --- salt/utils/gitfs.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/salt/utils/gitfs.py b/salt/utils/gitfs.py index 733ed3e29449..ffcae96f4537 100644 --- a/salt/utils/gitfs.py +++ b/salt/utils/gitfs.py @@ -119,6 +119,15 @@ with warnings.catch_warnings(): warnings.simplefilter("ignore") + if "HOME" not in os.environ: + # Make sure $HOME env variable is set before importing pygit2 to prevent + # _pygit2.GitError: error loading known_hosts in some libgit2 versions. + # The internal "git_sysdir__dirs" from libgit2, is initializated + # when importing pygit2. The $HOME env must be present to allow libgit2 + # guessing function to successfully set the homedir in the initializated + # libgit2 stack. + # https://github.com/saltstack/salt/issues/64121 + os.environ["HOME"] = os.path.expanduser("~") import pygit2 PYGIT2_VERSION = Version(pygit2.__version__) LIBGIT2_VERSION = Version(pygit2.LIBGIT2_VERSION) @@ -2012,13 +2021,9 @@ def init_remote(self): """ # https://github.com/libgit2/pygit2/issues/339 # https://github.com/libgit2/libgit2/issues/2122 - # https://github.com/saltstack/salt/issues/64121 - home = os.path.expanduser("~") - if "HOME" not in os.environ: - # Make sure $HOME env variable is set to prevent - # _pygit2.GitError: error loading known_hosts in some libgit2 versions. - os.environ["HOME"] = home - pygit2.settings.search_path[pygit2.GIT_CONFIG_LEVEL_GLOBAL] = home + pygit2.settings.search_path[pygit2.GIT_CONFIG_LEVEL_GLOBAL] = ( + os.path.expanduser("~") + ) new = False if not os.listdir(self._cachedir): # Repo cachedir is empty, initialize a new repo there From 7d8b04e66afb7cf85758871456d54d2fd39720da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= Date: Fri, 3 Oct 2025 12:14:30 +0100 Subject: [PATCH 2/4] Fix pygit2 unit test to check HOME is injected --- tests/pytests/unit/utils/test_gitfs.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/pytests/unit/utils/test_gitfs.py b/tests/pytests/unit/utils/test_gitfs.py index 683ac1a0333a..c3abd5a96184 100644 --- a/tests/pytests/unit/utils/test_gitfs.py +++ b/tests/pytests/unit/utils/test_gitfs.py @@ -264,7 +264,9 @@ def test_checkout_pygit2_with_home_env_unset(_prepare_provider): provider.credentials = None with patched_environ(__cleanup__=["HOME"]): assert "HOME" not in os.environ - provider.init_remote() + import importlib + + importlib.reload(salt.utils.gitfs) assert "HOME" in os.environ From 439d3c41d220a8d778d3016079e60ac64e7deeda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= Date: Fri, 3 Oct 2025 12:57:02 +0100 Subject: [PATCH 3/4] Add changelog entry --- changelog/64121.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/64121.fixed.md diff --git a/changelog/64121.fixed.md b/changelog/64121.fixed.md new file mode 100644 index 000000000000..255c1ca2b288 --- /dev/null +++ b/changelog/64121.fixed.md @@ -0,0 +1 @@ +Prevent `_pygit2.GitError: error loading known_hosts` with certain pygit2/libgit2 versions. From 6b52345384cf98c55ef6d4841ae0c862f1071b96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= Date: Mon, 13 Oct 2025 11:57:55 +0100 Subject: [PATCH 4/4] Move import statement to the beginning of the file --- tests/pytests/unit/utils/test_gitfs.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/pytests/unit/utils/test_gitfs.py b/tests/pytests/unit/utils/test_gitfs.py index c3abd5a96184..4df5b9af7bea 100644 --- a/tests/pytests/unit/utils/test_gitfs.py +++ b/tests/pytests/unit/utils/test_gitfs.py @@ -1,3 +1,4 @@ +import importlib import os import time @@ -264,8 +265,6 @@ def test_checkout_pygit2_with_home_env_unset(_prepare_provider): provider.credentials = None with patched_environ(__cleanup__=["HOME"]): assert "HOME" not in os.environ - import importlib - importlib.reload(salt.utils.gitfs) assert "HOME" in os.environ