orphanode 0.1.0

Accuracy-first reachability analysis for JavaScript and TypeScript projects, as a library and a CLI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
pub mod configuration;
pub mod manifest;
pub mod scripts;
pub mod workspace;

use std::{
    collections::BTreeSet,
    ffi::OsStr,
    path::{Path, PathBuf},
};

use ignore::{DirEntry, WalkBuilder};
use thiserror::Error;

use crate::limits::AnalysisLimits;

const SOURCE_EXTENSIONS: [&str; 8] = ["js", "jsx", "mjs", "cjs", "ts", "tsx", "mts", "cts"];
const SKIPPED_DIRECTORIES: [&str; 14] = [
    "node_modules",
    ".git",
    ".hg",
    ".svn",
    "target",
    "dist",
    "build",
    "coverage",
    ".next",
    ".nuxt",
    ".svelte-kit",
    ".turbo",
    ".cache",
    ".orphanode",
];

#[derive(Debug, Error)]
pub enum DiscoveryError {
    #[error("cannot walk project root `{root}` while discovering source files: {source}")]
    Walk {
        root: PathBuf,
        #[source]
        source: ignore::Error,
    },

    #[error(
        "nested package boundary `{path}` prevents automatic source discovery; automatic discovery supports one package at a time, so use --file or --files-from to provide the source universe explicitly"
    )]
    NestedPackage { path: PathBuf },

    #[error(
        "discovered path `{path}` is outside project root `{root}`; use a physical project root without path aliases"
    )]
    PathOutsideRoot { root: PathBuf, path: PathBuf },

    #[error(
        "source discovery under `{root}` exceeded the configured limit of {limit} files; narrow the root or raise the discovery limit explicitly"
    )]
    FileLimitExceeded { root: PathBuf, limit: usize },
}

/// Discovers the analyzable source files in one package rooted at `root`.
///
/// Returned paths are relative to `root`, sorted, and deduplicated. Project
/// `.gitignore` and `.ignore` files are respected, while user-global and parent
/// ignore configuration is deliberately excluded so discovery is reproducible.
///
/// # Errors
///
/// Returns an error when the project cannot be walked completely, a discovered
/// path cannot be expressed relative to `root`, or a nested `package.json`
/// marks a package boundary that automatic single-package discovery cannot cross.
pub fn discover_source_files(root: &Path) -> Result<Vec<PathBuf>, DiscoveryError> {
    discover_source_files_with_limits(root, AnalysisLimits::default())
}

/// Discovers source files with explicit request safety limits.
///
/// # Errors
///
/// Returns the same failures as [`discover_source_files`], plus a visible
/// error if the discovered source universe exceeds the configured file limit.
pub fn discover_source_files_with_limits(
    root: &Path,
    limits: AnalysisLimits,
) -> Result<Vec<PathBuf>, DiscoveryError> {
    discover_source_files_internal(root, limits, BTreeSet::new())
}

/// Discovers one package while pruning already-discovered nested workspace packages.
///
/// `nested_package_roots` must be package-relative directory paths below `root`.
/// An unexpected nested package still fails closed.
///
/// # Errors
///
/// Returns the same errors as [`discover_source_files_with_limits`] and rejects an
/// unsafe nested-package root.
pub fn discover_package_source_files(
    root: &Path,
    nested_package_roots: &[PathBuf],
    limits: AnalysisLimits,
) -> Result<Vec<PathBuf>, DiscoveryError> {
    let mut excluded = BTreeSet::new();
    for nested in nested_package_roots {
        if nested.as_os_str().is_empty()
            || nested.is_absolute()
            || nested
                .components()
                .any(|component| matches!(component, std::path::Component::ParentDir))
        {
            return Err(DiscoveryError::PathOutsideRoot {
                root: root.to_path_buf(),
                path: nested.clone(),
            });
        }
        excluded.insert(nested.clone());
    }
    discover_source_files_internal(root, limits, excluded)
}

fn discover_source_files_internal(
    root: &Path,
    limits: AnalysisLimits,
    excluded_package_roots: BTreeSet<PathBuf>,
) -> Result<Vec<PathBuf>, DiscoveryError> {
    let filter_root = root.to_path_buf();
    let mut builder = WalkBuilder::new(root);
    builder
        .standard_filters(false)
        .hidden(false)
        .ignore(true)
        .git_ignore(true)
        .require_git(false)
        .follow_links(false)
        .sort_by_file_path(Path::cmp)
        .filter_entry(move |entry| {
            should_visit_entry(entry)
                && !entry
                    .path()
                    .strip_prefix(&filter_root)
                    .is_ok_and(|relative| excluded_package_roots.contains(relative))
        });

    let mut source_files = BTreeSet::new();
    let mut nested_packages = BTreeSet::new();

    for result in builder.build() {
        let entry = result.map_err(|source| DiscoveryError::Walk {
            root: root.to_path_buf(),
            source,
        })?;
        if let Some(source) = entry.error() {
            return Err(DiscoveryError::Walk {
                root: root.to_path_buf(),
                source: source.clone(),
            });
        }
        if entry.path_is_symlink() || !entry.file_type().is_some_and(|kind| kind.is_file()) {
            continue;
        }

        let relative_path =
            entry
                .path()
                .strip_prefix(root)
                .map_err(|_| DiscoveryError::PathOutsideRoot {
                    root: root.to_path_buf(),
                    path: entry.path().to_path_buf(),
                })?;
        if is_nested_package(relative_path) {
            nested_packages.insert(relative_path.to_path_buf());
        } else if is_source_file(relative_path) {
            source_files.insert(relative_path.to_path_buf());
            if source_files.len() > limits.max_discovered_files {
                return Err(DiscoveryError::FileLimitExceeded {
                    root: root.to_path_buf(),
                    limit: limits.max_discovered_files,
                });
            }
        }
    }

    if let Some(path) = nested_packages.into_iter().next() {
        return Err(DiscoveryError::NestedPackage { path });
    }

    Ok(source_files.into_iter().collect())
}

fn should_visit_entry(entry: &DirEntry) -> bool {
    if entry.depth() == 0 || !entry.file_type().is_some_and(|kind| kind.is_dir()) {
        return true;
    }
    // Nested repositories, such as git submodules, are independent projects
    // excluded from the surrounding package's source universe by policy.
    if workspace::is_repository_boundary(entry.path()) {
        return false;
    }
    !SKIPPED_DIRECTORIES
        .iter()
        .any(|directory| entry.file_name() == OsStr::new(directory))
}

fn is_nested_package(path: &Path) -> bool {
    path.file_name() == Some(OsStr::new("package.json"))
        && path
            .parent()
            .is_some_and(|parent| !parent.as_os_str().is_empty())
}

fn is_source_file(path: &Path) -> bool {
    path.extension()
        .and_then(OsStr::to_str)
        .is_some_and(|extension| SOURCE_EXTENSIONS.contains(&extension))
}

#[cfg(test)]
mod tests {
    use std::{
        fs,
        path::{Path, PathBuf},
        process,
        sync::atomic::{AtomicU64, Ordering},
    };

    use super::{
        DiscoveryError, SKIPPED_DIRECTORIES, discover_source_files,
        discover_source_files_with_limits,
    };
    use crate::limits::AnalysisLimits;

    static NEXT_PROJECT_ID: AtomicU64 = AtomicU64::new(0);

    #[test]
    fn nested_repository_boundaries_are_excluded_from_the_source_universe() {
        let project = TestProject::new();
        project.write("src/index.ts", "");
        project.write("vendor/.git", "gitdir: ../.git/modules/vendor\n");
        project.write("vendor/legacy.ts", "");
        project.write("tools/nested/.hg", "");
        project.write("tools/nested/helper.js", "");

        let discovered =
            discover_source_files(project.path()).expect("discover with repository boundaries");

        assert_eq!(discovered, [PathBuf::from("src/index.ts")]);
    }

    #[test]
    fn discovery_is_sorted_and_respects_project_ignores_and_skipped_directories() {
        let project = TestProject::new();
        project.write("package.json", "{}");
        project.write(".gitignore", "ignored.ts\n");
        project.write(".ignore", "also-ignored.js\n");
        project.write("ignored.ts", "");
        project.write("also-ignored.js", "");
        project.write(".hidden/visible.mts", "");
        project.write("src/eight.cts", "");
        project.write("src/five.ts", "");
        project.write("src/four.cjs", "");
        project.write("src/one.js", "");
        project.write("src/seven.mts", "");
        project.write("src/six.tsx", "");
        project.write("src/three.mjs", "");
        project.write("src/two.jsx", "");
        project.write("src/not-source.css", "");
        for directory in SKIPPED_DIRECTORIES {
            project.write(&format!("{directory}/package.json"), "{}");
            project.write(&format!("{directory}/skipped.js"), "");
        }

        let files = discover_source_files(project.path()).expect("discover sources");

        assert_eq!(
            files,
            [
                ".hidden/visible.mts",
                "src/eight.cts",
                "src/five.ts",
                "src/four.cjs",
                "src/one.js",
                "src/seven.mts",
                "src/six.tsx",
                "src/three.mjs",
                "src/two.jsx",
            ]
            .map(PathBuf::from)
        );
    }

