Skip to content

refactor(workspace-coupling): parse imports with syn AST and emit NDJSON - #1948

Merged
josecelano merged 2 commits into
torrust:developfrom
mvanhorn:refactor/1805-workspace-coupling-syn-scanner
Jun 26, 2026
Merged

josecelano merged 2 commits into
torrust:developfrom
mvanhorn:refactor/1805-workspace-coupling-syn-scanner

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Summary

The workspace-coupling analysis tool found cross-crate imports with a regex scan over source text and printed its diagnostics as ad-hoc stderr lines. Regex matching over Rust source is brittle: it misclassifies imports inside strings or comments, and it has no real model of use trees. This replaces the scanner with a syn-based AST parser and moves the parsing into a lib.rs so it is unit-testable, and adopts a line-delimited JSON (NDJSON) output contract so the tool's results can be consumed programmatically.

Why this matters

Issue #1805 asks for the regex scanner to be replaced with a proper AST parse and for the tool to emit a structured output contract. Parsing the actual use items with syn removes the false positives/negatives inherent to text matching, and NDJSON makes the coupling report machine-readable instead of something a human has to eyeball in stderr. To avoid regressing coverage the old text scan happened to have, the parser also reports root-level crate aliases (use dep_crate as alias;) and dependency paths that appear only inside macro invocations (matches!(dep_crate::Thing::A, ...)), which a naive syn visitor would otherwise miss.

Changes

The import-extraction logic now lives in src/lib.rs as pure functions (parse_imports_from_source / try_parse_imports_from_source) that walk the syn AST, including UseTree renames and macro token streams, so the binary in main.rs only handles file discovery, invocation, and NDJSON emission. Cargo.toml gains the syn dependency. The change is confined to the contrib/dev-tools/analysis/workspace-coupling tool; no tracker code is affected.

Testing

Added tests/parse_imports.rs covering grouped/nested/aliased use trees, root-alias-only imports, macro-embedded dependency paths, and the binary's NDJSON output. Run with cargo test -p workspace-coupling (9 tests pass) and cargo clippy -p workspace-coupling --all-targets -- -D warnings (clean).

Closes #1805

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the contrib/dev-tools/analysis/workspace-coupling analysis tool to replace brittle regex-based import scanning with a syn AST-based parser, and to emit structured NDJSON events on stderr so the tool output can be consumed programmatically (per the linked issue/ADR requirements).

Changes:

  • Introduces a new workspace_coupling library (src/lib.rs) that extracts dependency import/path references via syn visitors (including UseTree groups/renames and macro token scans).
  • Updates the binary (src/main.rs) to use the new library, propagate parsing/IO errors, and emit NDJSON status/error events to stderr with explicit exit codes.
  • Adds unit/integration tests that validate import extraction behavior and NDJSON stderr output, and updates dependencies to add syn.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
contrib/dev-tools/analysis/workspace-coupling/tests/parse_imports.rs Adds coverage for syn-based parsing and validates the binary’s NDJSON/error behavior via a fixture workspace.
contrib/dev-tools/analysis/workspace-coupling/src/main.rs Switches to AST-based scanning via the new library and implements NDJSON stderr event emission + structured error handling.
contrib/dev-tools/analysis/workspace-coupling/src/lib.rs New import/path extraction library built on syn AST visitation with special handling for use-trees and macro token streams.
contrib/dev-tools/analysis/workspace-coupling/Cargo.toml Replaces regex with syn (full + visit) for AST parsing.
Cargo.lock Updates lockfile to reflect dependency changes (adds syn, removes regex from this crate).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +68 to +72
fn emit_event(event: &CliEvent) -> io::Result<()> {
let mut stderr = io::stderr().lock();
serde_json::to_writer(&mut stderr, event)?;
stderr.write_all(b"\n")
}
Comment on lines +227 to +231
impl Drop for FixtureWorkspace {
fn drop(&mut self) {
drop(fs::remove_dir_all(&self.root));
}
}
Comment on lines +219 to +220
let root = std::env::temp_dir().join(format!("workspace-coupling-{name}-{}-{timestamp}", std::process::id()));

@josecelano

Copy link
Copy Markdown
Member

ACK 12fa21f

@josecelano
josecelano merged commit a4e83c7 into torrust:develop Jun 26, 2026
16 checks passed

This branch was previously deployed

1 inactive deployment
coverage — 12fa21f2 Deployed Jun 26, 2026 by mvanhorn via Generate Coverage Report #1485
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Overhaul workspace-coupling report tool: replace regex scanner with syn and adopt CLI output contract

3 participants