"object index is not a member of package views.html" when opening scala play project in scala ide
Asked Answered
S

11

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..

Spinach answered 11/2, 2015 at 15:50 Comment(3)
Can you show us the code where the compiler complains?Oomph
I suspect that the play2 plugin is not compatible with scala ide 4. I will try to switch to scala ide 3. #28105468Spinach
Please post your build.sbtIaria
M
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

Michealmicheil answered 26/1, 2016 at 23:55 Comment(5)
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!Unscientific
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.Jamaaljamaica
@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).Michealmicheil
@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.Jamaaljamaica
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.Ashe
H
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.

Haughty answered 23/3, 2015 at 2:13 Comment(5)
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.Haughty
Update: I just use IntelliJ w/ Scala and SBT plugin nowHaughty
Had same issues with IntelliJ 2016.1. This solution works fine for me.Whiteness
@Haughty .... I can't find target/scala-2.11/src_managed/main directory. I have only target/scala-2.11/twirl/mainGeorgetta
Many will hit this page again and again.Indefinite
T
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.

Tibetoburman answered 16/2, 2015 at 22:49 Comment(2)
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 routesHaughty
I have never found such error. But in this other thread they discuss that same problem. Could it be that your routes file is empty?Tibetoburman
S
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.

Schnell answered 16/2, 2015 at 22:21 Comment(0)
P
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
Penhall answered 25/4, 2015 at 22:57 Comment(0)
S
2

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

Semivowel answered 30/4, 2015 at 5:40 Comment(1)
This fixed it for me as well. Not sure why the plugin doesn't find it.Insectile
N
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.

Net answered 3/1, 2017 at 23:57 Comment(0)
E
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.

Ernaldus answered 8/7, 2019 at 10:53 Comment(0)
K
0

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

Kab answered 18/7, 2015 at 10:32 Comment(0)
Y
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

Yacht answered 17/5, 2017 at 23:6 Comment(0)
U
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.

Urethroscope answered 9/11, 2017 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.