1

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.

Community
  • 1
  • 1
coltfred
  • 1,470
  • 9
  • 17

2 Answers2

3

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.

Shadowlands
  • 14,994
  • 4
  • 45
  • 43
  • Sure you could do that but the question is focused on why the IDE's presentation compiler requires babying that the normal compiler doesn't. – Leif Wickland Oct 10 '13 at 01:41
  • I am providing a work-around to the part of the question that focuses on: "is there anything I can do to fix it?" Is that not useful? – Shadowlands Oct 10 '13 at 01:55
  • Your suggestion does indeed fix my problem. I'd *really* like to find a way to actually fix it without a code change as it seems silly to have to put cruft in just to babysit eclipse. – coltfred Oct 10 '13 at 02:19
  • Sorry @shadowlands. My misdirected annoyance is at the IDE's brokenness., not your helpful workaround. – Leif Wickland Oct 10 '13 at 03:42
  • @LeifWickland That's cool. I'd be curious why this breaks too, although I don't use Scala IDE myself. – Shadowlands Oct 10 '13 at 03:49
  • 1
    @coltfred Could you please file a ticket in the [Scala IDE issue tracker](https://www.assembla.com/spaces/scala-ide/support/tickets). Please, make sure to provide a self-contained example that can be used to reproduce the issue. In fact, we do use nested packages ourselves (also in the Scala IDE codebase), but we have never seen the issue you have reported here. I've tried to reproduce the problem, but I'm out of luck. Also, for a next time, if you experience troubles with the Scala IDE, I'd suggest you stop by the [scala-ide-user ML](https://groups.google.com/d/forum/scala-ide-user). – Mirco Dotta Oct 10 '13 at 07:44
  • 2
    You seem to have a directory `com` inside util (as the error message says). Nested imports work like that. This `com` directory can be either in the target directory, or in the source folder. My guess is that you have it in the source folder, and Sbt does not set a sourcepath. – Iulian Dragos Oct 10 '13 at 08:38
1

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.

som-snytt
  • 39,429
  • 2
  • 47
  • 129
  • I call my directory pUtil. I always use a 'p' at the beginning of all my package names. – Rich Oliver Oct 10 '13 at 09:54
  • I selected this as the "right" answer, because it gets at the root of the problem, but beware future people that the test files are taken into consideration as well! – coltfred Oct 10 '13 at 19:24
  • @coltfred That's a good one. In earlier times, I kept tests in a separate eclipse project. With sbt or maven scopes, less of an issue. – som-snytt Oct 11 '13 at 00:38