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

generated zsh completion fails when source <()'d #3508

Closed
2 tasks done
drahnr opened this issue Feb 25, 2022 · 2 comments · Fixed by #4734
Closed
2 tasks done

generated zsh completion fails when source <()'d #3508

drahnr opened this issue Feb 25, 2022 · 2 comments · Fixed by #4734
Labels
A-completion Area: completion generator C-bug Category: Updating dependencies E-easy Call for participation: Experience needed to fix: Easy / not much

Comments

@drahnr
Copy link

drahnr commented Feb 25, 2022

Please complete the following tasks

Rust Version

rustc 1.58.1 (db9d1b20b 2022-01-20)

Clap Version

3.1.2

Minimal reproducible code

use clap::{AppSettings, Arg, Command, PossibleValue, Subcommand, ValueHint};
use clap_complete::{generate, Generator, Shell};
use std::{io, collections::{HashSet, HashMap}};

fn build_cli() -> Command<'static> {
    Command::new(env!("CARGO_PKG_NAME"))
        .subcommand_required(true)
        .subcommand(
            Command::new("foo")
            .arg(
                Arg::new("foo")
                    .long("foo")
                    .help("foos for real")
                    .possible_value(PossibleValue::new("7"))
                    .possible_value(PossibleValue::new("seven"))
            ),
        )
        .subcommand(
            Command::new("comp")
            .arg(
                Arg::new("shell")
                    .long("shell")
                    .help("shell to generate for")
                    .possible_values(Shell::possible_values()),
            ),
        )
}

fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
    generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}

fn main() {
    let app = build_cli();
    let matches = app.get_matches();

    match matches.subcommand() {
        Some(("comp", args)) => {
            if let Ok(shell) = args.value_of_t::<Shell>("shell") {
                let mut cmd = build_cli();
                eprintln!("Generating completion file for {}...", shell);
                print_completions(dbg!(shell), &mut cmd);
                return;
            }
            eprintln!("nope");
        }
        Some(("foo", _args)) => {
            println!("foo");
            return;
        }
        _ => {}
    }
    build_cli().print_long_help().unwrap();
}

Steps to reproduce the bug with the above code

source <(cargo run -- comp --shell zsh)

Actual Behaviour

errors out and the completion does not work with the binary afterwards

error:

# source <(cargo run -- comp --shell zsh)
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/clap-completion-zoink comp --shell zsh`
Generating completion file for zsh...
[src/main.rs:42] shell = Zsh

_arguments:comparguments:325: can only be called from completion function

Expected Behaviour

should source the completion and from thereon complete the execution

Additional Context

An almost minimal, reproducible example can be checked out at https://github.com/drahnr/clap-completion-zoink

Encountered in the wild on https://github.com/soywod/himalaya and reported as volta-cli/volta#496

Debug Output

No response

@drahnr drahnr added the C-bug Category: Updating dependencies label Feb 25, 2022
@epage epage added A-completion Area: completion generator E-easy Call for participation: Experience needed to fix: Easy / not much labels Feb 25, 2022
@jjmark15
Copy link

did you find a way to avoid the issue?

@drahnr
Copy link
Author

drahnr commented Apr 24, 2022

The generated file is meant to be written to a file, assuming the above project is foo, _foo would be the correct file name which has to reside in $fpath.

There also seems to be an artifact when writing to a file via foo comp --shell zsh > _foo:

<snip>
_cargo-spellcheck "$@"

which doesn't seem to be an ordinary linebreak when openeing an editor 🤷
grafik


Quick fix:

Delete the comment #compinit foo marker at the top and the last line _foo and it then should load fine, unless you hit #3022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completion Area: completion generator C-bug Category: Updating dependencies E-easy Call for participation: Experience needed to fix: Easy / not much
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants