refactor(languages/conda): adapt scripts path resolution

Cygwin isn't the only edge-case for a differing bin/ path. In addition there
also is MSYS2, which behaves differently than Cygwin. CPython compiled against
Microsoft Visual C++ Runtime (MSVCRT) through MinGW, will correctly identify as
an 'nt' platform, yet will prefer a POSIX path scheme.

In order to avoid having to patch every edge-case, we can spy on the system
configurations install paths through the `sysconfig` module and get the basename
of the global scripts path, which will be returned in accordance with the
preferred install path scheme of the system/platform.

Fixes: https://github.com/pre-commit/pre-commit/issues/3448
This commit is contained in:
Rodney, Tiara 2025-04-15 15:22:30 +02:00
parent 0d50ffc2b7
commit c68a3ba59f
No known key found for this signature in database
GPG key ID: 5CD8EC1D46106723

View file

@ -12,6 +12,7 @@ from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import SubstitutionT
from pre_commit.envcontext import UNSET
from pre_commit.envcontext import Var
from pre_commit.languages.python import bin_dir
from pre_commit.prefix import Prefix
from pre_commit.util import cmd_output_b
@ -26,10 +27,9 @@ def get_env_patch(env: str) -> PatchesT:
# they can be in $CONDA_PREFIX/bin, $CONDA_PREFIX/Library/bin,
# $CONDA_PREFIX/Scripts and $CONDA_PREFIX. Whereas the latter only
# seems to be used for python.exe.
path: SubstitutionT = (os.path.join(env, 'bin'), os.pathsep, Var('PATH'))
path: SubstitutionT = (str(bin_dir(env)), os.pathsep, Var('PATH'))
if sys.platform == 'win32': # pragma: win32 cover
path = (env, os.pathsep, *path)
path = (os.path.join(env, 'Scripts'), os.pathsep, *path)
path = (os.path.join(env, 'Library', 'bin'), os.pathsep, *path)
return (