regexident / cargo-modules

Visualize/analyze a Rust crate's internal structure
Mozilla Public License 2.0
948 stars 48 forks source link

Analyze and forbid dependency from one mod to another? #219

Open fzyzcjy opened 10 months ago

fzyzcjy commented 10 months ago

Hi thanks for the tool! Given the acyclic option, I guess this crate does analyze all dependencies between mods. However, it seems that currently it only outputs a dot graph that shows the ownership between modules, instead of dependency relationship.

In my case, say I have mod A, B, C. I want to check automatically that, the mod A is never used by mod B (but can be used by mod C), such that it satisfies things like layered architecture.

regexident commented 10 months ago

For "dependency" (/"uses") relationships though need to add the --uses flag to your cargo modules generate graph … command (which has --no-uses as implicit default).

That said there currently is no way (yet) for the tool to detect dependencies from function bodies though, which is an admittedly big blind spot of the --uses flag.

fzyzcjy commented 10 months ago

Thank you for the information!

That said there currently is no way (yet) for the tool to detect dependencies from function bodies though, which is an admittedly big blind spot of the --uses flag.

So I wonder will this be implemented in the future?

regexident commented 10 months ago

I very much hope so!

The tool uses rust-analyzer (RA) as a library dependency and I simply haven't yet been able to fully figure out how walk the types used inside a function body.

The internal API of RA is mostly rather sparsely documented, and often very indirect in nature due to its extensive use of interning, which makes navigating it somewhat difficult as an outsider. As such implementing a new feature in cargo-modules frequently turns into first having to search the RA codebase for potentially useful types and methods and then trying to figure out how to actually use them by reading through any usages within RA itself. The function body stuff has been particularly opaque so far, as it doesn't seem to work like the rest of RA. I recently found a possible approach but unfortunately ultimately couldn't manage to make it work. I'm not giving up just yet though.

Occasionally I've asked the RA devs for help on their Zulip but it feels kinda bad to ask them for customer support for their private API that isn't really intended for public use, afaict.

fzyzcjy commented 10 months ago

I see, thank you!

regexident commented 10 months ago

You may want to watch this one https://github.com/regexident/cargo-modules/pull/227 :wink:

fzyzcjy commented 10 months ago

Wow that looks great, thank you! 😄