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 bash completion script not work in POSIX mode #5011

Open
2 tasks done
hehaoqian opened this issue Jul 15, 2023 · 1 comment
Open
2 tasks done

Generated bash completion script not work in POSIX mode #5011

hehaoqian opened this issue Jul 15, 2023 · 1 comment
Labels
A-completion Area: completion generator C-bug Category: Updating dependencies E-medium Call for participation: Experience needed to fix: Medium / intermediate

Comments

@hehaoqian
Copy link

Please complete the following tasks

Rust Version

1.70.0

Clap Version

4.3.11

Minimal reproducible code

// build.rs

use clap::{CommandFactory, Parser};
use clap_complete::{generate_to, Shell};

#[derive(Parser)]
pub struct Args {
    pub input_file: String,
}

fn main() {
    let mut cmd = Args::command();
    let out_dir = std::env::var("OUT_DIR").unwrap();
    generate_to(Shell::Bash, &mut cmd, "hello-world", out_dir).unwrap();
}

Steps to reproduce the bug with the above code

  1. cd into output directory
  2. run the generated script in bash posix mode
bash --posix -c "source ./hello-world.bash"

Actual Behaviour

The shell errors with the following message, with non-zero return code

./hello-world.bash: line 36: `_hello-world': not a valid identifier

Expected Behaviour

The script should work in bash posix mode.

I want to emphasize that this issue report,
does NOT request to make the script work in non-bash, POSIX only "sh" shell,
such as /bin/sh in ubuntu.
Only requesting to make the script work in POSIX mode of /bin/bash

Bash doc https://www.gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode.html says

Function names must be valid shell names.
That is, they may not contain characters other than letters, digits, and underscores, and may not start with a digit.
Declaring a function with an invalid name causes a fatal syntax error in non-interactive shells.

In my example, the function name of generated script is _hello-world, which is invalid in POSIX mode

This should be simple to fix, if dash character is properly escaped

The simplest way would be replacing - by _,
but it would cause conflict if generating completion script for both hello-world and hello_world
Maybe a more complex escape is needed for robustness.
Also, additional escape needed to satisfy they may not contain characters other than letters, digits, and underscores, and may not start with a digit requirement

Additional Context

There are several ways to enter bash in posix mode:

  1. If bash is compiled with --enable-strict-posix-default, then posix mode is the default
  2. Environment variable POSIXLY_CORRECT is set when bash is invoked
  3. Shell variable POSIXLY_CORRECT is set in bash script
  4. bash is invoked with "bash --posix"
  5. bash is invoked with "sh", such as invoking /bin/sh in CentOS7, which symlinks /bin/sh to /bin/bash

Debug Output

No response

@hehaoqian hehaoqian added the C-bug Category: Updating dependencies label Jul 15, 2023
@epage
Copy link
Member

epage commented Jul 18, 2023

My first question is how much do we care about posix mode but then I saw

bash is invoked with "sh", such as invoking /bin/sh in CentOS7, which symlinks /bin/sh to /bin/bash

I'm fine with someone going in and fixing this with a reasonable level of care taken to avoid name conflicts.

@epage epage added E-medium Call for participation: Experience needed to fix: Medium / intermediate A-completion Area: completion generator labels Jul 18, 2023
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-medium Call for participation: Experience needed to fix: Medium / intermediate
Projects
None yet
Development

No branches or pull requests

2 participants