GNU bug report logs - #76007
31.0.50; Interpreter not correctly found when using nix-shell.

Previous Next

Package: emacs;

Reported by: Malcolm Purvis <malcolm <at> purvis.id.au>

Date: Sun, 2 Feb 2025 11:48:02 UTC

Severity: normal

Tags: notabug

Found in version 31.0.50

Done: Stefan Kangas <stefankangas <at> gmail.com>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 76007 in the body.
You can then email your comments to 76007 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#76007; Package emacs. (Sun, 02 Feb 2025 11:48:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Malcolm Purvis <malcolm <at> purvis.id.au>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sun, 02 Feb 2025 11:48:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Malcolm Purvis <malcolm <at> purvis.id.au>
To: bug-gnu-emacs <at> gnu.org
Subject: 31.0.50; Interpreter not correctly found when using nix-shell.
Date: Sun, 02 Feb 2025 22:46:44 +1100
In NixOS if you want to use a script that manages its own 
dependencies, you use a command called "nix-shell" or "nix shell" 
to launch the script.

The start of such a script looks like:

#! /usr/bin/env nix-shell
#! nix-shell -i bash -p imagemagick cowsay

or

#! /usr/bin/env nix
#! nix shell github:tomberek/-#perlWith.HTMLTokeParserSimple.LWP 
  --command perl -x

The OS interpreter is nix-shell and nix-shell launches the real 
interpreter to execute the rest of the script.

With these scripts, Emacs's current behaviour is as follows:

- nix-shell is not interpreter-mode-alist so Emacs looks at the 
 file's 
extension:

- If the file has an extension then the correct mode is used
- If the file does not have an extension then Fundamental mode is 
used 
  regardless of the script's actual language.

If the file is a shell script with the correct extension, sh-mode 
is used but sh-mode fails to guess the shell, leaving the variable 
sh-shell as 'nix-shell.

In most cases this means that some shell features are not enabled, 
but otherwise it is possible to edit the script.

However, lsp-mode's bash language support (lsp-bash.el) throws an 
error if sh-shell is not set to it's supported values ('sh and 
'bash), and as a result nix bash scripts fail.

The solution is to add a new regexp to 
auto-mode-interpreter-regexp that will detect a nix script and 
extract the real interpreter from the second line.  Nix shell can 
be invoked in a number of ways but this regexp matches the common 
forms. 


diff --git a/lisp/files.el b/lisp/files.el
index 5ff40c335d7..1f3ac1431d2 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -3389,6 +3389,7 @@ and `inhibit-local-variables-suffixes'.  If
   "\\(?:[^ \t\n]+=[^ \t\n]*[ \t]+\\)*"
   "\\)?"
   "\\)?"
+   "\\(?:\\(?:nix\\|nix-shell\\)\n#![ 
\t]+\\(?:nix\\|nix-shell\\).*\\(?:-i\\|--command\\)[ \t]+\\)?"
   ;; Group 2: interpreter.  "\\([^ \t\n]+\\)")
  "Regexp matching interpreters, for file mode determination.


Malcolm


In GNU Emacs 31.0.50 (build 2, x86_64-apple-darwin24.2.0, NS
appkit-2575.30 Version 15.2 (Build 24C101)) of 2025-01-11 built on
lamechWindowing system distributor 'Apple', version 10.3.2575
System Description:  macOS 15.2

Configured using:
'configure 
--enable-locallisppath=/usr/local/share/emacs/site-lisp 
--infodir=/usr/local/Cellar/emacs-head <at> 31/31.0.50_1/share/info/emacs 
--prefix=/usr/local/Cellar/emacs-head <at> 31/31.0.50_1 --without-x 
--without-dbus --with-native-compilation --with-imagemagick 
--with-modules --with-gnutls --with-rsvg --with-xml2 
--with-xwidgets --with-webp --with-ns --disable-ns-self-contained 
'CFLAGS=-O2 -march=native -pipe -DFD_SETSIZE=10000 
-DDARWIN_UNLIMITED_SELECT' 'LDFLAGS=-L/usr/local/lib/gcc/14 
-I/usr/local/opt/gcc/include -I/usr/local/opt/libgccjit/include 
-I/usr/local/opt/gmp/include -I/usr/local/opt/jpeg/include''
 

-- 
		    Malcolm Purvis <malcolm <at> purvis.id.au> 




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#76007; Package emacs. (Sun, 02 Feb 2025 13:50:02 GMT) Full text and rfc822 format available.

Message #8 received at 76007 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefankangas <at> gmail.com>
To: Malcolm Purvis <malcolm <at> purvis.id.au>, 76007 <at> debbugs.gnu.org
Subject: Re: bug#76007: 31.0.50;
 Interpreter not correctly found when using nix-shell.
Date: Sun, 2 Feb 2025 08:49:36 -0500
Malcolm Purvis <malcolm <at> purvis.id.au> writes:

> In NixOS if you want to use a script that manages its own
> dependencies, you use a command called "nix-shell" or "nix shell"
> to launch the script.
>
> The start of such a script looks like:
>
> #! /usr/bin/env nix-shell
> #! nix-shell -i bash -p imagemagick cowsay
>
> or
>
> #! /usr/bin/env nix
> #! nix shell github:tomberek/-#perlWith.HTMLTokeParserSimple.LWP
>    --command perl -x
>
> The OS interpreter is nix-shell and nix-shell launches the real
> interpreter to execute the rest of the script.
>
> With these scripts, Emacs's current behaviour is as follows:
>
> - nix-shell is not interpreter-mode-alist so Emacs looks at the
>   file's
>  extension:
>
>  - If the file has an extension then the correct mode is used
>  - If the file does not have an extension then Fundamental mode is
>  used
>    regardless of the script's actual language.
>
> If the file is a shell script with the correct extension, sh-mode
> is used but sh-mode fails to guess the shell, leaving the variable
> sh-shell as 'nix-shell.
>
> In most cases this means that some shell features are not enabled,
> but otherwise it is possible to edit the script.
>
> However, lsp-mode's bash language support (lsp-bash.el) throws an
> error if sh-shell is not set to it's supported values ('sh and
> 'bash), and as a result nix bash scripts fail.
>
> The solution is to add a new regexp to
> auto-mode-interpreter-regexp that will detect a nix script and
> extract the real interpreter from the second line.  Nix shell can
> be invoked in a number of ways but this regexp matches the common
> forms.
>
>
> diff --git a/lisp/files.el b/lisp/files.el
> index 5ff40c335d7..1f3ac1431d2 100644
> --- a/lisp/files.el
> +++ b/lisp/files.el
> @@ -3389,6 +3389,7 @@ and `inhibit-local-variables-suffixes'.  If
>     "\\(?:[^ \t\n]+=[^ \t\n]*[ \t]+\\)*"
>     "\\)?"
>     "\\)?"
> +   "\\(?:\\(?:nix\\|nix-shell\\)\n#![
> \t]+\\(?:nix\\|nix-shell\\).*\\(?:-i\\|--command\\)[ \t]+\\)?"
>     ;; Group 2: interpreter.  "\\([^ \t\n]+\\)")
>    "Regexp matching interpreters, for file mode determination.
>
>
> Malcolm

Thanks for the bug report.

This looks to me like a bug in lsp-mode, or lsp-bash.el.  I recommend
reporting the bug to them first.




Added tag(s) notabug. Request was from Stefan Kangas <stefankangas <at> gmail.com> to control <at> debbugs.gnu.org. (Sun, 02 Feb 2025 13:51:02 GMT) Full text and rfc822 format available.

Reply sent to Stefan Kangas <stefankangas <at> gmail.com>:
You have taken responsibility. (Sun, 02 Mar 2025 03:37:02 GMT) Full text and rfc822 format available.

Notification sent to Malcolm Purvis <malcolm <at> purvis.id.au>:
bug acknowledged by developer. (Sun, 02 Mar 2025 03:37:02 GMT) Full text and rfc822 format available.

Message #15 received at 76007-done <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefankangas <at> gmail.com>
To: Malcolm Purvis <malcolm <at> purvis.id.au>
Cc: 76007-done <at> debbugs.gnu.org
Subject: Re: bug#76007: 31.0.50;
 Interpreter not correctly found when using nix-shell.
Date: Sat, 1 Mar 2025 19:35:58 -0800
Stefan Kangas <stefankangas <at> gmail.com> writes:

> Malcolm Purvis <malcolm <at> purvis.id.au> writes:
>
>> In NixOS if you want to use a script that manages its own
>> dependencies, you use a command called "nix-shell" or "nix shell"
>> to launch the script.
>>
>> The start of such a script looks like:
>>
>> #! /usr/bin/env nix-shell
>> #! nix-shell -i bash -p imagemagick cowsay
>>
>> or
>>
>> #! /usr/bin/env nix
>> #! nix shell github:tomberek/-#perlWith.HTMLTokeParserSimple.LWP
>>    --command perl -x
>>
>> The OS interpreter is nix-shell and nix-shell launches the real
>> interpreter to execute the rest of the script.
>>
>> With these scripts, Emacs's current behaviour is as follows:
>>
>> - nix-shell is not interpreter-mode-alist so Emacs looks at the
>>   file's
>>  extension:
>>
>>  - If the file has an extension then the correct mode is used
>>  - If the file does not have an extension then Fundamental mode is
>>  used
>>    regardless of the script's actual language.
>>
>> If the file is a shell script with the correct extension, sh-mode
>> is used but sh-mode fails to guess the shell, leaving the variable
>> sh-shell as 'nix-shell.
>>
>> In most cases this means that some shell features are not enabled,
>> but otherwise it is possible to edit the script.
>>
>> However, lsp-mode's bash language support (lsp-bash.el) throws an
>> error if sh-shell is not set to it's supported values ('sh and
>> 'bash), and as a result nix bash scripts fail.
>>
>> The solution is to add a new regexp to
>> auto-mode-interpreter-regexp that will detect a nix script and
>> extract the real interpreter from the second line.  Nix shell can
>> be invoked in a number of ways but this regexp matches the common
>> forms.
>>
>>
>> diff --git a/lisp/files.el b/lisp/files.el
>> index 5ff40c335d7..1f3ac1431d2 100644
>> --- a/lisp/files.el
>> +++ b/lisp/files.el
>> @@ -3389,6 +3389,7 @@ and `inhibit-local-variables-suffixes'.  If
>>     "\\(?:[^ \t\n]+=[^ \t\n]*[ \t]+\\)*"
>>     "\\)?"
>>     "\\)?"
>> +   "\\(?:\\(?:nix\\|nix-shell\\)\n#![
>> \t]+\\(?:nix\\|nix-shell\\).*\\(?:-i\\|--command\\)[ \t]+\\)?"
>>     ;; Group 2: interpreter.  "\\([^ \t\n]+\\)")
>>    "Regexp matching interpreters, for file mode determination.
>>
>>
>> Malcolm
>
> Thanks for the bug report.
>
> This looks to me like a bug in lsp-mode, or lsp-bash.el.  I recommend
> reporting the bug to them first.

No further comments within 4 weeks, so I'm closing this bug.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 30 Mar 2025 11:24:09 GMT) Full text and rfc822 format available.

This bug report was last modified 92 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.