]> git.eshelyaron.com Git - emacs.git/commitdiff
Support more complex env invocations in shebang lines
authorKévin Le Gouguec <kevin.legouguec@gmail.com>
Sat, 10 Feb 2024 16:37:35 +0000 (17:37 +0100)
committerEshel Yaron <me@eshelyaron.com>
Sat, 17 Feb 2024 13:03:05 +0000 (14:03 +0100)
This is not an exact re-implementation of what env accepts, but
hopefully it should be "good enough".

Example of known limitation: we assume that arguments for
--long-options will be set with '=', but that is not
necessarily the case.  '--unset' (mandatory argument) can be
passed as '--unset=VAR' or '--unset VAR', but
'--default-signal' (optional argument) requires an '=' sign.

For bug#64939.

* lisp/files.el (auto-mode-interpreter-regexp): Account for
supplementary arguments passed beside -S/--split-string.
* test/lisp/files-tests.el (files-tests-auto-mode-interpreter):
Test some of these combinations.

(cherry picked from commit ecb9641ecb5f42899042ff9c164ec7dbb8e166fe)

lisp/files.el
test/lisp/files-tests.el

index 8dc2ada41237772f4b662d30def4dafb2cc3f0a5..52b42be3f0ec2b6b2b16c04b6c53ff2996052666 100644 (file)
@@ -3274,7 +3274,13 @@ and `inhibit-local-variables-suffixes'.  If
     ;; Optional group 1: env(1) invocation.
     "\\("
     "[^ \t\n]*/bin/env[ \t]*"
-    "\\(?:-S[ \t]*\\|--split-string\\(?:=\\|[ \t]*\\)\\)?"
+    ;; Within group 1: possible -S/--split-string.
+    "\\(?:"
+    ;; -S/--split-string
+    "\\(?:-[0a-z]*S[ \t]*\\|--split-string=\\)"
+    ;; More env arguments.
+    "\\(?:-[^ \t\n]+[ \t]+\\)*"
+    "\\)?"
     "\\)?"
     ;; Group 2: interpreter.
     "\\([^ \t\n]+\\)"))
index 23516ff0d7dd20b6fb24eb0f17e675088338a513..0a5c3b897e488afbd5fa16a2fd9bb9f6ab67a06b 100644 (file)
@@ -1687,8 +1687,14 @@ set to."
   (files-tests--check-shebang "#!/usr/bin/env python" 'python-base-mode)
   (files-tests--check-shebang "#!/usr/bin/env python3" 'python-base-mode)
   ;; Invocation through env, with supplementary arguments.
+  (files-tests--check-shebang "#!/usr/bin/env --split-string=bash -eux" 'sh-base-mode 'bash)
+  (files-tests--check-shebang "#!/usr/bin/env --split-string=-iv --default-signal bash -eux" 'sh-base-mode 'bash)
   (files-tests--check-shebang "#!/usr/bin/env -S awk -v FS=\"\\t\" -v OFS=\"\\t\" -f" 'awk-mode)
-  (files-tests--check-shebang "#!/usr/bin/env -S make -f" 'makefile-mode))
+  (files-tests--check-shebang "#!/usr/bin/env -S make -f" 'makefile-mode)
+  (files-tests--check-shebang "#!/usr/bin/env -S-vi bash -eux" 'sh-base-mode 'bash)
+  (files-tests--check-shebang "#!/usr/bin/env -ivS --default-signal=INT bash -eux" 'sh-base-mode 'bash)
+  (files-tests--check-shebang "#!/usr/bin/env -ivS --default-signal bash -eux" 'sh-base-mode 'bash)
+  (files-tests--check-shebang "#!/usr/bin/env -vS -uFOOBAR bash -eux" 'sh-base-mode 'bash))
 
 (ert-deftest files-test-dir-locals-auto-mode-alist ()
   "Test an `auto-mode-alist' entry in `.dir-locals.el'"