square / anvil

A Kotlin compiler plugin to make dependency injection with Dagger 2 easier.
Apache License 2.0
1.32k stars 83 forks source link

Add ability to generate a ContributesBinding baseline? #875

Open handstandsam opened 9 months ago

handstandsam commented 9 months ago

Could you generate a file that contains the following (see snippet) using https://github.com/square/anvil/blob/aa24f31780247c3331f0b64de2b70333011fe036/compiler/src/main/java/com/squareup/anvil/compiler/codegen/BindingModuleGenerator.kt#L58-L60 ?

@AppScope
something.Cool <- something.CoolImpl
com.ReplaceMe <- com.ReplaceMeImpl (**REPLACED**) <- com.IReplacedYou // Calls out what it is replacing

@ActivityScope
something.Else <- something.ElseImpl
standalone.Impl <- standalone.Impl // Provided as a concrete type

This sort of baseline would be incredibly useful in many cases and for debugging.


Writing out replaces is also something that would be nice to call out in bindings. I've seen bugs and issues come up when it wasn't known that a replaces was happening.

This file could be generated per module.


This file would get noisy in Git. Maybe just having the replaces only in the baseline by default would be good?

ZacSweers commented 9 months ago

is baseline the right word for this? Seems more like something similar to compose's compiler reports.

Sidenote - BindingModuleGenerator.kt is going away soon

handstandsam commented 9 months ago

I wanted to call it baseline because like Android lint or dependency-guard, I'd want to see in source control that my baseline changed.

This is because it's so easy to introduce a replaces and have no idea it's happening.

I was on a project previously where an important upgrade checking feature was being replaced by a fake implementation and it was undiscovered for a year.


For my use case, a report could be useful, but wouldn't help me automatically detect these sorts of changes.

ZacSweers commented 9 months ago

So you want something like a lock file for replacements?

handstandsam commented 5 months ago

Yes, and for ordering/priority.

ZacSweers commented 5 months ago

tbh this is probably a non-starter. See https://github.com/google/ksp/issues/1677

handstandsam commented 5 months ago

Thanks for linking that issue. Makes sense, but that's unfortunate.

RBusarow commented 3 months ago

The KSP issue above only applies to KSP. This is still on the table for embedded mode.