Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agent not added when using libraryDependencies := #30

Open
majk-p opened this issue Apr 12, 2024 · 0 comments
Open

Agent not added when using libraryDependencies := #30

majk-p opened this issue Apr 12, 2024 · 0 comments

Comments

@majk-p
Copy link

majk-p commented Apr 12, 2024

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 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant