Skip to main content

rumdl_lib/config/
loading.rs

1use indexmap::IndexSet;
2use std::collections::BTreeMap;
3use std::marker::PhantomData;
4use std::path::{Path, PathBuf};
5use std::sync::{Arc, OnceLock};
6
7use super::flavor::ConfigLoaded;
8use super::flavor::ConfigValidated;
9use super::parsers;
10use super::registry::RuleRegistry;
11use super::source_tracking::{
12    ConfigSource, ConfigValidationWarning, SourcedConfig, SourcedConfigFragment, SourcedGlobalConfig, SourcedValue,
13};
14use super::types::{Config, ConfigError, GlobalConfig, MARKDOWNLINT_CONFIG_FILES, RUMDL_CONFIG_FILES, RuleConfig};
15use super::validation::validate_config_sourced_internal;
16use crate::utils::upward_walk::UpwardWalk;
17
18/// Maximum depth for extends chains to prevent runaway recursion
19const MAX_EXTENDS_DEPTH: usize = 10;
20
21/// Cheap pre-filter for whether a `pyproject.toml` declares rumdl config.
22///
23/// Matches the flat section header `[tool.rumdl]` as well as dotted sections
24/// like `[tool.rumdl.MD013]` or `[tool.rumdl.rules.MD007]` (which are valid on
25/// their own, without a flat header). Requiring the leading `[` avoids matching
26/// a bare `tool.rumdl` in prose or dependency names; a literal `[tool.rumdl...`
27/// inside a comment or string would still match, but the subsequent parse
28/// handles that gracefully.
29fn pyproject_declares_rumdl_config(content: &str) -> bool {
30    content.contains("[tool.rumdl]") || content.contains("[tool.rumdl.")
31}
32
33/// Resolve an `extends` path relative to the config file that contains it.
34///
35/// - `~/` prefix: expanded to home directory
36/// - Relative paths: resolved against the config file's parent directory
37/// - Absolute paths: used as-is
38fn resolve_extends_path(extends_value: &str, config_file_path: &Path) -> PathBuf {
39    if let Some(suffix) = extends_value.strip_prefix("~/") {
40        // Expand tilde to home directory
41        #[cfg(feature = "native")]
42        {
43            use etcetera::{BaseStrategy, choose_base_strategy};
44            let home = choose_base_strategy().map_or_else(|_| PathBuf::from("~"), |s| s.home_dir().to_path_buf());
45            home.join(suffix)
46        }
47        #[cfg(not(feature = "native"))]
48        {
49            let _ = suffix;
50            PathBuf::from(extends_value)
51        }
52    } else {
53        let path = PathBuf::from(extends_value);
54        if path.is_absolute() {
55            path
56        } else {
57            // Resolve relative to config file's directory
58            let config_dir = config_file_path.parent().unwrap_or(Path::new("."));
59            config_dir.join(extends_value)
60        }
61    }
62}
63
64/// Determine ConfigSource from a config filename.
65fn source_from_filename(filename: &str) -> ConfigSource {
66    if filename == "pyproject.toml" {
67        ConfigSource::PyprojectToml
68    } else {
69        ConfigSource::ProjectConfig
70    }
71}
72
73/// The rumdl-native config files that actually exist in `dir`, in precedence order.
74///
75/// Walks `RUMDL_CONFIG_FILES` (the single source of truth for discovery) joined onto
76/// `dir`, so `.config/rumdl.toml` is recognised at the same level as `.rumdl.toml`.
77/// `pyproject.toml` counts only when it declares `[tool.rumdl]`. markdownlint configs
78/// are intentionally excluded: they are a separate fallback tier, not a same-tool
79/// collision, and projects routinely keep one around while migrating.
80pub(crate) fn rumdl_configs_in_dir(dir: &Path) -> Vec<PathBuf> {
81    RUMDL_CONFIG_FILES
82        .iter()
83        .map(|name| dir.join(name))
84        .filter(|path| {
85            if !path.exists() {
86                return false;
87            }
88            if path.file_name().and_then(|n| n.to_str()) == Some("pyproject.toml") {
89                std::fs::read_to_string(path).is_ok_and(|content| pyproject_declares_rumdl_config(&content))
90            } else {
91                true
92            }
93        })
94        .collect()
95}
96
97/// A directory holding more than one rumdl-native config file.
98///
99/// `winner` is the file discovery uses (highest precedence); `shadowed` are the
100/// silently-ignored siblings. Having both `.rumdl.toml` and `rumdl.toml` (or either
101/// plus a `[tool.rumdl]` in `pyproject.toml`) in one directory is redundant by
102/// construction and a common footgun: editing the shadowed file appears to do
103/// nothing. Resolution is unchanged (the dot file still wins, matching Ruff); this
104/// type only lets callers surface the collision.
105#[derive(Debug, Clone, PartialEq, Eq)]
106pub(crate) struct ShadowedConfigs {
107    pub dir: PathBuf,
108    pub winner: PathBuf,
109    pub shadowed: Vec<PathBuf>,
110}
111
112/// Detect rumdl-native config files that shadow each other in `dir`.
113///
114/// Returns `None` unless two or more rumdl-native configs coexist at this directory
115/// level (markdownlint files and configs in other directories never count). The
116/// highest-precedence file is the `winner`; the rest are silently `shadowed`.
117pub(crate) fn detect_shadowed_configs(dir: &Path) -> Option<ShadowedConfigs> {
118    let mut configs = rumdl_configs_in_dir(dir);
119    if configs.len() < 2 {
120        return None;
121    }
122    let winner = configs.remove(0);
123    Some(ShadowedConfigs {
124        dir: dir.to_path_buf(),
125        winner,
126        shadowed: configs,
127    })
128}
129
130/// Format a shadowed-config collision as a single user-facing warning line.
131///
132/// The directory is named once; the winner and shadowed files are shown relative
133/// to it (e.g. `.rumdl.toml`, `.config/rumdl.toml`) rather than repeating the full
134/// directory in every path. Paths are normalized to forward slashes on Windows for
135/// stable, copy-pasteable output; non-UTF-8 components degrade lossily rather than
136/// panicking.
137pub(crate) fn format_shadow_warning(shadow: &ShadowedConfigs) -> String {
138    let norm = |s: String| if cfg!(windows) { s.replace('\\', "/") } else { s };
139    let rel = |path: &Path| {
140        let relative = path.strip_prefix(&shadow.dir).unwrap_or(path);
141        norm(relative.to_string_lossy().into_owned())
142    };
143    let shadowed = shadow.shadowed.iter().map(|p| rel(p)).collect::<Vec<_>>().join(", ");
144    format!(
145        "multiple rumdl config files in {}: using {}, ignoring {}",
146        norm(shadow.dir.to_string_lossy().into_owned()),
147        rel(&shadow.winner),
148        shadowed,
149    )
150}
151
152/// Load a config file (and any base configs it extends) into a SourcedConfig.
153///
154/// This function handles the recursive `extends` chain:
155/// 1. Parse the config file into a fragment
156/// 2. If the fragment has `extends`, recursively load the base config first
157/// 3. Merge the base config, then merge this fragment on top
158fn load_config_with_extends(
159    sourced_config: &mut SourcedConfig<ConfigLoaded>,
160    config_file_path: &Path,
161    visited: &mut IndexSet<PathBuf>,
162    chain_source: ConfigSource,
163) -> Result<(), ConfigError> {
164    // Canonicalize the path for circular reference detection
165    let canonical = config_file_path
166        .canonicalize()
167        .unwrap_or_else(|_| config_file_path.to_path_buf());
168
169    // Check for circular references
170    if visited.contains(&canonical) {
171        let chain: Vec<String> = visited.iter().map(|p| p.display().to_string()).collect();
172        return Err(ConfigError::CircularExtends {
173            path: config_file_path.display().to_string(),
174            chain,
175        });
176    }
177
178    // Check depth limit
179    if visited.len() >= MAX_EXTENDS_DEPTH {
180        return Err(ConfigError::ExtendsDepthExceeded {
181            path: config_file_path.display().to_string(),
182            max_depth: MAX_EXTENDS_DEPTH,
183        });
184    }
185
186    // Mark as visited
187    visited.insert(canonical);
188
189    let path_str = config_file_path.display().to_string();
190    let filename = config_file_path.file_name().and_then(|n| n.to_str()).unwrap_or("");
191
192    // Read and parse the config file
193    let content = std::fs::read_to_string(config_file_path).map_err(|e| ConfigError::IoError {
194        source: e,
195        path: path_str.clone(),
196    })?;
197
198    let fragment = if filename == "pyproject.toml" {
199        match parsers::parse_pyproject_toml(&content, &path_str, chain_source)? {
200            Some(f) => f,
201            None => return Ok(()), // No [tool.rumdl] section
202        }
203    } else {
204        parsers::parse_rumdl_toml(&content, &path_str, chain_source)?
205    };
206
207    // If this fragment has `extends`, load the base config first
208    if let Some(ref extends_value) = fragment.extends {
209        let base_path = resolve_extends_path(extends_value, config_file_path);
210
211        if !base_path.exists() {
212            return Err(ConfigError::ExtendsNotFound {
213                path: base_path.display().to_string(),
214                from: path_str.clone(),
215            });
216        }
217
218        log::debug!(
219            "[rumdl-config] Config {} extends {}, loading base first",
220            path_str,
221            base_path.display()
222        );
223
224        // Recursively load the base config
225        load_config_with_extends(sourced_config, &base_path, visited, chain_source)?;
226    }
227
228    // Merge this fragment on top (base config was already merged if present)
229    // Strip the `extends` field since it's been consumed
230    let mut fragment_for_merge = fragment;
231    fragment_for_merge.extends = None;
232    sourced_config.merge(fragment_for_merge);
233    sourced_config.loaded_files.push(path_str);
234
235    Ok(())
236}
237
238impl SourcedConfig<ConfigLoaded> {
239    /// Merges another SourcedConfigFragment into this SourcedConfig.
240    /// Uses source precedence to determine which values take effect.
241    pub(super) fn merge(&mut self, fragment: SourcedConfigFragment) {
242        // Merge global config. Enable/disable use replace semantics (child
243        // config overrides parent, matching Ruff's `select`/`ignore`);
244        // extend-enable/extend-disable use union semantics (additive across
245        // config levels).
246        self.global.enable.merge_from(fragment.global.enable);
247        self.global.disable.merge_from(fragment.global.disable);
248        self.global
249            .extend_enable
250            .merge_union_from(fragment.global.extend_enable);
251        self.global
252            .extend_disable
253            .merge_union_from(fragment.global.extend_disable);
254
255        // Conflict resolution: Enable overrides disable
256        // Remove any rules from disable that appear in enable
257        self.global
258            .disable
259            .value
260            .retain(|rule| !self.global.enable.value.contains(rule));
261
262        self.global.include.merge_from(fragment.global.include);
263        self.global.exclude.merge_from(fragment.global.exclude);
264        self.global
265            .respect_gitignore
266            .merge_from(fragment.global.respect_gitignore);
267        self.global.line_length.merge_from(fragment.global.line_length);
268        self.global.fixable.merge_from(fragment.global.fixable);
269        self.global.unfixable.merge_from(fragment.global.unfixable);
270        self.global.flavor.merge_from(fragment.global.flavor);
271        self.global.force_exclude.merge_from(fragment.global.force_exclude);
272
273        // Merge output_format if present
274        if let Some(output_format_fragment) = fragment.global.output_format {
275            if let Some(ref mut output_format) = self.global.output_format {
276                output_format.merge_from(output_format_fragment);
277            } else {
278                self.global.output_format = Some(output_format_fragment);
279            }
280        }
281
282        // Merge cache_dir if present
283        if let Some(cache_dir_fragment) = fragment.global.cache_dir {
284            if let Some(ref mut cache_dir) = self.global.cache_dir {
285                cache_dir.merge_from(cache_dir_fragment);
286            } else {
287                self.global.cache_dir = Some(cache_dir_fragment);
288            }
289        }
290
291        // Merge cache if not default (only override when explicitly set)
292        if fragment.global.cache.source != ConfigSource::Default {
293            self.global.cache.merge_from(fragment.global.cache);
294        }
295
296        self.per_file_ignores.merge_from(fragment.per_file_ignores);
297        self.per_file_flavor.merge_from(fragment.per_file_flavor);
298        self.code_block_tools.merge_from(fragment.code_block_tools);
299
300        // Merge rule configs
301        for (rule_name, rule_fragment) in fragment.rules {
302            let norm_rule_name = rule_name.to_ascii_uppercase(); // Normalize to uppercase for case-insensitivity
303            let rule_entry = self.rules.entry(norm_rule_name).or_default();
304
305            // Merge severity if present in fragment
306            if let Some(severity_fragment) = rule_fragment.severity {
307                if let Some(ref mut existing_severity) = rule_entry.severity {
308                    existing_severity.merge_from(severity_fragment);
309                } else {
310                    rule_entry.severity = Some(severity_fragment);
311                }
312            }
313
314            // Merge values
315            for (key, sourced_value_fragment) in rule_fragment.values {
316                let sv_entry = rule_entry
317                    .values
318                    .entry(key.clone())
319                    .or_insert_with(|| SourcedValue::new(sourced_value_fragment.value.clone(), ConfigSource::Default));
320                sv_entry.merge_from(sourced_value_fragment);
321            }
322        }
323
324        // Merge unknown_keys from fragment
325        for (section, key, file_path) in fragment.unknown_keys {
326            // Deduplicate: only add if not already present
327            if !self.unknown_keys.iter().any(|(s, k, _)| s == &section && k == &key) {
328                self.unknown_keys.push((section, key, file_path));
329            }
330        }
331    }
332
333    /// Load and merge configurations from files and CLI overrides.
334    pub fn load(config_path: Option<&str>, cli_overrides: Option<&SourcedGlobalConfig>) -> Result<Self, ConfigError> {
335        Self::load_with_discovery(config_path, cli_overrides, false)
336    }
337
338    /// Finds project root by walking up from start_dir looking for .git directory.
339    /// Falls back to start_dir if no .git found.
340    fn find_project_root_from(start_dir: &Path) -> std::path::PathBuf {
341        UpwardWalk::new(start_dir)
342            .find(|dir| dir.join(".git").exists())
343            .unwrap_or_else(|| {
344                log::debug!(
345                    "[rumdl-config] No .git found, using config location as project root: {}",
346                    start_dir.display()
347                );
348                start_dir.to_path_buf()
349            })
350    }
351
352    /// Resolve the home-directory boundary used to stop project-config discovery.
353    ///
354    /// `home_override` wins (supplied by tests); otherwise the real home is resolved on
355    /// native builds via `etcetera`. Wasm has no home/project walk to bound, so it
356    /// returns `None` there.
357    fn resolve_home_boundary(home_override: Option<&Path>) -> Option<std::path::PathBuf> {
358        home_override.map(Path::to_path_buf).or_else(|| {
359            #[cfg(feature = "native")]
360            {
361                use etcetera::{BaseStrategy, choose_base_strategy};
362                choose_base_strategy().ok().map(|s| s.home_dir().to_path_buf())
363            }
364            #[cfg(not(feature = "native"))]
365            {
366                None
367            }
368        })
369    }
370
371    /// Discover configuration file by traversing up the directory tree.
372    /// Returns the first configuration file found.
373    /// Discovers config file and returns both the config path and project root.
374    /// Returns: (config_file_path, project_root_path)
375    /// Project root is the directory containing .git, or config parent as fallback.
376    ///
377    /// The walk stops at the home directory: a config file located in `$HOME`
378    /// itself is user-level, not a project config, and must reach the loader only
379    /// through the user-config fallback (`load_user_config`) so the platform
380    /// user-config directory keeps precedence over `~/.rumdl.toml`. The cwd is
381    /// exempt from that boundary: it is an explicitly chosen project context, so
382    /// its configs apply even when the cwd *is* `$HOME` (pre-commit.ci sets `HOME`
383    /// to the git checkout, and `pyproject.toml` has no user-config fallback).
384    /// `home_override` supplies the boundary for tests; production resolves the
385    /// real home directory.
386    fn discover_config_upward(
387        home_override: Option<&Path>,
388    ) -> Option<(std::path::PathBuf, std::path::PathBuf, Option<ShadowedConfigs>)> {
389        let start_dir = match std::env::current_dir() {
390            Ok(dir) => dir,
391            Err(e) => {
392                log::debug!("[rumdl-config] Failed to get current directory: {e}");
393                return None;
394            }
395        };
396
397        // `rumdl_configs_in_dir` is the single source of truth for "which rumdl
398        // configs live here", shared with the LSP and the shadow detector, so the
399        // winner and the silently-shadowed siblings are computed identically.
400        let (config_path, config_dir, shadow) = UpwardWalk::new(&start_dir)
401            .stop_below(Self::resolve_home_boundary(home_override))
402            .always_yield_start()
403            .stop_at_git_root()
404            .find_map(|dir| {
405                rumdl_configs_in_dir(&dir).into_iter().next().map(|winner| {
406                    log::debug!("[rumdl-config] Found config file: {}", winner.display());
407                    let shadow = detect_shadowed_configs(&dir);
408                    (winner, dir, shadow)
409                })
410            })?;
411
412        // Determine project root by walking up from the config location.
413        let project_root = Self::find_project_root_from(&config_dir);
414        Some((config_path, project_root, shadow))
415    }
416
417    /// Discover markdownlint configuration file by traversing up the directory tree.
418    /// Similar to discover_config_upward but for .markdownlint.yaml/json files, and
419    /// bounded at the home directory for the same reason: a markdownlint config in
420    /// `$HOME` is user-level, not a project config. The cwd is exempt from the
421    /// boundary just like rumdl config discovery, and markdownlint files have no
422    /// user-config fallback at all, so without the exemption a config in a
423    /// `HOME == cwd` checkout would be ignored entirely.
424    fn discover_markdownlint_config_upward(home_override: Option<&Path>) -> Option<std::path::PathBuf> {
425        let start_dir = match std::env::current_dir() {
426            Ok(dir) => dir,
427            Err(e) => {
428                log::debug!("[rumdl-config] Failed to get current directory for markdownlint discovery: {e}");
429                return None;
430            }
431        };
432
433        UpwardWalk::new(&start_dir)
434            .stop_below(Self::resolve_home_boundary(home_override))
435            .always_yield_start()
436            .stop_at_git_root()
437            .find_map(|dir| {
438                MARKDOWNLINT_CONFIG_FILES
439                    .iter()
440                    .map(|name| dir.join(name))
441                    .find(|path| path.exists())
442            })
443    }
444
445    /// Internal implementation that accepts config directory for testing
446    fn user_configuration_path_impl(config_dir: &Path) -> Option<std::path::PathBuf> {
447        let config_dir = config_dir.join("rumdl");
448
449        // Check for config files in precedence order (same as project discovery)
450        const USER_CONFIG_FILES: &[&str] = &[".rumdl.toml", "rumdl.toml", "pyproject.toml"];
451
452        log::debug!(
453            "[rumdl-config] Checking for user configuration in: {}",
454            config_dir.display()
455        );
456
457        for filename in USER_CONFIG_FILES {
458            let config_path = config_dir.join(filename);
459
460            if config_path.exists() {
461                // For pyproject.toml, verify it contains [tool.rumdl] section
462                if *filename == "pyproject.toml" {
463                    if let Ok(content) = std::fs::read_to_string(&config_path) {
464                        if pyproject_declares_rumdl_config(&content) {
465                            log::debug!("[rumdl-config] Found user configuration at: {}", config_path.display());
466                            return Some(config_path);
467                        }
468                        log::debug!("[rumdl-config] Found user pyproject.toml but no [tool.rumdl] section");
469                        continue;
470                    }
471                } else {
472                    log::debug!("[rumdl-config] Found user configuration at: {}", config_path.display());
473                    return Some(config_path);
474                }
475            }
476        }
477
478        log::debug!(
479            "[rumdl-config] No user configuration found in: {}",
480            config_dir.display()
481        );
482        None
483    }
484
485    /// Discover user-level configuration file from platform-specific config directory.
486    /// Returns the first configuration file found in the user config directory.
487    #[cfg(feature = "native")]
488    fn user_configuration_path() -> Option<std::path::PathBuf> {
489        use etcetera::{BaseStrategy, choose_base_strategy};
490
491        match choose_base_strategy() {
492            Ok(strategy) => {
493                let config_dir = strategy.config_dir();
494                Self::user_configuration_path_impl(&config_dir)
495            }
496            Err(e) => {
497                log::debug!("[rumdl-config] Failed to determine user config directory: {e}");
498                None
499            }
500        }
501    }
502
503    /// Stub for WASM builds - user config not supported
504    #[cfg(not(feature = "native"))]
505    fn user_configuration_path() -> Option<std::path::PathBuf> {
506        None
507    }
508
509    /// Internal implementation that accepts the home directory for testing.
510    ///
511    /// Probes `<home>/.rumdl.toml` then `<home>/rumdl.toml`, returning the first match.
512    ///
513    /// `pyproject.toml` is intentionally **not** searched in `$HOME`, even though
514    /// `user_configuration_path_impl` does check it inside the platform config dir.
515    /// The asymmetry is deliberate: a `pyproject.toml` directly in `$HOME` almost
516    /// always belongs to unrelated python tooling (poetry/uv/pip's user-level config),
517    /// and silently picking it up as a rumdl config would surprise users. The
518    /// platform config dir (`~/.config/rumdl/`) is rumdl-scoped, so the same
519    /// concern doesn't apply there.
520    fn home_configuration_path_impl(home_dir: &Path) -> Option<std::path::PathBuf> {
521        const HOME_CONFIG_FILES: &[&str] = &[".rumdl.toml", "rumdl.toml"];
522
523        log::debug!(
524            "[rumdl-config] Checking for home-directory configuration in: {}",
525            home_dir.display()
526        );
527
528        for filename in HOME_CONFIG_FILES {
529            let config_path = home_dir.join(filename);
530            if config_path.exists() {
531                log::debug!(
532                    "[rumdl-config] Found home-directory configuration at: {}",
533                    config_path.display()
534                );
535                return Some(config_path);
536            }
537        }
538
539        log::debug!(
540            "[rumdl-config] No home-directory configuration found in: {}",
541            home_dir.display()
542        );
543        None
544    }
545
546    /// Discover a home-directory configuration file (`~/.rumdl.toml` or `~/rumdl.toml`).
547    ///
548    /// This is a final fallback after the platform user-config directory
549    /// (`user_configuration_path`). It honors the classic Unix dotfile convention so
550    /// users who keep tool config in `$HOME` rather than `$XDG_CONFIG_HOME` are picked up.
551    #[cfg(feature = "native")]
552    fn home_configuration_path() -> Option<std::path::PathBuf> {
553        use etcetera::{BaseStrategy, choose_base_strategy};
554
555        match choose_base_strategy() {
556            Ok(strategy) => Self::home_configuration_path_impl(strategy.home_dir()),
557            Err(e) => {
558                log::debug!("[rumdl-config] Failed to determine home directory: {e}");
559                None
560            }
561        }
562    }
563
564    /// Stub for WASM builds - home config not supported
565    #[cfg(not(feature = "native"))]
566    fn home_configuration_path() -> Option<std::path::PathBuf> {
567        None
568    }
569
570    /// Load an explicit config file (standalone, no user config merging)
571    fn load_explicit_config(sourced_config: &mut Self, path: &str) -> Result<(), ConfigError> {
572        let path_obj = Path::new(path);
573        let filename = path_obj.file_name().and_then(|name| name.to_str()).unwrap_or("");
574        let path_str = path.to_string();
575
576        log::debug!("[rumdl-config] Loading explicit config file: {filename}");
577
578        // Find project root by walking up from config location looking for .git
579        if let Some(config_parent) = path_obj.parent() {
580            let project_root = Self::find_project_root_from(config_parent);
581            log::debug!(
582                "[rumdl-config] Project root (from explicit config): {}",
583                project_root.display()
584            );
585            sourced_config.project_root = Some(project_root);
586        }
587
588        // Known markdownlint config files
589        const MARKDOWNLINT_FILENAMES: &[&str] = &[
590            ".markdownlint-cli2.jsonc",
591            ".markdownlint-cli2.yaml",
592            ".markdownlint-cli2.yml",
593            ".markdownlint.json",
594            ".markdownlint.yaml",
595            ".markdownlint.yml",
596        ];
597
598        if filename == "pyproject.toml" || filename == ".rumdl.toml" || filename == "rumdl.toml" {
599            // Use extends-aware loading for rumdl TOML configs
600            let mut visited = IndexSet::new();
601            let chain_source = source_from_filename(filename);
602            load_config_with_extends(sourced_config, path_obj, &mut visited, chain_source)?;
603        } else if MARKDOWNLINT_FILENAMES.contains(&filename)
604            || path_str.ends_with(".json")
605            || path_str.ends_with(".jsonc")
606            || path_str.ends_with(".yaml")
607            || path_str.ends_with(".yml")
608        {
609            // Parse as markdownlint config (JSON/YAML) - no extends support
610            let fragment = parsers::load_from_markdownlint(&path_str)?;
611            sourced_config.merge(fragment);
612            sourced_config.loaded_files.push(path_str);
613        } else {
614            // Try TOML with extends support
615            let mut visited = IndexSet::new();
616            let chain_source = source_from_filename(filename);
617            load_config_with_extends(sourced_config, path_obj, &mut visited, chain_source)?;
618        }
619
620        Ok(())
621    }
622
623    /// Load and merge user-level configuration into this `SourcedConfig`.
624    ///
625    /// Discovers the user config file in this order, taking the first match:
626    /// 1. Platform user-config directory, resolved via `etcetera::choose_base_strategy`
627    ///    (the CLI/XDG convention): `~/.config` on Linux and macOS, `%APPDATA%` on
628    ///    Windows. Note macOS uses the XDG-style `~/.config`, not the GUI-app location
629    ///    `~/Library/Application Support`. Override with `user_config_dir` for tests.
630    /// 2. Home-directory dotfile (`~/.rumdl.toml`, then `~/rumdl.toml`). Override with
631    ///    `home_dir` for tests. Honors the classic Unix dotfile convention.
632    ///
633    /// Resolves any `extends` chain and merges each fragment with
634    /// `ConfigSource::UserConfig` precedence.
635    ///
636    /// Called in two contexts:
637    /// - When no project config is found: provides user defaults as the sole base
638    /// - When a markdownlint project config is found: provides rumdl-specific
639    ///   defaults that the markdownlint format cannot express; the markdownlint
640    ///   fragment is merged on top and wins on any overlapping key
641    fn load_user_config(
642        sourced_config: &mut Self,
643        user_config_dir: Option<&Path>,
644        home_dir: Option<&Path>,
645    ) -> Result<(), ConfigError> {
646        let user_config_path = if let Some(dir) = user_config_dir {
647            Self::user_configuration_path_impl(dir)
648        } else {
649            Self::user_configuration_path()
650        };
651
652        let user_config_path = user_config_path.or_else(|| match home_dir {
653            Some(home) => Self::home_configuration_path_impl(home),
654            None => Self::home_configuration_path(),
655        });
656
657        if let Some(user_config_path) = user_config_path {
658            let path_str = user_config_path.display().to_string();
659
660            log::debug!("[rumdl-config] Loading user config: {path_str}");
661
662            // User config fallback also supports extends chains.
663            // Use a uniform source across the chain so child overrides are determined by chain order.
664            let mut visited = IndexSet::new();
665            load_config_with_extends(
666                sourced_config,
667                &user_config_path,
668                &mut visited,
669                ConfigSource::UserConfig,
670            )?;
671        } else {
672            log::debug!("[rumdl-config] No user configuration file found");
673        }
674
675        Ok(())
676    }
677
678    /// Internal implementation that accepts user config directory and home directory for testing
679    #[doc(hidden)]
680    pub fn load_with_discovery_impl(
681        config_path: Option<&str>,
682        cli_overrides: Option<&SourcedGlobalConfig>,
683        skip_auto_discovery: bool,
684        user_config_dir: Option<&Path>,
685        home_dir: Option<&Path>,
686    ) -> Result<Self, ConfigError> {
687        use std::env;
688        log::debug!("[rumdl-config] Current working directory: {:?}", env::current_dir());
689
690        let mut sourced_config = SourcedConfig::default();
691
692        // Ruff model: Project config is standalone, user config is fallback only
693        //
694        // Priority order:
695        // 1. If explicit config path provided → use ONLY that (standalone)
696        // 2. Else if project config discovered → use ONLY that (standalone)
697        // 3. Else if user config exists → use it as fallback
698        // 4. CLI overrides always apply last
699        //
700        // This ensures project configs are reproducible across machines and
701        // CI/local runs behave identically.
702
703        // Explicit config path always takes precedence
704        if let Some(path) = config_path {
705            // Explicit config path provided - use ONLY this config (standalone)
706            log::debug!("[rumdl-config] Explicit config_path provided: {path:?}");
707            Self::load_explicit_config(&mut sourced_config, path)?;
708        } else if skip_auto_discovery {
709            log::debug!("[rumdl-config] Skipping config discovery due to --no-config/--isolated flag");
710            // No config loading, just apply CLI overrides at the end
711        } else {
712            // No explicit path - try auto-discovery
713            log::debug!("[rumdl-config] No explicit config_path, searching default locations");
714
715            // Try to discover project config first
716            if let Some((config_file, project_root, shadow)) = Self::discover_config_upward(home_dir) {
717                // Project config found - use ONLY this (standalone, no user config).
718                // Rumdl project configs can express all settings directly, so user config
719                // is not needed and omitting it ensures CI and local runs are identical.
720                log::debug!("[rumdl-config] Found project config: {}", config_file.display());
721                log::debug!("[rumdl-config] Project root: {}", project_root.display());
722
723                // Record any same-directory sibling configs that are silently shadowed,
724                // so the CLI and LSP can warn the user. Resolution is unchanged.
725                if let Some(shadow) = shadow {
726                    sourced_config.discovery_warnings.push(format_shadow_warning(&shadow));
727                }
728
729                sourced_config.project_root = Some(project_root);
730
731                // Use extends-aware loading for discovered configs
732                let mut visited = IndexSet::new();
733                let root_filename = config_file.file_name().and_then(|n| n.to_str()).unwrap_or("");
734                let chain_source = source_from_filename(root_filename);
735                load_config_with_extends(&mut sourced_config, &config_file, &mut visited, chain_source)?;
736            } else {
737                // No rumdl project config - try markdownlint config
738                log::debug!("[rumdl-config] No rumdl config found, checking markdownlint config");
739
740                if let Some(markdownlint_path) = Self::discover_markdownlint_config_upward(home_dir) {
741                    let path_str = markdownlint_path.display().to_string();
742                    log::debug!("[rumdl-config] Found markdownlint config: {path_str}");
743                    // Load user config first as a base so rumdl-specific settings (e.g. flavor,
744                    // cache) take effect. Markdownlint configs cannot express these settings.
745                    // The markdownlint fragment uses ConfigSource::ProjectConfig (precedence 3)
746                    // vs UserConfig (precedence 1), so project settings always win on overlap.
747                    Self::load_user_config(&mut sourced_config, user_config_dir, home_dir)?;
748                    match parsers::load_from_markdownlint(&path_str) {
749                        Ok(fragment) => {
750                            sourced_config.merge(fragment);
751                            sourced_config.loaded_files.push(path_str);
752                        }
753                        Err(_e) => {
754                            log::debug!("[rumdl-config] Failed to load markdownlint config");
755                        }
756                    }
757                } else {
758                    // No project config at all - use user config as fallback
759                    log::debug!("[rumdl-config] No project config found, using user config as fallback");
760                    Self::load_user_config(&mut sourced_config, user_config_dir, home_dir)?;
761                }
762            }
763        }
764
765        // Apply CLI overrides (highest precedence)
766        if let Some(cli) = cli_overrides {
767            sourced_config
768                .global
769                .enable
770                .merge_override(cli.enable.value.clone(), ConfigSource::Cli, None);
771            sourced_config
772                .global
773                .disable
774                .merge_override(cli.disable.value.clone(), ConfigSource::Cli, None);
775            sourced_config
776                .global
777                .exclude
778                .merge_override(cli.exclude.value.clone(), ConfigSource::Cli, None);
779            sourced_config
780                .global
781                .include
782                .merge_override(cli.include.value.clone(), ConfigSource::Cli, None);
783            sourced_config.global.respect_gitignore.merge_override(
784                cli.respect_gitignore.value,
785                ConfigSource::Cli,
786                None,
787            );
788            sourced_config
789                .global
790                .fixable
791                .merge_override(cli.fixable.value.clone(), ConfigSource::Cli, None);
792            sourced_config
793                .global
794                .unfixable
795                .merge_override(cli.unfixable.value.clone(), ConfigSource::Cli, None);
796            // No rule-specific CLI overrides implemented yet
797        }
798
799        // Unknown keys are now collected during parsing and validated via validate_config_sourced()
800
801        Ok(sourced_config)
802    }
803
804    /// Load and merge configurations from files and CLI overrides.
805    /// If skip_auto_discovery is true, only explicit config paths are loaded.
806    pub fn load_with_discovery(
807        config_path: Option<&str>,
808        cli_overrides: Option<&SourcedGlobalConfig>,
809        skip_auto_discovery: bool,
810    ) -> Result<Self, ConfigError> {
811        Self::load_with_discovery_impl(config_path, cli_overrides, skip_auto_discovery, None, None)
812    }
813
814    /// Validate the configuration against a rule registry.
815    ///
816    /// This method transitions the config from `ConfigLoaded` to `ConfigValidated` state,
817    /// enabling conversion to `Config`. Validation warnings are stored in the config
818    /// and can be displayed to the user.
819    ///
820    /// # Example
821    ///
822    /// ```ignore
823    /// let loaded = SourcedConfig::load_with_discovery(path, None, false)?;
824    /// let validated = loaded.validate(&registry)?;
825    /// let config: Config = validated.into();
826    /// ```
827    pub fn validate(self, registry: &RuleRegistry) -> Result<SourcedConfig<ConfigValidated>, ConfigError> {
828        let warnings = validate_config_sourced_internal(&self, registry);
829
830        Ok(SourcedConfig {
831            global: self.global,
832            per_file_ignores: self.per_file_ignores,
833            per_file_flavor: self.per_file_flavor,
834            code_block_tools: self.code_block_tools,
835            rules: self.rules,
836            loaded_files: self.loaded_files,
837            unknown_keys: self.unknown_keys,
838            project_root: self.project_root,
839            discovery_warnings: self.discovery_warnings,
840            validation_warnings: warnings,
841            _state: PhantomData,
842        })
843    }
844
845    /// Validate and convert to Config in one step (convenience method).
846    ///
847    /// This combines `validate()` and `into()` for callers who want the
848    /// validation warnings separately.
849    pub fn validate_into(self, registry: &RuleRegistry) -> Result<(Config, Vec<ConfigValidationWarning>), ConfigError> {
850        let validated = self.validate(registry)?;
851        let warnings = validated.validation_warnings.clone();
852        Ok((validated.into(), warnings))
853    }
854
855    /// Skip validation and convert directly to ConfigValidated state.
856    ///
857    /// # Safety
858    ///
859    /// This method bypasses validation. Use only when:
860    /// - You've already validated via `validate_config_sourced()`
861    /// - You're in test code that doesn't need validation
862    /// - You're migrating legacy code and will add proper validation later
863    ///
864    /// Prefer `validate()` for new code.
865    pub fn into_validated_unchecked(self) -> SourcedConfig<ConfigValidated> {
866        SourcedConfig {
867            global: self.global,
868            per_file_ignores: self.per_file_ignores,
869            per_file_flavor: self.per_file_flavor,
870            code_block_tools: self.code_block_tools,
871            rules: self.rules,
872            loaded_files: self.loaded_files,
873            unknown_keys: self.unknown_keys,
874            project_root: self.project_root,
875            discovery_warnings: self.discovery_warnings,
876            validation_warnings: Vec::new(),
877            _state: PhantomData,
878        }
879    }
880
881    /// Discover the nearest config file for a specific directory,
882    /// walking upward to `project_root` (inclusive).
883    ///
884    /// Searches for rumdl config files (`.rumdl.toml`, `rumdl.toml`,
885    /// `.config/rumdl.toml`, `pyproject.toml` with `[tool.rumdl]`) and
886    /// markdownlint config files at each directory level.
887    ///
888    /// Returns the config file path if found. Does NOT use CWD.
889    pub fn discover_config_for_dir(dir: &Path, project_root: &Path) -> Option<PathBuf> {
890        // The walk never canonicalizes the directories it yields (symlinks and
891        // Windows short names stay as the caller wrote them); only the stop
892        // checks inside `UpwardWalk` compare canonically. A relative `dir` is
893        // resolved against the current directory, so the returned config path
894        // is always absolute.
895        //
896        // The home boundary keeps the walk from treating `~/.rumdl.toml` as a
897        // project config, consistent with `discover_config_upward`. This only has
898        // an effect when `project_root` is at or above the home directory (e.g. a
899        // multi-path run whose grouping root spans the home boundary); for the
900        // usual project root below home the walk stops there first.
901        UpwardWalk::new(dir)
902            .stop_below(Self::resolve_home_boundary(None))
903            .stop_at(project_root)
904            .find_map(|current| {
905                // Check rumdl config files first (higher precedence)
906                for config_name in RUMDL_CONFIG_FILES {
907                    let config_path = current.join(config_name);
908                    if config_path.exists() {
909                        if *config_name == "pyproject.toml" {
910                            if let Ok(content) = std::fs::read_to_string(&config_path)
911                                && pyproject_declares_rumdl_config(&content)
912                            {
913                                return Some(config_path);
914                            }
915                            continue;
916                        }
917                        return Some(config_path);
918                    }
919                }
920
921                // Check markdownlint config files (lower precedence)
922                MARKDOWNLINT_CONFIG_FILES
923                    .iter()
924                    .map(|name| current.join(name))
925                    .find(|path| path.exists())
926            })
927    }
928
929    /// Load a config from a specific file path, with extends resolution, returning
930    /// the still-`Loaded` `SourcedConfig` (before validation and conversion).
931    ///
932    /// Used by per-directory resolution so the caller can layer CLI-level overrides
933    /// (e.g. inline `--config`) on top before converting to `Config`, matching the
934    /// precedence applied to the global config.
935    pub fn load_sourced_for_path(
936        config_path: &Path,
937        project_root: &Path,
938    ) -> Result<SourcedConfig<ConfigLoaded>, ConfigError> {
939        let mut sourced_config = SourcedConfig {
940            project_root: Some(project_root.to_path_buf()),
941            ..SourcedConfig::default()
942        };
943
944        let filename = config_path.file_name().and_then(|n| n.to_str()).unwrap_or("");
945        let path_str = config_path.display().to_string();
946
947        // Determine if this is a markdownlint config or rumdl config
948        let is_markdownlint = MARKDOWNLINT_CONFIG_FILES.contains(&filename)
949            || (filename != "pyproject.toml"
950                && filename != ".rumdl.toml"
951                && filename != "rumdl.toml"
952                && (path_str.ends_with(".json")
953                    || path_str.ends_with(".jsonc")
954                    || path_str.ends_with(".yaml")
955                    || path_str.ends_with(".yml")));
956
957        if is_markdownlint {
958            let fragment = parsers::load_from_markdownlint(&path_str)?;
959            sourced_config.merge(fragment);
960            sourced_config.loaded_files.push(path_str);
961        } else {
962            let mut visited = IndexSet::new();
963            let chain_source = source_from_filename(filename);
964            load_config_with_extends(&mut sourced_config, config_path, &mut visited, chain_source)?;
965        }
966
967        Ok(sourced_config)
968    }
969
970    /// Load a config from a specific file path, with extends resolution, and convert
971    /// to `Config`. Used for per-directory config loading where each subdirectory
972    /// config is standalone.
973    pub fn load_config_for_path(config_path: &Path, project_root: &Path) -> Result<Config, ConfigError> {
974        Ok(Self::load_sourced_for_path(config_path, project_root)?
975            .into_validated_unchecked()
976            .into())
977    }
978}
979
980/// Convert a validated configuration to the final Config type.
981///
982/// This implementation only exists for `SourcedConfig<ConfigValidated>`,
983/// ensuring that validation must occur before conversion.
984impl From<SourcedConfig<ConfigValidated>> for Config {
985    fn from(sourced: SourcedConfig<ConfigValidated>) -> Self {
986        let mut rules = BTreeMap::new();
987        for (rule_name, sourced_rule_cfg) in sourced.rules {
988            // Normalize rule name to uppercase for case-insensitive lookup
989            let normalized_rule_name = rule_name.to_ascii_uppercase();
990            let severity = sourced_rule_cfg.severity.map(|sv| sv.value);
991            let mut values = BTreeMap::new();
992            for (key, sourced_val) in sourced_rule_cfg.values {
993                values.insert(key, sourced_val.value);
994            }
995            rules.insert(normalized_rule_name, RuleConfig { severity, values });
996        }
997        // Enable is "explicit" if it was set by something other than the Default source
998        let enable_is_explicit = sourced.global.enable.source != ConfigSource::Default;
999
1000        #[allow(deprecated)]
1001        let global = GlobalConfig {
1002            enable: sourced.global.enable.value,
1003            disable: sourced.global.disable.value,
1004            exclude: sourced.global.exclude.value,
1005            include: sourced.global.include.value,
1006            respect_gitignore: sourced.global.respect_gitignore.value,
1007            line_length: sourced.global.line_length.value,
1008            output_format: sourced.global.output_format.as_ref().map(|v| v.value.clone()),
1009            fixable: sourced.global.fixable.value,
1010            unfixable: sourced.global.unfixable.value,
1011            flavor: sourced.global.flavor.value,
1012            force_exclude: sourced.global.force_exclude.value,
1013            cache_dir: sourced.global.cache_dir.as_ref().map(|v| v.value.clone()),
1014            cache: sourced.global.cache.value,
1015            extend_enable: sourced.global.extend_enable.value,
1016            extend_disable: sourced.global.extend_disable.value,
1017            enable_is_explicit,
1018        };
1019
1020        let mut config = Config {
1021            extends: None,
1022            global,
1023            per_file_ignores: sourced.per_file_ignores.value,
1024            per_file_flavor: sourced.per_file_flavor.value,
1025            code_block_tools: sourced.code_block_tools.value,
1026            rules,
1027            project_root: sourced.project_root,
1028            per_file_ignores_cache: Arc::new(OnceLock::new()),
1029            per_file_flavor_cache: Arc::new(OnceLock::new()),
1030            canonical_project_root_cache: Arc::new(OnceLock::new()),
1031        };
1032
1033        // Apply per-rule `enabled = true/false` to global enable/disable lists
1034        config.apply_per_rule_enabled();
1035
1036        // Enforce the runtime invariant: every rule-name list is canonicalised.
1037        // After this point, downstream consumers (`rules::filter_rules`, the LSP,
1038        // WASM, fix coordinator, per-file-ignores) can match against
1039        // `Rule::name()` with simple string equality regardless of whether the
1040        // user's config used canonical IDs (`"MD033"`) or aliases
1041        // (`"no-inline-html"`).
1042        config.canonicalize_rule_lists();
1043
1044        config
1045    }
1046}
1047
1048#[cfg(test)]
1049mod tests {
1050    use super::pyproject_declares_rumdl_config;
1051
1052    #[test]
1053    fn detects_flat_and_dotted_rumdl_sections() {
1054        assert!(pyproject_declares_rumdl_config("[tool.rumdl]\nline-length = 80\n"));
1055        // Dotted sections are valid on their own, without a flat header.
1056        assert!(pyproject_declares_rumdl_config(
1057            "[tool.rumdl.MD013]\nstyle = \"fixed\"\n"
1058        ));
1059        assert!(pyproject_declares_rumdl_config(
1060            "[tool.rumdl.rules.MD007]\nindent = 4\n"
1061        ));
1062    }
1063
1064    #[test]
1065    fn ignores_incidental_mentions() {
1066        // A bare `tool.rumdl` in a comment or string value must not be treated
1067        // as a config section.
1068        assert!(!pyproject_declares_rumdl_config("# configure tool.rumdl later\n"));
1069        assert!(!pyproject_declares_rumdl_config(
1070            "[project]\ndependencies = [\"tool.rumdl-helper\"]\n"
1071        ));
1072        assert!(!pyproject_declares_rumdl_config("[tool.black]\nline-length = 88\n"));
1073    }
1074
1075    /// Discovery must stop at the project root even when the root is supplied in
1076    /// a different path representation than the walked directory's ancestors.
1077    ///
1078    /// This reproduces the Windows 8.3-short-name / canonical mismatch using a
1079    /// Unix symlink: the project root is passed as a symlink to the real root, so
1080    /// it does not string-match the canonical ancestors of the starting
1081    /// directory. Without canonicalization the walk overshoots the project root
1082    /// and incorrectly picks up the config in the parent directory.
1083    #[cfg(unix)]
1084    #[test]
1085    fn discover_stops_at_project_root_across_path_representations() {
1086        use super::SourcedConfig;
1087        use std::os::unix::fs::symlink;
1088        use tempfile::tempdir;
1089
1090        let tmp = tempdir().unwrap();
1091        // A config ABOVE the project root that must never be discovered.
1092        std::fs::write(tmp.path().join(".rumdl.toml"), "").unwrap();
1093
1094        let real_root = tmp.path().join("project");
1095        let subdir = real_root.join("docs");
1096        std::fs::create_dir_all(&subdir).unwrap();
1097
1098        // Supply the project root via a symlink so it does not string-match the
1099        // canonical ancestors of `subdir`.
1100        let linked_root = tmp.path().join("project-link");
1101        symlink(&real_root, &linked_root).unwrap();
1102
1103        let found = SourcedConfig::discover_config_for_dir(&subdir, &linked_root);
1104        assert_eq!(
1105            found, None,
1106            "discovery must stop at the project root, not overshoot to the parent config"
1107        );
1108    }
1109
1110    mod shadowed_configs {
1111        use super::super::{ShadowedConfigs, detect_shadowed_configs, format_shadow_warning, rumdl_configs_in_dir};
1112        use tempfile::tempdir;
1113
1114        fn names(paths: &[std::path::PathBuf]) -> Vec<String> {
1115            paths
1116                .iter()
1117                .map(|p| {
1118                    // Use the last two components so `.config/rumdl.toml` is distinguishable
1119                    // from a top-level `rumdl.toml` without depending on the temp dir prefix.
1120                    let file = p.file_name().and_then(|n| n.to_str()).unwrap_or_default();
1121                    let parent = p.parent().and_then(|d| d.file_name()).and_then(|n| n.to_str());
1122                    match parent {
1123                        Some(".config") => format!(".config/{file}"),
1124                        _ => file.to_string(),
1125                    }
1126                })
1127                .collect()
1128        }
1129
1130        #[test]
1131        fn empty_directory_has_no_configs_and_no_shadow() {
1132            let tmp = tempdir().unwrap();
1133            assert!(rumdl_configs_in_dir(tmp.path()).is_empty());
1134            assert!(detect_shadowed_configs(tmp.path()).is_none());
1135        }
1136
1137        #[test]
1138        fn single_config_does_not_shadow() {
1139            let tmp = tempdir().unwrap();
1140            std::fs::write(tmp.path().join(".rumdl.toml"), "").unwrap();
1141            assert_eq!(names(&rumdl_configs_in_dir(tmp.path())), vec![".rumdl.toml"]);
1142            assert!(detect_shadowed_configs(tmp.path()).is_none());
1143        }
1144
1145        #[test]
1146        fn dot_wins_over_non_dot_and_non_dot_is_shadowed() {
1147            let tmp = tempdir().unwrap();
1148            std::fs::write(tmp.path().join(".rumdl.toml"), "").unwrap();
1149            std::fs::write(tmp.path().join("rumdl.toml"), "").unwrap();
1150
1151            let ShadowedConfigs { winner, shadowed, .. } = detect_shadowed_configs(tmp.path()).unwrap();
1152            assert_eq!(names(&[winner]), vec![".rumdl.toml"]);
1153            assert_eq!(names(&shadowed), vec!["rumdl.toml"]);
1154        }
1155
1156        #[test]
1157        fn config_subdir_counts_as_same_level_shadow() {
1158            let tmp = tempdir().unwrap();
1159            std::fs::write(tmp.path().join(".rumdl.toml"), "").unwrap();
1160            std::fs::create_dir_all(tmp.path().join(".config")).unwrap();
1161            std::fs::write(tmp.path().join(".config/rumdl.toml"), "").unwrap();
1162
1163            let ShadowedConfigs { winner, shadowed, .. } = detect_shadowed_configs(tmp.path()).unwrap();
1164            assert_eq!(names(&[winner]), vec![".rumdl.toml"]);
1165            assert_eq!(names(&shadowed), vec![".config/rumdl.toml"]);
1166        }
1167
1168        #[test]
1169        fn pyproject_counts_only_when_it_declares_rumdl() {
1170            // pyproject WITHOUT [tool.rumdl] is not a rumdl config source -> no shadow.
1171            let bare = tempdir().unwrap();
1172            std::fs::write(bare.path().join(".rumdl.toml"), "").unwrap();
1173            std::fs::write(bare.path().join("pyproject.toml"), "[tool.black]\nline-length = 88\n").unwrap();
1174            assert_eq!(names(&rumdl_configs_in_dir(bare.path())), vec![".rumdl.toml"]);
1175            assert!(detect_shadowed_configs(bare.path()).is_none());
1176
1177            // pyproject WITH [tool.rumdl] is a real shadowed source.
1178            let declared = tempdir().unwrap();
1179            std::fs::write(declared.path().join(".rumdl.toml"), "").unwrap();
1180            std::fs::write(
1181                declared.path().join("pyproject.toml"),
1182                "[tool.rumdl]\nline-length = 80\n",
1183            )
1184            .unwrap();
1185            let ShadowedConfigs { winner, shadowed, .. } = detect_shadowed_configs(declared.path()).unwrap();
1186            assert_eq!(names(&[winner]), vec![".rumdl.toml"]);
1187            assert_eq!(names(&shadowed), vec!["pyproject.toml"]);
1188        }
1189
1190        #[test]
1191        fn markdownlint_configs_are_not_rumdl_native_and_never_shadow() {
1192            let tmp = tempdir().unwrap();
1193            std::fs::write(tmp.path().join(".rumdl.toml"), "").unwrap();
1194            std::fs::write(tmp.path().join(".markdownlint.json"), "{}").unwrap();
1195            assert_eq!(names(&rumdl_configs_in_dir(tmp.path())), vec![".rumdl.toml"]);
1196            assert!(detect_shadowed_configs(tmp.path()).is_none());
1197        }
1198
1199        #[test]
1200        fn configs_returned_in_precedence_order() {
1201            let tmp = tempdir().unwrap();
1202            std::fs::write(tmp.path().join(".rumdl.toml"), "").unwrap();
1203            std::fs::write(tmp.path().join("rumdl.toml"), "").unwrap();
1204            std::fs::create_dir_all(tmp.path().join(".config")).unwrap();
1205            std::fs::write(tmp.path().join(".config/rumdl.toml"), "").unwrap();
1206            std::fs::write(tmp.path().join("pyproject.toml"), "[tool.rumdl]\n").unwrap();
1207
1208            assert_eq!(
1209                names(&rumdl_configs_in_dir(tmp.path())),
1210                vec![".rumdl.toml", "rumdl.toml", ".config/rumdl.toml", "pyproject.toml"]
1211            );
1212        }
1213
1214        #[test]
1215        fn warning_names_dir_once_with_relative_filenames() {
1216            let tmp = tempdir().unwrap();
1217            std::fs::write(tmp.path().join(".rumdl.toml"), "").unwrap();
1218            std::fs::write(tmp.path().join("rumdl.toml"), "").unwrap();
1219            std::fs::write(tmp.path().join("pyproject.toml"), "[tool.rumdl]\n").unwrap();
1220
1221            let shadow = detect_shadowed_configs(tmp.path()).unwrap();
1222            let msg = format_shadow_warning(&shadow);
1223
1224            let dir = {
1225                let s = tmp.path().to_string_lossy().into_owned();
1226                if cfg!(windows) { s.replace('\\', "/") } else { s }
1227            };
1228            assert!(msg.contains("multiple rumdl config files"), "got: {msg}");
1229            // The directory is named once; files are shown relative to it (no
1230            // repeated directory prefix on every path).
1231            assert_eq!(
1232                msg.matches(dir.as_str()).count(),
1233                1,
1234                "directory should appear exactly once, got: {msg}"
1235            );
1236            assert!(
1237                msg.contains("using .rumdl.toml, ignoring rumdl.toml, pyproject.toml"),
1238                "winner and shadowed files should be relative names in precedence order, got: {msg}"
1239            );
1240            // Paths are normalized to forward slashes on all platforms.
1241            assert!(!msg.contains('\\'), "paths must be normalized to '/': {msg}");
1242        }
1243    }
1244}