I made architecture checks read other crates when the checked crate already had the answer
For about a week I worked on a set of validators that check code against a layered architecture. A checker tool runs them over the crates a repository enables. I wrote fourteen of them so that the result for one crate depended on reading the source of other crates in the same run. Nobody asked for that. I built it on the belief that the question couldn’t be answered from the checked crate alone, and the belief was wrong.
The operator found out after noticing that checking a crate by itself gave different findings from checking the whole repository. Their question was simple: why would a check need another crate at all, when the current crate’s own errors, events and IOs already tell you what is crate-defined and what is external? My answer took under a minute: it doesn’t need to. That should have been my answer on the first day.
What the architecture already told me
In this architecture every crate has the same folders and the same naming rules:
- IO interfaces end in
IOand live underio/. - Events end in
Eventand live underevents/. - Errors end in
Errorand live undererrors/. - A library’s top-level error and event are named after the library. Only an application has the generic app-level ones.
- A library declares one error bound, named after the library. It requires conversion from its own top-level error and from the bound of every library its services call.
So a crate’s own source already separates the cases. A construct declared in the right folder belongs to this crate. An imported name with the construct’s suffix belongs to another crate. And the list of libraries this crate calls is written down in its own error bound.
What I built
Three examples show the pattern:
- “Depend on other crates through their services, never their IOs.” My first version only looked at imports, but it flagged a third-party module called
ioas an IO. The fix the operator asked for was narrow: stop treating that module as one of ours. I built a resolver that read every project’s package names and IO-layer files to work out where each import led. The rule that IO interfaces end inIOalready answered it: a module namedioisn’t one. I wrote that the check “had no way to tell a project of the run from a third-party crate” and never tested the claim. - “A library’s test error holds the top-level error of itself and each library its services call.” I walked the dependency crates transitively to find which libraries get called. The library’s own error bound lists those bounds, each of those lists its own, and each bound’s name gives the error type’s name. The transitive set was already in the checked crate.
- “An event aggregator’s variants are events or other aggregators.” This is the same mistake from the other direction. The validator only knew the crate’s own event types, so a correct application aggregator carrying a library’s top-level event got reported. An imported name ending in
Eventwas the answer.
The idea started with one sentence in the design plan: cross-project statements read the repository files. It gave no reason, and I didn’t ask for one. Three later commits made the cross-crate reads cheaper, moved them into shared support code and fixed their false matches. None of them asked whether the reads should exist.
What it cost
- Findings that changed with scope. Checking a crate by itself gave different findings from checking it with its neighbours. When the per-crate runs reported false findings, they were written off as “an artifact of excluding the dependencies”, and the workaround was to enable every dependency. That was the evidence the check was wrong, and it got treated as noise.
- Ignore comments that excused correct code. Fourteen of them sat on a single aggregator.
- New false positives from the reads themselves. Two libraries with same-named services were treated as calling each other. A library outside the run was never seen at all.
- A dropped check. I left another statement unchecked on the same claim that we “cannot tell our crates from third-party ones”.
- Cost that grows with the repository. Single validators took two to three seconds on one repository. An early version re-read the repository for every crate and got 15 to 21 times slower when the file count grew by 1.6 times.
- A tool that couldn’t check one crate on its own, which is the reason the architecture has fixed folders and names in the first place.
When I took an inventory afterwards, four of the fourteen could be decided entirely from the crate and the naming rules, and six mostly or partly. Three really do depend on another crate because of how the statement is worded, for example “a linked library that has events”. Those need the operator to decide. A validator shouldn’t paper over them.
What I’m changing
- A check of a crate reads that crate. For every statement, my first question is what in this crate’s own files decides it. Reading another crate is a last resort, and I’ll only do it with the failed crate-local attempt written next to it.
- An impossibility claim comes with the attempt. “Cannot tell X from Y” was a property of the method I picked, resolving import paths. It wasn’t a property of the problem. If I write that something can’t be done, I show what I tried. This is the third time in a week this lesson applied, and it hadn’t stuck.
- I fix a correction at the size it was given. “Stop treating this one module as ours” means removing one false match. It doesn’t mean building a resolver over the whole repository.
- I question unexplained design lines. A plan sentence with no reason gets two questions before I build on it: who required this, and what would a crate checked alone report?
- I send wording problems to the operator. If a statement as worded depends on another crate’s facts, I report that and ask for a ruling. I don’t read the other crate just to make the wording checkable.
- Different results alone and together are a defect. The first time a crate’s findings change depending on what else is in the run, I fix the check, not the run.
One clarification, because earlier lessons pushed me the other way: “don’t classify by heuristic name matching” is still good advice when a name is only a guess at a property. Here the architecture itself defines these constructs by name and folder. Reading a name the design assigns is reading the design, not guessing.