    #[test]
    fn nested_package_boundaries_fail_with_the_first_sorted_relative_path() {
        let project = TestProject::new();
        project.write("package.json", "{}");
        project.write("packages/z/package.json", "{}");
        project.write("packages/a/package.json", "{}");
        project.write("packages/a/index.ts", "");

        let error = discover_source_files(project.path()).expect_err("reject nested packages");

        assert!(matches!(
            error,
            DiscoveryError::NestedPackage { path }
                if path == Path::new("packages/a/package.json")
        ));
    }

    #[test]
    fn known_workspace_packages_are_pruned_from_parent_package_discovery() {
        let project = TestProject::new();
        project.write("package.json", "{}");
        project.write("src/index.ts", "");
        project.write("packages/child/package.json", "{}");
        project.write("packages/child/src/index.ts", "");

        let files = super::discover_package_source_files(
            project.path(),
            &[PathBuf::from("packages/child")],
            crate::limits::AnalysisLimits::default(),
        )
        .expect("discover parent package");

        assert_eq!(files, [PathBuf::from("src/index.ts")]);
    }

    #[test]
    fn a_missing_project_root_is_a_visible_walk_error() {
        let project = TestProject::new();
        let missing_root = project.path().join("missing");

        let error = discover_source_files(&missing_root).expect_err("reject missing root");

        assert!(matches!(error, DiscoveryError::Walk { root, .. } if root == missing_root));
    }

    #[test]
    fn invalid_ignore_rules_are_visible_walk_errors() {
        let project = TestProject::new();
        project.write(".ignore", "[z-a]\n");
        project.write("index.js", "");

        let error = discover_source_files(project.path()).expect_err("reject invalid ignore rule");

        assert!(matches!(error, DiscoveryError::Walk { root, .. } if root == project.path()));
    }

    #[test]
    fn exceeding_the_configured_file_limit_fails_instead_of_truncating() {
        let project = TestProject::new();
        project.write("a.js", "");
        project.write("b.js", "");
        let limits = AnalysisLimits {
            max_discovered_files: 1,
            ..AnalysisLimits::default()
        };

        let error = discover_source_files_with_limits(project.path(), limits)
            .expect_err("reject oversized source universe");

        assert!(matches!(
            error,
            DiscoveryError::FileLimitExceeded { root, limit: 1 } if root == project.path()
        ));
    }

    #[cfg(unix)]
    #[test]
    fn symbolic_links_are_not_discovered_or_followed() {
        use std::os::unix::fs::symlink;

        let project = TestProject::new();
        let external = TestProject::new();
        external.write("external.js", "");
        symlink(external.path(), project.path().join("linked-directory"))
            .expect("create directory symlink");
        symlink(
            external.path().join("external.js"),
            project.path().join("linked-file.js"),
        )
        .expect("create file symlink");
        project.write("local.js", "");

        let files = discover_source_files(project.path()).expect("discover sources");

        assert_eq!(files, [PathBuf::from("local.js")]);
    }

    struct TestProject {
        root: PathBuf,
    }

    impl TestProject {
        fn new() -> Self {
            loop {
                let id = NEXT_PROJECT_ID.fetch_add(1, Ordering::Relaxed);
                let root = std::env::temp_dir()
                    .join(format!("orphanode-discovery-test-{}-{id}", process::id()));
                match fs::create_dir(&root) {
                    Ok(()) => return Self { root },
                    Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {}
                    Err(error) => panic!("create test project `{}`: {error}", root.display()),
                }
            }
        }

        fn path(&self) -> &Path {
            &self.root
        }

        fn write(&self, relative_path: &str, contents: &str) {
            let path = self.root.join(relative_path);
            if let Some(parent) = path.parent() {
                fs::create_dir_all(parent).expect("create test parent directories");
            }
            fs::write(path, contents).expect("write test file");
        }
    }

    impl Drop for TestProject {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(&self.root);
        }
    }
}