Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nix-shell: improve shebang recognition #855

Closed
wants to merge 1 commit into from

Conversation

Profpatsch
Copy link
Member

Some languages don’t accept shebangs that are more than one line, e.g.
lua. This patch laxes the restriction that the second line needs to
start with #!, it can now start with any string instead.

E.g. in lua one can now use the comment symbols:

 #!/usr/bin/env nix-shell
 -- nix-shell -i …

Some languages don’t accept shebangs that are more than one line, e.g.
lua. This patch laxes the restriction that the second line needs to
start with #!, it can now start with any string instead.

E.g. in lua one can now use the comment symbols:
 #!/usr/bin/env nix-shell
 -- nix-shell -i …
@@ -56,7 +56,7 @@ if ($runEnv && defined $ARGV[0] && $ARGV[0] !~ /nix-shell/) {
@ARGV = ();
while (<SCRIPT>) {
chomp;
if (/^\#\!\s*nix-shell (.*)$/) {
if (/^.*\s*nix-shell (.*)$/) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: .*\s* -> .*\s

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Erm, but then you need a space, don’t you?
That would be quite an arbitrary constraint imho.

I played with the thought of removing \s* altogether, but I like the explicitness of this version.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Actually .*\s* is equivalent to just .*. Having \s* doesn't bring anything to the table since it's part of the .* set.

@Profpatsch
Copy link
Member Author

cc @edolstra

@edolstra
Copy link
Member

Don't think this is a good idea because it will match any line mentioning nix-shell, even (say) comments.

@Profpatsch
Copy link
Member Author

@edolstra You are right, I didn’t look at the surrounding loop very well.
It should of course only be possible to have the interpreter line as the second line of the file, or am I mistaken?

@zimbatm
Copy link
Member

zimbatm commented Mar 21, 2016

I think he meant the change is overly permissive. I guess the minimal change would be to add each language individually and so match /^(?:\#\!|--)\s*nix-shell (.*)$/ for now.

@Profpatsch
Copy link
Member Author

@zimbatm Congratulations on that advanced perlre-fu.

If I interpret the loop correctly, this will add any comment line containing the word nix-shell to the argument. How about stopping once the first non-matching line is reached?

            while (<SCRIPT>) {
                chomp;
                # #!: normal shebang line, ignored by most programming languages
                #     (even on the second line)
                # --: lua comment
                if (/^(?:\#\!|--)\s*nix-shell (.*)$/) {
                    push @ARGV, shellwords($1);
                } else {
                    last;
                }
            }

@zimbatm
Copy link
Member

zimbatm commented Mar 21, 2016

My knowledge is limited to Regexp unfortunately. Perl is still a bunch of characters to me :)

@Ericson2314
Copy link
Member

I don't know Perl at all, but I'm willing to try! :P

while (<SCRIPT> && chomp && /^.*nix-shell (.*)$/) {
    push @ARGV, shellwords($1);
}

@olejorgenb
Copy link
Contributor

Any progress here?

@Profpatsch
Copy link
Member Author

@olejorgenb Not from my side, no. That kind of slipped through.

@Profpatsch
Copy link
Member Author

Pretty much out-of-date since nix-shell has been rewritten in C++ for nix 2.0.

@Profpatsch Profpatsch closed this Feb 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants