Repository navigation
refactor(workspace-coupling): parse imports with syn AST and emit NDJSON - #1948
Merged
josecelano merged 2 commits intoJun 26, 2026
Merged
josecelano merged 2 commits into
josecelano merged 2 commits into
Conversation
There was a problem hiding this comment.
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_couplinglibrary (src/lib.rs) that extracts dependency import/path references viasynvisitors (includingUseTreegroups/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())); | ||
|
|
Member
|
ACK 12fa21f |
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
workspace-couplinganalysis 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 ofusetrees. This replaces the scanner with asyn-based AST parser and moves the parsing into alib.rsso 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
useitems withsynremoves 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 naivesynvisitor would otherwise miss.Changes
The import-extraction logic now lives in
src/lib.rsas pure functions (parse_imports_from_source/try_parse_imports_from_source) that walk thesynAST, includingUseTreerenames and macro token streams, so the binary inmain.rsonly handles file discovery, invocation, and NDJSON emission.Cargo.tomlgains thesyndependency. The change is confined to thecontrib/dev-tools/analysis/workspace-couplingtool; no tracker code is affected.Testing
Added
tests/parse_imports.rscovering grouped/nested/aliased use trees, root-alias-only imports, macro-embedded dependency paths, and the binary's NDJSON output. Run withcargo test -p workspace-coupling(9 tests pass) andcargo clippy -p workspace-coupling --all-targets -- -D warnings(clean).Closes #1805