Zac Sweers
08/03/2023, 8:04 PMTing-Yuan Huang
08/03/2023, 10:13 PMCodeGenerator.associateWithClasses
2. (non-root) Obtained indirectly via resolving a type reference. For example, the KSClassDeclaration
of Int
can be obtained by resolving the type of val x: Int
and then KSType.declaration
. The root of this reference / navigation chain should be associated with the corresponding outputs.
When there is a change in the classpath, KSP invalidates the navigation / visit / reference chains all the way toward roots. Source files that are reachable from the changes are considered dirty and will get re-processed.
In other words, whenever there is a change in the compile classpath / dependencies, KSP will invoke the processors and pass them source files that are considered dirty, even when none of the source files are dirty. Note that `Resolver.getAllFiles`/ Resolver.getSymbolsWithAnnotation
only returns / considers dirty files.Zac Sweers
09/03/2023, 8:14 PMCodeGenerator.associateWithPackage()
. Dagger/Hilt does something similar with a shared package, but works around this by inspecting the classpath prior to compilation and generating a processable source file that KSP can use instead.Ting-Yuan Huang
09/07/2023, 9:04 PMCodeGenerator.associateWithClass(outFile1, ...)
does not cause a class in classpath / library to appear in getSymbolsWithAnnotation
. It only causes the sources that are associated with outFile1
in the current compilation unit to be reprocessed.
If, with CodeGenerator.associateWithPackage(outFile1, pkg1)
, you mean that changes to or within pkg1
should cause sources that are associated with outFile1
in the currently compilation unit to be considered / returned by getSymbolsWithAnntoation
, then declaring the outFile1
to be aggregating might be what you need; Any changes in classpath will invalidate all aggregating outputs, and therefore their input files. Of course, that can be somewhat less efficient than a finer grained package-only API, but if it is already aggregating, there won't be any difference.
If you mean a way to return elements in libraries by getSymbolsWithAnnotation
, I'm afraid that could be slow. KSP would have to scan the classpath unconditionally and it can do no faster than other approaches. Similar to Anvil, you might write some hints to the annotation of a predefined class in each module, and reference + read them in the current compilation unit instead.
We do have Resolver.getDeclarationsFromPackage
and scanning with it should be easier than scanning jars. It isn't immune to the aforementioned performance implications though, so please use it at your discretion. and don't forget to declare the output aggregating.Zac Sweers
09/07/2023, 9:14 PMZac Sweers
09/07/2023, 9:15 PMTing-Yuan Huang
09/07/2023, 9:42 PMcom.hint.<deterministic-sparse-hash>
instead of hiding in all packages, then scanning a relatively small com.hint
should be ok.Zac Sweers
09/07/2023, 9:43 PMZac Sweers
09/07/2023, 9:45 PMTing-Yuan Huang
09/07/2023, 10:10 PM