77from pre_commit .envcontext import envcontext
88from pre_commit .envcontext import Var
99from pre_commit .languages import helpers
10+ from pre_commit .languages .python import bin_dir
1011from pre_commit .util import clean_path_on_failure
1112from pre_commit .util import cmd_output
1213from pre_commit .xargs import xargs
1718healthy = helpers .basic_healthy
1819
1920
20- def get_env_patch (venv ): # pragma: windows no cover
21+ def _envdir (prefix , version ):
22+ directory = helpers .environment_dir (ENVIRONMENT_DIR , version )
23+ return prefix .path (directory )
24+
25+
26+ def get_env_patch (venv ):
2127 if sys .platform == 'cygwin' : # pragma: no cover
2228 _ , win_venv , _ = cmd_output ('cygpath' , '-w' , venv )
2329 install_prefix = r'{}\bin' .format (win_venv .strip ())
30+ elif sys .platform == 'win32' : # pragma: no cover
31+ install_prefix = bin_dir (venv )
2432 else :
2533 install_prefix = venv
2634 return (
2735 ('NODE_VIRTUAL_ENV' , venv ),
2836 ('NPM_CONFIG_PREFIX' , install_prefix ),
2937 ('npm_config_prefix' , install_prefix ),
3038 ('NODE_PATH' , os .path .join (venv , 'lib' , 'node_modules' )),
31- ('PATH' , (os . path . join (venv , 'bin' ), os .pathsep , Var ('PATH' ))),
39+ ('PATH' , (bin_dir (venv ), os .pathsep , Var ('PATH' ))),
3240 )
3341
3442
3543@contextlib .contextmanager
36- def in_env (prefix , language_version ): # pragma: windows no cover
37- envdir = prefix .path (
38- helpers .environment_dir (ENVIRONMENT_DIR , language_version ),
39- )
40- with envcontext (get_env_patch (envdir )):
44+ def in_env (prefix , language_version ):
45+ with envcontext (get_env_patch (_envdir (prefix , language_version ))):
4146 yield
4247
4348
44- def install_environment (
45- prefix , version , additional_dependencies ,
46- ): # pragma: windows no cover
49+ def install_environment (prefix , version , additional_dependencies ):
4750 additional_dependencies = tuple (additional_dependencies )
4851 assert prefix .exists ('package.json' )
49- directory = helpers . environment_dir ( ENVIRONMENT_DIR , version )
52+ envdir = _envdir ( prefix , version )
5053
51- env_dir = prefix .path (directory )
52- with clean_path_on_failure (env_dir ):
53- cmd = [sys .executable , '-m' , 'nodeenv' , '--prebuilt' , env_dir ]
54+ # https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx?f=255&MSPPError=-2147217396#maxpath
55+ if sys .platform == 'win32' : # pragma: no cover
56+ envdir = '\\ \\ ?\\ ' + os .path .normpath (envdir )
57+ with clean_path_on_failure (envdir ):
58+ cmd = [sys .executable , '-m' , 'nodeenv' , '--prebuilt' , envdir ]
5459 if version != 'default' :
5560 cmd .extend (['-n' , version ])
5661 cmd_output (* cmd )
@@ -62,6 +67,6 @@ def install_environment(
6267 )
6368
6469
65- def run_hook (prefix , hook , file_args ): # pragma: windows no cover
70+ def run_hook (prefix , hook , file_args ):
6671 with in_env (prefix , hook ['language_version' ]):
6772 return xargs (helpers .to_cmd (hook ), file_args )
0 commit comments