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: support single quotes in shebangs, fix whitespace parsing #8470

Merged
merged 3 commits into from
Oct 23, 2023

Conversation

ncfavier
Copy link
Member

@ncfavier ncfavier commented Jun 7, 2023

Motivation and context

Fixes #2356

Checklist for maintainers

Maintainers: tick if completed or explain if not relevant

  • agreed on idea
  • agreed on implementation strategy
  • tests, as appropriate
    • functional tests - tests/**.sh
    • unit tests - src/*/tests
    • integration tests - tests/nixos/*
  • documentation in the manual
  • documentation in the internal API docs
  • code and comments are self-explanatory
  • commit message explains why the change was made
  • new feature or incompatible change: updated release notes

Priorities

Add 👍 to pull requests you find important.

@ncfavier ncfavier requested a review from edolstra as a code owner June 7, 2023 16:17
@github-actions github-actions bot added the with-tests Issues related to testing. PRs with tests have some priority label Jun 7, 2023
@ncfavier ncfavier force-pushed the shebang-single-quotes branch 2 times, most recently from c59460e to ac7e219 Compare June 7, 2023 17:52
@ncfavier ncfavier marked this pull request as draft June 7, 2023 23:03
@ncfavier
Copy link
Member Author

ncfavier commented Jun 7, 2023

I spotted another issue with the whitespace:

#! nix-shell   foo
results in
'' foo
#! nix-shell foo ''
results in
foo

I might as well fix that too.

@ncfavier ncfavier force-pushed the shebang-single-quotes branch from ac7e219 to dd8ac26 Compare June 8, 2023 08:33
@ncfavier ncfavier marked this pull request as ready for review June 8, 2023 08:33
@ncfavier
Copy link
Member Author

ncfavier commented Jun 8, 2023

Should be good now.

@ncfavier ncfavier changed the title nix-shell: support single quotes in shebangs nix-shell: support single quotes in shebangs, fix whitespace parsing Jun 8, 2023
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2023-06-09-nix-team-meeting-minutes-61/29163/1

@@ -1,4 +1,4 @@
#! @ENV_PROG@ nix-shell
#! nix-shell -I nixpkgs=shell.nix --no-substitute
#! nix-shell --pure -i bash -p foo bar
#! nix-shell --pure -i bash -p '(_: foo) "absurd"' "b\ar"
Copy link
Member

Choose a reason for hiding this comment

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

This test does not prove enough, because this works:

nix-repl> with { foo = true; }; (_: foo) absurd
true

What we need to see is that the parsed values are correct, including

  • " in '
  • ' in "
  • escaping
  • concatenation, for example is this a valid way to handle a single quote in a single-quoted string?
    does 'It'"'"'s one string' parse as a single string containing It's one string ?

To test this effectively, I think it'd be best to find a way to echo a whole string and not just some variables. Maybe --argstr could be useful?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@ncfavier ncfavier force-pushed the shebang-single-quotes branch 2 times, most recently from 406fa76 to bf64b4b Compare June 17, 2023 12:57
@ncfavier
Copy link
Member Author

Macos tests failing because of cachix/install-nix-action#183, need install-nix-action bump.

Would be cool not to cancel the ubuntu tests on failure.

@ncfavier ncfavier force-pushed the shebang-single-quotes branch from bf64b4b to 2978fc2 Compare June 17, 2023 13:09
@ncfavier ncfavier requested a review from roberth June 27, 2023 10:12
@ncfavier
Copy link
Member Author

ncfavier commented Aug 7, 2023

Does anything else need to be done here?

@ncfavier
Copy link
Member Author

ncfavier commented Aug 7, 2023

I guess I can write a release note.

@ncfavier ncfavier force-pushed the shebang-single-quotes branch from 2978fc2 to 3cc763d Compare August 7, 2023 11:42
Single quotes are a basic feature of shell syntax that people expect to
work. They are also more convenient for writing literal code expressions
with less escaping.
Leading whitespace after `nix-shell` used to produce an empty argument,
while an empty argument at the end of the line was ignored.

Fix the first issue by consuming the initial whitespace before calling
shellwords; fix the second issue by returning immediately if whitespace
is found at the end of the string instead of checking for an empty
string.

Also throw if quotes aren't terminated.
@roberth roberth force-pushed the shebang-single-quotes branch from 3cc763d to 5f8e057 Compare October 23, 2023 13:59
Copy link
Member

@roberth roberth left a comment

Choose a reason for hiding this comment

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

Looks good now. Sorry for the delay!

@roberth roberth enabled auto-merge October 23, 2023 14:00
@roberth roberth force-pushed the shebang-single-quotes branch from 5f8e057 to e053eeb Compare October 23, 2023 15:32
@roberth roberth merged commit 3b99c62 into NixOS:master Oct 23, 2023
7 checks passed
@ncfavier ncfavier deleted the shebang-single-quotes branch October 23, 2023 16:33
tebowy pushed a commit to tebowy/nix that referenced this pull request Jul 11, 2024
nix-shell: support single quotes in shebangs, fix whitespace parsing
(cherry picked from commit 3b99c62)
Change-Id: I2a431b21c3467eefa1ef95d5a36d672f45b6937a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug with-tests Issues related to testing. PRs with tests have some priority
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

nix-shell in second #! line only accepts double quotes for package expressions
4 participants