简体   繁体   中英

IntelliJ Idea can't resolve symbol that inside Scala managed sources (sbt-buildinfo)

I think it could be a problem that appeared in new versions, and can't be resolved as old versions, I got a Play Java project with sbt-buildinfo plugin the problem is really common is that IntelliJ keep saying it can't resolve that symbol

InjettiJ 无法解析符号

And here is the file:

生成的文件

Some solutions can be found IntelliJ Idea sbt managed source file and here sbt-buildinfo generated object cannot be referenced but none of the solutions provided were helpful for me:

InjelliJ 设置

The really strange thing is if I "Unmark the directory as a generated source" and then move that BuildInfo.scala file to the package folder manually to buildpkg (my custom package) instead of sbt-buildinfo package, IntelliJ see that file, but this change is useless because the file will be generated again and all changes will be discarded:

哇!

Here is my build.sbt :

lazy val root = (project in file(".")).enablePlugins(PlayJava, BuildInfoPlugin)
  .settings(
    buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion, buildInfoBuildNumber,
      "hostname" -> java.net.InetAddress.getLocalHost.getHostName,
      "gitHash" -> sys.env.getOrElse("BITBUCKET_COMMIT", "No BITBUCKET_COMMIT set")
    ),
    buildInfoPackage := "buildpkg",
    buildInfoOptions += BuildInfoOption.ToJson,
    buildInfoOptions += BuildInfoOption.BuildTime
  )

scalaVersion := "2.12.3"

and my plugins.sbt :

// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.5")

addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.7.0")

So what I'm missing, what the solution for this

  1. In the sbt settings in IntelliJ, enable the settings "Use sbt shell for build and import", since the sources are generated only when sbt runs the build.

  2. IntelliJ will currently only correctly update generated sources if they are in the correct package directory. For sbt-buildinfo, add the setting

    buildInfoUsePackageAsPath := true

    to your build.sbt .

This issue will also occur if the SBT based project is not "imported" in the first place.

To try to fix this issue:

  1. Close the IntelliJ project in question.
  2. Select Import Project
  3. In the Import Project dialog box, select "Import project from external model"
  4. Select "sbt". Note that "Use sbt shell for build..." is not required to be checked. If you do not click this, you can still take advantage of autoload. Be sure Library sources are checked.

Press Finish and let it build what it needs to now load the plugins and such for the first time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM