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::{
15    Config, ConfigError, ConfigOrigin, DiscoveredConfigError, GlobalConfig, MARKDOWNLINT_CONFIG_FILES,
16    RUMDL_CONFIG_FILES, RuleConfig, WITHHELD,
17};
18use super::validation::validate_config_sourced_internal;
19use crate::utils::upward_walk::UpwardWalk;
20
21/// Maximum depth for extends chains to prevent runaway recursion
22const MAX_EXTENDS_DEPTH: usize = 10;
23
24/// Cheap pre-filter for whether a `pyproject.toml` declares rumdl config.
25///
26/// Matches the flat section header `[tool.rumdl]` as well as dotted sections
27/// like `[tool.rumdl.MD013]` or `[tool.rumdl.rules.MD007]` (which are valid on
28/// their own, without a flat header). Requiring the leading `[` avoids matching
29/// a bare `tool.rumdl` in prose or dependency names; a literal `[tool.rumdl...`
30/// inside a comment or string would still match, but the subsequent parse
31/// handles that gracefully.
32fn pyproject_declares_rumdl_config(content: &str) -> bool {
33    content.contains("[tool.rumdl]") || content.contains("[tool.rumdl.")
34}
35
36/// True if `b` may start a `$VAR` identifier (`[A-Za-z_]`).
37fn is_var_name_start(b: u8) -> bool {
38    b == b'_' || b.is_ascii_alphabetic()
39}
40
41/// True if `b` may continue a `$VAR` identifier (`[A-Za-z0-9_]`).
42fn is_var_name_continue(b: u8) -> bool {
43    b == b'_' || b.is_ascii_alphanumeric()
44}
45
46/// True if `name` is a non-empty valid environment-variable identifier.
47fn is_valid_var_name(name: &str) -> bool {
48    let bytes = name.as_bytes();
49    !bytes.is_empty() && is_var_name_start(bytes[0]) && bytes[1..].iter().all(|&b| is_var_name_continue(b))
50}
51
52/// Expand `$VAR` and `${VAR}` references in `input` using `lookup`.
53///
54/// Grammar (frozen; documented in `docs/global-settings.md`):
55/// - `$NAME` / `${NAME}` with `NAME = [A-Za-z_][A-Za-z0-9_]*` expands to the variable's
56///   value; the longest valid identifier is matched (`$FOO_BAR` is one name).
57/// - `$$` is a literal `$` (escape), so `$$VAR` -> `$VAR` and `$${VAR}` -> `${VAR}` (no
58///   expansion of the escaped form).
59/// - Any other `$` is left literal: `$` before a non-identifier char (`$5`, trailing `$`),
60///   an empty `${}`, an unterminated `${VAR`, or a `${...}` whose body is not a valid
61///   identifier (e.g. nested `${A${B}}`) - the whole `${...}` span up to the first `}` is
62///   emitted literally.
63/// - Replacement values are inserted literally and are NOT re-scanned (single left-to-right
64///   pass): if `A="$B"`, then `$A` expands to the literal string `$B`.
65///
66/// Returns `Err(name)` on the first well-formed reference to an undefined variable. All
67/// special characters (`$`, `{`, `}`, identifier chars) are ASCII, so byte scanning never
68/// splits a multibyte UTF-8 sequence; non-ASCII bytes are copied verbatim as literals.
69fn expand_env_vars(input: &str, lookup: impl Fn(&str) -> Option<String>) -> Result<String, String> {
70    let bytes = input.as_bytes();
71    let mut out = String::with_capacity(input.len());
72    let mut i = 0;
73
74    while i < bytes.len() {
75        if bytes[i] != b'$' {
76            // Copy the maximal run of non-`$` bytes as a slice (preserves UTF-8).
77            let start = i;
78            while i < bytes.len() && bytes[i] != b'$' {
79                i += 1;
80            }
81            out.push_str(&input[start..i]);
82            continue;
83        }
84
85        match bytes.get(i + 1).copied() {
86            // `$$` -> literal `$`.
87            Some(b'$') => {
88                out.push('$');
89                i += 2;
90            }
91            // `${...}` braced form.
92            Some(b'{') => {
93                if let Some(rel) = input[i + 2..].find('}') {
94                    let close = i + 2 + rel;
95                    let name = &input[i + 2..close];
96                    if is_valid_var_name(name) {
97                        match lookup(name) {
98                            Some(value) => out.push_str(&value),
99                            None => return Err(name.to_string()),
100                        }
101                    } else {
102                        // Empty / invalid / nested body -> whole `${...}` span is literal.
103                        out.push_str(&input[i..=close]);
104                    }
105                    i = close + 1;
106                } else {
107                    // No closing `}` -> leave the `$` literal and resume at `{`.
108                    out.push('$');
109                    i += 1;
110                }
111            }
112            // `$NAME` bare form.
113            Some(b) if is_var_name_start(b) => {
114                let start = i + 1;
115                let mut j = start;
116                while j < bytes.len() && is_var_name_continue(bytes[j]) {
117                    j += 1;
118                }
119                let name = &input[start..j];
120                match lookup(name) {
121                    Some(value) => out.push_str(&value),
122                    None => return Err(name.to_string()),
123                }
124                i = j;
125            }
126            // `$` before a non-identifier char or at end of input -> literal `$`.
127            _ => {
128                out.push('$');
129                i += 1;
130            }
131        }
132    }
133
134    Ok(out)
135}
136
137/// An `extends` value as the user wrote it, so the file it reaches can be named
138/// without disclosing what the value expanded to. See [`ConfigOrigin`] for why
139/// that matters.
140///
141/// Nothing here needs the environment variables the value substitutes: the only
142/// forms expanded are `$NAME` and `${NAME}`, both of which stand verbatim in the
143/// written value, so naming the reference already names them.
144struct ExtendsRef {
145    /// The value exactly as written in the declaring config file, and `None`
146    /// when that file was itself reached through `extends`. An `extends` value is
147    /// a line of the file that wrote it, so a file whose unknown keys and invalid
148    /// values are withheld does not get to have this one line quoted instead.
149    written: Option<String>,
150    /// The short name of the config file that declared this `extends`, so a
151    /// chain of substituted paths never surfaces at any depth.
152    from: String,
153}
154
155impl ExtendsRef {
156    /// The reference as a message about the file it reaches should name it.
157    fn describe(&self) -> String {
158        format!("{} (referenced from {})", self.short(), self.from)
159    }
160
161    /// The reference alone, for a message that only has to say which file it
162    /// means. See [`ConfigOrigin::short_name`].
163    ///
164    /// A withheld reference names nothing, so two of them in one chain read
165    /// alike. What locates the problem is still there: the file that declared it,
166    /// which whoever hit the error can open.
167    fn short(&self) -> String {
168        match &self.written {
169            Some(written) => format!("'{written}'"),
170            None => WITHHELD.to_string(),
171        }
172    }
173}
174
175/// The `extends` chain walked so far.
176///
177/// `visited` holds canonicalized paths, which is what cycle detection needs.
178/// `names` holds how each file was reached, which is what a message about the
179/// chain may show: a path in it can hold expanded environment variables.
180#[derive(Default)]
181struct ExtendsChain {
182    visited: IndexSet<PathBuf>,
183    names: Vec<String>,
184}
185
186impl ExtendsChain {
187    fn contains(&self, canonical: &Path) -> bool {
188        self.visited.contains(canonical)
189    }
190
191    fn len(&self) -> usize {
192        self.visited.len()
193    }
194
195    fn push(&mut self, canonical: PathBuf, name: String) {
196        self.visited.insert(canonical);
197        self.names.push(name);
198    }
199
200    fn names(&self) -> Vec<String> {
201        self.names.clone()
202    }
203}
204
205/// Resolve an `extends` value against the config file that declares it, and
206/// describe the reference for any message about the file it reaches.
207///
208/// - `$VAR` / `${VAR}`: expanded from the environment first (see [`expand_env_vars`])
209/// - `~/` prefix: expanded to home directory
210/// - Relative paths: resolved against the config file's parent directory
211/// - Absolute paths: used as-is
212///
213/// `declared_by` is the origin of the file holding the value, which decides
214/// whether the value may be quoted: it is that file's text like any other.
215fn resolve_extends(
216    extends_value: &str,
217    config_file_path: &Path,
218    from: &str,
219    declared_by: ConfigOrigin<'_>,
220) -> Result<(PathBuf, ExtendsRef), ConfigError> {
221    let expanded = expand_env_vars(extends_value, |key| std::env::var(key).ok()).map_err(|var| {
222        // The variable name is written in the same value, so it is quotable
223        // exactly when the value is. Withholding it where the value is withheld
224        // also keeps a set and an unset variable from telling different amounts.
225        ConfigError::ExtendsUndefinedVar {
226            var: if declared_by.may_quote_contents() {
227                format!("${var}")
228            } else {
229                WITHHELD.to_string()
230            },
231            from: from.to_string(),
232        }
233    })?;
234
235    let reference = ExtendsRef {
236        written: declared_by.may_quote_contents().then(|| extends_value.to_string()),
237        from: from.to_string(),
238    };
239
240    Ok((resolve_expanded_extends_path(&expanded, config_file_path), reference))
241}
242
243/// Turn an already-expanded `extends` value into a path.
244fn resolve_expanded_extends_path(expanded: &str, config_file_path: &Path) -> PathBuf {
245    if let Some(suffix) = expanded.strip_prefix("~/") {
246        // Expand tilde to home directory
247        #[cfg(feature = "native")]
248        {
249            use etcetera::{BaseStrategy, choose_base_strategy};
250            let home = choose_base_strategy().map_or_else(|_| PathBuf::from("~"), |s| s.home_dir().to_path_buf());
251            home.join(suffix)
252        }
253        #[cfg(not(feature = "native"))]
254        {
255            let _ = suffix;
256            PathBuf::from(expanded)
257        }
258    } else {
259        let path = PathBuf::from(expanded);
260        if path.is_absolute() {
261            path
262        } else {
263            // Resolve relative to config file's directory
264            let config_dir = config_file_path.parent().unwrap_or(Path::new("."));
265            config_dir.join(expanded)
266        }
267    }
268}
269
270/// Determine ConfigSource from a config filename.
271fn source_from_filename(filename: &str) -> ConfigSource {
272    if filename == "pyproject.toml" {
273        ConfigSource::PyprojectToml
274    } else {
275        ConfigSource::ProjectConfig
276    }
277}
278
279/// The rumdl-native config files that actually exist in `dir`, in precedence order.
280///
281/// Walks `RUMDL_CONFIG_FILES` (the single source of truth for discovery) joined onto
282/// `dir`, so `.config/rumdl.toml` is recognised at the same level as `.rumdl.toml`.
283/// `pyproject.toml` counts only when it declares `[tool.rumdl]`. markdownlint configs
284/// are intentionally excluded: they are a separate fallback tier, not a same-tool
285/// collision, and projects routinely keep one around while migrating.
286pub(crate) fn rumdl_configs_in_dir(dir: &Path) -> Vec<PathBuf> {
287    RUMDL_CONFIG_FILES
288        .iter()
289        .map(|name| dir.join(name))
290        .filter(|path| {
291            if !path.exists() {
292                return false;
293            }
294            if path.file_name().and_then(|n| n.to_str()) == Some("pyproject.toml") {
295                std::fs::read_to_string(path).is_ok_and(|content| pyproject_declares_rumdl_config(&content))
296            } else {
297                true
298            }
299        })
300        .collect()
301}
302
303/// Collect project configuration candidates nearest-first.
304///
305/// Rumdl-native files precede markdownlint files within each directory. The
306/// walk includes `workspace_root`, when supplied, and excludes `home_dir`: a
307/// config in the home directory is user configuration rather than project
308/// configuration. Returning every candidate lets adapters recover from a
309/// malformed higher-precedence file without reimplementing discovery policy.
310pub(crate) fn collect_project_config_candidates(
311    search_dir: &Path,
312    workspace_root: Option<&Path>,
313    home_dir: Option<&Path>,
314) -> Vec<PathBuf> {
315    let mut candidates = Vec::new();
316    let walk = UpwardWalk::new(search_dir).stop_below(home_dir.map(Path::to_path_buf));
317    let walk = match workspace_root {
318        Some(root) => walk.stop_at(root),
319        None => walk,
320    };
321
322    for current_dir in walk {
323        candidates.extend(rumdl_configs_in_dir(&current_dir));
324        candidates.extend(
325            MARKDOWNLINT_CONFIG_FILES
326                .iter()
327                .map(|name| current_dir.join(name))
328                .filter(|path| path.exists()),
329        );
330    }
331
332    candidates
333}
334
335/// A directory holding more than one rumdl-native config file.
336///
337/// `winner` is the file discovery uses (highest precedence); `shadowed` are the
338/// silently-ignored siblings. Having both `.rumdl.toml` and `rumdl.toml` (or either
339/// plus a `[tool.rumdl]` in `pyproject.toml`) in one directory is redundant by
340/// construction and a common footgun: editing the shadowed file appears to do
341/// nothing. Resolution is unchanged (the dot file still wins, matching Ruff); this
342/// type only lets callers surface the collision.
343#[derive(Debug, Clone, PartialEq, Eq)]
344pub(crate) struct ShadowedConfigs {
345    pub dir: PathBuf,
346    pub winner: PathBuf,
347    pub shadowed: Vec<PathBuf>,
348}
349
350/// Detect rumdl-native config files that shadow each other in `dir`.
351///
352/// Returns `None` unless two or more rumdl-native configs coexist at this directory
353/// level (markdownlint files and configs in other directories never count). The
354/// highest-precedence file is the `winner`; the rest are silently `shadowed`.
355pub(crate) fn detect_shadowed_configs(dir: &Path) -> Option<ShadowedConfigs> {
356    let mut configs = rumdl_configs_in_dir(dir);
357    if configs.len() < 2 {
358        return None;
359    }
360    let winner = configs.remove(0);
361    Some(ShadowedConfigs {
362        dir: dir.to_path_buf(),
363        winner,
364        shadowed: configs,
365    })
366}
367
368/// Format a shadowed-config collision as a single user-facing warning line.
369///
370/// The directory is named once; the winner and shadowed files are shown relative
371/// to it (e.g. `.rumdl.toml`, `.config/rumdl.toml`) rather than repeating the full
372/// directory in every path. Paths are normalized to forward slashes on Windows for
373/// stable, copy-pasteable output; non-UTF-8 components degrade lossily rather than
374/// panicking.
375pub(crate) fn format_shadow_warning(shadow: &ShadowedConfigs) -> String {
376    let norm = |s: String| if cfg!(windows) { s.replace('\\', "/") } else { s };
377    let rel = |path: &Path| {
378        let relative = path.strip_prefix(&shadow.dir).unwrap_or(path);
379        norm(relative.to_string_lossy().into_owned())
380    };
381    let shadowed = shadow.shadowed.iter().map(|p| rel(p)).collect::<Vec<_>>().join(", ");
382    format!(
383        "multiple rumdl config files in {}: using {}, ignoring {}",
384        norm(shadow.dir.to_string_lossy().into_owned()),
385        rel(&shadow.winner),
386        shadowed,
387    )
388}
389
390/// Load a config file (and any base configs it extends) into a SourcedConfig.
391///
392/// This function handles the recursive `extends` chain:
393/// 1. Parse the config file into a fragment
394/// 2. If the fragment has `extends`, recursively load the base config first
395/// 3. Merge the base config, then merge this fragment on top
396///
397/// `origin` says how this file was reached, and every message about it is
398/// phrased through that: a file the recursion reached is named by the `extends`
399/// value that names it, never by the path that value expanded to. The resolved
400/// path is still recorded in [`SourcedConfig::loaded_files`], which is not a
401/// message about the file but the answer to a question someone asked about the
402/// configuration. See [`ConfigOrigin`].
403fn load_config_with_extends(
404    sourced_config: &mut SourcedConfig<ConfigLoaded>,
405    config_file_path: &Path,
406    chain: &mut ExtendsChain,
407    chain_source: ConfigSource,
408    origin: ConfigOrigin<'_>,
409) -> Result<(), ConfigError> {
410    // Canonicalize the path for circular reference detection
411    let canonical = config_file_path
412        .canonicalize()
413        .unwrap_or_else(|_| config_file_path.to_path_buf());
414
415    let path_str = config_file_path.display().to_string();
416    let described = origin.display_name(&path_str);
417    let short = origin.short_name(&path_str);
418
419    // Check for circular references
420    if chain.contains(&canonical) {
421        return Err(ConfigError::CircularExtends {
422            path: described,
423            chain: chain.names(),
424        });
425    }
426
427    // Check depth limit
428    if chain.len() >= MAX_EXTENDS_DEPTH {
429        return Err(ConfigError::ExtendsDepthExceeded {
430            path: described,
431            max_depth: MAX_EXTENDS_DEPTH,
432        });
433    }
434
435    // Mark as visited
436    chain.push(canonical, short.clone());
437
438    let filename = config_file_path.file_name().and_then(|n| n.to_str()).unwrap_or("");
439
440    // Read and parse the config file
441    let content = std::fs::read_to_string(config_file_path).map_err(|e| ConfigError::IoError {
442        source: e,
443        path: described.clone(),
444    })?;
445
446    let fragment = if filename == "pyproject.toml" {
447        match parsers::parse_pyproject_toml(&content, &path_str, chain_source, origin)? {
448            Some(f) => f,
449            None => return Ok(()), // No [tool.rumdl] section
450        }
451    } else {
452        parsers::parse_rumdl_toml(&content, &path_str, chain_source, origin)?
453    };
454
455    // If this fragment has `extends`, load the base config first
456    if let Some(ref extends_value) = fragment.extends {
457        let (base_path, reference) = resolve_extends(extends_value, config_file_path, &short, origin)?;
458        let base_described = reference.describe();
459        let base_short = reference.short();
460
461        if !base_path.exists() {
462            return Err(ConfigError::ExtendsNotFound {
463                path: base_short,
464                from: short,
465            });
466        }
467
468        log::debug!(
469            "[rumdl-config] Config {} extends {}, loading base first",
470            path_str,
471            base_path.display()
472        );
473
474        // Recursively load the base config
475        load_config_with_extends(
476            sourced_config,
477            &base_path,
478            chain,
479            chain_source,
480            ConfigOrigin::Extends {
481                described_as: &base_described,
482                short_name: &base_short,
483            },
484        )?;
485    }
486
487    // Merge this fragment on top (base config was already merged if present)
488    // Strip the `extends` field since it's been consumed
489    let mut fragment_for_merge = fragment;
490    fragment_for_merge.extends = None;
491    sourced_config.merge(fragment_for_merge);
492    sourced_config.loaded_files.push(path_str);
493
494    Ok(())
495}
496
497impl SourcedConfig<ConfigLoaded> {
498    /// Merges another SourcedConfigFragment into this SourcedConfig.
499    /// Uses source precedence to determine which values take effect.
500    pub(super) fn merge(&mut self, fragment: SourcedConfigFragment) {
501        // Merge global config. Enable/disable use replace semantics (child
502        // config overrides parent, matching Ruff's `select`/`ignore`);
503        // extend-enable/extend-disable use union semantics (additive across
504        // config levels).
505        self.global.enable.merge_from(fragment.global.enable);
506        self.global.disable.merge_from(fragment.global.disable);
507        self.global
508            .extend_enable
509            .merge_union_from(fragment.global.extend_enable);
510        self.global
511            .extend_disable
512            .merge_union_from(fragment.global.extend_disable);
513
514        // Conflict resolution: Enable overrides disable
515        // Remove any rules from disable that appear in enable
516        self.global
517            .disable
518            .value
519            .retain(|rule| !self.global.enable.value.contains(rule));
520
521        // Whether a message about the include patterns may quote them travels with
522        // the patterns: the file supplying the winning list supplies what may be
523        // said about it, in either direction.
524        if self.global.include.merge_from(fragment.global.include) {
525            self.global.include_withheld = fragment.global.include_withheld;
526        }
527        self.global.exclude.merge_from(fragment.global.exclude);
528        self.global
529            .respect_gitignore
530            .merge_from(fragment.global.respect_gitignore);
531        self.global.line_length.merge_from(fragment.global.line_length);
532        self.global.fixable.merge_from(fragment.global.fixable);
533        self.global.unfixable.merge_from(fragment.global.unfixable);
534        self.global.flavor.merge_from(fragment.global.flavor);
535        self.global.force_exclude.merge_from(fragment.global.force_exclude);
536        self.global.editorconfig.merge_from(fragment.global.editorconfig);
537
538        // Merge output_format if present
539        if let Some(output_format_fragment) = fragment.global.output_format {
540            if let Some(ref mut output_format) = self.global.output_format {
541                output_format.merge_from(output_format_fragment);
542            } else {
543                self.global.output_format = Some(output_format_fragment);
544            }
545        }
546
547        // Merge cache_dir if present
548        if let Some(cache_dir_fragment) = fragment.global.cache_dir {
549            if let Some(ref mut cache_dir) = self.global.cache_dir {
550                cache_dir.merge_from(cache_dir_fragment);
551            } else {
552                self.global.cache_dir = Some(cache_dir_fragment);
553            }
554        }
555
556        // Merge cache if not default (only override when explicitly set)
557        if fragment.global.cache.source != ConfigSource::Default {
558            self.global.cache.merge_from(fragment.global.cache);
559        }
560
561        self.per_file_ignores.merge_from(fragment.per_file_ignores);
562        self.per_file_flavor.merge_from(fragment.per_file_flavor);
563        self.code_block_tools.merge_from(fragment.code_block_tools);
564
565        // Merge rule configs
566        for (rule_name, rule_fragment) in fragment.rules {
567            let norm_rule_name = rule_name.to_ascii_uppercase(); // Normalize to uppercase for case-insensitivity
568            let rule_entry = self.rules.entry(norm_rule_name).or_default();
569
570            // Merge severity if present in fragment
571            if let Some(severity_fragment) = rule_fragment.severity {
572                if let Some(ref mut existing_severity) = rule_entry.severity {
573                    existing_severity.merge_from(severity_fragment);
574                } else {
575                    rule_entry.severity = Some(severity_fragment);
576                }
577            }
578
579            // Merge values. Whether a value may be quoted back travels with the
580            // value: a file that takes a key over also takes over what may be
581            // said about it, in either direction.
582            for (key, sourced_value_fragment) in rule_fragment.values {
583                let sv_entry = rule_entry
584                    .values
585                    .entry(key.clone())
586                    .or_insert_with(|| SourcedValue::new(sourced_value_fragment.value.clone(), ConfigSource::Default));
587                if sv_entry.merge_from(sourced_value_fragment) {
588                    if rule_fragment.withheld_keys.contains(&key) {
589                        rule_entry.withheld_keys.insert(key);
590                    } else {
591                        rule_entry.withheld_keys.remove(&key);
592                    }
593                }
594            }
595        }
596
597        // Merge unknown_keys from fragment
598        // A file reached twice through two `extends` chains has the same problem
599        // both times; the user wants to hear about it once.
600        for warning in fragment.load_warnings {
601            if !self.discovery_warnings.contains(&warning) {
602                self.discovery_warnings.push(warning);
603            }
604        }
605
606        for (section, key, file_path) in fragment.unknown_keys {
607            // Deduplicate: only add if not already present
608            if !self.unknown_keys.iter().any(|(s, k, _)| s == &section && k == &key) {
609                self.unknown_keys.push((section, key, file_path));
610            }
611        }
612    }
613
614    /// Load and merge configurations from files and CLI overrides.
615    pub fn load(config_path: Option<&str>, cli_overrides: Option<&SourcedGlobalConfig>) -> Result<Self, ConfigError> {
616        Self::load_with_discovery(config_path, cli_overrides, false)
617    }
618
619    /// Finds project root by walking up from start_dir looking for .git directory.
620    /// Falls back to start_dir if no .git found.
621    fn find_project_root_from(start_dir: &Path) -> std::path::PathBuf {
622        UpwardWalk::new(start_dir)
623            .find(|dir| dir.join(".git").exists())
624            .unwrap_or_else(|| {
625                log::debug!(
626                    "[rumdl-config] No .git found, using config location as project root: {}",
627                    start_dir.display()
628                );
629                start_dir.to_path_buf()
630            })
631    }
632
633    /// Resolve the home-directory boundary used to stop project-config discovery.
634    ///
635    /// `home_override` wins (supplied by tests); otherwise the real home is resolved on
636    /// native builds via `etcetera`. Wasm has no home/project walk to bound, so it
637    /// returns `None` there.
638    fn resolve_home_boundary(home_override: Option<&Path>) -> Option<std::path::PathBuf> {
639        home_override.map(Path::to_path_buf).or_else(|| {
640            #[cfg(feature = "native")]
641            {
642                use etcetera::{BaseStrategy, choose_base_strategy};
643                choose_base_strategy().ok().map(|s| s.home_dir().to_path_buf())
644            }
645            #[cfg(not(feature = "native"))]
646            {
647                None
648            }
649        })
650    }
651
652    /// Where an upward config walk begins.
653    ///
654    /// `start_override` is the directory a caller chose as its scope; the CLI has
655    /// none and uses the process working directory, the directory the user typed
656    /// the command in.
657    fn resolve_discovery_start(start_override: Option<&Path>) -> Option<std::path::PathBuf> {
658        if let Some(dir) = start_override {
659            return Some(dir.to_path_buf());
660        }
661        match std::env::current_dir() {
662            Ok(dir) => Some(dir),
663            Err(e) => {
664                log::debug!("[rumdl-config] Failed to get current directory: {e}");
665                None
666            }
667        }
668    }
669
670    /// Discover configuration file by traversing up the directory tree.
671    /// Returns the first configuration file found.
672    /// Discovers config file and returns both the config path and project root.
673    /// Returns: (config_file_path, project_root_path)
674    /// Project root is the directory containing .git, or config parent as fallback.
675    ///
676    /// The walk stops at the home directory: a config file located in `$HOME`
677    /// itself is user-level, not a project config, and must reach the loader only
678    /// through the user-config fallback (`load_user_config`) so the platform
679    /// user-config directory keeps precedence over `~/.rumdl.toml`. The start
680    /// directory is exempt from that boundary: it is an explicitly chosen project
681    /// context, so its configs apply even when it *is* `$HOME` (pre-commit.ci sets
682    /// `HOME` to the git checkout, and `pyproject.toml` has no user-config
683    /// fallback). `home_override` supplies the boundary for tests; production
684    /// resolves the real home directory.
685    fn discover_config_upward(
686        start_override: Option<&Path>,
687        home_override: Option<&Path>,
688    ) -> Option<(std::path::PathBuf, std::path::PathBuf, Option<ShadowedConfigs>)> {
689        let start_dir = Self::resolve_discovery_start(start_override)?;
690
691        // `rumdl_configs_in_dir` is the single source of truth for "which rumdl
692        // configs live here", shared with the LSP and the shadow detector, so the
693        // winner and the silently-shadowed siblings are computed identically.
694        let (config_path, config_dir, shadow) = UpwardWalk::new(&start_dir)
695            .stop_below(Self::resolve_home_boundary(home_override))
696            .always_yield_start()
697            .stop_at_git_root()
698            .find_map(|dir| {
699                rumdl_configs_in_dir(&dir).into_iter().next().map(|winner| {
700                    log::debug!("[rumdl-config] Found config file: {}", winner.display());
701                    let shadow = detect_shadowed_configs(&dir);
702                    (winner, dir, shadow)
703                })
704            })?;
705
706        // Determine project root by walking up from the config location.
707        let project_root = Self::find_project_root_from(&config_dir);
708        Some((config_path, project_root, shadow))
709    }
710
711    /// Discover markdownlint configuration file by traversing up the directory tree.
712    /// Similar to discover_config_upward but for .markdownlint.yaml/json files, and
713    /// bounded at the home directory for the same reason: a markdownlint config in
714    /// `$HOME` is user-level, not a project config. The start directory is exempt
715    /// from the boundary just like rumdl config discovery, and markdownlint files
716    /// have no user-config fallback at all, so without the exemption a config in a
717    /// checkout that is itself `$HOME` would be ignored entirely.
718    fn discover_markdownlint_config_upward(
719        start_override: Option<&Path>,
720        home_override: Option<&Path>,
721    ) -> Option<std::path::PathBuf> {
722        let start_dir = Self::resolve_discovery_start(start_override)?;
723
724        UpwardWalk::new(&start_dir)
725            .stop_below(Self::resolve_home_boundary(home_override))
726            .always_yield_start()
727            .stop_at_git_root()
728            .find_map(|dir| {
729                MARKDOWNLINT_CONFIG_FILES
730                    .iter()
731                    .map(|name| dir.join(name))
732                    .find(|path| path.exists())
733            })
734    }
735
736    /// Internal implementation that accepts config directory for testing
737    fn user_configuration_path_impl(config_dir: &Path) -> Option<std::path::PathBuf> {
738        let config_dir = config_dir.join("rumdl");
739
740        // Check for config files in precedence order (same as project discovery)
741        const USER_CONFIG_FILES: &[&str] = &[".rumdl.toml", "rumdl.toml", "pyproject.toml"];
742
743        log::debug!(
744            "[rumdl-config] Checking for user configuration in: {}",
745            config_dir.display()
746        );
747
748        for filename in USER_CONFIG_FILES {
749            let config_path = config_dir.join(filename);
750
751            if config_path.exists() {
752                // For pyproject.toml, verify it contains [tool.rumdl] section
753                if *filename == "pyproject.toml" {
754                    if let Ok(content) = std::fs::read_to_string(&config_path) {
755                        if pyproject_declares_rumdl_config(&content) {
756                            log::debug!("[rumdl-config] Found user configuration at: {}", config_path.display());
757                            return Some(config_path);
758                        }
759                        log::debug!("[rumdl-config] Found user pyproject.toml but no [tool.rumdl] section");
760                        continue;
761                    }
762                } else {
763                    log::debug!("[rumdl-config] Found user configuration at: {}", config_path.display());
764                    return Some(config_path);
765                }
766            }
767        }
768
769        log::debug!(
770            "[rumdl-config] No user configuration found in: {}",
771            config_dir.display()
772        );
773        None
774    }
775
776    /// Discover user-level configuration file from platform-specific config directory.
777    /// Returns the first configuration file found in the user config directory.
778    #[cfg(feature = "native")]
779    fn user_configuration_path() -> Option<std::path::PathBuf> {
780        use etcetera::{BaseStrategy, choose_base_strategy};
781
782        match choose_base_strategy() {
783            Ok(strategy) => {
784                let config_dir = strategy.config_dir();
785                Self::user_configuration_path_impl(&config_dir)
786            }
787            Err(e) => {
788                log::debug!("[rumdl-config] Failed to determine user config directory: {e}");
789                None
790            }
791        }
792    }
793
794    /// Stub for WASM builds - user config not supported
795    #[cfg(not(feature = "native"))]
796    fn user_configuration_path() -> Option<std::path::PathBuf> {
797        None
798    }
799
800    /// Internal implementation that accepts the home directory for testing.
801    ///
802    /// Probes `<home>/.rumdl.toml` then `<home>/rumdl.toml`, returning the first match.
803    ///
804    /// `pyproject.toml` is intentionally **not** searched in `$HOME`, even though
805    /// `user_configuration_path_impl` does check it inside the platform config dir.
806    /// The asymmetry is deliberate: a `pyproject.toml` directly in `$HOME` almost
807    /// always belongs to unrelated python tooling (poetry/uv/pip's user-level config),
808    /// and silently picking it up as a rumdl config would surprise users. The
809    /// platform config dir (`~/.config/rumdl/`) is rumdl-scoped, so the same
810    /// concern doesn't apply there.
811    fn home_configuration_path_impl(home_dir: &Path) -> Option<std::path::PathBuf> {
812        const HOME_CONFIG_FILES: &[&str] = &[".rumdl.toml", "rumdl.toml"];
813
814        log::debug!(
815            "[rumdl-config] Checking for home-directory configuration in: {}",
816            home_dir.display()
817        );
818
819        for filename in HOME_CONFIG_FILES {
820            let config_path = home_dir.join(filename);
821            if config_path.exists() {
822                log::debug!(
823                    "[rumdl-config] Found home-directory configuration at: {}",
824                    config_path.display()
825                );
826                return Some(config_path);
827            }
828        }
829
830        log::debug!(
831            "[rumdl-config] No home-directory configuration found in: {}",
832            home_dir.display()
833        );
834        None
835    }
836
837    /// Discover a home-directory configuration file (`~/.rumdl.toml` or `~/rumdl.toml`).
838    ///
839    /// This is a final fallback after the platform user-config directory
840    /// (`user_configuration_path`). It honors the classic Unix dotfile convention so
841    /// users who keep tool config in `$HOME` rather than `$XDG_CONFIG_HOME` are picked up.
842    #[cfg(feature = "native")]
843    fn home_configuration_path() -> Option<std::path::PathBuf> {
844        use etcetera::{BaseStrategy, choose_base_strategy};
845
846        match choose_base_strategy() {
847            Ok(strategy) => Self::home_configuration_path_impl(strategy.home_dir()),
848            Err(e) => {
849                log::debug!("[rumdl-config] Failed to determine home directory: {e}");
850                None
851            }
852        }
853    }
854
855    /// Stub for WASM builds - home config not supported
856    #[cfg(not(feature = "native"))]
857    fn home_configuration_path() -> Option<std::path::PathBuf> {
858        None
859    }
860
861    /// Load an explicit config file (standalone, no user config merging)
862    fn load_explicit_config(sourced_config: &mut Self, path: &str) -> Result<(), ConfigError> {
863        let path_obj = Path::new(path);
864        let filename = path_obj.file_name().and_then(|name| name.to_str()).unwrap_or("");
865        let path_str = path.to_string();
866
867        log::debug!("[rumdl-config] Loading explicit config file: {filename}");
868
869        // Find project root by walking up from config location looking for .git
870        if let Some(config_parent) = path_obj.parent() {
871            let project_root = Self::find_project_root_from(config_parent);
872            log::debug!(
873                "[rumdl-config] Project root (from explicit config): {}",
874                project_root.display()
875            );
876            sourced_config.project_root = Some(project_root);
877        }
878
879        // Known markdownlint config files
880        const MARKDOWNLINT_FILENAMES: &[&str] = &[
881            ".markdownlint-cli2.jsonc",
882            ".markdownlint-cli2.yaml",
883            ".markdownlint-cli2.yml",
884            ".markdownlint.json",
885            ".markdownlint.yaml",
886            ".markdownlint.yml",
887        ];
888
889        if filename == "pyproject.toml" || filename == ".rumdl.toml" || filename == "rumdl.toml" {
890            // Use extends-aware loading for rumdl TOML configs
891            let mut chain = ExtendsChain::default();
892            let chain_source = source_from_filename(filename);
893            load_config_with_extends(sourced_config, path_obj, &mut chain, chain_source, ConfigOrigin::Direct)?;
894        } else if MARKDOWNLINT_FILENAMES.contains(&filename)
895            || path_str.ends_with(".json")
896            || path_str.ends_with(".jsonc")
897            || path_str.ends_with(".yaml")
898            || path_str.ends_with(".yml")
899        {
900            // Parse as markdownlint config (JSON/YAML) - no extends support
901            let fragment = parsers::load_from_markdownlint(&path_str)?;
902            sourced_config.merge(fragment);
903            sourced_config.loaded_files.push(path_str);
904        } else {
905            // Try TOML with extends support
906            let mut chain = ExtendsChain::default();
907            let chain_source = source_from_filename(filename);
908            load_config_with_extends(sourced_config, path_obj, &mut chain, chain_source, ConfigOrigin::Direct)?;
909        }
910
911        Ok(())
912    }
913
914    /// Load and merge user-level configuration into this `SourcedConfig`.
915    ///
916    /// Discovers the user config file in this order, taking the first match:
917    /// 1. Platform user-config directory, resolved via `etcetera::choose_base_strategy`
918    ///    (the CLI/XDG convention): `~/.config` on Linux and macOS, `%APPDATA%` on
919    ///    Windows. Note macOS uses the XDG-style `~/.config`, not the GUI-app location
920    ///    `~/Library/Application Support`. Override with `user_config_dir` for tests.
921    /// 2. Home-directory dotfile (`~/.rumdl.toml`, then `~/rumdl.toml`). Override with
922    ///    `home_dir` for tests. Honors the classic Unix dotfile convention.
923    ///
924    /// Resolves any `extends` chain and merges each fragment with
925    /// `ConfigSource::UserConfig` precedence.
926    ///
927    /// Called in two contexts:
928    /// - When no project config is found: provides user defaults as the sole base
929    /// - When a markdownlint project config is found: provides rumdl-specific
930    ///   defaults that the markdownlint format cannot express; the markdownlint
931    ///   fragment is merged on top and wins on any overlapping key
932    fn load_user_config(
933        sourced_config: &mut Self,
934        user_config_dir: Option<&Path>,
935        home_dir: Option<&Path>,
936    ) -> Result<(), ConfigError> {
937        let user_config_path = if let Some(dir) = user_config_dir {
938            Self::user_configuration_path_impl(dir)
939        } else {
940            Self::user_configuration_path()
941        };
942
943        let user_config_path = user_config_path.or_else(|| match home_dir {
944            Some(home) => Self::home_configuration_path_impl(home),
945            None => Self::home_configuration_path(),
946        });
947
948        if let Some(user_config_path) = user_config_path {
949            let path_str = user_config_path.display().to_string();
950
951            log::debug!("[rumdl-config] Loading user config: {path_str}");
952
953            // User config fallback also supports extends chains.
954            // Use a uniform source across the chain so child overrides are determined by chain order.
955            let mut chain = ExtendsChain::default();
956            load_config_with_extends(
957                sourced_config,
958                &user_config_path,
959                &mut chain,
960                ConfigSource::UserConfig,
961                ConfigOrigin::Direct,
962            )?;
963        } else {
964            log::debug!("[rumdl-config] No user configuration file found");
965        }
966
967        Ok(())
968    }
969
970    /// Load a project config file that discovery found, as opposed to one the user
971    /// named explicitly.
972    ///
973    /// The two are not interchangeable. An explicit config is standalone by design
974    /// (`load_explicit_config`), and so is a discovered rumdl-native config: a
975    /// project's ruleset has to be reproducible on any machine. A discovered
976    /// *markdownlint* config is the exception. That format cannot express
977    /// rumdl-specific settings (flavor, cache, per-file ignores), so the user config
978    /// is loaded first as a base and the markdownlint fragment merged on top. The
979    /// fragment carries `ConfigSource::ProjectConfig` (precedence 3) against the
980    /// base's `ConfigSource::UserConfig` (1), so project settings still win on every
981    /// overlapping key.
982    ///
983    /// Both the CLI (`load_with_discovery_impl`) and the LSP (`load_discovered`,
984    /// via `RumdlLanguageServer::resolve_config_for_file`) load discovered files
985    /// through here, so a discovered config resolves the same way in an editor as
986    /// it does on the command line.
987    fn load_discovered_config(
988        sourced_config: &mut Self,
989        config_file: &Path,
990        user_config_dir: Option<&Path>,
991        home_dir: Option<&Path>,
992    ) -> Result<(), DiscoveredConfigError> {
993        let filename = config_file.file_name().and_then(|name| name.to_str()).unwrap_or("");
994
995        if MARKDOWNLINT_CONFIG_FILES.contains(&filename) {
996            Self::load_user_config(sourced_config, user_config_dir, home_dir)
997                .map_err(DiscoveredConfigError::UserConfig)?;
998
999            let path_str = config_file.display().to_string();
1000            let fragment = parsers::load_from_markdownlint(&path_str).map_err(DiscoveredConfigError::ProjectConfig)?;
1001            sourced_config.merge(fragment);
1002            sourced_config.loaded_files.push(path_str);
1003        } else {
1004            let mut chain = ExtendsChain::default();
1005            let chain_source = source_from_filename(filename);
1006            load_config_with_extends(
1007                sourced_config,
1008                config_file,
1009                &mut chain,
1010                chain_source,
1011                ConfigOrigin::Direct,
1012            )
1013            .map_err(DiscoveredConfigError::ProjectConfig)?;
1014        }
1015
1016        Ok(())
1017    }
1018
1019    /// Load a config file that the caller discovered by walking the tree itself.
1020    ///
1021    /// The LSP cannot use `load_with_discovery`: that walk starts at the process
1022    /// working directory, while the server resolves a config per document and stops
1023    /// at the workspace root. It finds the file with its own walk and hands it here,
1024    /// so the discovered-config rules in `load_discovered_config` still apply.
1025    ///
1026    /// `project_root` comes from the config file's own location, which is what
1027    /// per-file ignore globs are matched against.
1028    ///
1029    /// `user_config_dir` and `home_dir` override the platform user-config directory
1030    /// and the home directory; the server passes the home directory it already
1031    /// resolved for its walk boundary, and tests pass both.
1032    ///
1033    /// The error distinguishes an unusable discovered file from an unusable user
1034    /// config so a caller walking several candidates can tell "try the next one"
1035    /// from "nothing here will resolve correctly".
1036    pub fn load_discovered(
1037        config_file: &Path,
1038        user_config_dir: Option<&Path>,
1039        home_dir: Option<&Path>,
1040    ) -> Result<Self, DiscoveredConfigError> {
1041        let mut sourced_config = SourcedConfig::default();
1042
1043        if let Some(config_parent) = config_file.parent() {
1044            sourced_config.project_root = Some(Self::find_project_root_from(config_parent));
1045        }
1046
1047        Self::load_discovered_config(&mut sourced_config, config_file, user_config_dir, home_dir)?;
1048
1049        Ok(sourced_config)
1050    }
1051
1052    /// Load the configuration that applies to a directory, as if the CLI had run there.
1053    ///
1054    /// Discovery normally walks up from the process working directory, which is the
1055    /// scope the user chose when they typed `rumdl check`. A language server has no
1056    /// such directory: the editor launches it from wherever it happens to be, which
1057    /// may sit in an unrelated project. The workspace root is the scope the user
1058    /// chose, so the server passes that here and resolves what `rumdl check` would
1059    /// resolve inside it.
1060    ///
1061    /// `user_config_dir` and `home_dir` override the platform user-config directory
1062    /// and the home-directory walk boundary; the server passes the home directory it
1063    /// already resolved for its per-file walk, and tests pass both.
1064    pub fn load_for_workspace(
1065        start_dir: &Path,
1066        config_path: Option<&str>,
1067        user_config_dir: Option<&Path>,
1068        home_dir: Option<&Path>,
1069    ) -> Result<Self, ConfigError> {
1070        Self::load_with_discovery_from(Some(start_dir), config_path, None, false, user_config_dir, home_dir)
1071    }
1072
1073    /// Internal implementation that accepts user config directory and home directory for testing
1074    #[doc(hidden)]
1075    pub fn load_with_discovery_impl(
1076        config_path: Option<&str>,
1077        cli_overrides: Option<&SourcedGlobalConfig>,
1078        skip_auto_discovery: bool,
1079        user_config_dir: Option<&Path>,
1080        home_dir: Option<&Path>,
1081    ) -> Result<Self, ConfigError> {
1082        Self::load_with_discovery_from(
1083            None,
1084            config_path,
1085            cli_overrides,
1086            skip_auto_discovery,
1087            user_config_dir,
1088            home_dir,
1089        )
1090    }
1091
1092    /// Shared body of every discovery-based load.
1093    ///
1094    /// `start_dir` is where the upward walk begins; `None` means the process
1095    /// working directory, which is what the CLI wants.
1096    fn load_with_discovery_from(
1097        start_dir: Option<&Path>,
1098        config_path: Option<&str>,
1099        cli_overrides: Option<&SourcedGlobalConfig>,
1100        skip_auto_discovery: bool,
1101        user_config_dir: Option<&Path>,
1102        home_dir: Option<&Path>,
1103    ) -> Result<Self, ConfigError> {
1104        use std::env;
1105        log::debug!("[rumdl-config] Current working directory: {:?}", env::current_dir());
1106
1107        let mut sourced_config = SourcedConfig::default();
1108
1109        // Ruff model: Project config is standalone, user config is fallback only
1110        //
1111        // Priority order:
1112        // 1. If explicit config path provided → use ONLY that (standalone)
1113        // 2. Else if project config discovered → use ONLY that (standalone)
1114        // 3. Else if user config exists → use it as fallback
1115        // 4. CLI overrides always apply last
1116        //
1117        // This ensures project configs are reproducible across machines and
1118        // CI/local runs behave identically.
1119
1120        // Explicit config path always takes precedence
1121        if let Some(path) = config_path {
1122            // Explicit config path provided - use ONLY this config (standalone)
1123            log::debug!("[rumdl-config] Explicit config_path provided: {path:?}");
1124            Self::load_explicit_config(&mut sourced_config, path)?;
1125        } else if skip_auto_discovery {
1126            log::debug!("[rumdl-config] Skipping config discovery due to --no-config/--isolated flag");
1127            // No config loading, just apply CLI overrides at the end
1128        } else {
1129            // No explicit path - try auto-discovery
1130            log::debug!("[rumdl-config] No explicit config_path, searching default locations");
1131
1132            // Try to discover project config first
1133            if let Some((config_file, project_root, shadow)) = Self::discover_config_upward(start_dir, home_dir) {
1134                // Project config found - use ONLY this (standalone, no user config).
1135                // Rumdl project configs can express all settings directly, so user config
1136                // is not needed and omitting it ensures CI and local runs are identical.
1137                log::debug!("[rumdl-config] Found project config: {}", config_file.display());
1138                log::debug!("[rumdl-config] Project root: {}", project_root.display());
1139
1140                // Record any same-directory sibling configs that are silently shadowed,
1141                // so the CLI and LSP can warn the user. Resolution is unchanged.
1142                if let Some(shadow) = shadow {
1143                    sourced_config.discovery_warnings.push(format_shadow_warning(&shadow));
1144                }
1145
1146                sourced_config.project_root = Some(project_root);
1147
1148                Self::load_discovered_config(&mut sourced_config, &config_file, user_config_dir, home_dir)?;
1149            } else {
1150                // No rumdl project config - try markdownlint config
1151                log::debug!("[rumdl-config] No rumdl config found, checking markdownlint config");
1152
1153                if let Some(markdownlint_path) = Self::discover_markdownlint_config_upward(start_dir, home_dir) {
1154                    log::debug!(
1155                        "[rumdl-config] Found markdownlint config: {}",
1156                        markdownlint_path.display()
1157                    );
1158
1159                    if let Err(e) =
1160                        Self::load_discovered_config(&mut sourced_config, &markdownlint_path, user_config_dir, home_dir)
1161                    {
1162                        match e {
1163                            // A markdownlint file rumdl cannot parse is skipped rather
1164                            // than fatal: the user never named it, and rumdl only reads
1165                            // the format as a courtesy. The user config it would have
1166                            // merged onto is already loaded, which is the state of the
1167                            // no-project-config case.
1168                            DiscoveredConfigError::ProjectConfig(e) => {
1169                                log::debug!("[rumdl-config] Failed to load markdownlint config: {e}");
1170                            }
1171                            // A broken user config is fatal, as in every other arm.
1172                            DiscoveredConfigError::UserConfig(e) => return Err(e),
1173                        }
1174                    }
1175                } else {
1176                    // No project config at all - use user config as fallback
1177                    log::debug!("[rumdl-config] No project config found, using user config as fallback");
1178                    Self::load_user_config(&mut sourced_config, user_config_dir, home_dir)?;
1179                }
1180            }
1181        }
1182
1183        // Apply CLI overrides (highest precedence)
1184        if let Some(cli) = cli_overrides {
1185            sourced_config
1186                .global
1187                .enable
1188                .merge_override(cli.enable.value.clone(), ConfigSource::Cli, None);
1189            sourced_config
1190                .global
1191                .disable
1192                .merge_override(cli.disable.value.clone(), ConfigSource::Cli, None);
1193            sourced_config
1194                .global
1195                .exclude
1196                .merge_override(cli.exclude.value.clone(), ConfigSource::Cli, None);
1197            sourced_config
1198                .global
1199                .include
1200                .merge_override(cli.include.value.clone(), ConfigSource::Cli, None);
1201            sourced_config.global.respect_gitignore.merge_override(
1202                cli.respect_gitignore.value,
1203                ConfigSource::Cli,
1204                None,
1205            );
1206            sourced_config
1207                .global
1208                .fixable
1209                .merge_override(cli.fixable.value.clone(), ConfigSource::Cli, None);
1210            sourced_config
1211                .global
1212                .unfixable
1213                .merge_override(cli.unfixable.value.clone(), ConfigSource::Cli, None);
1214            // No rule-specific CLI overrides implemented yet
1215        }
1216
1217        // Unknown keys are now collected during parsing and validated via validate_config_sourced()
1218
1219        Ok(sourced_config)
1220    }
1221
1222    /// Load and merge configurations from files and CLI overrides.
1223    /// If skip_auto_discovery is true, only explicit config paths are loaded.
1224    pub fn load_with_discovery(
1225        config_path: Option<&str>,
1226        cli_overrides: Option<&SourcedGlobalConfig>,
1227        skip_auto_discovery: bool,
1228    ) -> Result<Self, ConfigError> {
1229        Self::load_with_discovery_impl(config_path, cli_overrides, skip_auto_discovery, None, None)
1230    }
1231
1232    /// Validate the configuration against a rule registry.
1233    ///
1234    /// This method transitions the config from `ConfigLoaded` to `ConfigValidated` state,
1235    /// enabling conversion to `Config`. Validation warnings are stored in the config
1236    /// and can be displayed to the user.
1237    ///
1238    /// # Example
1239    ///
1240    /// ```ignore
1241    /// let loaded = SourcedConfig::load_with_discovery(path, None, false)?;
1242    /// let validated = loaded.validate(&registry)?;
1243    /// let config: Config = validated.into();
1244    /// ```
1245    pub fn validate(self, registry: &RuleRegistry) -> Result<SourcedConfig<ConfigValidated>, ConfigError> {
1246        let warnings = validate_config_sourced_internal(&self, registry);
1247
1248        Ok(SourcedConfig {
1249            global: self.global,
1250            per_file_ignores: self.per_file_ignores,
1251            per_file_flavor: self.per_file_flavor,
1252            code_block_tools: self.code_block_tools,
1253            rules: self.rules,
1254            loaded_files: self.loaded_files,
1255            unknown_keys: self.unknown_keys,
1256            project_root: self.project_root,
1257            discovery_warnings: self.discovery_warnings,
1258            validation_warnings: warnings,
1259            _state: PhantomData,
1260        })
1261    }
1262
1263    /// Validate and convert to Config in one step (convenience method).
1264    ///
1265    /// This combines `validate()` and `into()` for callers who want the
1266    /// validation warnings separately.
1267    pub fn validate_into(self, registry: &RuleRegistry) -> Result<(Config, Vec<ConfigValidationWarning>), ConfigError> {
1268        let validated = self.validate(registry)?;
1269        let warnings = validated.validation_warnings.clone();
1270        Ok((validated.into(), warnings))
1271    }
1272
1273    /// Skip validation and convert directly to ConfigValidated state.
1274    ///
1275    /// # Safety
1276    ///
1277    /// This method bypasses validation. Use only when:
1278    /// - You've already validated via `validate_config_sourced()`
1279    /// - You're in test code that doesn't need validation
1280    /// - You're migrating legacy code and will add proper validation later
1281    ///
1282    /// Prefer `validate()` for new code.
1283    pub fn into_validated_unchecked(self) -> SourcedConfig<ConfigValidated> {
1284        SourcedConfig {
1285            global: self.global,
1286            per_file_ignores: self.per_file_ignores,
1287            per_file_flavor: self.per_file_flavor,
1288            code_block_tools: self.code_block_tools,
1289            rules: self.rules,
1290            loaded_files: self.loaded_files,
1291            unknown_keys: self.unknown_keys,
1292            project_root: self.project_root,
1293            discovery_warnings: self.discovery_warnings,
1294            validation_warnings: Vec::new(),
1295            _state: PhantomData,
1296        }
1297    }
1298
1299    /// Discover the nearest config file for a specific directory,
1300    /// walking upward to `project_root` (inclusive).
1301    ///
1302    /// Searches for rumdl config files (`.rumdl.toml`, `rumdl.toml`,
1303    /// `.config/rumdl.toml`, `pyproject.toml` with `[tool.rumdl]`) and
1304    /// markdownlint config files at each directory level.
1305    ///
1306    /// Returns the config file path if found. Does NOT use CWD.
1307    pub fn discover_config_for_dir(dir: &Path, project_root: &Path) -> Option<PathBuf> {
1308        // The walk never canonicalizes the directories it yields (symlinks and
1309        // Windows short names stay as the caller wrote them); only stop checks
1310        // compare canonically. A relative `dir` is resolved against the current
1311        // directory, so the returned config path is always absolute. UpwardWalk
1312        // also retains the shared traversal depth bound.
1313        //
1314        // The home boundary keeps the walk from treating `~/.rumdl.toml` as a
1315        // project config, consistent with `discover_config_upward`. This only has
1316        // an effect when `project_root` is at or above the home directory (e.g. a
1317        // multi-path run whose grouping root spans the home boundary); for the
1318        // usual project root below home the walk stops there first.
1319        collect_project_config_candidates(dir, Some(project_root), Self::resolve_home_boundary(None).as_deref())
1320            .into_iter()
1321            .next()
1322    }
1323
1324    /// Load a config from a specific file path, with extends resolution, returning
1325    /// the still-`Loaded` `SourcedConfig` (before validation and conversion).
1326    ///
1327    /// Used by per-directory resolution so the caller can layer CLI-level overrides
1328    /// (e.g. inline `--config`) on top before converting to `Config`, matching the
1329    /// precedence applied to the global config.
1330    pub fn load_sourced_for_path(
1331        config_path: &Path,
1332        project_root: &Path,
1333    ) -> Result<SourcedConfig<ConfigLoaded>, ConfigError> {
1334        let mut sourced_config = SourcedConfig {
1335            project_root: Some(project_root.to_path_buf()),
1336            ..SourcedConfig::default()
1337        };
1338
1339        let filename = config_path.file_name().and_then(|n| n.to_str()).unwrap_or("");
1340        let path_str = config_path.display().to_string();
1341
1342        // Determine if this is a markdownlint config or rumdl config
1343        let is_markdownlint = MARKDOWNLINT_CONFIG_FILES.contains(&filename)
1344            || (filename != "pyproject.toml"
1345                && filename != ".rumdl.toml"
1346                && filename != "rumdl.toml"
1347                && (path_str.ends_with(".json")
1348                    || path_str.ends_with(".jsonc")
1349                    || path_str.ends_with(".yaml")
1350                    || path_str.ends_with(".yml")));
1351
1352        if is_markdownlint {
1353            let fragment = parsers::load_from_markdownlint(&path_str)?;
1354            sourced_config.merge(fragment);
1355            sourced_config.loaded_files.push(path_str);
1356        } else {
1357            let mut chain = ExtendsChain::default();
1358            let chain_source = source_from_filename(filename);
1359            load_config_with_extends(
1360                &mut sourced_config,
1361                config_path,
1362                &mut chain,
1363                chain_source,
1364                ConfigOrigin::Direct,
1365            )?;
1366        }
1367
1368        Ok(sourced_config)
1369    }
1370
1371    /// Load a config from a specific file path, with extends resolution, and convert
1372    /// to `Config`. Used for per-directory config loading where each subdirectory
1373    /// config is standalone.
1374    pub fn load_config_for_path(config_path: &Path, project_root: &Path) -> Result<Config, ConfigError> {
1375        Ok(Self::load_sourced_for_path(config_path, project_root)?
1376            .into_validated_unchecked()
1377            .into())
1378    }
1379}
1380
1381/// Convert a validated configuration to the final Config type.
1382///
1383/// This implementation only exists for `SourcedConfig<ConfigValidated>`,
1384/// ensuring that validation must occur before conversion.
1385impl From<SourcedConfig<ConfigValidated>> for Config {
1386    fn from(sourced: SourcedConfig<ConfigValidated>) -> Self {
1387        let mut rules = BTreeMap::new();
1388        let mut withheld_rule_values = std::collections::BTreeSet::new();
1389        for (rule_name, sourced_rule_cfg) in sourced.rules {
1390            // Normalize rule name to uppercase for case-insensitive lookup
1391            let normalized_rule_name = rule_name.to_ascii_uppercase();
1392            let severity = sourced_rule_cfg.severity.map(|sv| sv.value);
1393            let mut values = BTreeMap::new();
1394            for (key, sourced_val) in sourced_rule_cfg.values {
1395                values.insert(key, sourced_val.value);
1396            }
1397            if values.keys().any(|key| sourced_rule_cfg.withheld_keys.contains(key)) {
1398                withheld_rule_values.insert(normalized_rule_name.clone());
1399            }
1400            rules.insert(normalized_rule_name, RuleConfig { severity, values });
1401        }
1402        // Enable is "explicit" if it was set by something other than the Default source
1403        let enable_is_explicit = sourced.global.enable.source != ConfigSource::Default;
1404
1405        #[allow(deprecated)]
1406        let global = GlobalConfig {
1407            enable: sourced.global.enable.value,
1408            disable: sourced.global.disable.value,
1409            exclude: sourced.global.exclude.value,
1410            include: sourced.global.include.value,
1411            respect_gitignore: sourced.global.respect_gitignore.value,
1412            line_length: sourced.global.line_length.value,
1413            output_format: sourced.global.output_format.as_ref().map(|v| v.value.clone()),
1414            fixable: sourced.global.fixable.value,
1415            unfixable: sourced.global.unfixable.value,
1416            flavor: sourced.global.flavor.value,
1417            force_exclude: sourced.global.force_exclude.value,
1418            cache_dir: sourced.global.cache_dir.as_ref().map(|v| v.value.clone()),
1419            cache: sourced.global.cache.value,
1420            extend_enable: sourced.global.extend_enable.value,
1421            extend_disable: sourced.global.extend_disable.value,
1422            editorconfig: sourced.global.editorconfig.value,
1423            enable_is_explicit,
1424            include_withheld: sourced.global.include_withheld,
1425        };
1426
1427        let mut config = Config {
1428            extends: None,
1429            global,
1430            per_file_ignores: sourced.per_file_ignores.value,
1431            per_file_flavor: sourced.per_file_flavor.value,
1432            code_block_tools: sourced.code_block_tools.value,
1433            rules,
1434            withheld_rule_values,
1435            project_root: sourced.project_root,
1436            per_file_ignores_cache: Arc::new(OnceLock::new()),
1437            per_file_flavor_cache: Arc::new(OnceLock::new()),
1438            canonical_project_root_cache: Arc::new(OnceLock::new()),
1439        };
1440
1441        // Apply per-rule `enabled = true/false` to global enable/disable lists
1442        config.apply_per_rule_enabled();
1443
1444        // Enforce the runtime invariant: every rule-name list is canonicalised.
1445        // After this point, downstream consumers (`rules::filter_rules`, the LSP,
1446        // WASM, fix coordinator, per-file-ignores) can match against
1447        // `Rule::name()` with simple string equality regardless of whether the
1448        // user's config used canonical IDs (`"MD033"`) or aliases
1449        // (`"no-inline-html"`).
1450        config.canonicalize_rule_lists();
1451
1452        config
1453    }
1454}
1455
1456#[cfg(test)]
1457mod tests {
1458    use super::pyproject_declares_rumdl_config;
1459
1460    #[test]
1461    fn detects_flat_and_dotted_rumdl_sections() {
1462        assert!(pyproject_declares_rumdl_config("[tool.rumdl]\nline-length = 80\n"));
1463        // Dotted sections are valid on their own, without a flat header.
1464        assert!(pyproject_declares_rumdl_config(
1465            "[tool.rumdl.MD013]\nstyle = \"fixed\"\n"
1466        ));
1467        assert!(pyproject_declares_rumdl_config(
1468            "[tool.rumdl.rules.MD007]\nindent = 4\n"
1469        ));
1470    }
1471
1472    #[test]
1473    fn ignores_incidental_mentions() {
1474        // A bare `tool.rumdl` in a comment or string value must not be treated
1475        // as a config section.
1476        assert!(!pyproject_declares_rumdl_config("# configure tool.rumdl later\n"));
1477        assert!(!pyproject_declares_rumdl_config(
1478            "[project]\ndependencies = [\"tool.rumdl-helper\"]\n"
1479        ));
1480        assert!(!pyproject_declares_rumdl_config("[tool.black]\nline-length = 88\n"));
1481    }
1482
1483    /// Pure tests for the `$VAR` / `${VAR}` expander used by `extends` resolution.
1484    /// The injected `lookup` keeps these independent of the real process environment.
1485    mod expand_env_vars {
1486        use super::super::expand_env_vars;
1487        use std::collections::HashMap;
1488
1489        /// Build a lookup closure from `(name, value)` pairs.
1490        fn env(pairs: &[(&str, &str)]) -> impl Fn(&str) -> Option<String> {
1491            let map: HashMap<String, String> = pairs
1492                .iter()
1493                .map(|(k, v)| ((*k).to_string(), (*v).to_string()))
1494                .collect();
1495            move |k: &str| map.get(k).cloned()
1496        }
1497
1498        #[test]
1499        fn expands_bare_and_braced_forms() {
1500            let e = env(&[("VAR", "val"), ("FOO_BAR", "fb")]);
1501            assert_eq!(expand_env_vars("$VAR", &e).unwrap(), "val");
1502            assert_eq!(expand_env_vars("${VAR}", &e).unwrap(), "val");
1503            // Longest-match identifier: `$FOO_BAR` is one name, not `$FOO` + `_BAR`.
1504            assert_eq!(expand_env_vars("$FOO_BAR", &e).unwrap(), "fb");
1505        }
1506
1507        #[test]
1508        fn expands_within_paths() {
1509            let e = env(&[("BASE", "/opt/cfg"), ("A", "x"), ("B", "y")]);
1510            assert_eq!(expand_env_vars("$BASE/x/y.toml", &e).unwrap(), "/opt/cfg/x/y.toml");
1511            assert_eq!(expand_env_vars("$A/$B", &e).unwrap(), "x/y");
1512            assert_eq!(expand_env_vars("${A}suffix", &e).unwrap(), "xsuffix");
1513        }
1514
1515        #[test]
1516        fn dollar_dollar_is_a_literal_dollar() {
1517            let e = env(&[("VAR", "val")]);
1518            assert_eq!(expand_env_vars("$$", &e).unwrap(), "$");
1519            // The escaped `$` is consumed; what follows is literal (not expanded).
1520            assert_eq!(expand_env_vars("$$VAR", &e).unwrap(), "$VAR");
1521            assert_eq!(expand_env_vars("$${VAR}", &e).unwrap(), "${VAR}");
1522            // `$$` is how a literal `$` in a path is written once this feature exists.
1523            assert_eq!(expand_env_vars("file-$$name.toml", &e).unwrap(), "file-$name.toml");
1524        }
1525
1526        #[test]
1527        fn bare_dollar_name_in_path_is_a_variable_reference() {
1528            // Documented behavior change: an unescaped `$name` in a path is a variable,
1529            // not a literal. `$$` writes a literal `$` (see dollar_dollar test above).
1530            let e = env(&[("name", "core")]);
1531            assert_eq!(expand_env_vars("file-$name.toml", &e).unwrap(), "file-core.toml");
1532        }
1533
1534        #[test]
1535        fn incidental_dollar_stays_literal() {
1536            let e = env(&[]);
1537            // `$` before a non-identifier-start char (or end of input) is literal.
1538            assert_eq!(expand_env_vars("$5", &e).unwrap(), "$5");
1539            assert_eq!(expand_env_vars("cost$", &e).unwrap(), "cost$");
1540            assert_eq!(expand_env_vars("a$/b", &e).unwrap(), "a$/b");
1541        }
1542
1543        #[test]
1544        fn malformed_braces_stay_literal() {
1545            let e = env(&[("B", "x")]);
1546            assert_eq!(expand_env_vars("${}", &e).unwrap(), "${}");
1547            assert_eq!(expand_env_vars("${VAR", &e).unwrap(), "${VAR");
1548            // Nested `${...}` is not supported: the whole span is literal, no partial expand.
1549            assert_eq!(expand_env_vars("${A${B}}", &e).unwrap(), "${A${B}}");
1550        }
1551
1552        #[test]
1553        fn undefined_variable_is_an_error() {
1554            let e = env(&[]);
1555            assert_eq!(expand_env_vars("$NOPE", &e).unwrap_err(), "NOPE");
1556            assert_eq!(expand_env_vars("${NOPE}", &e).unwrap_err(), "NOPE");
1557            assert_eq!(expand_env_vars("prefix/$NOPE/x", &e).unwrap_err(), "NOPE");
1558        }
1559
1560        #[test]
1561        fn replacement_is_not_rescanned() {
1562            // If `A` expands to "$B", the result is the literal "$B"; `B` is NOT expanded.
1563            let e = env(&[("A", "$B"), ("B", "should-not-appear")]);
1564            assert_eq!(expand_env_vars("$A", &e).unwrap(), "$B");
1565            assert_eq!(expand_env_vars("${A}", &e).unwrap(), "$B");
1566        }
1567
1568        #[test]
1569        fn identifiers_are_ascii_only_unicode_stays_literal() {
1570            let e = env(&[("VAR", "v")]);
1571            // Non-ASCII inside braces is not a valid identifier -> whole span literal.
1572            assert_eq!(expand_env_vars("${föö}", &e).unwrap(), "${föö}");
1573            // A name ends at the first non-identifier byte; trailing unicode is preserved.
1574            assert_eq!(expand_env_vars("$VARö", &e).unwrap(), "vö");
1575            // Literal runs preserve multibyte content around an expansion.
1576            assert_eq!(expand_env_vars("café/$VAR", &e).unwrap(), "café/v");
1577        }
1578
1579        #[test]
1580        fn passthrough_for_plain_input() {
1581            let e = env(&[]);
1582            assert_eq!(expand_env_vars("", &e).unwrap(), "");
1583            assert_eq!(expand_env_vars("/plain/path.toml", &e).unwrap(), "/plain/path.toml");
1584        }
1585    }
1586
1587    /// Discovery must stop at the project root even when the root is supplied in
1588    /// a different path representation than the walked directory's ancestors.
1589    ///
1590    /// This reproduces the Windows 8.3-short-name / canonical mismatch using a
1591    /// Unix symlink: the project root is passed as a symlink to the real root, so
1592    /// it does not string-match the canonical ancestors of the starting
1593    /// directory. Without canonicalization the walk overshoots the project root
1594    /// and incorrectly picks up the config in the parent directory.
1595    #[cfg(unix)]
1596    #[test]
1597    fn discover_stops_at_project_root_across_path_representations() {
1598        use super::SourcedConfig;
1599        use std::os::unix::fs::symlink;
1600        use tempfile::tempdir;
1601
1602        let tmp = tempdir().unwrap();
1603        // A config ABOVE the project root that must never be discovered.
1604        std::fs::write(tmp.path().join(".rumdl.toml"), "").unwrap();
1605
1606        let real_root = tmp.path().join("project");
1607        let subdir = real_root.join("docs");
1608        std::fs::create_dir_all(&subdir).unwrap();
1609
1610        // Supply the project root via a symlink so it does not string-match the
1611        // canonical ancestors of `subdir`.
1612        let linked_root = tmp.path().join("project-link");
1613        symlink(&real_root, &linked_root).unwrap();
1614
1615        let found = SourcedConfig::discover_config_for_dir(&subdir, &linked_root);
1616        assert_eq!(
1617            found, None,
1618            "discovery must stop at the project root, not overshoot to the parent config"
1619        );
1620    }
1621
1622    #[test]
1623    #[serial_test::serial]
1624    fn project_candidates_absolutize_a_relative_start() {
1625        let cwd = std::env::current_dir().unwrap();
1626        let temp = tempfile::Builder::new()
1627            .prefix("rumdl-relative-config-")
1628            .tempdir_in(&cwd)
1629            .unwrap();
1630        let nested = temp.path().join("docs");
1631        std::fs::create_dir(&nested).unwrap();
1632        let config = temp.path().join(".rumdl.toml");
1633        std::fs::write(&config, "").unwrap();
1634
1635        let relative_root = temp.path().strip_prefix(&cwd).unwrap();
1636        let relative_nested = nested.strip_prefix(&cwd).unwrap();
1637        let candidates = super::collect_project_config_candidates(relative_nested, Some(relative_root), None);
1638
1639        assert_eq!(candidates.first(), Some(&config));
1640        assert!(candidates.iter().all(|path| path.is_absolute()));
1641    }
1642
1643    mod shadowed_configs {
1644        use super::super::{ShadowedConfigs, detect_shadowed_configs, format_shadow_warning, rumdl_configs_in_dir};
1645        use tempfile::tempdir;
1646
1647        fn names(paths: &[std::path::PathBuf]) -> Vec<String> {
1648            paths
1649                .iter()
1650                .map(|p| {
1651                    // Use the last two components so `.config/rumdl.toml` is distinguishable
1652                    // from a top-level `rumdl.toml` without depending on the temp dir prefix.
1653                    let file = p.file_name().and_then(|n| n.to_str()).unwrap_or_default();
1654                    let parent = p.parent().and_then(|d| d.file_name()).and_then(|n| n.to_str());
1655                    match parent {
1656                        Some(".config") => format!(".config/{file}"),
1657                        _ => file.to_string(),
1658                    }
1659                })
1660                .collect()
1661        }
1662
1663        #[test]
1664        fn empty_directory_has_no_configs_and_no_shadow() {
1665            let tmp = tempdir().unwrap();
1666            assert!(rumdl_configs_in_dir(tmp.path()).is_empty());
1667            assert!(detect_shadowed_configs(tmp.path()).is_none());
1668        }
1669
1670        #[test]
1671        fn single_config_does_not_shadow() {
1672            let tmp = tempdir().unwrap();
1673            std::fs::write(tmp.path().join(".rumdl.toml"), "").unwrap();
1674            assert_eq!(names(&rumdl_configs_in_dir(tmp.path())), vec![".rumdl.toml"]);
1675            assert!(detect_shadowed_configs(tmp.path()).is_none());
1676        }
1677
1678        #[test]
1679        fn dot_wins_over_non_dot_and_non_dot_is_shadowed() {
1680            let tmp = tempdir().unwrap();
1681            std::fs::write(tmp.path().join(".rumdl.toml"), "").unwrap();
1682            std::fs::write(tmp.path().join("rumdl.toml"), "").unwrap();
1683
1684            let ShadowedConfigs { winner, shadowed, .. } = detect_shadowed_configs(tmp.path()).unwrap();
1685            assert_eq!(names(&[winner]), vec![".rumdl.toml"]);
1686            assert_eq!(names(&shadowed), vec!["rumdl.toml"]);
1687        }
1688
1689        #[test]
1690        fn config_subdir_counts_as_same_level_shadow() {
1691            let tmp = tempdir().unwrap();
1692            std::fs::write(tmp.path().join(".rumdl.toml"), "").unwrap();
1693            std::fs::create_dir_all(tmp.path().join(".config")).unwrap();
1694            std::fs::write(tmp.path().join(".config/rumdl.toml"), "").unwrap();
1695
1696            let ShadowedConfigs { winner, shadowed, .. } = detect_shadowed_configs(tmp.path()).unwrap();
1697            assert_eq!(names(&[winner]), vec![".rumdl.toml"]);
1698            assert_eq!(names(&shadowed), vec![".config/rumdl.toml"]);
1699        }
1700
1701        #[test]
1702        fn pyproject_counts_only_when_it_declares_rumdl() {
1703            // pyproject WITHOUT [tool.rumdl] is not a rumdl config source -> no shadow.
1704            let bare = tempdir().unwrap();
1705            std::fs::write(bare.path().join(".rumdl.toml"), "").unwrap();
1706            std::fs::write(bare.path().join("pyproject.toml"), "[tool.black]\nline-length = 88\n").unwrap();
1707            assert_eq!(names(&rumdl_configs_in_dir(bare.path())), vec![".rumdl.toml"]);
1708            assert!(detect_shadowed_configs(bare.path()).is_none());
1709
1710            // pyproject WITH [tool.rumdl] is a real shadowed source.
1711            let declared = tempdir().unwrap();
1712            std::fs::write(declared.path().join(".rumdl.toml"), "").unwrap();
1713            std::fs::write(
1714                declared.path().join("pyproject.toml"),
1715                "[tool.rumdl]\nline-length = 80\n",
1716            )
1717            .unwrap();
1718            let ShadowedConfigs { winner, shadowed, .. } = detect_shadowed_configs(declared.path()).unwrap();
1719            assert_eq!(names(&[winner]), vec![".rumdl.toml"]);
1720            assert_eq!(names(&shadowed), vec!["pyproject.toml"]);
1721        }
1722
1723        #[test]
1724        fn markdownlint_configs_are_not_rumdl_native_and_never_shadow() {
1725            let tmp = tempdir().unwrap();
1726            std::fs::write(tmp.path().join(".rumdl.toml"), "").unwrap();
1727            std::fs::write(tmp.path().join(".markdownlint.json"), "{}").unwrap();
1728            assert_eq!(names(&rumdl_configs_in_dir(tmp.path())), vec![".rumdl.toml"]);
1729            assert!(detect_shadowed_configs(tmp.path()).is_none());
1730        }
1731
1732        #[test]
1733        fn configs_returned_in_precedence_order() {
1734            let tmp = tempdir().unwrap();
1735            std::fs::write(tmp.path().join(".rumdl.toml"), "").unwrap();
1736            std::fs::write(tmp.path().join("rumdl.toml"), "").unwrap();
1737            std::fs::create_dir_all(tmp.path().join(".config")).unwrap();
1738            std::fs::write(tmp.path().join(".config/rumdl.toml"), "").unwrap();
1739            std::fs::write(tmp.path().join("pyproject.toml"), "[tool.rumdl]\n").unwrap();
1740
1741            assert_eq!(
1742                names(&rumdl_configs_in_dir(tmp.path())),
1743                vec![".rumdl.toml", "rumdl.toml", ".config/rumdl.toml", "pyproject.toml"]
1744            );
1745        }
1746
1747        #[test]
1748        fn warning_names_dir_once_with_relative_filenames() {
1749            let tmp = tempdir().unwrap();
1750            std::fs::write(tmp.path().join(".rumdl.toml"), "").unwrap();
1751            std::fs::write(tmp.path().join("rumdl.toml"), "").unwrap();
1752            std::fs::write(tmp.path().join("pyproject.toml"), "[tool.rumdl]\n").unwrap();
1753
1754            let shadow = detect_shadowed_configs(tmp.path()).unwrap();
1755            let msg = format_shadow_warning(&shadow);
1756
1757            let dir = {
1758                let s = tmp.path().to_string_lossy().into_owned();
1759                if cfg!(windows) { s.replace('\\', "/") } else { s }
1760            };
1761            assert!(msg.contains("multiple rumdl config files"), "got: {msg}");
1762            // The directory is named once; files are shown relative to it (no
1763            // repeated directory prefix on every path).
1764            assert_eq!(
1765                msg.matches(dir.as_str()).count(),
1766                1,
1767                "directory should appear exactly once, got: {msg}"
1768            );
1769            assert!(
1770                msg.contains("using .rumdl.toml, ignoring rumdl.toml, pyproject.toml"),
1771                "winner and shadowed files should be relative names in precedence order, got: {msg}"
1772            );
1773            // Paths are normalized to forward slashes on all platforms.
1774            assert!(!msg.contains('\\'), "paths must be normalized to '/': {msg}");
1775        }
1776    }
1777}