Luis Sisamon
Luis Sisamon

Reputation: 101

Object not found when is in src_managed folder

This is (I think) a different question from Type not found: type .. when type is in src_managed folder.

I am building from sbt, 1.1.1, I have set up a code generation task in sbt that is runnign as expected and creaing a number of files with the same structure.

package com.a3.traffic
package object Vendor

And they are imported in other files as:

import com.a3.traffic.Vendor._

The files are generated under src_managed. I have tried two different setups

src_managed / main / Vendor

src_managed / main / scala / com / a3 / traffic / Vendor

In both cases I get the following error:

[error] /Users/luis/IdeaProjects/SparkTrafficAllocation/core/src/main/scala/com/a3/traffic/Params.scala:5:28: object Vendor is not a member of package com.a3.traffic
[error] import com.a3.traffic.Vendor._      

I can fix that by moving the generatd code to src / main / scala / com / a3 / traffic / Vendor (that is with the rest of my code) but then I get this.

[error] /Users/luis/IdeaProjects/SparkTrafficAllocation/core/target/scala-2.11/src_managed/main/scala/com/a3/traffic /Vendor/Vendor.scala:3:16: package is already defined as package object Vendor
[error] package object Vendor {

I find this quite puzzling. The objects defined in src_managed can not be seen from my code, but it can see what is in the package.How can I make the objects in src_managed available to the rest of the package?

EDIT I created a minimal project to show this https://github.com/sisamon/MinimalApp

EDIT 2 I am using a name / package.scala / package.scala => object name as the original name.scala case class / case object was not working.

Upvotes: 2

Views: 298

Answers (1)

The problem is in here.

def generator (x: Country) = {
  generateADT("Vendor", x.vendor)
  generateADT("InstallationType", x.installationType)
}

Remember that your task MUST returns a Seq with the ALL Files that were generated!.

And, your generateADT each return a Seq of one File, and as such, you are returning only the Seq of the last call (which in this case is InstallationType), that is why your Vendor is not found!

You can check that by commenting the second line, which will make the first line the return, in that case the Vendor will be found!

There a couple of ways to fix this, the most simple & elegant (IMHO) would be this:

def generator (x: Country): List[File] =
  List(
    ("Vendor", x.vendor),
    ("InstallationType", x.installationType)
  ).map((generateADT _).tupled)

def generateADT (base: String, d: Descriptor): File = {
  // ...
  // The path really does not matter, as long as it is inside the src_managed folder.
  val adtFile = (sourceManaged in Compile).value / s"${base}.scala"
  IO.writeLines(adtFile, code)
  adtFile
}

PS: As an advice, you should explicitly put the return type of all functions/methods. Not only it will help the type inference of other things, also it will avoid a couple of compile errors and will increase the readability of your code.

Upvotes: 2

Related Questions