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

lib/cli: add wrappingValueString option to toGNUCommandLineShell #373332

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

llakala
Copy link
Contributor

@llakala llakala commented Jan 13, 2025

Closes #373291.
The function currently doesn't provide a way to create a result like --foo "bar" or --foo="bar", where the argument is quoted. This new option resolves that. We set it to an empty string by default, so we can always interpolate it into the string, even if the user didn't set it.

Included in this PR is also a very small change to the original behavior. Now, the input foo = "bar" with optionValueSeparator unset will be transformed into [ "--foo bar" ], instead of [ "--foo" "bar" ]. This resulted in cleaner code, and I think it's more intuitive anyways.

nixpkgs-review is running right now on my system as I write this. There could be some packages that rely on this buggy behavior as mentioned above. I'll add the results in a comment when it finishes.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

Use `lib.concatStrings` for readability, and move `optionValueSeparator`
to the top.
@llakala
Copy link
Contributor Author

llakala commented Jan 13, 2025

Fixed the failing lib test, since it still expected the old format. Getting an error with nixpkgs-review, trying to debug if it's a me problem.

@llakala llakala changed the title lib/cli: add wrappingValueString option lib/cli: add wrappingValueString option to toGNUCommandLineShell Jan 13, 2025
@llakala
Copy link
Contributor Author

llakala commented Jan 13, 2025

Yep, it's an OOM error (according to Mic92/nixpkgs-review#387). If someone with more RAM could run nixpkgs-review, I'd appreciate it.

Working on fixing the other lib test right now.

@llakala
Copy link
Contributor Author

llakala commented Jan 13, 2025

Ah, I see the trouble now. The code for optionValueSeparator ensuring it got split into two list elements did have a purpose - it ensured that the behavior would be valid with lib.escapeShellArgs, which is used by toGNUCommandLineShell. I'll revert all those changes, then. It makes the code longer, but then there shouldn't be any lib tests to change, and nixpkgs-review shouldn't return any breakages.

The function currently doesn't provide a way to create a result like
`--foo "bar"` or `--foo="bar"`, where the argument is quoted. This
option resolves that. We set it to an empty string by default, so we can
always interpolate it into the string, even if the user didn't set it.
It wasn't clear to me why `toGNUCommandLine` would take an input like
this `foo = "bar"`, and turn it into `[ "foo" "bar" ]`. I implemented
a change to this, only to realize that this behavior is actually for
`toGNUCommandLineShell`. It uses `lib.escapeShellArgs`, which treats
the input `[ "--foo" "bar" ]` and `[ "--foo bar" ]` differently. The
comment will hopefully prevent others from going down the same rabbit
hole that I did.
Copy link
Member

@infinisil infinisil left a comment

Choose a reason for hiding this comment

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

This doesn't sound right, the point of toGNUCommandLineShell is to return a list of arguments, which you can then use with e.g. escapeShellArgs (or some different escaping mechanism) to turn it into a string suitable to pass to e.g. bash. Is there a reason this doesn't work for you? #373291 doesn't mention anything about that.

@llakala
Copy link
Contributor Author

llakala commented Jan 13, 2025

This doesn't sound right, the point of toGNUCommandLineShell is to return a list of arguments, which you can then use with e.g. escapeShellArgs (or some different escaping mechanism) to turn it into a string suitable to pass to e.g. bash. Is there a reason this doesn't work for you? #373291 doesn't mention anything about that.

Well, it's toGNUCommandLine that returns a list. toGNUCommandLineShell is a wrapper adding escapeShellArgs.

And I think you may be confused because I made changes since the original post, as mentioned in the comments(should've made an edit clarifying, that's my bad). The plan was never to make it not return a list - it was to make it put an option and its value in the same list element. So, this input:

{
  foo = "bar";
  hello = "hi";
}
when passed into `GNUcommandLineShell`, resulted in:
```nix
[
  "--foo"
  "bar"
  "--hello"
  "hi"
]

My change would've created an output like this:

[
  "--foo bar"
  "--hello hi"
]

But I realized that would create unforeseen changes and break a lot of workflows, including toGNUCommandLineShell with escapeShellArgs. So I removed that change from the commits. Now, the PR only adds the new flag, plus a lib test.

@infinisil
Copy link
Member

Well, it's toGNUCommandLine that returns a list. toGNUCommandLineShell is a wrapper adding escapeShellArgs.

Right, though both of those functions get the new wrappingValueString argument.

I guess I'll rephrase my question: What's your use case for needing --foo="bar"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lib.cli.toGnuCommandLine: no way to quote arguments
2 participants