archunit/common/extraction/
enumerate_source_files.rs

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
use std::{
    collections::{BTreeMap, BTreeSet},
    ffi::OsStr,
    fs,
    path::{Path, PathBuf},
};

use super::{CargoProject, SourceFile, SourceOptions, cargo_project::workspace_identifier};
use crate::common::{ArchUnitError, TechnicalError};

/// Directory names omitted from source discovery wherever they occur.
///
/// Cargo's actual target directory is excluded separately, including when configured outside the
/// conventional `target` path.
pub const DEFAULT_EXCLUDED_DIRECTORIES: &[&str] = &[
    ".cache",
    ".git",
    ".hg",
    ".svn",
    "node_modules",
    "target",
    "vendor",
];

const CONVENTIONAL_DEV_DIRECTORIES: &[&str] = &["benches", "examples", "tests"];

/// Enumerates Rust source files under Cargo workspace members.
///
/// Results are deduplicated and sorted by normalized workspace-relative identifier. Symlinked
/// directories are not followed, which prevents escaping a member or traversing a cycle.
pub fn enumerate_source_files(
    project: &CargoProject,
    options: SourceOptions,
) -> Result<Vec<SourceFile>, ArchUnitError> {
    let member_roots = project
        .member_roots()
        .map(Path::to_path_buf)
        .collect::<Vec<_>>();
    let dev_target_roots = project
        .targets()
        .iter()
        .filter(|target| target.is_dev_only())
        .map(|target| target.source().path().to_path_buf())
        .collect::<BTreeSet<_>>();
    let mut sources = BTreeMap::new();

    for member_root in &member_roots {
        visit_directory(
            project,
            member_root,
            member_root,
            &member_roots,
            &dev_target_roots,
            options,
            &mut sources,
        )?;
    }

    Ok(sources.into_values().collect())
}

#[allow(clippy::too_many_arguments)]
fn visit_directory(
    project: &CargoProject,
    member_root: &Path,
    directory: &Path,
    member_roots: &[PathBuf],
    dev_target_roots: &BTreeSet<PathBuf>,
    options: SourceOptions,
    sources: &mut BTreeMap<String, SourceFile>,
) -> Result<(), ArchUnitError> {
    let entries = fs::read_dir(directory).map_err(|source| {
        ArchUnitError::from(TechnicalError::with_source(
            format!(
                "could not enumerate Rust sources under {}",
                directory.display()
            ),
            source,
        ))
    })?;
    let mut entries = entries.collect::<Result<Vec<_>, _>>().map_err(|source| {
        ArchUnitError::from(TechnicalError::with_source(
            format!(
                "could not enumerate Rust sources under {}",
                directory.display()
            ),
            source,
        ))
    })?;
    entries.sort_by_key(fs::DirEntry::path);

    for entry in entries {
        let path = entry.path();
        let file_type = entry.file_type().map_err(|source| {
            ArchUnitError::from(TechnicalError::with_source(
                format!("could not inspect source path {}", path.display()),
                source,
            ))
        })?;

        if file_type.is_dir() {
            if should_prune_directory(project, member_root, &path, member_roots) {
                continue;
            }
            visit_directory(
                project,
                member_root,
                &path,
                member_roots,
                dev_target_roots,
                options,
                sources,
            )?;
            continue;
        }
        if !file_type.is_file() || path.extension() != Some(OsStr::new("rs")) {
            continue;
        }
        if !options.includes_dev_targets()
            && (dev_target_roots.contains(&path) || is_conventional_dev_source(member_root, &path))
        {
            continue;
        }
        let Some(identifier) = workspace_identifier(project.root(), &path) else {
            continue;
        };
        sources.insert(identifier.clone(), SourceFile::new(path, identifier));
    }

    Ok(())
}

fn should_prune_directory(
    project: &CargoProject,
    current_member_root: &Path,
    directory: &Path,
    member_roots: &[PathBuf],
) -> bool {
    if directory.starts_with(project.target_directory()) {
        return true;
    }
    if directory
        .file_name()
        .is_some_and(is_default_excluded_directory)
    {
        return true;
    }
    if directory != current_member_root && member_roots.iter().any(|root| root == directory) {
        return true;
    }

    directory != current_member_root
        && directory.join("Cargo.toml").is_file()
        && !member_roots.iter().any(|root| root == directory)
}

fn is_default_excluded_directory(name: &OsStr) -> bool {
    let name = name.to_string_lossy();
    DEFAULT_EXCLUDED_DIRECTORIES
        .iter()
        .any(|excluded| name.eq_ignore_ascii_case(excluded))
}

fn is_conventional_dev_source(member_root: &Path, path: &Path) -> bool {
    let Ok(relative) = path.strip_prefix(member_root) else {
        return false;
    };
    let Some(first) = relative.components().next() else {
        return false;
    };
    let first = first.as_os_str().to_string_lossy();
    CONVENTIONAL_DEV_DIRECTORIES
        .iter()
        .any(|directory| first.eq_ignore_ascii_case(directory))
}

#[cfg(test)]
mod tests {
    use std::ffi::OsStr;

    use super::is_default_excluded_directory;

    #[test]
    fn default_exclusions_are_ascii_case_insensitive_on_every_host() {
        assert!(is_default_excluded_directory(OsStr::new("target")));
        assert!(is_default_excluded_directory(OsStr::new("VENDOR")));
        assert!(is_default_excluded_directory(OsStr::new(".Git")));
        assert!(!is_default_excluded_directory(OsStr::new("src")));
    }
}