Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .github/ISSUE_TEMPLATE/3-cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ body:

If you need help or support using Code, and are not reporting a bug, please post on [code/discussions](https://github.com/just-every/code/discussions), where you can ask questions or engage with others on ideas for how to improve Code.

Run `code update-check` to make sure you are using the latest GitHub Release build. The bug you are experiencing may already have been fixed.
Run `code doctor` (or `<your-command> doctor` if your binary is
renamed) and include the output below. It shows the version, Every Code
Lab build tag, repository, executable path, and update source.

- type: input
id: version
attributes:
label: What version of Code is running?
description: Copy the output of `code --version` (or `coder --version`)
label: What version and build of Code is running?
description: >-
Copy the version/build lines from `code doctor` (or `<your-command>
doctor`)
- type: input
id: model
attributes:
Expand Down
4 changes: 3 additions & 1 deletion code-rs/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,8 @@ async fn doctor_main() -> anyhow::Result<()> {
.map(|p| p.display().to_string())
.unwrap_or_else(|_| "<unknown>".to_string());
println!("code version: {}", code_version::version());
println!("product: {}", code_version::LAB_BUILD_NAME);
println!("repository: {}", code_version::LAB_REPOSITORY);
println!("current_exe: {}", exe);

// PATH
Expand Down Expand Up @@ -1523,7 +1525,7 @@ async fn doctor_main() -> anyhow::Result<()> {
show_versions("coder --version by path", &coder_paths).await;

println!("\nIf versions differ, remove older PATH entries or reorder PATH so the intended Code binary appears first.");
println!("Run `code update-check` from the intended binary to inspect the current GitHub Release update source.");
println!("Run `code update-check` or `<your-command> update-check` from the intended binary to inspect the current GitHub Release update source.");

Ok(())
}
Expand Down
108 changes: 104 additions & 4 deletions code-rs/cli/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use sha2::Sha256;

const DEFAULT_REPOSITORY: &str = "cbusillo/code";
const DEFAULT_CHANNEL: &str = "stable";
const COMMAND_NAME_ENV: &str = "CODE_COMMAND_NAME";

#[derive(Debug, Parser)]
pub struct UpdateCheckCommand {
Expand Down Expand Up @@ -68,22 +69,26 @@ enum VersionOrdering {

pub async fn run_update_check(args: UpdateCheckCommand) -> anyhow::Result<()> {
let report = fetch_update_report(args.repo.as_deref(), args.tag.as_deref()).await?;
print_update_report(&report);
let identity = RuntimeIdentity::detect(None);
print_update_report(&report, &identity);
Ok(())
}

pub async fn run_update(args: UpdateCommand) -> anyhow::Result<()> {
let report = fetch_update_report(args.repo.as_deref(), args.tag.as_deref()).await?;
print_update_report(&report);
let exe = env::current_exe().context("failed to resolve current executable")?;
let identity = RuntimeIdentity::detect(Some(&exe));
print_update_report(&report, &identity);

if report.ordering != VersionOrdering::Newer {
println!("No update needed.");
return Ok(());
}

let exe = env::current_exe().context("failed to resolve current executable")?;
let install_target = resolve_install_target(&exe);
let install = detect_install_source_for_path(&install_target);
println!("command: {}", identity.command_name);
println!("install target: {}", install_target.display());
println!("install source: {}", install.description());
if !install.can_self_update() {
bail!(
Expand Down Expand Up @@ -197,7 +202,10 @@ async fn latest_release_tag(repo: &str) -> anyhow::Result<String> {
Ok(release.tag_name)
}

fn print_update_report(report: &UpdateReport) {
fn print_update_report(report: &UpdateReport, identity: &RuntimeIdentity) {
println!("product: {}", code_version::LAB_BUILD_NAME);
println!("repository: {}", code_version::LAB_REPOSITORY);
println!("command: {}", identity.command_name);
println!("current version: {}", report.current_version);
println!("latest version: {}", report.manifest.version);
println!("channel: {}", report.manifest.channel);
Expand All @@ -217,6 +225,38 @@ fn print_update_report(report: &UpdateReport) {
}
}

struct RuntimeIdentity {
command_name: String,
}

impl RuntimeIdentity {
fn detect(exe: Option<&Path>) -> Self {
let command_name = env::var(COMMAND_NAME_ENV)
.ok()
.and_then(|name| valid_command_name(&name))
.or_else(|| exe.and_then(command_name_from_path))
.or_else(|| env::args_os().next().and_then(|arg| command_name_from_path(Path::new(&arg))))
.unwrap_or_else(|| "code".to_string());

Self { command_name }
}
}

fn command_name_from_path(path: &Path) -> Option<String> {
path.file_name()
.and_then(|name| name.to_str())
.and_then(valid_command_name)
}

fn valid_command_name(name: &str) -> Option<String> {
let trimmed = name.trim();
if trimmed.is_empty() || trimmed.contains(std::path::MAIN_SEPARATOR) {
None
} else {
Some(trimmed.to_string())
}
}

fn http_client(user_agent: &str) -> anyhow::Result<reqwest::Client> {
Ok(reqwest::Client::builder()
.user_agent(user_agent)
Expand Down Expand Up @@ -368,6 +408,7 @@ fn detect_install_source_for_path(exe: &Path) -> InstallSource {
}
if path.contains("/.code/bin/")
|| path.contains("/.local/bin/")
|| path.contains("/usr/local/bin/")
|| path.contains("/code-rs/target/release/")
{
return InstallSource::Direct;
Expand Down Expand Up @@ -408,6 +449,9 @@ fn parse_version_triplet(version: &str) -> Option<(u64, u64, u64)> {
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Mutex;

static ENV_TEST_LOCK: Mutex<()> = Mutex::new(());

#[test]
fn normalize_tag_adds_v_prefix_when_missing() {
Expand Down Expand Up @@ -523,6 +567,62 @@ mod tests {
detect_install_source_for_path(Path::new("/Users/me/.local/bin/code")),
InstallSource::Direct
));
assert!(matches!(
detect_install_source_for_path(Path::new("/usr/local/bin/chris-code")),
InstallSource::Direct
));
}

#[test]
fn runtime_identity_prefers_command_name_env() {
let _lock = ENV_TEST_LOCK.lock().unwrap();
let _reset = EnvReset::capture(COMMAND_NAME_ENV);
unsafe {
env::set_var(COMMAND_NAME_ENV, "chris-code");
}

let identity = RuntimeIdentity::detect(Some(Path::new("/usr/local/bin/code")));

assert_eq!(identity.command_name, "chris-code");
}

#[test]
fn runtime_identity_falls_back_to_exe_name() {
let _lock = ENV_TEST_LOCK.lock().unwrap();
let _reset = EnvReset::capture(COMMAND_NAME_ENV);
unsafe {
env::remove_var(COMMAND_NAME_ENV);
}

let identity = RuntimeIdentity::detect(Some(Path::new("/usr/local/bin/chris-code")));

assert_eq!(identity.command_name, "chris-code");
}

struct EnvReset {
key: &'static str,
value: Option<String>,
}

impl EnvReset {
fn capture(key: &'static str) -> Self {
Self {
key,
value: env::var(key).ok(),
}
}
}

impl Drop for EnvReset {
fn drop(&mut self) {
unsafe {
if let Some(value) = &self.value {
env::set_var(self.key, value);
} else {
env::remove_var(self.key);
}
}
}
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions code-rs/code-version/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub const CODE_VERSION: &str = {
}
};

pub const PRODUCT_NAME: &str = "Every Code";
pub const LAB_BUILD_NAME: &str = "Every Code Lab";
pub const LAB_REPOSITORY: &str = "cbusillo/code";

const ANNOUNCEMENT_TIP: &str = include_str!("../../../announcement_tip.toml");
const MODELS_MANIFEST: &str = include_str!("../../../codex-rs/models-manager/models.json");
pub const MIN_WIRE_COMPAT_VERSION_FALLBACK: &str = "0.101.0";
Expand Down
2 changes: 1 addition & 1 deletion code-rs/tui/src/chatwidget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31095,7 +31095,7 @@ Have we met every part of this goal and is there no further work to do?"#

// Title follows theme text color
spans.push(Span::styled(
"Every Code",
code_version::LAB_BUILD_NAME,
Style::default()
.fg(crate::colors::text())
.add_modifier(Modifier::BOLD),
Expand Down
Loading