use super::layers;
use crate::context::{Context, Environment};
use crate::host::Os;
use std::path::{Path, PathBuf};
pub(crate) const AUTH_FILE: &str = "auth.json";
pub(crate) fn home(ctx: &Context) -> PathBuf {
match ctx.codex_home() {
Some(dir) if !dir.is_empty() => match ctx.os {
Os::MacOs | Os::Linux => PathBuf::from(dir),
Os::Windows => std::fs::canonicalize(dir)
.map_or_else(|_| PathBuf::from(dir), crate::host::without_verbatim_prefix),
},
_ => ctx.profile.as_deref().unwrap_or(ctx.home()).join(".codex"),
}
}
pub(crate) fn missing_home(ctx: &Context) -> Option<PathBuf> {
let dir = ctx.codex_home().filter(|dir| !dir.is_empty())?;
(ctx.os == Os::Windows && !Path::new(dir).is_dir()).then(|| PathBuf::from(dir))
}
pub(crate) fn auth_file(ctx: &Context) -> PathBuf {
home(ctx).join(AUTH_FILE)
}
pub(crate) fn install_places(env: &Environment) -> Vec<PathBuf> {
let standalone = match env.os() {
Os::MacOs | Os::Linux => Some(env.home().join(".local").join("bin")),
Os::Windows => env
.named_folder("CODEX_INSTALL_DIR")
.filter(|dir| !dir.to_string_lossy().trim().is_empty())
.or_else(|| {
let mut dir = env.named_folder("LOCALAPPDATA")?;
dir.extend(["Programs", "OpenAI", "Codex", "bin"]);
Some(dir)
}),
};
standalone
.into_iter()
.chain(env.os().package_bins(env))
.collect()
}
pub(crate) fn npm_native(script: &Path) -> Option<(PathBuf, crate::provider::Launched)> {
let named = |path: &Path, name: &str| {
path.file_name()
.is_some_and(|its| its.eq_ignore_ascii_case(name))
};
let bin = script.parent().filter(|bin| named(bin, "bin"))?;
let package = bin.parent().filter(|package| named(package, "codex"))?;
package.parent().filter(|scope| named(scope, "@openai"))?;
if !named(script, "codex.js") {
return None;
}
let program = NPM_TARGETS.iter().find_map(|(target, platform)| {
let vendor = bin
.ancestors()
.filter(|dir| !named(dir, "node_modules"))
.map(|dir| dir.join("node_modules").join("@openai").join(platform))
.find(|found| found.join("package.json").is_file())
.unwrap_or_else(|| package.to_path_buf())
.join("vendor");
let program = vendor.join(target).join("bin").join("codex.exe");
crate::host::proc::can_run(&program).then_some(program)
})?;
let root = std::fs::canonicalize(package).map_or_else(
|_| package.to_path_buf(),
crate::host::without_verbatim_prefix,
);
let mut set: crate::provider::Launched = vec![
("CODEX_MANAGED_PACKAGE_ROOT", Some(root.into_os_string())),
("CODEX_MANAGED_BY_NPM", Some("1".into())),
];
set.extend(
[
"CODEX_MANAGED_BY_BUN",
"CODEX_MANAGED_BY_PNPM",
"CODEX_MANAGED_BY_VITE_PLUS",
]
.map(|other| (other, None)),
);
Some((program, set))
}
const NPM_TARGETS: [(&str, &str); 2] = if cfg!(target_arch = "aarch64") {
[
("aarch64-pc-windows-msvc", "codex-win32-arm64"),
("x86_64-pc-windows-msvc", "codex-win32-x64"),
]
} else {
[
("x86_64-pc-windows-msvc", "codex-win32-x64"),
("aarch64-pc-windows-msvc", "codex-win32-arm64"),
]
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Backend {
File,
Keyring,
Either,
Ephemeral,
Secrets,
Unknown,
}
pub(crate) fn config_file(ctx: &Context) -> PathBuf {
home(ctx).join("config.toml")
}
pub(crate) fn store(ctx: &Context) -> layers::Store {
layers::of(ctx, &config_file(ctx))
}
pub(crate) fn restrictions(ctx: &Context) -> layers::Restrictions {
layers::restrictions_of(ctx, &config_file(ctx))
}
pub(crate) fn sign_in_store(ctx: &Context) -> layers::Store {
layers::of_a_sign_in(ctx, &config_file(ctx))
}
pub(crate) fn installed_version(program: &Path) -> Option<String> {
use crate::host::program::{looks_like_a_version, npm_shim_target, version_where};
let program = npm_shim_target(program).unwrap_or_else(|| program.to_path_buf());
let resolved = std::fs::canonicalize(program).ok()?;
let near: Vec<&Path> = resolved.ancestors().skip(1).take(2).collect();
near.iter()
.find_map(|dir| {
dir.file_name()
.and_then(|n| n.to_str())
.and_then(|name| name.split('-').next())
.filter(|leading| looks_like_a_version(leading))
.map(str::to_owned)
})
.or_else(|| {
near.iter().find_map(|dir| {
version_where(&dir.join("package.json"), "name", "@openai/codex")
.or_else(|| version_where(&dir.join("codex-package.json"), "variant", "codex"))
})
})
}
#[cfg(test)]
#[allow(
clippy::disallowed_methods,
reason = "a test sets up its scratch files as a person or a tool would"
)]
mod tests {
use super::*;
fn backend(ctx: &Context) -> Backend {
store(ctx).backend
}
fn keyring_or(alone: Backend) -> Backend {
match crate::host::OS {
Os::MacOs | Os::Linux => alone,
Os::Windows => Backend::Secrets,
}
}
fn at(dir: &Path, config: &str) -> Context {
std::fs::create_dir_all(dir).expect("a scratch codex home");
std::fs::write(dir.join("config.toml"), config).expect("a config");
Context::for_unit_test().with_codex_home(dir.to_string_lossy().into())
}
#[test]
fn codex_finds_its_home_on_windows_as_codex_does() {
use crate::context::Environment;
use crate::host::Os;
let scratch = tempfile::tempdir().expect("a scratch folder");
let profile = scratch.path().join("profile");
std::fs::create_dir_all(profile.join("codex")).expect("a scratch codex home");
std::fs::write(profile.join("file"), "").expect("a file");
let named = |path: PathBuf| path.to_string_lossy().into_owned();
let read = |os: Os, codex: Option<PathBuf>| {
let pairs = [
("USERPROFILE", named(profile.clone())),
("HOME", named(profile.clone())),
]
.into_iter()
.chain(codex.map(|dir| ("CODEX_HOME", named(dir))));
Context::read(&Environment::on(os, pairs), "cli")
};
let handed = Context::new(profile.clone());
assert_eq!(home(&handed), profile.join(".codex"), "a handed home's");
let unset = read(Os::Windows, None);
assert_eq!(
home(&unset),
profile.join(".codex"),
"no profile is looked up in a test"
);
let elsewhere = profile.join("Profile");
assert_eq!(
home(&unset.with_profile(elsewhere.clone())),
elsewhere.join(".codex"),
"not the home, where the profile is elsewhere"
);
let given = read(Os::Windows, Some(profile.join(".").join("codex")));
let canonical = std::fs::canonicalize(profile.join("codex")).expect("there");
assert_eq!(
home(&given),
crate::host::without_verbatim_prefix(canonical)
);
assert_eq!(missing_home(&given), None);
for nothing in [profile.join("never-made"), profile.join("file")] {
assert_eq!(
missing_home(&read(Os::Windows, Some(nothing.clone()))),
Some(nothing)
);
}
for os in [Os::MacOs, Os::Linux] {
let missing = profile.join("never-made");
let ctx = read(os, Some(missing.clone()));
assert_eq!((home(&ctx), missing_home(&ctx)), (missing, None), "{os:?}");
}
}
#[test]
fn no_setting_means_the_file() {
let scratch = tempfile::tempdir().expect("a scratch folder");
let dir = scratch.path().join("dir");
let ctx = at(&dir, "model = \"gpt-5\"\n");
assert_eq!(backend(&ctx), Backend::File);
assert_eq!(auth_file(&ctx), home(&ctx).join("auth.json"));
assert_eq!(
std::fs::canonicalize(home(&ctx)).ok(),
std::fs::canonicalize(&dir).ok()
);
}
#[test]
fn each_store_is_recognised() {
let scratch = tempfile::tempdir().expect("a scratch folder");
let dir = scratch.path().join("dir");
for (written, expected) in [
(
"cli_auth_credentials_store = \"keyring\"",
keyring_or(Backend::Keyring),
),
(
"cli_auth_credentials_store=\"auto\"",
keyring_or(Backend::Either),
),
(
" cli_auth_credentials_store = 'ephemeral'",
Backend::Ephemeral,
),
("cli_auth_credentials_store = \"file\"", Backend::File),
(
"cli_auth_credentials_store = \"nonsense\"",
Backend::Unknown,
),
("cli_auth_credentials_store = keyring", Backend::Unknown),
] {
let ctx = at(&dir, written);
assert_eq!(backend(&ctx), expected, "{written}");
}
}
#[test]
fn a_comment_after_the_value_is_not_the_value() {
let scratch = tempfile::tempdir().expect("a scratch folder");
let dir = scratch.path().join("dir");
let ctx = at(
&dir,
"cli_auth_credentials_store = \"keyring\" # on this mac\n",
);
assert_eq!(backend(&ctx), keyring_or(Backend::Keyring));
}
#[test]
fn only_the_top_level_key_is_the_setting() {
let scratch = tempfile::tempdir().expect("a scratch folder");
let dir = scratch.path().join("dir");
for (config, expected, why) in [
(
"[profiles.work]\ncli_auth_credentials_store = \"keyring\"\n",
Backend::File,
"a key inside another table is not the setting",
),
(
"cli_auth_credentials_store = \"keyring\"\n\
[features]\nsecret_auth_storage = true\n",
Backend::Secrets,
"an encrypted file whose key is in the keychain is a store of its own",
),
(
"[features]\nsecret_auth_storage = true\n",
Backend::File,
"the feature only changes a keyring store",
),
] {
assert_eq!(backend(&at(&dir, config)), expected, "{why}");
}
}
#[test]
fn the_system_layers_are_read_as_well_as_the_persons_own() {
let scratch = tempfile::tempdir().expect("a scratch folder");
let dir = scratch.path().join("dir");
let host = crate::host::memory::MemoryHost::new();
let ctx = at(&dir, "model = \"gpt-5\"\n").with_memory_stores(host.clone());
let system = |name| layers::system_file(&ctx, name);
host.administers(
system("config.toml"),
"cli_auth_credentials_store = \"keyring\"\n",
);
assert_eq!(backend(&ctx), keyring_or(Backend::Keyring));
std::fs::write(
dir.join("config.toml"),
"cli_auth_credentials_store = \"file\"\n",
)
.expect("a config");
assert_eq!(backend(&ctx), Backend::File);
host.administers(
system("requirements.toml"),
"cli_auth_credentials_store = \"keyring\"\n",
);
assert_eq!(backend(&ctx), keyring_or(Backend::Keyring));
assert_eq!(
sign_in_store(&ctx).backend,
keyring_or(Backend::Keyring),
"a requirement pins a sign-in's store too"
);
}
#[test]
fn a_version_is_read_out_of_each_way_codex_is_installed() {
use crate::host::fs::testing;
let scratch = tempfile::tempdir().expect("a scratch home");
let root = scratch.path().to_path_buf();
let place = |at: &str| {
let path = root.join(at);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(&path, "").unwrap();
path
};
let standalone =
place("codex/packages/standalone/releases/0.154.0-aarch64-apple-darwin/bin/codex");
std::fs::write(
standalone.with_file_name("package.json"),
r#"{"name": "@openai/codex", "version": "9.9.9"}"#,
)
.unwrap();
let link = root.join("bin/codex");
std::fs::create_dir_all(link.parent().unwrap()).unwrap();
testing::link(&standalone, &link).expect("a link");
assert_eq!(installed_version(&link).as_deref(), Some("0.154.0"));
let cask = place("Caskroom/codex/0.153.2/codex-aarch64-apple-darwin");
assert_eq!(installed_version(&cask).as_deref(), Some("0.153.2"));
let npm = place("lib/node_modules/@openai/codex/bin/codex.js");
std::fs::write(
root.join("lib/node_modules/@openai/codex/package.json"),
r#"{"name": "@openai/codex", "version": "0.150.1"}"#,
)
.unwrap();
assert_eq!(installed_version(&npm).as_deref(), Some("0.150.1"));
let shim = place("volta/bin/volta-shim");
std::fs::write(
root.join("package.json"),
r#"{"name": "@openai/codex", "version": "1.2.3"}"#,
)
.unwrap();
assert_eq!(installed_version(&shim), None);
}
#[test]
fn a_version_is_read_where_codex_is_installed_on_windows() {
let scratch = tempfile::tempdir().expect("a scratch home");
let root = scratch.path().to_path_buf();
let place = |at: &str| {
let path = root.join(at);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(&path, "").unwrap();
path
};
let layout = |version: &str, variant: &str| {
format!(
r#"{{"layoutVersion": 1, "version": "{version}", "target": "x86_64-pc-windows-msvc", "variant": "{variant}", "entrypoint": "bin/codex.exe"}}"#
)
};
let vendored = place("codex-win32-x64/vendor/x86_64-pc-windows-msvc/bin/codex.exe");
let package = root.join("codex-win32-x64/vendor/x86_64-pc-windows-msvc/codex-package.json");
std::fs::write(&package, layout("0.162.1", "codex")).unwrap();
assert_eq!(installed_version(&vendored).as_deref(), Some("0.162.1"));
std::fs::write(&package, layout("0.162.1", "something-else")).unwrap();
assert_eq!(
installed_version(&vendored),
None,
"only Codex's own package"
);
place("npm/node_modules/@openai/codex/bin/codex.js");
std::fs::write(
root.join("npm/node_modules/@openai/codex/package.json"),
r#"{"name": "@openai/codex", "version": "0.161.0"}"#,
)
.unwrap();
let shim = root.join("npm/codex.cmd");
std::fs::write(
&shim,
"@ECHO off\r\nIF EXIST \"%dp0%\\node.exe\" (\r\n SET \"_prog=%dp0%\\node.exe\"\r\n)\r\n\
\"%_prog%\" \"%dp0%\\node_modules\\@openai\\codex\\bin\\codex.js\" %*\r\n",
)
.unwrap();
assert_eq!(installed_version(&shim).as_deref(), Some("0.161.0"));
}
}