Question

In scala it is common practice to stack package statements to allow shorter imports, but when I load a file using stacked packages into the scala ide and I attempt to use an import starting with the same organization I get a compiler error from what appears to be the presentation compiler. The code compiles fine in sbt outside of the IDE.

An example code snippet is as follows:

package com.coltfred
package util
package time

import com.github.nscala_time.time.Imports._

On the import I get the error object github is not a member of package com.coltfred.util.com.

If I move the import to a single line the error will go away, but we've used this practice frequently in our code base so changing them all to be single line package statements would be a pain.

Why is this happening and is there anything I can do to fix it?

Edit:

I used the eclipse-sbt plugin to generate the eclipse project file for this. The directory structure is what it should be and all of the dependencies are in the classpath.

Edit 2:

It turns out there was a file in the test tree of the util package (which should have been in the same package), but had a duplicate package statement at the top. I didn't check the test tree because it shouldn't affect the compilation of the main tree, but apparently I was wrong.

Was it helpful?

Solution 2

This is a common annoyance that annoyed paulp into an attempt to fix it. His idea was that a dir that doesn't contribute class files shouldn't be taken as a package. If you can take util as scala.util, you should do so in preference to foo.util where that util is empty.

The util dir is the usual suspect, because who doesn't have a util dir lying around, and in particular, ./util?

apm@mara:~/tmp/coltfred$ mkdir -p com/coltfred/util/time
apm@mara:~/tmp/coltfred$ mkdir -p com/coltfred/util/com
apm@mara:~/tmp/coltfred$ vi com/coltfred/util/time/test.scala
apm@mara:~/tmp/coltfred$ scalac com/coltfred/util/time/test.scala
./com/coltfred/util/time/test.scala:5: error: object github is not a member of package com.coltfred.util.com
import com.github.nscala_time.time._
           ^
one error found
apm@mara:~/tmp/coltfred$ cat com/coltfred/util/time/test.scala
package com.coltfred
package util
package time

import com.github.nscala_time.time._

class Test

apm@mara:~/tmp/coltfred$ 

To debug, find out where the offending package is getting loaded from.

OTHER TIPS

Not sure why the Scala IDE is not liking this, but you can force the import to start at the top level using _root_:

import _root_.com.github.nscala_time.time.Imports._

See if that avoids irritating the IDE.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top