Skip to main content

fallow_graph/resolve/
mod.rs

1//! Import specifier resolution using `oxc_resolver`.
2//!
3//! Orchestrates the resolution pipeline: for every extracted module, resolves all
4//! import specifiers in parallel (via rayon) to an [`ResolveResult`] — internal file,
5//! npm package, external file, or unresolvable. The entry point is [`resolve_all_imports`].
6//!
7//! Resolution is split into submodules by import kind:
8//! - `static_imports` — ES `import` declarations
9//! - `dynamic_imports` — `import()` expressions and glob-based dynamic patterns
10//! - `require_imports` — CommonJS `require()` calls
11//! - `re_exports` — `export { x } from './y'` re-export sources
12//! - `upgrades` — post-resolution pass fixing non-deterministic bare specifier results
13//!
14//! Handles tsconfig path aliases (auto-discovered per file), pnpm virtual store paths,
15//! React Native platform extensions, and package.json `exports` subpath resolution with
16//! output-to-source directory fallback.
17
18mod dynamic_imports;
19pub(crate) mod fallbacks;
20mod path_info;
21mod re_exports;
22mod react_native;
23mod require_imports;
24mod specifier;
25mod static_imports;
26#[cfg(test)]
27mod tests;
28mod types;
29mod upgrades;
30
31pub use path_info::{extract_package_name, is_bare_specifier, is_path_alias};
32pub use types::{ResolveResult, ResolvedImport, ResolvedModule, ResolvedReExport};
33
34use std::path::{Path, PathBuf};
35use std::sync::Mutex;
36
37use rayon::prelude::*;
38use rustc_hash::{FxHashMap, FxHashSet};
39
40use fallow_types::discover::{DiscoveredFile, FileId};
41use fallow_types::extract::ModuleInfo;
42
43use dynamic_imports::{resolve_dynamic_imports, resolve_dynamic_patterns};
44use re_exports::resolve_re_exports;
45use require_imports::resolve_require_imports;
46use specifier::create_resolver;
47use static_imports::resolve_static_imports;
48use types::ResolveContext;
49use upgrades::apply_specifier_upgrades;
50
51/// Resolve all imports across all modules in parallel.
52#[must_use]
53pub fn resolve_all_imports(
54    modules: &[ModuleInfo],
55    files: &[DiscoveredFile],
56    workspaces: &[fallow_config::WorkspaceInfo],
57    active_plugins: &[String],
58    path_aliases: &[(String, String)],
59    root: &Path,
60) -> Vec<ResolvedModule> {
61    // Build workspace name → root index for pnpm store fallback.
62    // Canonicalize roots to match path_to_id (which uses canonical paths).
63    // Without this, macOS /var → /private/var and similar platform symlinks
64    // cause workspace roots to mismatch canonical file paths.
65    let canonical_ws_roots: Vec<PathBuf> = workspaces
66        .par_iter()
67        .map(|ws| dunce::canonicalize(&ws.root).unwrap_or_else(|_| ws.root.clone()))
68        .collect();
69    let workspace_roots: FxHashMap<&str, &Path> = workspaces
70        .iter()
71        .zip(canonical_ws_roots.iter())
72        .map(|(ws, canonical)| (ws.name.as_str(), canonical.as_path()))
73        .collect();
74
75    // Check if project root is already canonical (no symlinks in path).
76    // When true, raw paths == canonical paths for files under root, so we can skip
77    // the upfront bulk canonicalize() of all source files (21k+ syscalls on large projects).
78    // A lazy CanonicalFallback handles the rare intra-project symlink case.
79    let root_is_canonical = dunce::canonicalize(root).is_ok_and(|c| c == root);
80
81    // Pre-compute canonical paths ONCE for all files in parallel (avoiding repeated syscalls).
82    // Skipped when root is canonical — the lazy fallback below handles edge cases.
83    let canonical_paths: Vec<PathBuf> = if root_is_canonical {
84        Vec::new()
85    } else {
86        files
87            .par_iter()
88            .map(|f| dunce::canonicalize(&f.path).unwrap_or_else(|_| f.path.clone()))
89            .collect()
90    };
91
92    // Primary path → FileId index. When root is canonical, uses raw paths (fast).
93    // Otherwise uses pre-computed canonical paths (correct for all symlink configurations).
94    let path_to_id: FxHashMap<&Path, FileId> = if root_is_canonical {
95        files.iter().map(|f| (f.path.as_path(), f.id)).collect()
96    } else {
97        canonical_paths
98            .iter()
99            .enumerate()
100            .map(|(idx, canonical)| (canonical.as_path(), files[idx].id))
101            .collect()
102    };
103
104    // Also index by non-canonical path for fallback lookups
105    let raw_path_to_id: FxHashMap<&Path, FileId> =
106        files.iter().map(|f| (f.path.as_path(), f.id)).collect();
107
108    // FileIds are sequential 0..n, so direct array indexing is faster than FxHashMap.
109    let file_paths: Vec<&Path> = files.iter().map(|f| f.path.as_path()).collect();
110
111    // Create resolver ONCE and share across threads (oxc_resolver::Resolver is Send + Sync)
112    let resolver = create_resolver(active_plugins);
113
114    // Lazy canonical fallback — only needed when root is canonical (path_to_id uses raw paths).
115    // When root is NOT canonical, path_to_id already uses canonical paths, no fallback needed.
116    let canonical_fallback = if root_is_canonical {
117        Some(types::CanonicalFallback::new(files))
118    } else {
119        None
120    };
121
122    // Dedup set for broken-tsconfig warnings. See `ResolveContext::tsconfig_warned`.
123    let tsconfig_warned: Mutex<FxHashSet<String>> = Mutex::new(FxHashSet::default());
124
125    // Shared resolution context — avoids passing 6 arguments to every resolve_specifier call
126    let ctx = ResolveContext {
127        resolver: &resolver,
128        path_to_id: &path_to_id,
129        raw_path_to_id: &raw_path_to_id,
130        workspace_roots: &workspace_roots,
131        path_aliases,
132        root,
133        canonical_fallback: canonical_fallback.as_ref(),
134        tsconfig_warned: &tsconfig_warned,
135    };
136
137    // Resolve in parallel — shared resolver instance.
138    // Each file resolves its own imports independently (no shared bare specifier cache).
139    // oxc_resolver's internal caches (package.json, tsconfig, directory entries) are
140    // shared across threads for performance.
141    let mut resolved: Vec<ResolvedModule> = modules
142        .par_iter()
143        .filter_map(|module| {
144            let Some(file_path) = file_paths.get(module.file_id.0 as usize) else {
145                tracing::warn!(
146                    file_id = module.file_id.0,
147                    "Skipping module with unknown file_id during resolution"
148                );
149                return None;
150            };
151
152            let mut all_imports = resolve_static_imports(&ctx, file_path, &module.imports);
153            all_imports.extend(resolve_require_imports(
154                &ctx,
155                file_path,
156                &module.require_calls,
157            ));
158
159            let from_dir = if canonical_paths.is_empty() {
160                // Root is canonical — raw paths are canonical
161                file_path.parent().unwrap_or(file_path)
162            } else {
163                canonical_paths
164                    .get(module.file_id.0 as usize)
165                    .and_then(|p| p.parent())
166                    .unwrap_or(file_path)
167            };
168
169            Some(ResolvedModule {
170                file_id: module.file_id,
171                path: file_path.to_path_buf(),
172                exports: module.exports.clone(),
173                re_exports: resolve_re_exports(&ctx, file_path, &module.re_exports),
174                resolved_imports: all_imports,
175                resolved_dynamic_imports: resolve_dynamic_imports(
176                    &ctx,
177                    file_path,
178                    &module.dynamic_imports,
179                ),
180                resolved_dynamic_patterns: resolve_dynamic_patterns(
181                    from_dir,
182                    &module.dynamic_import_patterns,
183                    &canonical_paths,
184                    files,
185                ),
186                member_accesses: module.member_accesses.clone(),
187                whole_object_uses: module.whole_object_uses.clone(),
188                has_cjs_exports: module.has_cjs_exports,
189                unused_import_bindings: module.unused_import_bindings.iter().cloned().collect(),
190            })
191        })
192        .collect();
193
194    apply_specifier_upgrades(&mut resolved);
195
196    resolved
197}