From cc471029d2bc7bd6051ac1f93e5eba77bdfc398e Mon Sep 17 00:00:00 2001 From: Gabriel Gondim <52431033+GGVidal@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:28:31 -0300 Subject: [PATCH] fix(xml): map Doctrine ORM XSD through a LemMinX catalog (#3881) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Doctrine ORM XML mappings that follow Doctrine's docs and point xsi:schemaLocation at https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd failed validation (TargetNamespace.1, cvc-elt.1.a): the hosted XSD declares the schema URL as its targetNamespace after an http→https rewrite. The upstream schema (MIT) is bundled and an OASIS catalog, written to a private per-process directory, is prepended to LemMinX's xml.catalogs so the namespace and both system IDs resolve to it. Closes #3843 Co-authored-by: GGVidal <52431033+GGVidal@users.noreply.github.com> --- crates/ide-lsp/schemas/xml/NOTICE | 5 + .../ide-lsp/schemas/xml/doctrine-mapping.xsd | 643 ++++++++++++++++++ crates/ide-lsp/src/lib.rs | 4 + crates/ide-lsp/src/xml_catalog.rs | 149 ++++ 4 files changed, 801 insertions(+) create mode 100644 crates/ide-lsp/schemas/xml/NOTICE create mode 100644 crates/ide-lsp/schemas/xml/doctrine-mapping.xsd create mode 100644 crates/ide-lsp/src/xml_catalog.rs diff --git a/crates/ide-lsp/schemas/xml/NOTICE b/crates/ide-lsp/schemas/xml/NOTICE new file mode 100644 index 000000000..a4e5b77d7 --- /dev/null +++ b/crates/ide-lsp/schemas/xml/NOTICE @@ -0,0 +1,5 @@ +doctrine-mapping.xsd is from Doctrine ORM (https://github.com/doctrine/orm), +MIT License. Copyright (c) Doctrine Project. +The copy published at https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd +is broken (wrong targetNamespace / XMLSchema URI after an http→https rewrite); +this file is the upstream schema with the correct namespace. diff --git a/crates/ide-lsp/schemas/xml/doctrine-mapping.xsd b/crates/ide-lsp/schemas/xml/doctrine-mapping.xsd new file mode 100644 index 000000000..651c9d29c --- /dev/null +++ b/crates/ide-lsp/schemas/xml/doctrine-mapping.xsd @@ -0,0 +1,643 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/crates/ide-lsp/src/lib.rs b/crates/ide-lsp/src/lib.rs index a1e2d327f..0c59f6561 100644 --- a/crates/ide-lsp/src/lib.rs +++ b/crates/ide-lsp/src/lib.rs @@ -40,6 +40,7 @@ mod uri; mod virtual_doc; mod vue_navigation; mod vue_refs; +mod xml_catalog; pub use injections::{InjectedRange, InjectionProvider, MAX_INJECTED_DOCUMENT_BYTES}; pub use inspect::{CheckedServer, DocumentCheck, FileInspection, InspectHost, InspectOptions}; @@ -3795,6 +3796,9 @@ impl LspManager { if cfg.name == compose::SERVER { compose::configure_yaml_compose_schemas(&mut cfg, &self.open_compose_files()); } + if cfg.name == xml_catalog::SERVER { + xml_catalog::configure_lemminx_catalogs(&mut cfg); + } cfg } diff --git a/crates/ide-lsp/src/xml_catalog.rs b/crates/ide-lsp/src/xml_catalog.rs new file mode 100644 index 000000000..4258b3fb0 --- /dev/null +++ b/crates/ide-lsp/src/xml_catalog.rs @@ -0,0 +1,149 @@ +//! Bundled XML catalogs for LemMinX so known-broken remote XSDs resolve locally. +//! +//! Doctrine's hosted `doctrine-mapping.xsd` advertises the wrong `targetNamespace` +//! (the schema URL itself) after an http→https rewrite, so any mapping file that +//! points `xsi:schemaLocation` at that URL fails with `TargetNamespace.1` / +//! `cvc-elt.1.a`. An OASIS catalog maps the namespace and those system IDs to the +//! correct upstream schema we ship. + +use ide_language::LanguageServerConfig; +use serde_json::{Value, json}; +use std::path::{Path, PathBuf}; + +pub const SERVER: &str = "lemminx"; + +const DOCTRINE_XSD: &str = include_str!("../schemas/xml/doctrine-mapping.xsd"); + +const CATALOG_TEMPLATE: &str = r#" + + + + + + +"#; + +/// Writes the bundled catalog (idempotent) and prepends it to LemMinX `xml.catalogs`. +pub fn configure_lemminx_catalogs(cfg: &mut LanguageServerConfig) { + if cfg.name != SERVER { + return; + } + let Some(catalog) = ensure_catalog() else { return }; + let catalog = path_for_settings(&catalog); + let mut settings = cfg.settings.take().filter(Value::is_object).unwrap_or_else(|| json!({})); + let Some(root) = settings.as_object_mut() else { + cfg.settings = Some(settings); + return; + }; + let Some(xml) = root.entry("xml").or_insert_with(|| json!({})).as_object_mut() else { + cfg.settings = Some(settings); + return; + }; + let Some(catalogs) = xml.entry("catalogs").or_insert_with(|| json!([])).as_array_mut() else { + cfg.settings = Some(settings); + return; + }; + if catalogs.iter().any(|v| v.as_str() == Some(catalog.as_str())) { + cfg.settings = Some(settings); + return; + } + catalogs.insert(0, Value::String(catalog)); + cfg.settings = Some(settings); +} + +fn ensure_catalog() -> Option { + // Per-pid directory: shared `/tmp/openidea-xml-catalog` would let another + // local user plant a catalog that remaps arbitrary schema URIs. + let dir = std::env::temp_dir().join(format!("openidea-xml-catalog-{}", std::process::id())); + if let Err(e) = ide_vfs::create_private_dir(&dir) { + tracing::warn!("xml catalog: cannot create {}: {e}", dir.display()); + return None; + } + let xsd = dir.join("doctrine-mapping.xsd"); + if !write_if_changed(&xsd, DOCTRINE_XSD.as_bytes()) { + tracing::warn!("xml catalog: cannot write {}", xsd.display()); + return None; + } + let catalog = dir.join("catalog.xml"); + if !write_if_changed(&catalog, CATALOG_TEMPLATE.as_bytes()) { + tracing::warn!("xml catalog: cannot write {}", catalog.display()); + return None; + } + Some(catalog) +} + +fn write_if_changed(path: &Path, bytes: &[u8]) -> bool { + if std::fs::read(path).ok().as_deref() == Some(bytes) { + return true; + } + ide_vfs::write_atomic_private(path, bytes).is_ok() +} + +fn path_for_settings(path: &Path) -> String { + path.to_string_lossy().replace('\\', "/") +} + +#[cfg(test)] +mod tests { + use super::*; + use ide_language::LanguageRegistry; + + const DOCTRINE_NS: &str = "http://doctrine-project.org/schemas/orm/doctrine-mapping"; + const DOCTRINE_SCHEMA_HTTPS: &str = "https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd"; + const DOCTRINE_SCHEMA_HTTP: &str = "http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd"; + + #[test] + fn bundled_xsd_keeps_http_namespace() { + assert!(DOCTRINE_XSD.contains(&format!("targetNamespace=\"{DOCTRINE_NS}\""))); + assert!(!DOCTRINE_XSD.contains("xmlns:xs=\"https://www.w3.org/2001/XMLSchema\"")); + assert!(!DOCTRINE_XSD.contains("targetNamespace=\"https://www.doctrine-project.org")); + } + + #[test] + fn catalog_maps_namespace_and_system_ids() { + assert!(CATALOG_TEMPLATE.contains(&format!(r#"name="{DOCTRINE_NS}""#))); + assert!(CATALOG_TEMPLATE.contains(&format!(r#"name="{DOCTRINE_SCHEMA_HTTPS}""#))); + assert!(CATALOG_TEMPLATE.contains(&format!(r#"systemId="{DOCTRINE_SCHEMA_HTTPS}""#))); + assert!(CATALOG_TEMPLATE.contains(&format!(r#"systemId="{DOCTRINE_SCHEMA_HTTP}""#))); + assert_eq!(CATALOG_TEMPLATE.matches(r#"uri="doctrine-mapping.xsd""#).count(), 4); + } + + #[test] + fn prepends_catalog_without_dropping_user_entries() { + let reg = LanguageRegistry::load(None).unwrap(); + let xml = reg.get(&ide_core::LanguageId::new("xml")).unwrap(); + let mut cfg = xml.language_servers[0].clone(); + cfg.settings = Some(json!({"xml": {"catalogs": ["/user/catalog.xml"]}})); + configure_lemminx_catalogs(&mut cfg); + let list: Vec = cfg.settings.as_ref().unwrap()["xml"]["catalogs"] + .as_array() + .unwrap() + .iter() + .filter_map(Value::as_str) + .map(str::to_owned) + .collect(); + assert_eq!(list.len(), 2); + assert!(list[0].ends_with("catalog.xml")); + assert!(list[0].contains(&format!("openidea-xml-catalog-{}", std::process::id()))); + assert_eq!(list[1], "/user/catalog.xml"); + configure_lemminx_catalogs(&mut cfg); + assert_eq!(cfg.settings.as_ref().unwrap()["xml"]["catalogs"].as_array().unwrap().len(), 2); + + let catalog = PathBuf::from(&list[0]); + let body = std::fs::read_to_string(&catalog).unwrap(); + assert_eq!(body, CATALOG_TEMPLATE); + assert!(body.contains(&format!(r#"name="{DOCTRINE_NS}""#))); + let xsd = catalog.with_file_name("doctrine-mapping.xsd"); + assert_eq!(std::fs::read_to_string(xsd).unwrap(), DOCTRINE_XSD); + } + + #[test] + fn ignores_other_servers() { + let reg = LanguageRegistry::load(None).unwrap(); + let yaml = reg.get(&ide_core::LanguageId::new("yaml")).unwrap(); + let mut cfg = yaml.language_servers[0].clone(); + let before = cfg.settings.clone(); + configure_lemminx_catalogs(&mut cfg); + assert_eq!(cfg.settings, before); + } +}