Play framework tutorial: Cannot resolve symbol 'index'?
Asked Answered
C

7

24

I followed a tutorial here and although I got the entire tutorial working just fine, one of the classes in my IntelliJ project is marked with "red warnings". I cannot figure out what the problem is here? The project works but it shows these warnings? Any ideas on what this is and how to fix it? In the tutorial, about 3.5 minutes into it, the red marks magically disappear and the video presenter doesn't explain it.

enter image description here

Conidiophore answered 5/6, 2013 at 4:35 Comment(6)
What version of IntelliJ are you using? Make sure that you have the Play 2.0 support plugin installed.Fanlight
I am using the community edition 12.1.3. I heard it has built-in support. Is that true?Conidiophore
I don't use IntelliJ, but in the screenshot in your question the import views.html.* is greyed out, doesn't that mean it's been commented out?Crematorium
@Crematorium - No, that is just a warning message. That being said, are the static 'index' and 'routes' objects in the 'views' package? If not, what package are they in? I suspect I am just missing a static import that IntelliJ is not smart enough to resolve?Conidiophore
The routes object is at controllers.routes. Maybe it's just been a long day but I'm not sure I know what you mean by the index object ... do you mean the compiled version of a index.scala.html file? That would be at views.html.index assuming it was in the root of the views folder of the project, otherwise views.html.<package>.index.Crematorium
I realise you're referring to the file show in the photo in your post now. I guess it has been a long day.Crematorium
F
8

The Play 2.0 support plugin is only available in the IntelliJ IDEA Ultimate edition.

enter image description here

Fanlight answered 5/6, 2013 at 15:33 Comment(1)
It turns out that this was the answer but I had to fiddle with it for a while before I triggered something that caused it to start working. Thanks.Conidiophore
F
32

This process worked for me:

  1. go into [File -> Project Structure -> Modules -> [YourAppModule] -> Sources Tab]
  2. navigate to the "target/scala-[version]/src_managed" directory (panel on the right).
  3. Right click the "main" directory and flag it as a source folder.
  4. Right click "controllers" and "views" and remove the "sources" flag (you should see the controllers and views folder become package directories).
  5. Apply -> OK -> recompile.

from https://github.com/playframework/playframework/issues/1784#issuecomment-26345523

Filiano answered 30/1, 2014 at 11:19 Comment(1)
since play 2.3 you can do the same for "target/scala-<version>/twirl/main". just ensure that "main" is marked as the source-folder.Tortricid
F
8

The Play 2.0 support plugin is only available in the IntelliJ IDEA Ultimate edition.

enter image description here

Fanlight answered 5/6, 2013 at 15:33 Comment(1)
It turns out that this was the answer but I had to fiddle with it for a while before I triggered something that caused it to start working. Thanks.Conidiophore
D
8

I managed to make it work on IDEA Community 14 by solely cleaning IDEA cache (File -> Invalidate Caches / Restart)

Darrick answered 9/3, 2015 at 10:13 Comment(0)
P
8

Views / templates are not classes, which is why IDE cannot recognize them. IDE should be configured to look for the compiled version of the views. This click will fix your issue

enter image description here

Pantia answered 18/9, 2015 at 22:58 Comment(0)
C
5

The routes file and the views are compiled only when the project is first loaded in the browser. Till then, IntelliJ shows the error as it cannot find those classes. But the errors magically dissppear when you run the application and load it into browser.

You can verify this in the play console. After you do a play run , and load the url localhost:9000 in the browser, you can see compilation messages in the play console saying

...compiling 22 scala sources....

Checkroom answered 5/6, 2013 at 5:1 Comment(5)
I tried all kinds of variations on what you are suggesting but my IDE still fails to recognize the 'index' symbol.Conidiophore
I am not sure but could you try play idealize myAppCheckroom
IDEs seem to genereally have big issues with Play! projects. Try runing clean-all from the command line on the project and then compile it from there too. Eventually your IDE should get whats going on...until you do changes to the view files the next time, then DOOOOM.Jointly
You also need Scala plug-in, even if you run Java test projectMarimaria
Thank @Jakob. That really helped.. :)Lauds
F
1

Go to the Run configuration for your Play project and remove the 'Make' step from the "Before Launch" section. Then, IntelliJ would simply run the Play project without attempting to compile, and the views will get compiled automatically.

Until the views are compiled, IntelliJ doesn't know they exist, and can't make the project itself. Clearly this is a problem with IntelliJ's plugin, but since you're already relying on Play to do your compiling dynamically, this workaround doesn't hurt the workflow too much.

Fatherland answered 31/7, 2013 at 22:52 Comment(0)
D
1
  1. Open "Project" sidebar
  2. Go to target/scala-<version>/src_managed
  3. Right-click the src_managed, "Mark Directory As"/"Generated Source Root"
  4. Subdirectories of src_managed are from a strange reason marked as source roots. You can disable it.
  5. Enjoy.

Note that javascript routes seem to be still ignored from a strange reason, so this is not a perfect solution. Nevertheless, it is better than nothing.

Dereliction answered 10/12, 2013 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.