Skip to content

Fixes autocomplete when using sudo#8094

Merged
rgwood merged 9 commits intonushell:mainfrom
alesito85:fix-autocomplete-with-sudo
Feb 24, 2023
Merged

Fixes autocomplete when using sudo#8094
rgwood merged 9 commits intonushell:mainfrom
alesito85:fix-autocomplete-with-sudo

Conversation

@alesito85
Copy link
Contributor

Description

This PR addresses issue #2047 in order to enable autocomplete functionality when using sudo for executing commands. I'e done a couple of auxiliary checks such as ignoring whitespace and the last pipe in order to determine the last command.

User-Facing Changes

The only user facing change should be the autocomplete working.

Tests + Formatting

All tests and formatting pass.

Screenshots

image

Suggestions welcome

I still don't know the in's and out's if nushell very well, any suggestions for improvements are welcome.

@codecov
Copy link

codecov bot commented Feb 16, 2023

Codecov Report

Merging #8094 (c80f9a8) into main (789b2e6) will increase coverage by 0.56%.
The diff coverage is 95.23%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8094      +/-   ##
==========================================
+ Coverage   54.23%   54.79%   +0.56%     
==========================================
  Files         608      608              
  Lines       99003    99147     +144     
==========================================
+ Hits        53693    54331     +638     
+ Misses      45310    44816     -494     
Impacted Files Coverage Δ
...ates/nu-cli/src/completions/command_completions.rs 79.68% <95.00%> (+79.68%) ⬆️
crates/nu-cli/src/completions/completer.rs 48.82% <95.00%> (+48.82%) ⬆️
crates/nu-protocol/src/engine/engine_state.rs 66.22% <100.00%> (+3.91%) ⬆️
crates/nu-command/src/viewers/table.rs 31.19% <0.00%> (-0.49%) ⬇️
crates/nu_plugin_formats/src/lib.rs 0.00% <0.00%> (ø)
crates/nu-command/src/conversions/fill.rs 90.77% <0.00%> (+0.04%) ⬆️
crates/nu-path/src/expansions.rs 97.95% <0.00%> (+2.04%) ⬆️
crates/nu-command/src/env/with_env.rs 84.55% <0.00%> (+3.71%) ⬆️
crates/nu-parser/src/flatten.rs 55.25% <0.00%> (+5.40%) ⬆️
... and 5 more

@alesito85
Copy link
Contributor Author

alesito85 commented Feb 17, 2023

@rgwood I'm not really sure what to look for in the failed CI since everything passes locally. Could this be a temporary issue or something that actually needs a fix?

@sholderbach
Copy link
Member

I think you came across the flakiness of the plugin tests on the Mac CI runners. Restarted them, should typically resolve.

Copy link
Member

@sholderbach sholderbach left a comment

Choose a reason for hiding this comment

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

Thank you very much for tackling this and figuring out some way to get you there.

I am not that up to speed on how the completion parsing and generation works at the moment, so I can not weigh in too much on that. But I have some concerns about the implementation that would make the code harder to maintain in the future.

To convince us to take this change it would probably also be beneficial to have a full test of a completion with sudo prefixed that includes some potential edge cases to make sure we don't break unexpectedly.

@alesito85
Copy link
Contributor Author

Thank you for taking your time. I've applied fixes according to your comments, they make sense to me and I see I'll have to check more of what Rust has already implemented to not do double work where applicable.

I still have to add the test, which I expect to do shortly.

@alesito85
Copy link
Contributor Author

@sholderbach I believe I've applied all the changes, including the test. Hopefully it captures what you meant.

@sholderbach
Copy link
Member

Thanks for updating the code and adding the additional independent tests!
I think this addresses what I asked for.

