rust-llm-tidy-cli 0.3.0

CLI for reordering and linting Rust source code. Intended for LLM use.
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
//! C# project-reference scope and source-versioned parse cache for indexed linting.

use crate::paths;
use quick_xml::XmlVersion;
use quick_xml::events::Event;
use quick_xml::reader::Reader;
use rayon::prelude::*;
use rust_llm_tidy_lang::backends::CanThrowIndex;
use rust_llm_tidy_model::parse::ParseResult;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

/// Run-owned C# parses and shared throw answers.
#[derive(Default)]
pub(crate) struct CSharpIndex {
    parses: HashMap<PathBuf, ParseResult>,
    /// Qualified throw answers for the current cached source versions.
    pub(crate) index: CanThrowIndex,
}

impl CSharpIndex {
    /// Parse the project-reference scope of `inputs`, retaining successful parses.
    ///
    /// # Errors
    /// Returns an error when collecting project source entries fails.
    pub(crate) fn build(inputs: &[PathBuf]) -> anyhow::Result<Self> {
        let files = project_scope(inputs)?;

        let parse = |path: &PathBuf| {
            let source = std::fs::read_to_string(path).ok()?;
            let parsed = rust_llm_tidy_lang::backend_for("cs")?.parse(&source).ok()?;
            Some((path.clone(), parsed))
        };
        let parses = if crate::pipeline::should_parallelize(&files) {
            files.par_iter().filter_map(parse).collect()
        } else {
            files.iter().filter_map(parse).collect()
        };

        let mut cache = Self {
            parses,
            index: CanThrowIndex::default(),
        };
        cache.index = CanThrowIndex::from_parses(cache.parses.values());
        Ok(cache)
    }

    /// Refresh changed `inputs` after mutations, then rebuild affected throw answers.
    /// Unreadable sources lose their cached facts and fail in the per-file lint pass.
    pub(crate) fn refresh(&mut self, inputs: &[PathBuf]) {
        let mut changed = false;
        for path in inputs {
            if !paths::ext_in(path.extension().and_then(|e| e.to_str()), &["cs"]) {
                continue;
            }
            // Deleted sources keep their canonical cache key so the stale parse is removed.
            let key = path
                .canonicalize()
                .unwrap_or_else(|_| missing_source_key(path));
            let source = std::fs::read_to_string(path).ok();
            if source.as_deref() == self.parses.get(&key).map(|p| p.source.as_str()) {
                continue;
            }

            changed = true;
            self.parses.remove(&key);
            if let Some(source) = source
                && let Some(backend) = rust_llm_tidy_lang::backend_for("cs")
                && let Ok(parsed) = backend.parse(&source)
            {
                self.parses.insert(key, parsed);
            }
        }
        if changed {
            self.index = CanThrowIndex::from_parses(self.parses.values());
        }
    }

    /// Return the cached parse for the canonical identity of `path`.
    pub(crate) fn parsed(&self, path: &Path) -> Option<&ParseResult> {
        self.parses.get(&path.canonicalize().ok()?)
    }
}

/// Reproduce the canonical cache key a now-deleted `path` received from
/// [`project_scope`], so refresh still invalidates its cached parse.
///
/// Canonicalizes the surviving parent directory and rejoins the file name;
/// bare file names anchor at the current directory instead.
fn missing_source_key(path: &Path) -> PathBuf {
    let Some(name) = path.file_name() else {
        return path.to_path_buf();
    };
    let parent = path.parent().unwrap_or(Path::new(""));
    let anchor = if parent.as_os_str().is_empty() {
        std::env::current_dir().unwrap_or_default()
    } else {
        parent.to_path_buf()
    };
    anchor.canonicalize().unwrap_or(anchor).join(name)
}

