Intellij IDEA can not resolve symbol with Play framework
Asked Answered
H

8

33

I am using IDEA 13 with Play 2.2.1 and was doing the examples on the play site.

public static Result index() {
   return redirect(routes.Application.tasks());
}

IDEA says "cannot resolve method redirect(?)" and red underlines routes.Application.tasks()

I have done "compile" and then "idea" from the play console.

Haematosis answered 5/2, 2014 at 12:39 Comment(1)
Could you share your imports and controller cobject first line...Infer
D
24

I had a similar problem when testing my controller. I solved it by "File" > "Open Project" and choose the base folder of the Play framework project (delete all idea settings file from the folder before so that it will reimport using the SBT settings).

The last version of the SBT support for IDEA did 90% of the work. Then F4 to enter module settings dialog. Set up your project dependencies like in the picture below (the bright red folder have been set by IDEA and do not exist yet in my project so do not worry if they do in yours). It is a matter of including managed classes and generated code in the code that idea will use and index.

enter image description here

now I can use code like this from within IDEA and debug it no problem.

Result result = Helpers.callAction(controllers.routes.ref.CrudController.createEntity(CrudEntities.contact.name()),
        new FakeRequest().withJsonBody(paramJson)
);

your way of doing it should work as well.

Distraint answered 6/2, 2014 at 7:5 Comment(5)
Thank you with all of my cold dark heart. the reactivemongo import simply wouldn't work for me in intellij IDEA until I basically hacked the project. Deleted the .idea dir and then re-opened it in intellij. (backed up first though). This has literally sapped days off my life. Thank you.Chalcography
@Chalcography "My sister/brother from another mother." I feel you and I have been through this too. On. Every. IDEA. Update.Distraint
I hate this software... it has now completely forgotten that it used to know what import play.api.Play is supposed to do... it's like it's got an issue with me personally... thing is when it works its really nice... it's like a girlfriend who's really fun until she has tequila...Chalcography
OK when something weird like that happen I check that the SBT is valid (if you can enter SBT console from that project it means it is valid) update dependencies (because idea stops reloading if even one is not resolvable) and then I drop the nuclear solution on it aka File > Invalidate Cache and RestartDistraint
I am having Intellij 15, Play 2.4.6 framework. I am also getting this same error of cannot resolve symbol routes. How to get resolved with this ? Need help.Drawshave
D
13

Well I am using "IntelliJ Idea 13" and doing "sbt" Playframework project with "Activator". After everything tried, I finally solved this issue with following steps in terminal/cmd at my existing root sbt project

  1. enter this command in terminal/cmd : sbt clean
  2. then delete these files and folders : ".idea", "all _.iml files" , "all target folders reside in project folder(this may not be necessary but I did it.)"
  3. enter this command in terminal/cmd: sbt idea
  4. now in IntelliJ : open project

Note: If you are building the Playframework project with "Play" command instead of "Activator" instead of sbt idea command you can try play idea or play "idea with-sources=yes"

Hope this will help you.

Dian answered 5/8, 2014 at 18:11 Comment(2)
This worked for me, thanks! After re-opening the project, IDEA prompted me that it was generated using an older project format and said I should convert it, which I did. It then prompted me to use the SBT importer, which I did. And voila! it works.Folia
It results with an error "Not a valid command". What is the problem? sbt idea >.\sbt-dist\bin\sbt.bat idea Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0 [info] Loading project definition from D:\CODE\Web Development\play-java\project [info] Set current project to play-java-intro (in build file:/D:/CODE/Web%20Development/play-java/) [error] Not a valid command: idea (similar: eval, alias) [error] Not a valid key: idea (similar: clean) [error] idea [error] ^Accede
C
6

These steps work for me: add following lines to your plugins.sbt file:

resolvers += "sbt-idea-repo" at "http://mpeltonen.github.com/maven/"
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.2.0")
but if you don't know the correct version of sbt-idea then use this instead:
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "latest.integration")

Warning - It will take too much time to build as it need to parse all the version and get the latest version.

Run this command -

sbt gen-idea
Cushat answered 4/12, 2015 at 9:35 Comment(0)
E
6

I could solve the problem by marking the generated code directories as the "Sources" folder. Go to File -> Project Structure -> "root" module -> Sources.

Intellij Project Structure with Module Settings

Eucken answered 10/12, 2015 at 7:39 Comment(1)
To be more explicit, remove target/scala-2.11/routes/main from the Excluded Folders and add it to the Source Folders.Marathi
F
4

I had to add the below plugin entry to my plugins.sbt (under projects folder) for intellij IDEA to resolve play variables like routes etc.

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")

Note: After adding the above plugin to plugins.sbt, run the below commands to rebuild idea and resolve the issue:

  • activator clean
  • activator idea
Floweret answered 22/11, 2014 at 8:5 Comment(1)
>activator idea is no longer "a valid key" (OSX) (as of Play 2.4).Doyen
L
1

Install the scala plugin from the marketplace. Then open the build.sbt : File>open>$yourBuild.sbt

IDEA version: 2022.1.2

Livelong answered 2/2, 2023 at 16:39 Comment(0)
R
0

In my case running the sbt command from a user that doesn't have permission for the project you're entering sbt in. Clean up and then change folder permissions with chown -R yourUser /path/to/project and it should fix it. After that ./sbt compile

Rhiamon answered 19/5, 2016 at 15:25 Comment(0)
S
0

I had a similar problem - IDEA could not resolve Play! framework dependencies.

In my case there was a problem with .idea folder. I copied project to separate folder and run "activator idea" command in the copied project root folder. Then I compared .idea folders in original project and copied project - there I found a difference! In copied project in folder .idea/libraries there were lots of XML files containing links to dependencies, so I copied them to the same folder to original project, re-indexed project and everything works perfect from that moment :) I did it like this because I didnt want to loose my project settings (deleting .idea folder and run command "activator idea" in original project folder should resolve the problem, but settings for project would be lost).

Hope this helps somehow :).

Salcedo answered 4/12, 2017 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.