I would hold off on merging for a moment as @kubouch is currently fighting with the completion logic a bit to track down a nasty panic or logic bug (#7779 #8102). We certainly need to reign in a bunch of kludgy special cases that already exist and make everything a bit brittle. If we get this cleared it is probably easier to say how your fix fits in.
So we probably have to wait until after the imminent release.

This maybe also gives us some time to clarify how sudo should behave in regards to internal and external commands (referring to your #8099)

Thanks for being responsive and eager to help out with nushell!

@kubouch
Copy link
Contributor

kubouch commented Feb 19, 2023

I'm OK with it, I was tracking mostly bugs related to aliases, this seems unrelated.

I would move find_non_whitespace_index() and is_sudo() away from the EngineState because the first does not reference EngineState at all and I'm not sure we want to hard-code sudo detection into the EngineState.

Another change I would do is to rename is_sudo() to something more generic, like is_passthrough_command() because tools like bear or gdb would benefit from the same autocomplete support as sudo. We can keep "sudo" hardcoded there for now and consider adding support for more commands later. When adding support for more command, we don't want to hard-code every single command into Nushell, so one way to do it would be to have an environment variable with a list of commands (populated by sudo by default) that the completer would look up.

@alesito85
Copy link
Contributor Author

Excellent suggestions, thank you. I'll take care of them tomorrow I think.

}

pub fn get_file_contents(&self) -> Vec<(Vec<u8>, usize, usize)> {
self.file_contents.clone()
Copy link
Member

Choose a reason for hiding this comment

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

Can we get away here without cloning?

Copy link
Contributor

@kubouch kubouch Feb 21, 2023

Choose a reason for hiding this comment

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

Agreed, you'd copy the entire source ever read by Nushell just to find sudo, that seems like an overkill. Most likely you can return &[u8].

@kubouch
Copy link
Contributor

kubouch commented Feb 21, 2023

Instead of utils, I would put the two functions into the command_completer.rs because they're used only for the completions.

The logic for detecting the sudo is also a bit gnarly. The completer has access to the parsed result so there is no need to iterate bytes, you should be able to check for Expr::ExternalCall and see if it matches sudo and go from there. Or something like that. But I think as long as it works and does not break anything, it should be OK to merge as well.

@alesito85
Copy link
Contributor Author

This will benefit from some squashing before merging. Let me know if it's alright as it is.

@kubouch
Copy link
Contributor

kubouch commented Feb 21, 2023

We won't merge it at least for the next 1-2 days because we're doing the release, so if you want to keep hacking on it, go ahead. In general, looking up the bytes manually seems a bit unsound to me, I'd rather use the parsed output.

One more thing, though. You're using direct array access with [] which can panic. In Nushell, if something can panic, it will panic at some point. It would be better to use .get() or something similar that doesn't panic.

@alesito85
Copy link
Contributor Author

alesito85 commented Feb 21, 2023

No worries, I'm not in a rush for having this merged, just saying when you feel it's ready, I'll also also squash the changes.

I've replaced [..] with .get, thanks for pointing it out to me.

I think I'm only using bytes where I don't have the full string available so that can probably stay like that? I'm perfectly fine with adapting the code if you point it out where you think I should do it differently.

Copy link
Member

@sholderbach sholderbach left a comment

Choose a reason for hiding this comment

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

OK seems good now!

@rgwood rgwood merged commit 7c28575 into nushell:main Feb 24, 2023
@myrkvi
Copy link

myrkvi commented Sep 2, 2023

This doesn't seem to allow completions to complete local paths (i.e. programs that are not in PATH).

Example:

You have a script in a folder that requires root privileges to run. It is not in the PATH environment variable.
If you are currently in your home directory, and the script is in ~/scripts/do_admin_stuff.nu, you could invoke it as sudo scripts/do_admin_stuff.nu or sudo ./scripts/do_admin_stuff.nu. Trying to complete either of these paths results in the message "NO RECORDS FOUND". Trying to define an extern to override this functionality also does not work.

sholderbach added a commit that referenced this pull request Sep 28, 2023
# Description

Fixes #2047 but for the `doas` command the same way as in #8094

# User-Facing Changes
No breaking changes. If people not using `doas`, no difference at all.

# Tests
I have not added any tests since its using same logic as for "sudo". I
guess if something would go wrong in this part, sudo tests will cover
it?

# Additional context
As a nushell user I could not find a way to implement custom completion
for a "sudo like command". Since I can see `sudo` being hardcoded in
sources, this is what I propose.

~~Also I have almost zero knowledge of rust and this is definitely not
the clean way yet~~

---------

Co-authored-by: Stefan Holderbach <[email protected]>
hardfau1t pushed a commit to hardfau1t/nushell that referenced this pull request Dec 14, 2023
# Description

Fixes nushell#2047 but for the `doas` command the same way as in nushell#8094

# User-Facing Changes
No breaking changes. If people not using `doas`, no difference at all.

# Tests
I have not added any tests since its using same logic as for "sudo". I
guess if something would go wrong in this part, sudo tests will cover
it?

# Additional context
As a nushell user I could not find a way to implement custom completion
for a "sudo like command". Since I can see `sudo` being hardcoded in
sources, this is what I propose.

~~Also I have almost zero knowledge of rust and this is definitely not
the clean way yet~~

---------

Co-authored-by: Stefan Holderbach <[email protected]>
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.

5 participants