diff --git a/crates/clash-verge-runtime-bundle/src/lib.rs b/crates/clash-verge-runtime-bundle/src/lib.rs index 6813da12b..728de705f 100644 --- a/crates/clash-verge-runtime-bundle/src/lib.rs +++ b/crates/clash-verge-runtime-bundle/src/lib.rs @@ -299,13 +299,15 @@ fn plan_provider( }; let destination = destination_below_root(config_root, &source)?; let reserved = TriStr::from(destination.as_str()); - if destinations.insert(reserved.clone()) - && let Some(assets) = assets - { - assets.push(RuntimeAsset { - source: source.to_string_lossy().into_owned(), - destination: reserved, - }); + if destinations.insert(reserved.clone()) { + if let Some(assets) = assets { + assets.push(RuntimeAsset { + source: source.to_string_lossy().into_owned(), + destination: reserved, + }); + } + } else if remote_providers.iter().any(|provider| provider.destination == reserved) { + return Err(RuntimeBundleError::DuplicateDestination(destination)); } destination } @@ -461,6 +463,26 @@ fn normalized_destination(relative: &Path) -> Result { mod tests { use super::{collect_runtime_bundle, remote_providers_of, resolve_provider_path_conflicts}; + #[test] + fn local_and_remote_providers_cannot_claim_the_same_file() -> Result<(), Box> { + let root = std::env::temp_dir().join(format!("clash-verge-mixed-providers-{}", std::process::id())); + std::fs::create_dir_all(&root)?; + std::fs::write(root.join("shared.yaml"), "payload: []")?; + let local = "type: file, path: ./shared.yaml"; + let remote = "type: http, url: 'https://example.com/provider', path: shared.yaml"; + for (proxy, rule) in [(local, remote), (remote, local)] { + let config = serde_yaml_ng::from_str(&format!( + "proxy-providers: {{a: {{{proxy}}}}}\nrule-providers: {{b: {{{rule}}}}}" + ))?; + assert!(matches!( + remote_providers_of(&config, &root), + Err(super::RuntimeBundleError::DuplicateDestination(path)) if path == "shared.yaml" + )); + } + std::fs::remove_dir_all(root)?; + Ok(()) + } + #[tokio::test] async fn conflicting_sources_get_separate_paths_in_config_bundle_and_readback() -> std::result::Result<(), Box> {