34

I've created a play project with play 2.3.7

In the project directory, I ran activator and ran the eclipse command to generate eclipse project files.

When I go to eclipse (I'm using the Scala IDE from typesafe Build id: 4.0.0-vfinal-20150119-1023-Typesafe , there is an error in my Application.scala file:

object index is not a member of package views.html

Is there something amiss with my setup? The app runs fine when I execute run at the activation console prompt.

Thanks!

EDIT: Added code

package controllers

import play.api._
import play.api.mvc._

object Application extends Controller {

  def index = Action {
    Ok(views.html.index("Your new application is ready."))
  }

} 

The error is on the 'Ok..' line.

There is a file in views called index.scala.html, and the app runs file when I run it from activator..

user384842
  • 1,946
  • 3
  • 17
  • 24
  • 1
    Can you show us the code where the compiler complains? – Jean Logeart Feb 11 '15 at 16:01
  • I suspect that the play2 plugin is not compatible with scala ide 4. I will try to switch to scala ide 3. http://stackoverflow.com/questions/28104968/scala-ide-4-0-0-thinks-theres-errors-in-an-out-of-the-box-play-framework-2-3-7 – user384842 Feb 11 '15 at 16:39
  • Please post your `build.sbt` – BAR Feb 13 '15 at 15:54

11 Answers11

29

Occasionally after adding a view in Play 2.4.x, IntelliJ IDEA sometimes gets confused and absolutely refuses to build. Even rebuild Project fails:

enter image description here

This still happens from time-to-time in IDEA 15. And when it does, the command line provides the quickest, most-reliable fix:

sbt clean; sbt compile

That's it! IDEA will now compile the project as expected.

Update:

In the rare case that sbt compile completed successfully on the command line, but IntelliJ IDEA 15 still gives the same "object x is not a member" error, then this has solved IDEA's confusion:

File Menu:

enter image description here

Brent Faust
  • 9,103
  • 6
  • 53
  • 57
  • 2
    It worked for me but not until I opened the Terminal in Intellij 15 and then installed sbt with Homebrew i.e brew install sbt. There after, the sbt clean; sbt combile was enough to make remove the bug. Thanks! – Taiwotman Mar 16 '16 at 18:30
  • 1
    This solution also fixed my issue. By chance, did you change the version of Scala used via IJ in the middle of rebuilds? I did this and it seems to have triggered it, requiring me to use `sbt clean; sbt compile` from the terminal. – Thomas Farvour Apr 10 '16 at 22:41
  • @ThomasFarvour Not that I recall, but good idea to keep in mind. I haven't seen it happen lately (since I've been using IDEA 16.1 EAP). – Brent Faust Apr 13 '16 at 22:11
  • @BrentFoust turns out in my case, I had to manually add back `target/scala-2.11/routes/main` to the list of sources in my root module defined in sbt. I suspect this is an IntelliJ bug during the auto-import phase of sbt as others seem to have indicating having to do this for twirl. – Thomas Farvour Apr 14 '16 at 23:26
  • 1
    Was having OP's exact problem, and this solution, specifically the Invalidate Caches, worked for me. Running IntelliJ with play 2.6.0. I had to manually update the Scala, SBT and Play versions in my build and properties files to be the most up to date and then used this solution. – NateH06 Jul 05 '17 at 14:49
12

The other solutions did not work for me. Some would give me different errors, some would clear the Problems tab but leave me with a red squiggle under views.html.index and auto-complete would not work with the scala.html templates.

What finally worked was to open the project's properties, go to Java Build Path > Source, and add both of the following directories:

target/scala-2.11/src_managed/main
target/scala-2.11/twirl/main

If you only do target/scala-2.11/twirl/main then you'll miss out on the class files generated from the conf directory.

Neil
  • 24,551
  • 15
  • 60
  • 81
