Skip to content

Commit b985768

Browse files
Add codex update command (#19933)
## Why Addresses #9274 Running `codex update` currently starts an interactive Codex session with `update` as the prompt. That is a rough edge for users who expect a direct self-update command after seeing the existing update notice, and it forces them to copy the suggested package-manager command manually. ## What changed - Added a top-level `codex update` subcommand. - Reused the existing install-channel detection and update command runner that the TUI already uses for update prompts. - Exposed the update-action lookup from `codex-tui` so the CLI can invoke the same behavior. - Added CLI coverage to ensure `codex update` is parsed as a subcommand instead of becoming an interactive prompt. ## Verification - `cargo test -p codex-cli` - `cargo test -p codex-tui update_action::tests`
1 parent 0a32c8b commit b985768

5 files changed

Lines changed: 64 additions & 2 deletions

File tree

‎codex-rs/cli/src/main.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ enum Subcommand {
134134
/// Generate shell completion scripts.
135135
Completion(CompletionCommand),
136136

137+
/// Update Codex to the latest version.
138+
Update,
139+
137140
/// Run commands within a Codex-provided sandbox.
138141
Sandbox(SandboxArgs),
139142

@@ -615,6 +618,25 @@ fn run_update_action(action: UpdateAction) -> anyhow::Result<()> {
615618
Ok(())
616619
}
617620

621+
fn run_update_command() -> anyhow::Result<()> {
622+
#[cfg(debug_assertions)]
623+
{
624+
anyhow::bail!(
625+
"`codex update` is not available in debug builds. Install a release build of Codex to use this command."
626+
);
627+
}
628+
629+
#[cfg(not(debug_assertions))]
630+
{
631+
let Some(action) = codex_tui::get_update_action() else {
632+
anyhow::bail!(
633+
"Could not detect the Codex installation method. Please update manually: https://developers.openai.com/codex/cli/"
634+
);
635+
};
636+
run_update_action(action)
637+
}
638+
}
639+
618640
fn run_execpolicycheck(cmd: ExecPolicyCheckCommand) -> anyhow::Result<()> {
619641
cmd.run()
620642
}
@@ -998,6 +1020,14 @@ async fn cli_main(arg0_paths: Arg0DispatchPaths) -> anyhow::Result<()> {
9981020
)?;
9991021
print_completion(completion_cli);
10001022
}
1023+
Some(Subcommand::Update) => {
1024+
reject_remote_mode_for_subcommand(
1025+
root_remote.as_deref(),
1026+
root_remote_auth_token_env.as_deref(),
1027+
"update",
1028+
)?;
1029+
run_update_command()?;
1030+
}
10011031
Some(Subcommand::Cloud(mut cloud_cli)) => {
10021032
reject_remote_mode_for_subcommand(
10031033
root_remote.as_deref(),
@@ -1890,6 +1920,12 @@ mod tests {
18901920
assert!(matches!(cli.subcommand, Some(Subcommand::Plugin(_))));
18911921
}
18921922

1923+
#[test]
1924+
fn update_parses_as_update_subcommand() {
1925+
let cli = MultitoolCli::try_parse_from(["codex", "update"]).expect("parse");
1926+
assert!(matches!(cli.subcommand, Some(Subcommand::Update)));
1927+
}
1928+
18931929
#[test]
18941930
fn plugin_marketplace_remove_parses_under_plugin() {
18951931
let cli =

‎codex-rs/cli/tests/marketplace_upgrade.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async fn marketplace_upgrade_no_longer_runs_at_top_level() -> Result<()> {
3030
.args(["marketplace", "upgrade"])
3131
.assert()
3232
.failure()
33-
.stderr(contains("unexpected argument 'upgrade' found"));
33+
.stderr(contains("unrecognized subcommand 'upgrade'"));
3434

3535
Ok(())
3636
}

‎codex-rs/cli/tests/update.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use anyhow::Result;
2+
use predicates::str::contains;
3+
use std::path::Path;
4+
use tempfile::TempDir;
5+
6+
fn codex_command(codex_home: &Path) -> Result<assert_cmd::Command> {
7+
let mut cmd = assert_cmd::Command::new(codex_utils_cargo_bin::cargo_bin("codex")?);
8+
cmd.env("CODEX_HOME", codex_home);
9+
Ok(cmd)
10+
}
11+
12+
#[cfg(debug_assertions)]
13+
#[tokio::test]
14+
async fn update_does_not_start_interactive_prompt() -> Result<()> {
15+
let codex_home = TempDir::new()?;
16+
17+
codex_command(codex_home.path())?
18+
.arg("update")
19+
.assert()
20+
.failure()
21+
.stderr(contains("`codex update` is not available in debug builds"));
22+
23+
Ok(())
24+
}

‎codex-rs/tui/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ mod tui;
171171
mod ui_consts;
172172
pub(crate) mod update_action;
173173
pub use update_action::UpdateAction;
174+
#[cfg(not(debug_assertions))]
175+
pub use update_action::get_update_action;
174176
mod update_prompt;
175177
#[cfg(any(not(debug_assertions), test))]
176178
mod update_versions;

‎codex-rs/tui/src/update_action.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl UpdateAction {
5959
}
6060

6161
#[cfg(not(debug_assertions))]
62-
pub(crate) fn get_update_action() -> Option<UpdateAction> {
62+
pub fn get_update_action() -> Option<UpdateAction> {
6363
UpdateAction::from_install_context(InstallContext::current())
6464
}
6565

0 commit comments

Comments
 (0)