/// Resolve nearest projects and their literal reference closure for C# `inputs`.
///
/// # Errors
/// Returns an error when a project's source directory entries cannot be read.
fn project_scope(inputs: &[PathBuf]) -> anyhow::Result<Vec<PathBuf>> {
    let mut files = HashSet::new();
    let mut pending = Vec::new();
    let mut nearest: HashMap<PathBuf, Vec<PathBuf>> = HashMap::new();
    for path in inputs {
        if !paths::ext_in(path.extension().and_then(|e| e.to_str()), &["cs"]) {
            continue;
        }
        let path = path.canonicalize().unwrap_or_else(|_| path.clone());
        files.insert(path.clone());
        if let Some(parent) = path.parent() {
            if nearest.contains_key(parent) {
                continue;
            }
            let mut searched = Vec::new();
            let mut found = Vec::new();
            for dir in parent.ancestors() {
                if let Some(projects) = nearest.get(dir) {
                    found = projects.clone();
                    break;
                }
                searched.push(dir.to_path_buf());
                let mut projects: Vec<_> = std::fs::read_dir(dir)
                    .into_iter()
                    .flatten()
                    .filter_map(Result::ok)
                    .map(|entry| entry.path())
                    .filter(|p| p.extension().is_some_and(|e| e == "csproj") && p.is_file())
                    .collect();
                if !projects.is_empty() {
                    projects.sort();
                    found = projects;
                    break;
                }
                if dir.join(".git").exists() {
                    break;
                }
            }
            for dir in searched {
                nearest.insert(dir, found.clone());
            }
            pending.extend(found);
        }
    }

    let mut visited = HashSet::new();
    while let Some(project) = pending.pop() {
        let Ok(project) = project.canonicalize() else {
            continue;
        };
        if !visited.insert(project.clone()) {
            continue;
        }
        let Some(dir) = project.parent() else {
            continue;
        };
        let Ok(source) = std::fs::read_to_string(&project) else {
            continue;
        };
        let mut sources = Vec::new();
        paths::collect_project_files(dir, &["cs"], &mut sources, true)?;
        files.extend(sources.into_iter().filter_map(|p| p.canonicalize().ok()));

        for include in literal_project_includes(&source) {
            pending.push(dir.join(include.replace('\\', "/")));
        }
    }

    let mut files: Vec<_> = files.into_iter().collect();
    files.sort();
    Ok(files)
}

/// Extract the literal `Include` targets of `<ProjectReference>` elements from
/// project XML, matching element and attribute local names so namespaces never
/// hide a reference.
///
/// Commented-out references contribute nothing, and targets containing MSBuild
/// properties or wildcards (`$`, `*`, `?`) are skipped as non-literal. XML
/// parsing stops at malformed content, keeping the targets read before it.
fn literal_project_includes(source: &str) -> Vec<String> {
    let mut reader = Reader::from_str(source);

    // Project fragments may nest or close loosely; only element shape matters here.
    reader.config_mut().check_end_names = false;
    let mut includes = Vec::new();
    loop {
        match reader.read_event() {
            Ok(Event::Start(e) | Event::Empty(e)) => {
                if e.name().local_name().as_ref() != "ProjectReference" {
                    continue;
                }
                for attr in e.attributes().flatten() {
                    if attr.key.local_name().as_ref() == "Include"
                        && let Ok(value) = attr.normalized_value(XmlVersion::Implicit1_0)
                        && !value.contains(['$', '*', '?'])
                    {
                        includes.push(value.into_owned());
                    }
                }
            }
            Ok(Event::Eof) | Err(_) => break,
            Ok(_) => {}
        }
    }
    includes
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Nearest projects close over literal references without crossing ignored
    /// or nested repository boundaries.
    #[test]
    fn scope_should_follow_reference_cycles_and_skip_unresolvable_targets() {
        let root = std::env::temp_dir().join(format!("rlt-csharp-scope-{}", std::process::id()));
        let _guard = Directory(root.clone());
        for dir in [
            ".git",
            "a/src",
            "a/nested/.git",
            "a/ignored",
            "b",
            "c",
            "loose",
        ] {
            std::fs::create_dir_all(root.join(dir)).unwrap();
        }
        // project_scope canonicalizes its results; align expectations when the
        // platform temp dir is a symlink (macOS /var -> /private/var).
        let root = root.canonicalize().unwrap();
        for path in [
            "a/src/A.cs",
            "a/nested/Hidden.cs",
            "a/ignored/Hidden.cs",
            "b/B.cs",
            "c/C.cs",
            "loose/L.cs",
        ] {
            std::fs::write(root.join(path), "class C {}").unwrap();
        }
        std::fs::write(root.join(".gitignore"), "a/ignored/\n").unwrap();
        std::fs::write(root.join("a/a.csproj"), "<ProjectReference Include=\"../b/b.csproj\"/><ProjectReference Include=\"missing.csproj\"/>").unwrap();
        std::fs::write(
            root.join("b/b.csproj"),
            "<ProjectReference Include=\"../c/c.csproj\"/>",
        )
        .unwrap();
        std::fs::write(
            root.join("c/c.csproj"),
            "<ProjectReference Include=\"../a/a.csproj\"/>",
        )
        .unwrap();

        let loose = project_scope(&[root.join("loose/L.cs")]).unwrap();
        std::fs::write(root.join("outer.csproj"), "<Project />").unwrap();
        std::fs::write(root.join("Outer.cs"), "class Outer {}").unwrap();
        let scope = project_scope(&[root.join("a/src/A.cs")]).unwrap();

        assert_eq!(
            scope,
            ["a/src/A.cs", "b/B.cs", "c/C.cs"].map(|p| root.join(p))
        );
        assert_eq!(loose, [root.join("loose/L.cs")]);
    }

    /// Include targets survive quote style, spaced equals signs, paired
    /// elements, prefixed names, backslashes, and malformed tails, while
    /// commented-out references and MSBuild-variable includes stay out of scope.
    #[test]
    fn scope_should_read_xml_includes_and_skip_commented_or_variable_references() {
        let root = std::env::temp_dir().join(format!("rlt-csharp-xml-{}", std::process::id()));
        let _guard = Directory(root.clone());
        for dir in ["a", "b", "c", "commented", "vars"] {
            std::fs::create_dir_all(root.join(dir)).unwrap();
        }
        // project_scope canonicalizes its results; align expectations when the
        // platform temp dir is a symlink (macOS /var -> /private/var).
        let root = root.canonicalize().unwrap();
        for path in [
            "a/A.cs",
            "b/B.cs",
            "c/C.cs",
            "commented/Commented.cs",
            "vars/V.cs",
        ] {
            std::fs::write(root.join(path), "class C {}").unwrap();
        }
        // The commented and $(V) projects exist on disk so skipping their
        // references is observable in the asserted scope.
        std::fs::write(root.join("b/b.csproj"), "<Project />").unwrap();
        std::fs::write(root.join("c/c.csproj"), "<Project />").unwrap();
        std::fs::write(root.join("commented/commented.csproj"), "<Project />").unwrap();
        std::fs::write(root.join("vars/$(V).csproj"), "<Project />").unwrap();
        std::fs::write(
            root.join("a/a.csproj"),
            r#"<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
              xmlns:msb="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- <ProjectReference Include="../commented/commented.csproj" /> -->
  <ItemGroup>
    <ProjectReference Include = "..\b\b.csproj"><Project>../b/b.csproj</Project></ProjectReference>
    <msb:ProjectReference Include='../c/c.csproj' />
    <ProjectReference Include="../vars/$(V).csproj" />
  </ItemGroup>
</Project>
<ProjectReference Include="../trunc"#,
        )
        .unwrap();

        let scope = project_scope(&[root.join("a/A.cs")]).unwrap();

        assert_eq!(scope, ["a/A.cs", "b/B.cs", "c/C.cs"].map(|p| root.join(p)));
    }

    /// Changed sources replace throw evidence while unchanged sources retain
    /// their cached syntax tree.
    #[test]
    fn refresh_should_replace_changed_facts_and_reuse_unchanged_parses() {
        let root = std::env::temp_dir().join(format!("rlt-csharp-refresh-{}", std::process::id()));
        let _guard = Directory(root.clone());
        std::fs::create_dir_all(&root).unwrap();
        std::fs::write(root.join(".git"), "").unwrap();
        let caller = root.join("A.cs");
        let helper = root.join("T.cs");
        std::fs::write(&caller, "class A { public void Caller() { T.Helper(); } }").unwrap();
        std::fs::write(&helper, "class T { void Helper() { throw new E(); } }").unwrap();
        let paths = [caller.clone(), helper.clone()];
        let mut cache = CSharpIndex::build(&paths).unwrap();
        let backend = rust_llm_tidy_lang::backend_for("cs").unwrap();
        let initial = backend.lint_indexed(cache.parsed(&caller).unwrap(), &cache.index);
        let tree = cache
            .parsed(&caller)
            .unwrap()
            .syntax_tree()
            .root_node()
            .id();
        std::fs::write(&helper, "class T { void Helper() {} }").unwrap();

        cache.refresh(&paths);
        let refreshed = backend.lint_indexed(cache.parsed(&caller).unwrap(), &cache.index);

        assert!(initial.iter().any(|d| d.code == "DOC002"));
        assert!(!refreshed.iter().any(|d| d.code == "DOC002"));
        assert_eq!(
            cache
                .parsed(&caller)
                .unwrap()
                .syntax_tree()
                .root_node()
                .id(),
            tree
        );
    }

    /// Deleted helper sources drop their throw facts from the shared index,
    /// even when the supplied inputs are relative paths.
    #[test]
    fn refresh_should_drop_deleted_source_facts_for_relative_inputs() {
        let root = std::env::temp_dir().join(format!("rlt-csharp-relative-{}", std::process::id()));
        let _guard = Directory(root.clone());
        std::fs::create_dir_all(&root).unwrap();
        std::fs::write(root.join(".git"), "").unwrap();
        let caller = root.join("A.cs");
        let helper = root.join("T.cs");
        std::fs::write(&caller, "class A { public void Caller() { T.Helper(); } }").unwrap();
        std::fs::write(&helper, "class T { void Helper() { throw new E(); } }").unwrap();

        // Explicit CLI inputs arrive as given: relative to the cwd.
        let inputs = [path_from_cwd(&caller), path_from_cwd(&helper)];
        let mut cache = CSharpIndex::build(&inputs).unwrap();
        let backend = rust_llm_tidy_lang::backend_for("cs").unwrap();
        let initial = backend.lint_indexed(cache.parsed(&caller).unwrap(), &cache.index);
        std::fs::remove_file(&helper).unwrap();

        cache.refresh(&inputs);
        let refreshed = backend.lint_indexed(cache.parsed(&caller).unwrap(), &cache.index);

        assert!(initial.iter().any(|d| d.code == "DOC002"));
        assert!(!refreshed.iter().any(|d| d.code == "DOC002"));
    }

    /// Express `target` relative to the current directory without mutating it,
    /// mirroring how explicit CLI inputs reach the pipeline.
    fn path_from_cwd(target: &Path) -> PathBuf {
        let cwd = std::env::current_dir().unwrap();
        let cwd: Vec<_> = cwd.components().collect();
        let target_parts: Vec<_> = target.components().collect();
        let common = cwd
            .iter()
            .zip(&target_parts)
            .take_while(|(a, b)| **a == **b)
            .count();

        let mut rel = PathBuf::new();
        for _ in common..cwd.len() {
            rel.push("..");
        }
        for part in &target_parts[common..] {
            rel.push(part.as_os_str());
        }
        rel
    }

    struct Directory(PathBuf);

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