From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?K=C3=A9vin?= Le Gouguec Newsgroups: gmane.emacs.bugs Subject: bug#66902: 30.0.50; Recognize env -S/--split-string in shebangs Date: Sat, 18 Nov 2023 18:44:06 +0100 Message-ID: <87wmufardl.fsf@gmail.com> References: <87ttq3lvpm.fsf@gmail.com> <871qcuuacb.fsf@gmail.com> <83ttpjmm8v.fsf@gnu.org> <8734x3cpz2.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="11109"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 66902@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Nov 18 18:45:17 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1r4PNd-0002hF-JS for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 18 Nov 2023 18:45:17 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r4PNP-0005t8-MH; Sat, 18 Nov 2023 12:45:03 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r4PNO-0005su-52 for bug-gnu-emacs@gnu.org; Sat, 18 Nov 2023 12:45:02 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1r4PNN-0005lH-Se for bug-gnu-emacs@gnu.org; Sat, 18 Nov 2023 12:45:01 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1r4PNO-0007Dk-B5 for bug-gnu-emacs@gnu.org; Sat, 18 Nov 2023 12:45:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: =?UTF-8?Q?K=C3=A9vin?= Le Gouguec Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 18 Nov 2023 17:45:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 66902 X-GNU-PR-Package: emacs Original-Received: via spool by 66902-submit@debbugs.gnu.org id=B66902.170032945827657 (code B ref 66902); Sat, 18 Nov 2023 17:45:02 +0000 Original-Received: (at 66902) by debbugs.gnu.org; 18 Nov 2023 17:44:18 +0000 Original-Received: from localhost ([127.0.0.1]:49454 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r4PMg-0007C0-B8 for submit@debbugs.gnu.org; Sat, 18 Nov 2023 12:44:18 -0500 Original-Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]:58631) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r4PMd-0007Bi-06 for 66902@debbugs.gnu.org; Sat, 18 Nov 2023 12:44:16 -0500 Original-Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-40842752c6eso4697455e9.1 for <66902@debbugs.gnu.org>; Sat, 18 Nov 2023 09:44:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700329448; x=1700934248; darn=debbugs.gnu.org; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=6tLe8O37ecY8IgGgmQq2bikp1spHrVDd9uRBTjSZ9CA=; b=Onr1MZBPj1V9/Np9x9oD3jxRTyTDUAkupIh5R6sbKB5SxQWe4QdP1305vYczkIQfpV bkin/NETACfDpMUMo//nx3frayDXhPpBmi76gp+VWEGsHm9lMtRgkvhUE+3MvSrBtAoJ BPPYDUZvAQg5QimGBv+O0G1JeMJXWclWNRNg8EnaF+53MH3zjR4OD5Jt5xa5/pAAM9bA vnawLi4pbUSXgNbQJyn3ZKcAdUNa4NyABGb0w3F7TBYQK7V7D3WmWFfnkYHAiYq9pjmx jLBaN1NRqva/1LQb9fMWa5zT/QpdshHokuzjkcgnrsBEEb3wLdVH3Vf7L9Eh64rcqzjf K95w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700329448; x=1700934248; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=6tLe8O37ecY8IgGgmQq2bikp1spHrVDd9uRBTjSZ9CA=; b=bXkf2Z5gVYcjRFXU1UhrnxGf4PvJ/K/u2QF47XIdH62VEZ2TuAFrZZFabaxFTCmQAT RSvg/FdCn6Q4F2nqJI4JRiaMSTvTLI8vonk7GCmvXxaU/Z+7PcIkQnHzogwJsttwCtHG VVJ7p+cRd+0qvaZhPRBI0CUwhr4VF/ifGmxkD4p0QwVXRaxIFAzzw77zZyuwLT6YrvBP We5ZtS2LV+l7OJmP0IR4Iet3pX+kQWhrvPgsTrX43v6/qCvQp9NbaK0KJiBqzuV5YPRo K/32ZC5GBsCAQ59E+TKr2gDN+ahDq2jrUdh9rLUYI8sjIbYMf+Rd2VuZ+JEfYYRMMdW3 vebA== X-Gm-Message-State: AOJu0YyfNA+fZDKvoL7AnlhEF/h5MaM6H8vB1kNmbmVuP+Kdo5kXxWwk mNzmWULQAiYDVIje1ceO0y51sFIhBbIrebrG X-Google-Smtp-Source: AGHT+IERppNBj92q3aWO8L57GqZxuLbFAmHdU3+932U3xZnc7EPUyOIM1r0A9Lfumv6O6fcc9vkJ7w== X-Received: by 2002:a05:600c:1c96:b0:405:336b:8307 with SMTP id k22-20020a05600c1c9600b00405336b8307mr2276474wms.7.1700329447468; Sat, 18 Nov 2023 09:44:07 -0800 (PST) Original-Received: from amdahl30 ([2a01:e0a:253:fe0:2ef0:5dff:fed2:7b49]) by smtp.gmail.com with ESMTPSA id b8-20020a5d45c8000000b003142e438e8csm5839631wrs.26.2023.11.18.09.44.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Nov 2023 09:44:06 -0800 (PST) In-Reply-To: <8734x3cpz2.fsf@gmail.com> ("=?UTF-8?Q?K=C3=A9vin?= Le Gouguec"'s message of "Sat, 18 Nov 2023 11:31:29 +0100") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:274564 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable K=C3=A9vin Le Gouguec writes: > Eli Zaretskii writes: > >> I'd prefer not to have rx required in files.el, so could you please >> rewrite those parts of your patch and resubmit? Also, please add a >> NEWS entry about the change.=20=20 > > ACK; will get to it in the coming days. s/days/hours/ I left a 'concat' in, because (a) it lets us interleave comments (b) the byte-compiler seems to smartly condense it all to one big string literal anyway. (Though if files.el is preloaded, everything happens at build-time and the .elc does not matter much, IIUC?) Let me know if we would prefer a plain raw string literal. Added a NEWS entry (under =C2=A7 'Changes in Emacs 30.1 / Miscellaneous', assuming 'master'); added a bug reference; squashed it all. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Recognize-shebang-lines-that-pass-S-split-string-to-.patch >From 95068836b5970c1aebb088e987741ad316007b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Le=20Gouguec?= Date: Sun, 12 Nov 2023 10:55:24 +0100 Subject: [PATCH] Recognize shebang lines that pass -S/--split-string to env (bug#66902) * etc/NEWS: announce the change. * lisp/files.el (auto-mode-interpreter-regexp): Add optional -S switch to the ignored group capturing the env invocation. Allow multiple spaces between #!, interpreter and first argument: empirically, Linux's execve accepts that. * test/lisp/files-tests.el (files-tests--check-shebang): New helper to generate a temporary file with a given interpreter line, and assert that the mode picked by 'set-auto-mode' is derived from an expected mode. Write the 'should' form so that failure reports include useful context; for example: (ert-test-failed ((should (equal (list shebang actual-mode) (list shebang expected-mode))) :form (equal ("#!/usr/bin/env -S make -f" fundamental-mode) ("#!/usr/bin/env -S make -f" makefile-mode)) :value nil :explanation (list-elt 1 (different-atoms fundamental-mode makefile-mode)))) (files-tests-auto-mode-interpreter): New test; exercise some aspects of interpreter-mode-alist. --- etc/NEWS | 6 ++++++ lisp/files.el | 12 ++++++++++-- test/lisp/files-tests.el | 25 +++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 12ae8058cb1..b9ee3747040 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -233,6 +233,12 @@ to enter the file you want to modify. It can be used to customize the look of the appointment notification displayed on the mode line when 'appt-display-mode-line' is non-nil. +--- +*** Emacs now recognizes shebang lines that pass -S/--split-string to env. +When visiting a script that invokes 'env -S INTERPRETER ARGS...' in +its shebang line, Emacs will now skip over 'env -S' and deduce the +major mode based on the interpreter. + ** Emacs Server and Client --- diff --git a/lisp/files.el b/lisp/files.el index d729bdf8c25..1cdcec23b11 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -3245,8 +3245,16 @@ inhibit-local-variables-p temp)) (defvar auto-mode-interpreter-regexp - (purecopy "#![ \t]?\\([^ \t\n]*\ -/bin/env[ \t]\\)?\\([^ \t\n]+\\)") + (purecopy + (concat + "#![ \t]*" + ;; Optional group 1: env(1) invocation. + "\\(" + "[^ \t\n]*/bin/env[ \t]*" + "\\(?:-S[ \t]*\\|--split-string\\(?:=\\|[ \t]*\\)\\)?" + "\\)?" + ;; Group 2: interpreter. + "\\([^ \t\n]+\\)")) "Regexp matching interpreters, for file mode determination. This regular expression is matched against the first line of a file to determine the file's mode in `set-auto-mode'. If it matches, the file diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el index 3492bd701b2..3e499fff468 100644 --- a/test/lisp/files-tests.el +++ b/test/lisp/files-tests.el @@ -1656,6 +1656,31 @@ files-tests-file-name-base (should (equal (file-name-base "foo") "foo")) (should (equal (file-name-base "foo/bar") "bar"))) +(defun files-tests--check-shebang (shebang expected-mode) + "Assert that mode for SHEBANG derives from EXPECTED-MODE." + (let ((actual-mode + (ert-with-temp-file script-file + :text shebang + (find-file script-file) + (if (derived-mode-p expected-mode) + expected-mode + major-mode)))) + ;; Tuck all the information we need in the `should' form: input + ;; shebang, expected mode vs actual. + (should + (equal (list shebang actual-mode) + (list shebang expected-mode))))) + +(ert-deftest files-tests-auto-mode-interpreter () + "Test that `set-auto-mode' deduces correct modes from shebangs." + (files-tests--check-shebang "#!/bin/bash" 'sh-mode) + (files-tests--check-shebang "#!/usr/bin/env bash" 'sh-mode) + (files-tests--check-shebang "#!/usr/bin/env python" 'python-base-mode) + (files-tests--check-shebang "#!/usr/bin/env python3" 'python-base-mode) + (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/make -f" 'makefile-mode)) + (ert-deftest files-test-dir-locals-auto-mode-alist () "Test an `auto-mode-alist' entry in `.dir-locals.el'" (find-file (ert-resource-file "whatever.quux")) -- 2.42.1 --=-=-=--