Respect config layers when removing plugin marketplaces (#40683)

## Why

Removing a base-user marketplace must not delete a snapshot that is still
referenced by another enabled configuration layer.

## What changed

- Load the current configuration stack for marketplace commands in the CLI and
  app server.
- Reject removal when a matching marketplace is defined by another enabled
  layer, and identify the configuration source that must be updated instead.
- Allow cleanup of an installed marketplace snapshot even when no base-user
  entry exists.

## Testing

Cover project, enterprise-managed, system, and session-flag configurations, as
well as snapshot-only removal through the CLI and app server.

GitOrigin-RevId: 4f0853a40c2b5fae0410696b79eee4284239c7a9
This commit is contained in:
willwang-openai
2026-08-25 19:38:39 +00:00
committed by copyberry
parent f1a6f7c9b3
commit a6e63f9f32
7 changed files with 253 additions and 47 deletions
+101 -5
View File
@@ -1,5 +1,9 @@
use crate::installed_marketplaces::marketplace_install_root;
use codex_config::CONFIG_TOML_FILE;
use codex_config::ConfigLayerSource;
use codex_config::ConfigLayerStack;
use codex_config::RemoveMarketplaceConfigOutcome;
use codex_config::format_config_layer_source;
use codex_config::remove_user_marketplace_config;
use codex_plugin::validate_plugin_segment;
use codex_utils_absolute_path::AbsolutePathBuf;
@@ -26,25 +30,54 @@ pub enum MarketplaceRemoveError {
Internal(String),
}
/// Removes a marketplace's snapshot and any base-user entry only when no other
/// enabled layer in the current operation's config stack references its name.
pub async fn remove_marketplace(
codex_home: PathBuf,
config_layer_stack: ConfigLayerStack,
request: MarketplaceRemoveRequest,
) -> Result<MarketplaceRemoveOutcome, MarketplaceRemoveError> {
tokio::task::spawn_blocking(move || remove_marketplace_sync(codex_home.as_path(), request))
.await
.map_err(|err| {
MarketplaceRemoveError::Internal(format!("failed to remove marketplace: {err}"))
})?
tokio::task::spawn_blocking(move || {
remove_marketplace_sync(codex_home.as_path(), &config_layer_stack, request)
})
.await
.map_err(|err| {
MarketplaceRemoveError::Internal(format!("failed to remove marketplace: {err}"))
})?
}
fn remove_marketplace_sync(
codex_home: &Path,
config_layer_stack: &ConfigLayerStack,
request: MarketplaceRemoveRequest,
) -> Result<MarketplaceRemoveOutcome, MarketplaceRemoveError> {
let marketplace_name = request.marketplace_name;
validate_plugin_segment(&marketplace_name, "marketplace name")
.map_err(MarketplaceRemoveError::InvalidRequest)?;
// Removing a user override must not delete a snapshot still referenced by
// another enabled layer, including a lower-precedence layer it shadows.
let user_config_file = AbsolutePathBuf::resolve_path_against_base(CONFIG_TOML_FILE, codex_home);
if let Some(layer) = config_layer_stack.layers_high_to_low().find(|layer| {
!matches!(
&layer.name,
ConfigLayerSource::User { file, profile: None } if file == &user_config_file
) && layer
.config
.get("marketplaces")
.and_then(toml::Value::as_table)
.is_some_and(|marketplaces| {
marketplaces
.keys()
.any(|name| name.eq_ignore_ascii_case(&marketplace_name))
})
}) {
let source = format_config_layer_source(&layer.name, CONFIG_TOML_FILE);
return Err(MarketplaceRemoveError::InvalidRequest(format!(
"marketplace `{marketplace_name}` is configured in {source}; remove it from that configuration source instead"
)));
}
let destination = marketplace_install_root(codex_home).join(&marketplace_name);
let config_outcome =
remove_user_marketplace_config(codex_home, &marketplace_name).map_err(|err| {
@@ -107,6 +140,7 @@ fn remove_marketplace_root(root: &Path) -> Result<Option<AbsolutePathBuf>, Marke
#[cfg(test)]
mod tests {
use super::*;
use codex_config::ConfigLayerEntry;
use codex_config::MarketplaceConfigUpdate;
use codex_config::record_user_marketplace;
use pretty_assertions::assert_eq;
@@ -136,6 +170,7 @@ mod tests {
let outcome = remove_marketplace_sync(
codex_home.path(),
&ConfigLayerStack::default(),
MarketplaceRemoveRequest {
marketplace_name: "debug".to_string(),
},
@@ -159,6 +194,7 @@ mod tests {
let err = remove_marketplace_sync(
codex_home.path(),
&ConfigLayerStack::default(),
MarketplaceRemoveRequest {
marketplace_name: "debug".to_string(),
},
@@ -171,6 +207,62 @@ mod tests {
);
}
#[test]
fn remove_marketplace_sync_preserves_shadowed_system_marketplace() {
let codex_home = TempDir::new().unwrap();
let config = "[marketplaces.debug]\nsource_type = \"git\"\nsource = \"https://github.com/owner/repo.git\"\n";
let user_file =
AbsolutePathBuf::resolve_path_against_base(CONFIG_TOML_FILE, codex_home.path());
let system_file =
AbsolutePathBuf::resolve_path_against_base("system.toml", codex_home.path());
fs::write(&user_file, config).unwrap();
fs::write(&system_file, config).unwrap();
let installed_root = marketplace_install_root(codex_home.path()).join("debug");
fs::create_dir_all(&installed_root).unwrap();
let marker = installed_root.join("marker.txt");
fs::write(&marker, "installed").unwrap();
let stack = ConfigLayerStack::new(
vec![
ConfigLayerEntry::new(
ConfigLayerSource::System {
file: system_file.clone(),
},
toml::from_str(config).unwrap(),
),
ConfigLayerEntry::new(
ConfigLayerSource::User {
file: user_file.clone(),
profile: None,
},
toml::from_str(config).unwrap(),
),
],
Default::default(),
Default::default(),
)
.unwrap();
let err = remove_marketplace_sync(
codex_home.path(),
&stack,
MarketplaceRemoveRequest {
marketplace_name: "debug".to_string(),
},
)
.unwrap_err();
assert_eq!(
err.to_string(),
format!(
"marketplace `debug` is configured in system ({}); remove it from that configuration source instead",
system_file.display()
),
);
assert_eq!(fs::read_to_string(user_file).unwrap(), config);
assert_eq!(fs::read_to_string(system_file).unwrap(), config);
assert_eq!(fs::read_to_string(marker).unwrap(), "installed");
}
#[test]
fn remove_marketplace_sync_rejects_case_mismatched_configured_name() {
let codex_home = TempDir::new().unwrap();
@@ -190,6 +282,7 @@ mod tests {
let err = remove_marketplace_sync(
codex_home.path(),
&ConfigLayerStack::default(),
MarketplaceRemoveRequest {
marketplace_name: "Debug".to_string(),
},
@@ -219,6 +312,7 @@ mod tests {
let err = remove_marketplace_sync(
codex_home.path(),
&ConfigLayerStack::default(),
MarketplaceRemoveRequest {
marketplace_name: "debug".to_string(),
},
@@ -252,6 +346,7 @@ mod tests {
let outcome = remove_marketplace_sync(
codex_home.path(),
&ConfigLayerStack::default(),
MarketplaceRemoveRequest {
marketplace_name: "debug".to_string(),
},
@@ -288,6 +383,7 @@ marketplaces = { debug = { source_type = "git", source = "https://github.com/own
let outcome = remove_marketplace_sync(
codex_home.path(),
&ConfigLayerStack::default(),
MarketplaceRemoveRequest {
marketplace_name: "debug".to_string(),
},