sbt-javaagent
sbt-javaagent copied to clipboard
Agent not added when using `libraryDependencies :=`
Problem description
The plugin doesn't add agent when user overrides libraryDependencies
with :=
as opposed to appending them. For example when project is defined like this:
lazy val root =
(project in file("."))
.enablePlugins(JavaAgent)
.settings(
libraryDependencies := Seq("com.datadoghq" % "dd-trace-api" % "1.32.0"),
javaAgents += {
"com.datadoghq" % "dd-java-agent" % "1.32.0" % "test"
}
)
The agent gets defined in terms of the plugin, but it's not added to java options. Here's the sbt output:
sbt:root> show root/javaAgents
[info] * AgentModule(dd-java-agent,com.datadoghq:dd-java-agent:1.32.0:javaagent,AgentScope(false,true,false,true),)
sbt:root> show root/javaOptions
[info] *
[success] Total time: 0 s, completed 12 kwi 2024, 11:46:57
sbt:root> show root/resolvedJavaAgents
[info] *
[success] Total time: 0 s, completed 12 kwi 2024, 11:47:02
Root cause analysis
This happens, because for javaOptions
to be populated, the plugin goes over defined agents and tries to resolve them. It only adds the ones that are resolved, as per snippet below
private def resolveAgents = Def.task[Seq[ResolvedAgent]] {
javaAgents.value flatMap { agent =>
update.value.matching(Modules.exactFilter(agent.module)).headOption map {
jar => ResolvedAgent(agent, jar)
}
}
}
This works because update
resolves all dependencies defined in libraryDependencies
, and the plugin's projectSettings
append agent to the dependencies with:
libraryDependencies ++= javaAgents.value.map(_.module),
Now when the user overrides libraryDependencies
the entire mechanism breaks.
Expected behavior
Overriding libraryDependencies
should not interfere with the plugin logic. This might be especially counterintuitive and difficult to debug for users less familiar with sbt. Additionally it would be very nice to have the plugin log warning when javaOptions
end up being empty when javaAgents
are not.
Full reproduction example: https://github.com/majk-p/sbt-javaagent-vs-librarydependencies
I'm happy to help with the fix :wink: