ast-grep/ast-grep · warning

Unexpected choice

Error message

Unexpected choice

What it means

print_diff_and_prompt_action maps the keystroke typed at the diff prompt to an InteractionChoice; any character outside the recognized set (y/e/q/n branches) hits the default arm and returns this bare "Unexpected choice" error.

Source

Thrown at crates/cli/src/print/interactive_print.rs:366

      let display = display[index].clone();
      interactive.inner.process(display)?;
      interactive.inner.print_diff_title(&titles, index)?;
      break match interactive.prompt_edit() {
        '\t' => {
          index = (index + 1) % len;
          clear()?;
          continue;
        }
        'y' => InteractionChoice::Yes(confirmed),
        'a' => InteractionChoice::All(confirmed),
        'e' => {
          let pos = confirmed.first_line;
          open_in_editor(path, pos)?;
          InteractionChoice::No
        }
        'q' => InteractionChoice::Quit,
        'n' => InteractionChoice::No,
        _ => return Err(anyhow::anyhow!("Unexpected choice")),
      };
    };
    Ok(ret)
  })
}

fn apply_rewrite(diffs: Diffs<()>) -> String {
  let mut new_content = String::new();
  let old_content = diffs.old_source;
  let mut start = 0;
  for mut diff_list in diffs.contents {
    let diff = diff_list.remove(0);
    let range = diff.range;
    new_content.push_str(&old_content[start..range.start]);
    new_content.push_str(&diff.replacement);
    start = range.end;
  }
  // add trailing statements

View on GitHub (pinned to fc2b1530db)

Solutions

  1. Press one of the documented keys shown in the prompt (y/e/q/n).
  2. If keys are misinterpreted (arrows etc.), use a standard terminal without key-translation quirks.
  3. For automation, supply exact single-character responses or use non-interactive mode.
Defensive patterns

Strategy: validation

Try / catch

// surface invalid keypresses before they reach the CLI
let resp = prompt_key();
if !matches!(resp, 'y' | 'e' | 'q' | 'n') {
    eprintln!("Unsupported key '{resp}'; use y/e/q/n.");
}

Prevention

When it happens

Trigger: The user (or an automated input source) presses a key at the diff prompt that is not one of the accepted choices — e.g. Enter, arrow keys rendering as escape sequences, or piped bytes.

Common situations: Piping scripted input into the interactive prompt; terminals sending multi-byte sequences for arrow keys; users pressing unsupported keys expecting other behavior.

Related errors


AI-assisted analysis of ast-grep/ast-grep@fc2b1530db (2026-09-05). Data as JSON: /api/errors/5b6bdb058e88f169. Report an issue: GitHub.