resholve icon indicating copy to clipboard operation
resholve copied to clipboard

Resholve doesn’t understand `sudo --verison`

Open Jayman2000 opened this issue 1 year ago • 4 comments

Resholve’s parser for sudo is supposed to support sudo --version, but it doesn’t work. This Nix expression:

{ pkgs ? import <nixpkgs> { } }:
pkgs.resholve.writeScript "sudo-fix-example" {
  inputs = [ pkgs.sudo ];
  fix = [ "sudo" ];
  interpreter = "${pkgs.bash}/bin/bash";
} ''
  sudo --version
''

fails to build with the following error:

[resholve context] : invoking resholve with PWD=/build
[resholve context] RESHOLVE_LORE=/nix/store/yhc5n7zv9sw87nbfn3qhnv05jifvpj76-more-binlore
[resholve context] RESHOLVE_FIX=sudo
[resholve context] RESHOLVE_INPUTS=/nix/store/q56d1m0ws1kf8vddfw71d8nfagnv0x6g-sudo-1.9.15p2/bin
[resholve context] RESHOLVE_INTERPRETER=/nix/store/7dpxg7ki7g8ynkdwcqf493p2x8divb4i-bash-5.2-p15/bin/bash
[resholve context] /nix/store/c99gv3kyb0qzqhppsb4iqijnzqx475vh-resholve-0.9.0/bin/resholve --overwrite /nix/store/bqah3m79k7wyrq3cah4sjbxwppm42xqk-sudo-fix-example
WARNING:__main__:CommandParser CommandParser(prog='sudo (generic)', usage=None, description=None, version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=False) passing instead of 'too few arguments'
WARNING:__main__:CommandParser CommandParser(prog='sudo (generic)', usage=None, description=None, version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=False) passing instead of 'argument commands is required'
  sudo --version
  ^~~~
'/nix/store/bqah3m79k7wyrq3cah4sjbxwppm42xqk-sudo-fix-example':2: 'sudo' executes its arguments in some circumstances, and none of my command-specific rules for figuring out if this specific invocation does or not succeeded. Look above for warning messages with more context on what went wrong.

Jayman2000 avatar Feb 15 '24 13:02 Jayman2000

Egh :)

My kingdom for a cli syntax parser that's a better fit for what we need.

I think the positional arg is causing this. ~~Probably need to make a 2nd parser for sudo to catch the forms w/o a positional arg.~~

abathur avatar Feb 15 '24 15:02 abathur

I pushed a tentative fix in https://github.com/abathur/resholve/commit/39f5012b419c97343b31d3aebba542d9c1c62907 if you're able to test it?

abathur avatar Feb 16 '24 21:02 abathur

It looks like that tentative fix works, but I think that I’ve discovered another problem. If I use RESHOLVE_FIX=sudo, then repo/tests/parse_sudo.sh becomes this:

#!/nix/store/f2ibj8np6dyddf3kq68dkcvwfr6bmi9n-bash-interactive-5.2-p15/bin/bash
# Caution: you're faking sudo as a script that runs `true`
/nix/store/q56d1m0ws1kf8vddfw71d8nfagnv0x6g-sudo-1.9.15p2/bin/sudo --version
/nix/store/q56d1m0ws1kf8vddfw71d8nfagnv0x6g-sudo-1.9.15p2/bin/sudo -v
/nix/store/q56d1m0ws1kf8vddfw71d8nfagnv0x6g-sudo-1.9.15p2/bin/sudo /nix/store/m38gwq0w8w7qyjn9s00balyp7cv3m5p9-coreutils-9.3/bin/ls

### resholve directives (auto-generated) ## format_version: 3
# resholve: fix sudo
# resholve: keep /nix/store/m38gwq0w8w7qyjn9s00balyp7cv3m5p9-coreutils-9.3/bin/ls
# resholve: keep /nix/store/q56d1m0ws1kf8vddfw71d8nfagnv0x6g-sudo-1.9.15p2/bin/sudo

But, if I use RESHOLVE_FAKE=external:sudo then repo/tests/parse_sudo.sh becomes this:

#!/nix/store/f2ibj8np6dyddf3kq68dkcvwfr6bmi9n-bash-interactive-5.2-p15/bin/bash
# Caution: you're faking sudo as a script that runs `true`
sudo --version
sudo -v
sudo ls

### resholve directives (auto-generated) ## format_version: 3
# resholve: fake external:sudo

Shouldn’t ls have been resolved, or am I misunderstanding how fake directives are supposed to work?

Jayman2000 avatar Feb 17 '24 19:02 Jayman2000

Shouldn’t ls have been resolved, or am I misunderstanding how fake directives are supposed to work?

As implemented, fake directives preempt resolution, and sub-exec is part of resolution.

That said, fake directives were implemented before sub-exec resolution, and I agree there's potential for confusion here.

I'm tempted to say these might both deserve independent levers, though I'm not sure how much work it is and there are some confounding variables like whether faking a function/alias sudo should pull in sub-exec processing or not...

abathur avatar Feb 23 '24 02:02 abathur