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 fallbacks::extract_package_name_from_node_modules_path;
32pub use path_info::{
33    extract_package_name, is_bare_specifier, is_path_alias, is_valid_package_name,
34};
35pub use types::{
36    ResolveResult, ResolvedImport, ResolvedModule, ResolvedReExport, ResolvedSourceEdge,
37};
38
39use std::path::{Path, PathBuf};
40use std::sync::Mutex;
41
42use rayon::prelude::*;
43use rustc_hash::{FxHashMap, FxHashSet};
44
45use fallow_config::{AutoImportKind, AutoImportRule};
46use fallow_types::discover::{DiscoveredFile, FileId};
47use fallow_types::extract::{ImportInfo, ImportedName, ModuleInfo};
48use oxc_span::Span;
49
50use dynamic_imports::{resolve_dynamic_imports, resolve_dynamic_patterns};
51use re_exports::resolve_re_exports;
52use react_native::{build_condition_names, build_extensions};
53use require_imports::resolve_require_imports;
54use specifier::create_resolver;
55use static_imports::resolve_static_imports;
56use types::{PackageManifestInfo, ResolveContext};
57use upgrades::apply_specifier_upgrades;
58
59/// Resolve all imports across all modules in parallel.
60#[must_use]
61#[expect(
62    clippy::too_many_arguments,
63    reason = "resolver inputs come from disjoint sources (config, plugins, workspace, filesystem); \
64              bundling them into a struct would be a cross-cutting refactor outside this task"
65)]
66pub fn resolve_all_imports(
67    modules: &[ModuleInfo],
68    files: &[DiscoveredFile],
69    workspaces: &[fallow_config::WorkspaceInfo],
70    active_plugins: &[String],
71    path_aliases: &[(String, String)],
72    auto_imports: &[AutoImportRule],
73    scss_include_paths: &[PathBuf],
74    static_dir_mappings: &[(PathBuf, String)],
75    root: &Path,
76    extra_conditions: &[String],
77) -> Vec<ResolvedModule> {
78    // Build workspace name → root index for pnpm store fallback.
79    // Canonicalize roots to match path_to_id (which uses canonical paths).
80    // Without this, macOS /var → /private/var and similar platform symlinks
81    // cause workspace roots to mismatch canonical file paths.
82    let canonical_ws_roots: Vec<PathBuf> = workspaces
83        .par_iter()
84        .map(|ws| dunce::canonicalize(&ws.root).unwrap_or_else(|_| ws.root.clone()))
85        .collect();
86    let workspace_roots: FxHashMap<&str, &Path> = workspaces
87        .iter()
88        .zip(canonical_ws_roots.iter())
89        .map(|(ws, canonical)| (ws.name.as_str(), canonical.as_path()))
90        .collect();
91    let root_canonical = dunce::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
92    let mut package_manifests = Vec::new();
93    if let Ok(package_json) = fallow_config::PackageJson::load(&root.join("package.json")) {
94        package_manifests.push(PackageManifestInfo {
95            root: root.to_path_buf(),
96            canonical_root: root_canonical,
97            name: package_json.name.clone(),
98            package_json,
99        });
100    }
101    for (ws, canonical_root) in workspaces.iter().zip(canonical_ws_roots.iter()) {
102        if let Ok(package_json) = fallow_config::PackageJson::load(&ws.root.join("package.json")) {
103            package_manifests.push(PackageManifestInfo {
104                root: ws.root.clone(),
105                canonical_root: canonical_root.clone(),
106                name: package_json.name.clone().or_else(|| Some(ws.name.clone())),
107                package_json,
108            });
109        }
110    }
111
112    // Check if project root is already canonical (no symlinks in path).
113    // When true, raw paths == canonical paths for files under root, so we can skip
114    // the upfront bulk canonicalize() of all source files (21k+ syscalls on large projects).
115    // A lazy CanonicalFallback handles the rare intra-project symlink case.
116    let root_is_canonical = dunce::canonicalize(root).is_ok_and(|c| c == root);
117
118    // Pre-compute canonical paths ONCE for all files in parallel (avoiding repeated syscalls).
119    // Skipped when root is canonical — the lazy fallback below handles edge cases.
120    let canonical_paths: Vec<PathBuf> = if root_is_canonical {
121        Vec::new()
122    } else {
123        files
124            .par_iter()
125            .map(|f| dunce::canonicalize(&f.path).unwrap_or_else(|_| f.path.clone()))
126            .collect()
127    };
128
129    // Primary path → FileId index. When root is canonical, uses raw paths (fast).
130    // Otherwise uses pre-computed canonical paths (correct for all symlink configurations).
131    let path_to_id: FxHashMap<&Path, FileId> = if root_is_canonical {
132        files.iter().map(|f| (f.path.as_path(), f.id)).collect()
133    } else {
134        canonical_paths
135            .iter()
136            .enumerate()
137            .map(|(idx, canonical)| (canonical.as_path(), files[idx].id))
138            .collect()
139    };
140
141    // Also index by non-canonical path for fallback lookups
142    let raw_path_to_id: FxHashMap<&Path, FileId> =
143        files.iter().map(|f| (f.path.as_path(), f.id)).collect();
144
145    // FileIds are sequential 0..n, so direct array indexing is faster than FxHashMap.
146    let file_paths: Vec<&Path> = files.iter().map(|f| f.path.as_path()).collect();
147
148    // Create resolvers ONCE and share across threads (oxc_resolver::Resolver is Send + Sync).
149    let extensions = build_extensions(active_plugins);
150    let condition_names = build_condition_names(active_plugins, extra_conditions);
151    let resolver = create_resolver(active_plugins, extra_conditions);
152    let mut style_conditions = extra_conditions.to_vec();
153    style_conditions.push("style".to_string());
154    let style_resolver = create_resolver(active_plugins, &style_conditions);
155
156    // Lazy canonical fallback — only needed when root is canonical (path_to_id uses raw paths).
157    // When root is NOT canonical, path_to_id already uses canonical paths, no fallback needed.
158    let canonical_fallback = if root_is_canonical {
159        Some(types::CanonicalFallback::new(files))
160    } else {
161        None
162    };
163
164    // Dedup set for broken-tsconfig warnings. See `ResolveContext::tsconfig_warned`.
165    let tsconfig_warned: Mutex<FxHashSet<String>> = Mutex::new(FxHashSet::default());
166
167    // Shared resolution context — avoids passing 6 arguments to every resolve_specifier call
168    let ctx = ResolveContext {
169        resolver: &resolver,
170        style_resolver: &style_resolver,
171        extensions: &extensions,
172        path_to_id: &path_to_id,
173        raw_path_to_id: &raw_path_to_id,
174        workspace_roots: &workspace_roots,
175        package_manifests: &package_manifests,
176        condition_names: &condition_names,
177        path_aliases,
178        scss_include_paths,
179        static_dir_mappings,
180        root,
181        canonical_fallback: canonical_fallback.as_ref(),
182        tsconfig_warned: &tsconfig_warned,
183    };
184
185    // Resolve in parallel — shared resolver instance.
186    // Each file resolves its own imports independently (no shared bare specifier cache).
187    // oxc_resolver's internal caches (package.json, tsconfig, directory entries) are
188    // shared across threads for performance.
189    let mut resolved: Vec<ResolvedModule> = modules
190        .par_iter()
191        .filter_map(|module| {
192            let Some(file_path) = file_paths.get(module.file_id.0 as usize) else {
193                tracing::warn!(
194                    file_id = module.file_id.0,
195                    "Skipping module with unknown file_id during resolution"
196                );
197                return None;
198            };
199
200            let mut all_imports = resolve_static_imports(&ctx, file_path, &module.imports);
201            all_imports.extend(resolve_require_imports(
202                &ctx,
203                file_path,
204                &module.require_calls,
205            ));
206
207            let from_dir = if canonical_paths.is_empty() {
208                // Root is canonical — raw paths are canonical
209                file_path.parent().unwrap_or(file_path)
210            } else {
211                canonical_paths
212                    .get(module.file_id.0 as usize)
213                    .and_then(|p| p.parent())
214                    .unwrap_or(file_path)
215            };
216
217            Some(ResolvedModule {
218                file_id: module.file_id,
219                path: file_path.to_path_buf(),
220                exports: module.exports.clone(),
221                re_exports: resolve_re_exports(&ctx, file_path, &module.re_exports),
222                resolved_imports: all_imports,
223                resolved_dynamic_imports: resolve_dynamic_imports(
224                    &ctx,
225                    file_path,
226                    &module.dynamic_imports,
227                ),
228                resolved_dynamic_patterns: resolve_dynamic_patterns(
229                    from_dir,
230                    &module.dynamic_import_patterns,
231                    &canonical_paths,
232                    files,
233                ),
234                member_accesses: module.member_accesses.clone(),
235                whole_object_uses: module.whole_object_uses.clone(),
236                has_cjs_exports: module.has_cjs_exports,
237                has_angular_component_template_url: module.has_angular_component_template_url,
238                unused_import_bindings: module.unused_import_bindings.iter().cloned().collect(),
239                type_referenced_import_bindings: module.type_referenced_import_bindings.clone(),
240                value_referenced_import_bindings: module.value_referenced_import_bindings.clone(),
241                namespace_object_aliases: module.namespace_object_aliases.clone(),
242            })
243        })
244        .collect();
245
246    apply_specifier_upgrades(&mut resolved);
247
248    synthesize_auto_import_edges(
249        &mut resolved,
250        modules,
251        auto_imports,
252        &path_to_id,
253        &raw_path_to_id,
254    );
255
256    resolved
257}
258
259/// Synthesize module-graph edges for convention auto-imports.
260///
261/// For each module, every captured `auto_import_candidates` name is matched
262/// against the active plugins' auto-import table; on a hit a synthetic
263/// [`ResolvedImport`] to the providing file is appended so the existing graph
264/// builder credits the edge. Name collisions across files (e.g. `Comments`
265/// declared in both `Comments.client.vue` and `Comments.server.vue`) over-credit
266/// every match, keeping each provider reachable. Resolution is recomputed every
267/// run from the live file index, so it is never folded into per-file caching.
268/// See issue #704.
269fn synthesize_auto_import_edges(
270    resolved: &mut [ResolvedModule],
271    modules: &[ModuleInfo],
272    auto_imports: &[AutoImportRule],
273    path_to_id: &FxHashMap<&Path, FileId>,
274    raw_path_to_id: &FxHashMap<&Path, FileId>,
275) {
276    if auto_imports.is_empty() {
277        return;
278    }
279
280    // Name -> providing files. Built from the live file index, so a rule whose
281    // source file was not discovered is skipped rather than synthesizing a dangling edge.
282    let mut table: FxHashMap<&str, Vec<(FileId, AutoImportKind)>> = FxHashMap::default();
283    for rule in auto_imports {
284        let source = rule.source.as_path();
285        let Some(file_id) = raw_path_to_id
286            .get(source)
287            .or_else(|| path_to_id.get(source))
288            .copied()
289        else {
290            continue;
291        };
292        table
293            .entry(rule.name.as_str())
294            .or_default()
295            .push((file_id, rule.kind));
296    }
297    if table.is_empty() {
298        return;
299    }
300
301    // Captured candidate names keyed by the file that referenced them.
302    let candidates: FxHashMap<FileId, &[String]> = modules
303        .iter()
304        .filter(|module| !module.auto_import_candidates.is_empty())
305        .map(|module| (module.file_id, module.auto_import_candidates.as_slice()))
306        .collect();
307    if candidates.is_empty() {
308        return;
309    }
310
311    for module in resolved.iter_mut() {
312        let Some(names) = candidates.get(&module.file_id) else {
313            continue;
314        };
315        for name in *names {
316            let Some(targets) = table.get(name.as_str()) else {
317                continue;
318            };
319            for (target_id, kind) in targets {
320                if *target_id == module.file_id {
321                    continue;
322                }
323                module.resolved_imports.push(ResolvedImport {
324                    info: synthetic_auto_import_info(name, *kind),
325                    target: ResolveResult::InternalModule(*target_id),
326                });
327            }
328        }
329    }
330}
331
332/// Build a synthetic [`ImportInfo`] for a convention auto-import. Component and
333/// default kinds credit the default export; named kinds credit the named export.
334fn synthetic_auto_import_info(name: &str, kind: AutoImportKind) -> ImportInfo {
335    let imported_name = match kind {
336        AutoImportKind::Named => ImportedName::Named(name.to_string()),
337        AutoImportKind::Default | AutoImportKind::DefaultComponent => ImportedName::Default,
338    };
339    ImportInfo {
340        source: format!("<auto-import:{name}>"),
341        imported_name,
342        local_name: name.to_string(),
343        is_type_only: false,
344        from_style: false,
345        span: Span::default(),
346        source_span: Span::default(),
347    }
348}