julianpeeters / sbt-avrohugger

sbt plugin for generating Scala sources for Apache Avro schemas and protocols.
Apache License 2.0
133 stars 50 forks source link

sbt-avrohugger conflicts with custom source generators #19

Open chuwy opened 8 years ago

chuwy commented 8 years ago

We have a custom source generator in our project, which dumps some build info in project sources:

    sourceGenerators in Compile += Seq(Def.task {
      val file = (sourceManaged in Compile).value / "settings.scala"
      IO.write(file, s"""package com.snowplowanalytics.${name.value}.generated
                         |object ProjectSettings {
                         |  val version = "${version.value}"
                         |  val name = "${name.value}"
                         |  val organization = "${organization.value}"
                         |  val scalaVersion = "${scalaVersion.value}"
                         |}
                         |""".stripMargin)
      Seq(file)
    }.taskValue)

It was working well for a long time, but now (after we added sbt-avrohugger) it fails compile with following error:

[info] Compiling 38 Scala sources to /vagrant/target/scala-2.11/classes...
[error] /vagrant/target/scala-2.11/src_managed/main/ProjectSettings.scala:2: ProjectSettings is already defined as object ProjectSettings
[error] object ProjectSettings {
[error]        ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 23 s, completed Aug 3, 2016 11:32:33 AM

This problem arise at very weird circumstances - at first run it works fine, but then starts to fail with above error even if I delete target directory.

julianpeeters commented 8 years ago

I'm afraid I'm not sure what is going on there. If you have a minimal runnable example, I may have some time to take a look this weekend.

Re: deleting target, you may try deleting project/target

chuwy commented 8 years ago

Hello @julianpeeters

Here's the repository with extracted configuration, leading to failure: https://github.com/chuwy/avrofail

I don't know if this is supposed behavior, but problem seems gone when I add (scalaSource in avroConfig) := (sourceManaged in Compile).value / "avro" to my scalifySettings. It looks like it doesn't change anything in actual generated sources structure, but I don't see above error anymore.

Thanks.

julianpeeters commented 8 years ago

Thanks for the example project @chuwy

Your solution seems to be an improvement, regardless. I don't recall why I left the path ending empty, but now I think it should be:

val outputDir = scalaSource <<= (sourceManaged in Compile) { _ / "compiled_avrohugger" }

(calling the output dir "compiled_avrohugger" in order to

  1. achieve some uniformity with sbt-avro
  2. avoids having "scala" in the name, since both Scala and Java enums can be generated)

The downside is that I'm a little busy currently and my queue is out likely two weeks, so even if I got a PR for the work, I still won't be able to publish a new version for a while.

Cheers