Skip to main content

harn_modules/
lib.rs

1use std::collections::{HashMap, HashSet};
2use std::path::{Path, PathBuf};
3
4use crate::package_imports::acquire_package_snapshots;
5use crate::package_snapshot::PackageSnapshot;
6use harn_lexer::Span;
7use harn_parser::{Parser, SNode};
8
9pub mod asset_paths;
10mod declarations;
11pub mod fingerprint;
12mod import_recording;
13pub mod manifest_walk;
14mod namespace_imports;
15mod namespace_signatures;
16pub mod package_execution;
17mod package_imports;
18pub mod package_snapshot;
19pub mod personas;
20pub mod project_config;
21mod stdlib;
22mod symbol_reachability;
23mod type_dependencies;
24
25use declarations::{
26    callable_decl_name, collect_callable_declarations, collect_module_info,
27    collect_type_declarations, decl_site, type_decl_name,
28};
29pub use declarations::{public_declarations, DefKind, PublicDeclaration};
30pub use namespace_imports::NamespaceImportInfo;
31pub use namespace_signatures::NamespaceMemberSignature;
32pub use package_imports::{
33    resolve_import_path, resolve_import_path_with_guard, resolve_import_path_with_snapshot,
34};
35pub use symbol_reachability::{
36    closed_program_reachability, ExportDemand, ModuleSymbolDemand, SymbolReachability,
37};
38
39/// A resolved definition site within a module.
40#[derive(Debug, Clone)]
41pub struct DefSite {
42    pub name: String,
43    pub file: PathBuf,
44    pub kind: DefKind,
45    pub span: Span,
46}
47
48/// Wildcard import resolution status for a single importing module.
49#[derive(Debug, Clone)]
50pub enum WildcardResolution {
51    /// Resolved all wildcard imports and can expose wildcard exports.
52    Resolved(HashSet<String>),
53    /// At least one wildcard import could not be resolved.
54    Unknown,
55}
56
57/// Parsed information for a set of module files.
58#[derive(Debug, Default)]
59pub struct ModuleGraph {
60    modules: HashMap<PathBuf, ModuleInfo>,
61    // Resolved definition/import paths remain valid for the graph lifetime.
62    _package_snapshots: Vec<PackageSnapshot>,
63}
64
65#[derive(Debug, Clone)]
66pub struct ParsedModuleSource {
67    pub source: String,
68    pub program: Vec<SNode>,
69}
70
71#[derive(Debug, Default)]
72pub struct ModuleGraphBuild {
73    pub graph: ModuleGraph,
74    pub parsed_sources: HashMap<PathBuf, ParsedModuleSource>,
75}
76
77#[derive(Debug, Default)]
78struct ModuleInfo {
79    /// All declarations visible in this module (for local symbol lookup and
80    /// go-to-definition resolution).
81    declarations: HashMap<String, DefSite>,
82    /// Names exported by this module after re-export resolution. Equal to
83    /// [`own_exports`] union the keys of [`selective_re_exports`] union the
84    /// transitive exports of [`wildcard_re_export_paths`]. Populated in
85    /// `build()` after all modules are loaded.
86    exports: HashSet<String>,
87    /// Names declared locally and exported by this module — i.e. `pub fn`,
88    /// `pub struct`, etc.
89    own_exports: HashSet<String>,
90    /// Selective re-exports introduced by `pub import { name } from "..."`.
91    /// Maps the re-exported name to every canonical source module path it
92    /// could originate from. Multiple entries per name indicate a conflict
93    /// (`pub import { foo } from "a"` and `pub import { foo } from "b"`)
94    /// and are surfaced by [`ModuleGraph::re_export_conflicts`]. Lookup
95    /// callers (e.g. go-to-definition) follow the first recorded source.
96    selective_re_exports: HashMap<String, Vec<PathBuf>>,
97    /// Wildcard re-exports introduced by `pub import "..."`. Each entry is
98    /// the canonical path of a module whose entire public export surface
99    /// this module re-exports.
100    wildcard_re_export_paths: Vec<PathBuf>,
101    /// Namespace re-exports introduced by `pub import * as alias from "..."`.
102    /// Maps the published alias to the canonical target module path. Unlike
103    /// wildcard re-exports, members are **not** flattened into this module's
104    /// public surface — only the alias object itself is exported
105    /// (`DefKind::Variable`). Folded into [`exports`] / [`declarations`] at
106    /// collection time so graph finalization needs no second pass.
107    namespace_re_exports: HashMap<String, PathBuf>,
108    /// Names introduced by selective imports across this module.
109    selective_import_names: HashSet<String>,
110    /// Import references encountered in this file.
111    imports: Vec<ImportRef>,
112    /// True when at least one wildcard import could not be resolved.
113    has_unresolved_wildcard_import: bool,
114    /// True when at least one selective import could not be resolved
115    /// (importing file path missing). Prevents `imported_names_for_file`
116    /// from returning a partial answer when any import is broken.
117    has_unresolved_selective_import: bool,
118    /// True when at least one namespace import could not be resolved.
119    has_unresolved_namespace_import: bool,
120    /// Top-level type-like declarations that can be imported into a caller's
121    /// static type environment.
122    type_declarations: Vec<SNode>,
123    /// Top-level callable declarations whose signatures can be imported into
124    /// a caller's static type environment.
125    callable_declarations: Vec<SNode>,
126    /// Set when this module's own source failed to lex or parse. The module is
127    /// still recorded in the graph (with an otherwise-empty surface) so that
128    /// importers can be told their target is broken — instead of silently
129    /// seeing zero exports and mislabeling the imported symbol as "undefined"
130    /// at the call site.
131    load_error: Option<ModuleLoadError>,
132}
133
134/// A lex/parse failure captured while loading a module into the graph.
135///
136/// Retained so that `harn check <consumer>` can surface the real error in an
137/// imported file rather than downgrading its exports to "undefined" at the
138/// consumer's call site.
139#[derive(Debug, Clone)]
140pub struct ModuleLoadError {
141    /// Rendered lex/parse error message (includes the failing line:column).
142    pub message: String,
143    /// Span of the failure within the imported module's own source.
144    pub span: Span,
145}
146
147/// A consumer import whose resolved target module failed to compile. Reported
148/// by [`ModuleGraph::import_compile_failures`].
149#[derive(Debug, Clone)]
150pub struct ImportCompileFailure {
151    /// The import path exactly as written in the consumer.
152    pub import_raw_path: String,
153    /// Span of the consumer's `import` statement.
154    pub import_span: Span,
155    /// Canonical path of the broken imported module.
156    pub module_path: PathBuf,
157    /// The imported module's real lex/parse error.
158    pub error: ModuleLoadError,
159}
160
161#[derive(Debug, Clone)]
162struct ImportRef {
163    raw_path: String,
164    path: Option<PathBuf>,
165    selective_names: Option<HashSet<String>>,
166    /// When set, this is `import * as alias from "..."` — only the alias is
167    /// bound in the importer (members stay under the namespace object).
168    namespace_alias: Option<String>,
169    is_pub: bool,
170    import_span: Span,
171}
172
173/// Public import edge summary for static module graph consumers.
174#[derive(Debug, Clone, PartialEq, Eq)]
175pub struct ModuleImport {
176    /// The import string as written in source.
177    pub raw_path: String,
178    /// Resolved module path when the import could be resolved.
179    pub resolved_path: Option<PathBuf>,
180    /// `None` for wildcard / namespace imports; otherwise the selected names.
181    pub selective_names: Option<Vec<String>>,
182    /// When set, this is a namespace import (`import * as alias from "..."`).
183    pub namespace_alias: Option<String>,
184    /// Whether the import republishes its selected projection.
185    pub is_pub: bool,
186}
187
188/// Return the source for a resolved module path.
189///
190/// Real paths are read from disk. `<std>/<module>` virtual paths are backed by
191/// the embedded stdlib source table, so callers can parse resolved stdlib
192/// modules without knowing about the stdlib mirror layout.
193pub fn read_module_source(path: &Path) -> Option<String> {
194    if let Some(stdlib_module) = stdlib_module_from_path(path) {
195        return stdlib::get_stdlib_source(stdlib_module).map(ToString::to_string);
196    }
197    std::fs::read_to_string(path).ok()
198}
199
200/// Build a module graph from a set of files.
201///
202/// Files referenced via `import` statements are loaded recursively so the
203/// graph contains every module reachable from the seed set. Cycles and
204/// already-loaded files are skipped via a visited set.
205pub fn build(files: &[PathBuf]) -> ModuleGraph {
206    build_inner(files, None, false, None).graph
207}
208
209/// Build a module graph using caller-owned source for one root file.
210///
211/// Imported modules still resolve from their normal filesystem or embedded
212/// stdlib locations. This keeps editor diagnostics aligned with unsaved root
213/// buffers without creating a second module resolver.
214pub fn build_with_source(file: &Path, source: &str) -> ModuleGraph {
215    let file = normalize_path(file);
216    let source_overrides = HashMap::from([(file.clone(), source.to_string())]);
217    build_inner(&[file], None, false, Some(&source_overrides)).graph
218}
219
220/// Build a module graph while retaining parsed sources for the seed files.
221///
222/// Imported-only modules still participate in the graph, but their ASTs are
223/// dropped after graph extraction so callers do not pay extra peak memory for
224/// parsed sources they will not reuse.
225pub fn build_with_parsed_sources(files: &[PathBuf]) -> ModuleGraphBuild {
226    let parsed_source_targets = files.iter().map(|file| normalize_path(file)).collect();
227    build_inner(files, Some(&parsed_source_targets), false, None)
228}
229
230/// Build a closed module graph while retaining every reachable parsed source.
231///
232/// This is intentionally separate from editor-oriented graph construction:
233/// package linking consumes every module AST, while ordinary diagnostics should
234/// not retain an entire dependency graph after extracting its public surface.
235pub fn build_closed_program(files: &[PathBuf]) -> ModuleGraphBuild {
236    build_inner(files, None, true, None)
237}
238
239fn build_inner(
240    files: &[PathBuf],
241    parsed_source_targets: Option<&HashSet<PathBuf>>,
242    retain_all_parsed_sources: bool,
243    source_overrides: Option<&HashMap<PathBuf, String>>,
244) -> ModuleGraphBuild {
245    let package_snapshots = acquire_package_snapshots(files);
246    let mut modules: HashMap<PathBuf, ModuleInfo> = HashMap::new();
247    let mut parsed_sources: HashMap<PathBuf, ParsedModuleSource> = HashMap::new();
248    let mut seen: HashSet<PathBuf> = HashSet::new();
249    let mut wave: Vec<PathBuf> = Vec::new();
250    for file in files {
251        let canonical = normalize_path(file);
252        if seen.insert(canonical.clone()) {
253            wave.push(canonical);
254        }
255    }
256    // Breadth-first over import waves. Every path in a wave is new (the
257    // `seen` set dedupes before enqueue), and `load_module` is a pure
258    // read+lex+parse+extract, so each wave loads in parallel; discovery of
259    // the next wave stays sequential to keep the dedup deterministic. A
260    // whole-tree seed set arrives as one large first wave, which is where
261    // nearly all the parse work is, so the serial-BFS tail on deep import
262    // chains does not matter in practice.
263    while !wave.is_empty() {
264        let loaded = load_wave(&wave, &package_snapshots, source_overrides);
265        let mut next_wave: Vec<PathBuf> = Vec::new();
266        for (path, (module, parsed)) in wave.drain(..).zip(loaded) {
267            let retain_parsed_source = retain_all_parsed_sources
268                || parsed_source_targets.is_some_and(|targets| targets.contains(&path));
269            if retain_parsed_source {
270                if let Some(parsed) = parsed {
271                    parsed_sources.insert(path.clone(), parsed);
272                }
273            }
274            // Enqueue resolved import targets so the whole reachable graph is
275            // discovered without the caller having to pre-walk imports.
276            //
277            // `resolve_import_path` returns paths as `base.join(import)` —
278            // i.e. with `..` segments preserved rather than collapsed. If we
279            // dedupe on those raw forms, two files that import each other
280            // across sibling dirs (`lib/context/` ↔ `lib/runtime/`) produce a
281            // different path spelling on every cycle — `.../context/../runtime/`,
282            // then `.../context/../runtime/../context/`, and so on — each of
283            // which is treated as a new file. The walk only terminates when
284            // `path.exists()` starts failing at the filesystem's `PATH_MAX`,
285            // which is 1024 on macOS but 4096 on Linux. Linux therefore
286            // re-parses the same handful of files thousands of times, balloons
287            // RSS into the multi-GB range, and gets SIGKILL'd by CI runners.
288            // Canonicalize once here so `seen` dedupes by the underlying file,
289            // not by its path spelling.
290            for import in &module.imports {
291                if let Some(import_path) = &import.path {
292                    let canonical = normalize_path(import_path);
293                    if seen.insert(canonical.clone()) {
294                        next_wave.push(canonical);
295                    }
296                }
297            }
298            modules.insert(path, module);
299        }
300        wave = next_wave;
301    }
302    resolve_re_exports(&mut modules);
303    ModuleGraphBuild {
304        graph: ModuleGraph {
305            modules,
306            _package_snapshots: package_snapshots,
307        },
308        parsed_sources,
309    }
310}
311
312/// Environment override for the graph-build worker-pool size. `1` forces the
313/// serial walk; unset defaults to the machine's available parallelism.
314pub const MODULE_GRAPH_JOBS_ENV: &str = "HARN_MODULE_GRAPH_JOBS";
315
316/// Load one BFS wave of module paths, in parallel when the wave is large
317/// enough to pay for the threads. Results are index-aligned with `paths`.
318fn load_wave(
319    paths: &[PathBuf],
320    package_snapshots: &[PackageSnapshot],
321    source_overrides: Option<&HashMap<PathBuf, String>>,
322) -> Vec<(ModuleInfo, Option<ParsedModuleSource>)> {
323    const MIN_PARALLEL_WAVE: usize = 8;
324    let configured = std::env::var(MODULE_GRAPH_JOBS_ENV)
325        .ok()
326        .and_then(|value| value.parse::<usize>().ok())
327        .filter(|&jobs| jobs > 0);
328    let workers = configured
329        .unwrap_or_else(|| {
330            std::thread::available_parallelism()
331                .map(std::num::NonZeroUsize::get)
332                .unwrap_or(1)
333        })
334        .min(paths.len());
335    if workers <= 1 || paths.len() < MIN_PARALLEL_WAVE {
336        return paths
337            .iter()
338            .map(|path| load_module(path, package_snapshots, source_overrides))
339            .collect();
340    }
341    let next = std::sync::atomic::AtomicUsize::new(0);
342    let mut produced: Vec<(usize, (ModuleInfo, Option<ParsedModuleSource>))> =
343        std::thread::scope(|scope| {
344            let handles: Vec<_> = (0..workers)
345                .map(|_| {
346                    scope.spawn(|| {
347                        let mut local = Vec::new();
348                        loop {
349                            let index = next.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
350                            let Some(path) = paths.get(index) else {
351                                break;
352                            };
353                            local.push((
354                                index,
355                                load_module(path, package_snapshots, source_overrides),
356                            ));
357                        }
358                        local
359                    })
360                })
361                .collect();
362            handles
363                .into_iter()
364                .flat_map(|handle| match handle.join() {
365                    Ok(local) => local,
366                    Err(panic) => std::panic::resume_unwind(panic),
367                })
368                .collect()
369        });
370    produced.sort_unstable_by_key(|(index, _)| *index);
371    produced.into_iter().map(|(_, loaded)| loaded).collect()
372}
373
374/// Iteratively expand each module's `exports` set to include the transitive
375/// public surface of its `pub import "..."` re-export targets. Cycles are
376/// safe because the loop only adds names — once no module's set grows in a
377/// pass, the fixpoint is reached.
378fn resolve_re_exports(modules: &mut HashMap<PathBuf, ModuleInfo>) {
379    let keys: Vec<PathBuf> = modules.keys().cloned().collect();
380    loop {
381        let mut changed = false;
382        for path in &keys {
383            // Snapshot the wildcard target list and gather the union of
384            // their current exports without holding a mutable borrow.
385            let wildcard_paths = modules
386                .get(path)
387                .map(|m| m.wildcard_re_export_paths.clone())
388                .unwrap_or_default();
389            if wildcard_paths.is_empty() {
390                continue;
391            }
392            let mut additions: Vec<String> = Vec::new();
393            for src in &wildcard_paths {
394                let src_canonical = normalize_path(src);
395                if let Some(src_module) = modules.get(src).or_else(|| modules.get(&src_canonical)) {
396                    additions.extend(src_module.exports.iter().cloned());
397                }
398            }
399            if let Some(module) = modules.get_mut(path) {
400                for name in additions {
401                    if module.exports.insert(name) {
402                        changed = true;
403                    }
404                }
405            }
406        }
407        if !changed {
408            break;
409        }
410    }
411}
412
413impl ModuleGraph {
414    /// Sorted list of every module path discovered by [`build`]. Includes
415    /// `<std>/<name>` virtual paths for stdlib modules reached transitively.
416    /// Callers that want only real-disk modules can filter for paths whose
417    /// string form does not start with `<std>/`.
418    pub fn module_paths(&self) -> Vec<PathBuf> {
419        let mut paths: Vec<PathBuf> = self.modules.keys().cloned().collect();
420        paths.sort();
421        paths
422    }
423
424    /// True when `path` (or its canonical form) was discovered during the
425    /// module-graph walk.
426    pub fn contains_module(&self, path: &Path) -> bool {
427        self.modules.contains_key(path) || self.modules.contains_key(&normalize_path(path))
428    }
429
430    /// Collect every name used in selective imports from all files.
431    pub fn all_selective_import_names(&self) -> HashSet<&str> {
432        let mut names = HashSet::new();
433        for module in self.modules.values() {
434            for name in &module.selective_import_names {
435                names.insert(name.as_str());
436            }
437        }
438        names
439    }
440
441    /// Files that directly import `target`. Resolves `target` to a
442    /// canonical path before lookup so callers can pass either spelling.
443    pub fn importers_of(&self, target: &Path) -> Vec<PathBuf> {
444        let target = normalize_path(target);
445        let mut out: Vec<PathBuf> = self
446            .modules
447            .iter()
448            .filter(|(_, info)| {
449                info.imports.iter().any(|import| {
450                    import
451                        .path
452                        .as_ref()
453                        .is_some_and(|p| normalize_path(p) == target)
454                })
455            })
456            .map(|(path, _)| path.clone())
457            .collect();
458        out.sort();
459        out
460    }
461
462    /// Import edges declared by `file`, sorted by raw path and selected names.
463    pub fn imports_for_module(&self, file: &Path) -> Vec<ModuleImport> {
464        let file = normalize_path(file);
465        let Some(module) = self.modules.get(&file) else {
466            return Vec::new();
467        };
468        let mut imports: Vec<ModuleImport> = module
469            .imports
470            .iter()
471            .map(|import| {
472                let mut selective_names = import
473                    .selective_names
474                    .as_ref()
475                    .map(|names| names.iter().cloned().collect::<Vec<_>>());
476                if let Some(names) = selective_names.as_mut() {
477                    names.sort();
478                }
479                ModuleImport {
480                    raw_path: import.raw_path.clone(),
481                    resolved_path: import.path.as_ref().map(|path| normalize_path(path)),
482                    selective_names,
483                    namespace_alias: import.namespace_alias.clone(),
484                    is_pub: import.is_pub,
485                }
486            })
487            .collect();
488        imports.sort_by(|left, right| {
489            left.raw_path
490                .cmp(&right.raw_path)
491                .then_with(|| left.selective_names.cmp(&right.selective_names))
492                .then_with(|| left.namespace_alias.cmp(&right.namespace_alias))
493                .then_with(|| left.resolved_path.cmp(&right.resolved_path))
494        });
495        imports
496    }
497
498    /// Exported symbol names for `file`, sorted alphabetically.
499    pub fn exports_for_module(&self, file: &Path) -> Vec<String> {
500        let file = normalize_path(file);
501        let Some(module) = self.modules.get(&file) else {
502            return Vec::new();
503        };
504        let mut exports: Vec<String> = module.exports.iter().cloned().collect();
505        exports.sort();
506        exports
507    }
508
509    /// Resolve wildcard imports for `file`.
510    ///
511    /// Returns `Unknown` when any wildcard import cannot be resolved, because
512    /// callers should conservatively disable wildcard-import-sensitive checks.
513    pub fn wildcard_exports_for(&self, file: &Path) -> WildcardResolution {
514        let file = normalize_path(file);
515        let Some(module) = self.modules.get(&file) else {
516            return WildcardResolution::Unknown;
517        };
518        if module.has_unresolved_wildcard_import {
519            return WildcardResolution::Unknown;
520        }
521
522        let mut names = HashSet::new();
523        for import in module
524            .imports
525            .iter()
526            .filter(|import| import.selective_names.is_none())
527        {
528            let Some(import_path) = &import.path else {
529                return WildcardResolution::Unknown;
530            };
531            let imported = self.modules.get(import_path).or_else(|| {
532                let normalized = normalize_path(import_path);
533                self.modules.get(&normalized)
534            });
535            let Some(imported) = imported else {
536                return WildcardResolution::Unknown;
537            };
538            names.extend(imported.exports.iter().cloned());
539        }
540        WildcardResolution::Resolved(names)
541    }
542
543    /// Collect every statically callable/referenceable name introduced into
544    /// `file` by its imports.
545    ///
546    /// Returns `Some` only when **every** import (wildcard or selective) in
547    /// `file` is fully resolvable via the graph. Returns `None` when any
548    /// import is unresolved, so callers can fall back to conservative
549    /// behavior instead of emitting spurious "undefined name" errors.
550    ///
551    /// The returned set contains:
552    /// - all public exports from wildcard-imported modules (transitively
553    ///   following `pub import` re-export chains), and
554    /// - selectively imported names that the target module actually exports
555    ///   (its `pub` surface or re-exports) — matching what the VM accepts at
556    ///   runtime. A name that exists only privately in the target is NOT
557    ///   importable.
558    ///
559    /// Every import in `file` whose resolved target module failed to lex or
560    /// parse. Lets `harn check <file>` surface the real error inside the
561    /// imported module (anchored at the consumer's `import` statement) instead
562    /// of downgrading the imported symbols to "undefined" at their call sites.
563    #[must_use]
564    pub fn import_compile_failures(&self, file: &Path) -> Vec<ImportCompileFailure> {
565        let file = normalize_path(file);
566        let Some(module) = self.modules.get(&file) else {
567            return Vec::new();
568        };
569        let mut failures = Vec::new();
570        for import in &module.imports {
571            let Some(import_path) = &import.path else {
572                continue;
573            };
574            let Some(target) = self
575                .modules
576                .get(import_path)
577                .or_else(|| self.modules.get(&normalize_path(import_path)))
578            else {
579                continue;
580            };
581            if let Some(error) = &target.load_error {
582                failures.push(ImportCompileFailure {
583                    import_raw_path: import.raw_path.clone(),
584                    import_span: import.import_span,
585                    module_path: normalize_path(import_path),
586                    error: error.clone(),
587                });
588            }
589        }
590        failures
591    }
592
593    pub fn imported_names_for_file(&self, file: &Path) -> Option<HashSet<String>> {
594        let file = normalize_path(file);
595        let module = self.modules.get(&file)?;
596        if module.has_unresolved_wildcard_import
597            || module.has_unresolved_selective_import
598            || module.has_unresolved_namespace_import
599        {
600            return None;
601        }
602
603        let mut names = HashSet::new();
604        for import in &module.imports {
605            // Namespace imports bind only the alias — never flatten members.
606            if let Some(alias) = &import.namespace_alias {
607                names.insert(alias.clone());
608                continue;
609            }
610            let import_path = import.path.as_ref()?;
611            let imported = self
612                .modules
613                .get(import_path)
614                .or_else(|| self.modules.get(&normalize_path(import_path)))?;
615            // The target parsed nothing (lex/parse failure). Fall back to the
616            // conservative `None` answer so the cross-module undefined-name
617            // check stays silent — the real error is surfaced separately by
618            // `import_compile_failures`, not mislabeled as an undefined symbol
619            // at this consumer's call site.
620            if imported.load_error.is_some() {
621                return None;
622            }
623            match &import.selective_names {
624                None => {
625                    names.extend(imported.exports.iter().cloned());
626                }
627                Some(selective) => {
628                    // A selectively imported name is in scope when it exists in
629                    // the target module (as a declaration or a re-export). The
630                    // stricter "must be `pub`" check is reported precisely at
631                    // the import site by the `HARN-IMP-002` preflight scan
632                    // (`scan_selective_import_visibility`) and enforced at load
633                    // time, so it is intentionally *not* duplicated here —
634                    // otherwise a private import would surface both an
635                    // import-site and a redundant call-site error.
636                    for name in selective {
637                        if imported.declarations.contains_key(name)
638                            || imported.exports.contains(name)
639                        {
640                            names.insert(name.clone());
641                        }
642                    }
643                }
644            }
645        }
646        Some(names)
647    }
648
649    /// Collect imported names of one declaration kind from a module's public
650    /// imports. This preserves the target module's export boundary across
651    /// selective, wildcard, and re-exported imports while giving compilers
652    /// the narrow semantic metadata needed for syntax-sensitive lowering.
653    pub fn imported_names_by_kind_for_file(
654        &self,
655        file: &Path,
656        kind: DefKind,
657    ) -> Option<HashSet<String>> {
658        let file = normalize_path(file);
659        let module = self.modules.get(&file)?;
660        if module.has_unresolved_wildcard_import
661            || module.has_unresolved_selective_import
662            || module.has_unresolved_namespace_import
663        {
664            return None;
665        }
666
667        let mut names = HashSet::new();
668        for import in &module.imports {
669            // Namespace aliases are variables, not flattened member kinds.
670            if import.namespace_alias.is_some() {
671                continue;
672            }
673            let import_path = import.path.as_ref()?;
674            let imported_names: Vec<String> = match &import.selective_names {
675                Some(selective) => selective.iter().cloned().collect(),
676                None => self
677                    .modules
678                    .get(import_path)
679                    .or_else(|| self.modules.get(&normalize_path(import_path)))?
680                    .exports
681                    .iter()
682                    .cloned()
683                    .collect(),
684            };
685            for name in imported_names {
686                if self.exported_kind(import_path, &name) == Some(kind) {
687                    names.insert(name);
688                }
689            }
690        }
691        Some(names)
692    }
693
694    /// Collect type / struct / enum / interface declarations made visible to
695    /// `file` by its imports. Returns `None` when any import is unresolved so
696    /// callers can fall back to conservative behavior.
697    pub fn imported_type_declarations_for_file(&self, file: &Path) -> Option<Vec<SNode>> {
698        let file = normalize_path(file);
699        let module = self.modules.get(&file)?;
700        if module.has_unresolved_wildcard_import
701            || module.has_unresolved_selective_import
702            || module.has_unresolved_namespace_import
703        {
704            return None;
705        }
706
707        let mut decls = Vec::new();
708        let mut seen = HashSet::new();
709        for import in &module.imports {
710            // Namespace imports do not flatten type names into the caller.
711            if import.namespace_alias.is_some() {
712                continue;
713            }
714            let import_path = import.path.as_ref()?;
715            let imported = self
716                .modules
717                .get(import_path)
718                .or_else(|| self.modules.get(&normalize_path(import_path)))?;
719            // The target parsed nothing (lex/parse failure). Fall back to the
720            // conservative `None` answer so the cross-module undefined-name
721            // check stays silent — the real error is surfaced separately by
722            // `import_compile_failures`, not mislabeled as an undefined symbol
723            // at this consumer's call site.
724            if imported.load_error.is_some() {
725                return None;
726            }
727            let mut names_to_collect: Vec<String> = match &import.selective_names {
728                None => imported.exports.iter().cloned().collect(),
729                Some(selective) => selective.iter().cloned().collect(),
730            };
731            names_to_collect.sort();
732            for name in &names_to_collect {
733                let mut visited = HashSet::new();
734                if let Some(decl) = self.find_exported_type_decl(import_path, name, &mut visited) {
735                    let origin = self
736                        .export_definition_of(import_path, name)
737                        .map_or_else(|| import_path.clone(), |definition| definition.file);
738                    self.extend_type_dependency(&origin, &decl, &mut decls, &mut seen);
739                }
740            }
741            // Every type alias / struct / enum / interface declared in the
742            // imported module is visible to the *typechecker*, `pub` or not:
743            // an imported fn's signature may reference a module-private alias
744            // ("options: PickKeysOptions"), and without its definition the
745            // caller sees only a phantom `Named(...)` and skips contract
746            // checks. This visibility is typing-only — name-level import
747            // privacy is still enforced by `selective_import_issues`
748            // and the runtime loader, which reject importing a non-`pub`
749            // type by name.
750            for ty_decl in &imported.type_declarations {
751                if type_decl_name(ty_decl).is_some() {
752                    self.extend_type_dependency(import_path, ty_decl, &mut decls, &mut seen);
753                }
754            }
755
756            for name in &names_to_collect {
757                let mut visited = HashSet::new();
758                let Some(callable) =
759                    self.find_exported_callable_decl(import_path, name, &mut visited)
760                else {
761                    continue;
762                };
763                let origin = self
764                    .export_definition_of(import_path, name)
765                    .map_or_else(|| import_path.clone(), |definition| definition.file);
766                self.extend_callable_type_dependencies(&origin, &callable, &mut decls, &mut seen);
767            }
768        }
769        Some(decls)
770    }
771
772    /// Collect callable declarations made visible to `file` by its imports.
773    /// Only signatures are consumed by the type checker; imported bodies
774    /// remain owned by their defining modules.
775    pub fn imported_callable_declarations_for_file(&self, file: &Path) -> Option<Vec<SNode>> {
776        let file = normalize_path(file);
777        let module = self.modules.get(&file)?;
778        if module.has_unresolved_wildcard_import
779            || module.has_unresolved_selective_import
780            || module.has_unresolved_namespace_import
781        {
782            return None;
783        }
784
785        let mut decls = Vec::new();
786        for import in &module.imports {
787            // Namespace imports keep callables under the alias object.
788            if import.namespace_alias.is_some() {
789                continue;
790            }
791            let import_path = import.path.as_ref()?;
792            let imported = self
793                .modules
794                .get(import_path)
795                .or_else(|| self.modules.get(&normalize_path(import_path)))?;
796            // The target parsed nothing (lex/parse failure). Fall back to the
797            // conservative `None` answer so the cross-module undefined-name
798            // check stays silent — the real error is surfaced separately by
799            // `import_compile_failures`, not mislabeled as an undefined symbol
800            // at this consumer's call site.
801            if imported.load_error.is_some() {
802                return None;
803            }
804            let selective_import = import.selective_names.is_some();
805            let names_to_collect: Vec<String> = match &import.selective_names {
806                None => imported.exports.iter().cloned().collect(),
807                Some(selective) => selective.iter().cloned().collect(),
808            };
809            for name in &names_to_collect {
810                if selective_import || imported.own_exports.contains(name) {
811                    if let Some(decl) = imported
812                        .callable_declarations
813                        .iter()
814                        .find(|decl| callable_decl_name(decl) == Some(name.as_str()))
815                    {
816                        decls.push(decl.clone());
817                        continue;
818                    }
819                }
820                let mut visited = HashSet::new();
821                if let Some(decl) =
822                    self.find_exported_callable_decl(import_path, name, &mut visited)
823                {
824                    decls.push(decl);
825                }
826            }
827        }
828        Some(decls)
829    }
830
831    /// Walk a module's local type declarations and re-export chains to find
832    /// the SNode for an exported type/struct/enum/interface named `name`.
833    fn find_exported_type_decl(
834        &self,
835        path: &Path,
836        name: &str,
837        visited: &mut HashSet<PathBuf>,
838    ) -> Option<SNode> {
839        let canonical = normalize_path(path);
840        if !visited.insert(canonical.clone()) {
841            return None;
842        }
843        let module = self
844            .modules
845            .get(&canonical)
846            .or_else(|| self.modules.get(path))?;
847        for decl in &module.type_declarations {
848            if type_decl_name(decl) == Some(name) && module.own_exports.contains(name) {
849                return Some(decl.clone());
850            }
851        }
852        if let Some(sources) = module.selective_re_exports.get(name) {
853            for source in sources {
854                if let Some(decl) = self.find_exported_type_decl(source, name, visited) {
855                    return Some(decl);
856                }
857            }
858        }
859        for source in &module.wildcard_re_export_paths {
860            if let Some(decl) = self.find_exported_type_decl(source, name, visited) {
861                return Some(decl);
862            }
863        }
864        None
865    }
866
867    fn find_exported_callable_decl(
868        &self,
869        path: &Path,
870        name: &str,
871        visited: &mut HashSet<PathBuf>,
872    ) -> Option<SNode> {
873        let canonical = normalize_path(path);
874        if !visited.insert(canonical.clone()) {
875            return None;
876        }
877        let module = self
878            .modules
879            .get(&canonical)
880            .or_else(|| self.modules.get(path))?;
881        for decl in &module.callable_declarations {
882            if callable_decl_name(decl) == Some(name) && module.own_exports.contains(name) {
883                return Some(decl.clone());
884            }
885        }
886        if let Some(sources) = module.selective_re_exports.get(name) {
887            for source in sources {
888                if let Some(decl) = self.find_exported_callable_decl(source, name, visited) {
889                    return Some(decl);
890                }
891            }
892        }
893        for source in &module.wildcard_re_export_paths {
894            if let Some(decl) = self.find_exported_callable_decl(source, name, visited) {
895                return Some(decl);
896            }
897        }
898        None
899    }
900
901    /// Find the definition of `name` visible from `file`.
902    ///
903    /// Recurses through `pub import` re-export chains so go-to-definition
904    /// lands on the symbol's actual declaration site instead of the facade
905    /// module that forwarded it.
906    pub fn definition_of(&self, file: &Path, name: &str) -> Option<DefSite> {
907        let mut visited = HashSet::new();
908        self.definition_of_inner(file, name, &mut visited)
909    }
910
911    /// Find the declaration that contributes exported `name` to `file`.
912    ///
913    /// Unlike [`Self::definition_of`], this ignores private local declarations
914    /// and private imports. It follows only the module's public declaration and
915    /// `pub import` graph, making it suitable for package manifests, API docs,
916    /// and other consumers of a module's externally visible surface.
917    pub fn export_definition_of(&self, file: &Path, name: &str) -> Option<DefSite> {
918        let mut visited = HashSet::new();
919        self.export_definition_of_inner(file, name, &mut visited)
920    }
921
922    /// Sorted names of every declaration recorded for `file` (functions,
923    /// pipelines, tools, structs, ...). Used by the check-result cache to
924    /// key the cross-file lint-exemption subset that applies to this file.
925    pub fn declared_names_for_file(&self, file: &Path) -> Option<Vec<&str>> {
926        let module = self.modules.get(&normalize_path(file))?;
927        let mut names: Vec<&str> = module.declarations.keys().map(String::as_str).collect();
928        names.sort_unstable();
929        Some(names)
930    }
931
932    fn definition_of_inner(
933        &self,
934        file: &Path,
935        name: &str,
936        visited: &mut HashSet<PathBuf>,
937    ) -> Option<DefSite> {
938        let file = normalize_path(file);
939        if !visited.insert(file.clone()) {
940            return None;
941        }
942        let current = self.modules.get(&file)?;
943
944        if let Some(local) = current.declarations.get(name) {
945            return Some(local.clone());
946        }
947
948        // `pub import { name } from "..."` — follow the first recorded
949        // source. Conflicting re-exports surface separately as
950        // diagnostics; here we just pick a canonical destination so
951        // go-to-definition lands somewhere useful.
952        if let Some(sources) = current.selective_re_exports.get(name) {
953            for source in sources {
954                if let Some(def) = self.definition_of_inner(source, name, visited) {
955                    return Some(def);
956                }
957            }
958        }
959
960        // `pub import "..."` — chase each wildcard re-export source.
961        for source in &current.wildcard_re_export_paths {
962            if let Some(def) = self.definition_of_inner(source, name, visited) {
963                return Some(def);
964            }
965        }
966
967        // Private selective imports.
968        for import in &current.imports {
969            let Some(selective_names) = &import.selective_names else {
970                continue;
971            };
972            if !selective_names.contains(name) {
973                continue;
974            }
975            if let Some(path) = &import.path {
976                if let Some(def) = self.definition_of_inner(path, name, visited) {
977                    return Some(def);
978                }
979            }
980        }
981
982        // Private wildcard imports (not namespace — those bind only the alias).
983        for import in &current.imports {
984            if import.selective_names.is_some() || import.namespace_alias.is_some() {
985                continue;
986            }
987            if let Some(path) = &import.path {
988                if let Some(def) = self.definition_of_inner(path, name, visited) {
989                    return Some(def);
990                }
991            }
992        }
993
994        None
995    }
996
997    fn export_definition_of_inner(
998        &self,
999        file: &Path,
1000        name: &str,
1001        visited: &mut HashSet<PathBuf>,
1002    ) -> Option<DefSite> {
1003        let file = normalize_path(file);
1004        if !visited.insert(file.clone()) {
1005            return None;
1006        }
1007        let current = self.modules.get(&file)?;
1008
1009        if current.own_exports.contains(name) {
1010            if let Some(local) = current.declarations.get(name) {
1011                return Some(local.clone());
1012            }
1013        }
1014        if let Some(sources) = current.selective_re_exports.get(name) {
1015            for source in sources {
1016                if let Some(definition) = self.export_definition_of_inner(source, name, visited) {
1017                    return Some(definition);
1018                }
1019            }
1020        }
1021        for source in &current.wildcard_re_export_paths {
1022            if let Some(definition) = self.export_definition_of_inner(source, name, visited) {
1023                return Some(definition);
1024            }
1025        }
1026        None
1027    }
1028
1029    /// Diagnostics for re-export conflicts inside `file`. Each diagnostic
1030    /// names the conflicting symbol and the modules that contributed it,
1031    /// so check-time errors can be precise.
1032    pub fn re_export_conflicts(&self, file: &Path) -> Vec<ReExportConflict> {
1033        let file = normalize_path(file);
1034        let Some(module) = self.modules.get(&file) else {
1035            return Vec::new();
1036        };
1037
1038        // Build, for each re-exported name, the set of source modules it
1039        // could resolve to. Names that resolve to more than one source are
1040        // ambiguous and reported.
1041        let mut sources: HashMap<String, Vec<PathBuf>> = HashMap::new();
1042
1043        for (name, srcs) in &module.selective_re_exports {
1044            sources
1045                .entry(name.clone())
1046                .or_default()
1047                .extend(srcs.iter().cloned());
1048        }
1049        for src in &module.wildcard_re_export_paths {
1050            let canonical = normalize_path(src);
1051            let Some(src_module) = self
1052                .modules
1053                .get(&canonical)
1054                .or_else(|| self.modules.get(src))
1055            else {
1056                continue;
1057            };
1058            for name in &src_module.exports {
1059                sources
1060                    .entry(name.clone())
1061                    .or_default()
1062                    .push(canonical.clone());
1063            }
1064        }
1065
1066        // A re-export that collides with a locally exported declaration is
1067        // also an error: the facade module cannot expose two different
1068        // bindings under the same name.
1069        for name in &module.own_exports {
1070            if let Some(entry) = sources.get_mut(name) {
1071                entry.push(file.clone());
1072            }
1073        }
1074
1075        let mut conflicts = Vec::new();
1076        for (name, mut srcs) in sources {
1077            srcs.sort();
1078            srcs.dedup();
1079            if srcs.len() > 1 {
1080                conflicts.push(ReExportConflict {
1081                    name,
1082                    sources: srcs,
1083                });
1084            }
1085        }
1086        conflicts.sort_by(|a, b| a.name.cmp(&b.name));
1087        conflicts
1088    }
1089
1090    /// Invalid selective imports in `file`, classified as missing or private.
1091    ///
1092    /// This is the static single source of truth for the runtime's export
1093    /// boundary. Consumers project the typed issue into CLI or editor
1094    /// diagnostics without independently re-resolving names or spans.
1095    pub fn selective_import_issues(&self, file: &Path) -> Vec<SelectiveImportIssue> {
1096        let file = normalize_path(file);
1097        let Some(module) = self.modules.get(&file) else {
1098            return Vec::new();
1099        };
1100
1101        let mut out = Vec::new();
1102        for import in &module.imports {
1103            let Some(selective) = &import.selective_names else {
1104                continue;
1105            };
1106            let Some(import_path) = &import.path else {
1107                continue;
1108            };
1109            let Some(target) = self
1110                .modules
1111                .get(import_path)
1112                .or_else(|| self.modules.get(&normalize_path(import_path)))
1113            else {
1114                continue;
1115            };
1116            if target.load_error.is_some() {
1117                continue;
1118            }
1119            for name in selective {
1120                let kind = if target.exports.contains(name) {
1121                    continue;
1122                } else if target.declarations.contains_key(name) {
1123                    SelectiveImportIssueKind::Private
1124                } else {
1125                    SelectiveImportIssueKind::Missing
1126                };
1127                out.push(SelectiveImportIssue {
1128                    name: name.clone(),
1129                    module: import.raw_path.clone(),
1130                    span: import.import_span,
1131                    kind,
1132                });
1133            }
1134        }
1135        out.sort_by(|a, b| (&a.name, &a.module, a.kind).cmp(&(&b.name, &b.module, b.kind)));
1136        out.dedup();
1137        out
1138    }
1139
1140    /// Return the declaration kind for a name on a module's public surface.
1141    /// Explicit and wildcard re-exports are followed so callers can consume
1142    /// the same source-owned contract regardless of the import path used.
1143    pub fn exported_kind(&self, file: &Path, name: &str) -> Option<DefKind> {
1144        self.exported_kind_inner(file, name, &mut HashSet::new())
1145    }
1146
1147    fn exported_kind_inner(
1148        &self,
1149        file: &Path,
1150        name: &str,
1151        visited: &mut HashSet<PathBuf>,
1152    ) -> Option<DefKind> {
1153        let file = normalize_path(file);
1154        if !visited.insert(file.clone()) {
1155            return None;
1156        }
1157        let result = self.modules.get(&file).and_then(|module| {
1158            if module.own_exports.contains(name) {
1159                return module
1160                    .declarations
1161                    .get(name)
1162                    .map(|definition| definition.kind)
1163                    .or_else(|| {
1164                        stdlib_module_from_path(&file).and_then(|stdlib_module| {
1165                            stdlib::builtin_reexports(stdlib_module)
1166                                .contains(&name)
1167                                .then_some(DefKind::Function)
1168                        })
1169                    });
1170            }
1171            if let Some(sources) = module.selective_re_exports.get(name) {
1172                for source in sources {
1173                    if let Some(kind) = self.exported_kind_inner(source, name, visited) {
1174                        return Some(kind);
1175                    }
1176                }
1177            }
1178            for source in &module.wildcard_re_export_paths {
1179                if let Some(kind) = self.exported_kind_inner(source, name, visited) {
1180                    return Some(kind);
1181                }
1182            }
1183            None
1184        });
1185        visited.remove(&file);
1186        result
1187    }
1188}
1189
1190/// A duplicate or ambiguous re-export inside a single module. Reported by
1191/// [`ModuleGraph::re_export_conflicts`].
1192#[derive(Debug, Clone, PartialEq, Eq)]
1193pub struct ReExportConflict {
1194    pub name: String,
1195    pub sources: Vec<PathBuf>,
1196}
1197
1198/// Why a selective import cannot cross the target module's export boundary.
1199#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
1200pub enum SelectiveImportIssueKind {
1201    /// No declaration or transitive export has the requested name.
1202    Missing,
1203    /// The target declares the requested name without exporting it.
1204    Private,
1205}
1206
1207/// A selective import rejected by the target module's public surface.
1208#[derive(Debug, Clone, PartialEq, Eq)]
1209pub struct SelectiveImportIssue {
1210    /// The requested name.
1211    pub name: String,
1212    /// The module path exactly as written in the import statement.
1213    pub module: String,
1214    /// Span anchored to the selective import statement.
1215    pub span: Span,
1216    /// Whether the requested name is absent or private.
1217    pub kind: SelectiveImportIssueKind,
1218}
1219
1220impl SelectiveImportIssue {
1221    /// Stable user-facing explanation shared by CLI and editor projections.
1222    #[must_use]
1223    pub fn message(&self) -> String {
1224        match self.kind {
1225            SelectiveImportIssueKind::Missing => format!(
1226                "imported symbol `{}` does not exist in `{}`",
1227                self.name, self.module
1228            ),
1229            SelectiveImportIssueKind::Private => format!(
1230                "imported symbol `{}` is not exported by `{}` — it is defined there but not `pub`",
1231                self.name, self.module
1232            ),
1233        }
1234    }
1235
1236    /// Stable repair guidance shared by CLI and editor projections.
1237    #[must_use]
1238    pub fn help(&self) -> String {
1239        match self.kind {
1240            SelectiveImportIssueKind::Missing => format!(
1241                "update the import to a symbol exported by `{}`",
1242                self.module
1243            ),
1244            SelectiveImportIssueKind::Private => {
1245                format!(
1246                    "mark `{}` as `pub` in `{}` to export it",
1247                    self.name, self.module
1248                )
1249            }
1250        }
1251    }
1252}
1253
1254fn load_module(
1255    path: &Path,
1256    package_snapshots: &[PackageSnapshot],
1257    source_overrides: Option<&HashMap<PathBuf, String>>,
1258) -> (ModuleInfo, Option<ParsedModuleSource>) {
1259    let source = source_overrides
1260        .and_then(|overrides| overrides.get(&normalize_path(path)).cloned())
1261        .or_else(|| read_module_source(path));
1262    let Some(source) = source else {
1263        return (ModuleInfo::default(), None);
1264    };
1265    let mut lexer = harn_lexer::Lexer::new(&source);
1266    let tokens = match lexer.tokenize() {
1267        Ok(tokens) => tokens,
1268        Err(error) => {
1269            let module = ModuleInfo {
1270                load_error: Some(ModuleLoadError {
1271                    message: error.to_string(),
1272                    span: error.span(),
1273                }),
1274                ..ModuleInfo::default()
1275            };
1276            return (module, None);
1277        }
1278    };
1279    let mut parser = Parser::new(tokens);
1280    let program = match parser.parse() {
1281        Ok(program) => program,
1282        Err(error) => {
1283            let module = ModuleInfo {
1284                load_error: Some(ModuleLoadError {
1285                    message: error.to_string(),
1286                    span: error.span(),
1287                }),
1288                ..ModuleInfo::default()
1289            };
1290            return (module, None);
1291        }
1292    };
1293
1294    let mut module = ModuleInfo::default();
1295    for node in &program {
1296        collect_module_info(path, node, &mut module, package_snapshots);
1297        collect_type_declarations(node, &mut module.type_declarations);
1298        collect_callable_declarations(node, &mut module.callable_declarations);
1299    }
1300    if let Some(stdlib_module) = stdlib_module_from_path(path) {
1301        module.own_exports.extend(
1302            stdlib::builtin_reexports(stdlib_module)
1303                .iter()
1304                .map(|name| (*name).to_string()),
1305        );
1306    }
1307    // Seed the transitive `exports` set from local exports plus selective
1308    // re-export names. Wildcard re-exports are folded in by
1309    // [`resolve_re_exports`] after every module has been loaded.
1310    module.exports.extend(module.own_exports.iter().cloned());
1311    module
1312        .exports
1313        .extend(module.selective_re_exports.keys().cloned());
1314    let parsed = ParsedModuleSource { source, program };
1315    (module, Some(parsed))
1316}
1317
1318/// Extract the stdlib module name when `path` is a `<std>/<name>`
1319/// virtual path, otherwise `None`.
1320fn stdlib_module_from_path(path: &Path) -> Option<&str> {
1321    let s = path.to_str()?;
1322    s.strip_prefix("<std>/")
1323}
1324
1325fn normalize_path(path: &Path) -> PathBuf {
1326    canonical_path(path)
1327}
1328
1329/// Canonicalize `path`, memoized process-wide.
1330///
1331/// Module-graph construction and every per-file graph query canonicalize
1332/// paths to dedupe import-edge spellings, and the check preflight scan
1333/// canonicalizes each visited module per checked file. `Path::canonicalize`
1334/// resolves every component through the kernel, so a whole-tree `harn check`
1335/// used to spend the bulk of its wall clock in path-resolution syscalls
1336/// (`getattrlist` dominated system time). One positive-result memo removes
1337/// the `O(files x import closure)` repetition; failed canonicalizations are
1338/// not memoized (mirroring the bytecode cache's `canonicalize_cached`) so a
1339/// file that appears later still resolves correctly in long-lived processes.
1340/// `<std>/` virtual paths pass through untouched.
1341pub fn canonical_path(path: &Path) -> PathBuf {
1342    use std::sync::OnceLock;
1343    if stdlib_module_from_path(path).is_some() {
1344        return path.to_path_buf();
1345    }
1346    static MEMO: OnceLock<std::sync::Mutex<HashMap<PathBuf, PathBuf>>> = OnceLock::new();
1347    let memo = MEMO.get_or_init(|| std::sync::Mutex::new(HashMap::new()));
1348    if let Some(hit) = memo
1349        .lock()
1350        .expect("canonical path memo lock poisoned")
1351        .get(path)
1352        .cloned()
1353    {
1354        return hit;
1355    }
1356    match path.canonicalize() {
1357        Ok(canonical) => {
1358            memo.lock()
1359                .expect("canonical path memo lock poisoned")
1360                .insert(path.to_path_buf(), canonical.clone());
1361            canonical
1362        }
1363        Err(_) => path.to_path_buf(),
1364    }
1365}
1366
1367#[cfg(test)]
1368#[path = "tests.rs"]
1369mod tests;