@@ -367,15 +367,35 @@ def _extra_requirements_args() -> Iterator[list[str]]:
367367 if candidate_fp .read (2 ) != b"#!" :
368368 continue
369369 shebang = candidate_fp .readline ()
370- if not shebang .startswith (work_dir_path_bytes ):
370+ if shebang != b"/bin/sh \n " and not shebang .startswith (work_dir_path_bytes ):
371371 continue
372+
372373 rewrite_target = candidate_console_script .with_suffix (".rewrite" )
373374 with rewrite_target .open ("wb" ) as rewrite_fp :
374375 rewrite_fp .write (b"#!" )
375- rewrite_fp .write (
376- shebang .replace (work_dir_path_bytes , venv_dir_path_bytes )
377- )
378- shutil .copyfileobj (candidate_fp , rewrite_fp )
376+ if shebang .startswith (work_dir_path_bytes ):
377+ rewrite_fp .write (
378+ shebang .replace (work_dir_path_bytes , venv_dir_path_bytes )
379+ )
380+ shutil .copyfileobj (candidate_fp , rewrite_fp )
381+ else :
382+ # N.B.: Scripts with too-long shebangs will use the `#!/bin/sh` trick.
383+ # Like so:
384+ # #!/bin/sh
385+ # # N.B.: This python script executes via a /bin/sh re-exec as a hack to work around a
386+ # # potential maximum shebang length of 128 bytes on this system which
387+ # # the python interpreter `exec`ed below would violate.
388+ # ''''exec /too/long/lead-in/path/.dev-cmd/venvs/Dik2FlYfLsaDdskunQh_vGTlBS1My7KattEsxC0M9-k.work/bin/python2.7 "$0" "$@"
389+ # '''
390+ # # -*- coding: utf-8 -*-
391+ # import importlib
392+ # ...
393+ rewrite_fp .write (shebang )
394+ rewrite_fp .write (
395+ candidate_fp .read ().replace (
396+ work_dir_path_bytes , venv_dir_path_bytes , 1
397+ )
398+ )
379399 rewrite_target .replace (candidate_console_script )
380400 _chmod_plus_x (candidate_console_script )
381401
0 commit comments