einnocent
  • 3,567
  • 4
  • 32
  • 42
  • 1
    Note: when you do `sbt eclipse`, the custom project properties above get overwritten. I'm still looking for a solution that gets these directories into the Eclipse classpath w/o getting lost on that operation, I suspect it involves editing `build.sbt`, but I'm not quite sure how yet. I'll edit my answer when I find it. – einnocent Mar 26 '15 at 17:27
  • Update: I just use IntelliJ w/ Scala and SBT plugin now – einnocent Dec 08 '15 at 20:23
  • Had same issues with IntelliJ 2016.1. This solution works fine for me. – Vova Rozhkov Apr 22 '16 at 15:28
  • @einnocent .... I can't find **target/scala-2.11/src_managed/main** directory. I have only **target/scala-2.11/twirl/main** – Necromancer Aug 26 '16 at 02:15
  • Many will hit this page again and again. – Laeeq Dec 28 '17 at 15:56
8

In Scala IDE 4.0.0 thinks there's errors in an out-of-the-box Play Framework 2.3.7 program you can find the solution (in brief: adding target/scala-2.11/twirl/main folder to the compilation path), give it a try.

Community
  • 1
  • 1
Luis Rodero-Merino
  • 1,929
  • 1
  • 12
  • 8
  • Adding that folder to the Java Build Path > Source removes the original error from the Problems tab, but adds three more, all: `not found: value routes` – einnocent Mar 23 '15 at 01:53
  • I have never found such error. But in [this other thread](http://stackoverflow.com/questions/18196123/scala-play-not-found-value-routes-eclipse-and-idea) they discuss that same problem. Could it be that your routes file is empty? – Luis Rodero-Merino Mar 27 '15 at 08:59
4

I had the same problem. I added target/scala-2.x/classes and target/scala-2.x/classes_managed to my Java build path and Eclipse stopped complaining.

Neil
  • 24,551
  • 15
  • 60
  • 81
2

I had the same issue running Play 2.4.0-RC1 using default SBT layout (disablePlugins(PlayLayoutPlugin)) and solved it by adding to build.sbt:

sourceDirectories in (Compile, TwirlKeys.compileTemplates) :=
    (unmanagedSourceDirectories in Compile).value
Neil
  • 24,551
  • 15
  • 60
  • 81
Lera
  • 51
  • 2
2

Adding target/scala-2.11/twirl/main which is having views.html package to source fixed for me.

Neil
  • 24,551
  • 15
  • 60
  • 81
1

@brent-foust 's answer worked for me but only initially. Every time I rebuilt the project from within IDEA I would then get "not found: routes" errors from within target\scala-2.11\twirl\main\views\html\main.template.scala until I performed Brent's workaround again.

I eventually discovered the solution to that was changing a line in the .iml file from

<excludeFolder url="file://$MODULE_DIR$/target/scala-2.11/src_managed/main" />

to

<sourceFolder url="file://$MODULE_DIR$/target/scala-2.11/src_managed/main" isTestSource="false" />

I don't know what the long term implications of doing this are but it has fixed this problem. Some of the other similar problems mentioned might also be fixed by applying the same change to some of the other folders listed in the .iml.

1

I tried all solutions without any positiv result.
So I went to Preferences > Build, Execution, Deployment > Build Tools > sbt and checked Use sbt shell for imports and builds.
This let the compile button in intelliJ compile with the sbt shell. I think this is better anyway, since a build server or something similar will compile the same way and not like intelliJ.

Nabil A.
  • 3,270
  • 17
  • 29
0

For me when importing the project into intellij making sure I "checked" the "auto import" checkbox did the trick.

Tomer Ben David
  • 8,286
  • 1
  • 43
  • 24
0

1) Add the following line to your sbt.build file:

EclipseKeys.preTasks := Seq(compile in Compile)

2) Add the follwing line to your plugins.sbt file under the project folder:

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0")

3) Run the "eclipse" command from within sbt

as explained in the documentation of the play framework:

Setting up your preferred IDE

user2038596
  • 535
  • 1
  • 3
  • 14
0

Basically we need a way to put the compiled classes on the path for this to work.

I did the following to fix it. Since the projects compiles to the target directory. I went to the Project Properties -> Java Build Path and added a few folders that look like this,

target/scala-2.12/routes/main target/scala-2.12/twirl target/scala-2.12/twirl/main

Now i dont want you to assume you will have these exact folders in your case too. That depends on your project setup. But you should add the folders inside the target/scala-2.x folder.

Som Bhattacharyya
  • 3,972
  • 35
  • 54