scalasbtsbt-pluginscalafmt

SBT Plugin Published Locally Fails with an Error When Included in a Project


I have implemented a simple sbt plugin which I published locally to my .ivy repository. I wanted to use this plugin on one of my project for which I added it in my plugins.sbt file. I then tried to run this from the console and it failed with the following error:

me@me-InfinityBook-S-14-v5:~/scala-projects/open-electrons/oscp-scala$ sbt scalafmt
[info] welcome to sbt 1.6.2 (Private Build Java 1.8.0_352)
[info] loading settings for project oscp-scala-build from plugins.sbt ...
[info] loading project definition from /home/me/scala-projects/open-electrons/oscp-scala/project
[info] loading settings for project oscpScala from build.sbt ...
[info] set current project to oscpScala (in build file:/home/me/scala-projects/open-electrons/oscp-scala/)
[warn] there's a key that's not used by any other settings/tasks:
[warn]  
[warn] * ThisBuild / publishMavenStyle
[warn]   +- /home/me/scala-projects/open-electrons/oscp-scala/build.sbt:13
[warn]  
[warn] note: a setting might still be used by a command; to exclude a key from this `lintUnused` check
[warn] either append it to `Global / excludeLintKeys` or call .withRank(KeyRanks.Invisible) on the key
[error] Not a valid command: scalafmt (similar: last)
[error] Not a valid project ID: scalafmt (similar: oscpScala)
[error] Expected ':'
[error] Not a valid key: scalafmt (similar: scalaHome, scalaArtifacts, scalaInstance)
[error] scalafmt
[error]  

   ^

As it can be seen that I'm trying to add a common scalafmt.conf file which I would publish as a plugin and include it from the individual projects scalafmt.conf file. To run this I just navigated to the project where I'm using this plugin and it failed. Here is what I have in my plugin code:

import sbt._
object MyScalafmtPlugin extends AutoPlugin {
  override def trigger = allRequirements
  override def requires = plugins.JvmPlugin
  override def buildSettings: Seq[Def.Setting[_]] = {
    SettingKey[Unit]("scalafmtGenerateConfig") :=
      IO.write(
        // writes to file once when build is loaded
        file(".scalafmt-common.conf"),
        ("version = 3.6.1\n" +
          "maxColumn = 120"
          ).stripMargin.getBytes("UTF-8")
      )
  }
}

And the build.sbt that builds this Plugin looks like this:

lazy val root = (project in file(".")).
  settings(
    name := "openelectrons-scalafmt-common-sbt-plugin",
    version := "0.0.1",
    organization := "com.openelectrons",
    scalaVersion := "2.12.17",
    sbtPlugin := true,
    sbtVersion := "1.6.2"
  )

Any clues as to what I'm doing wrong?

EDIT: I'm now facing this error:

[error] org.scalafmt.sbt.ScalafmtSbtReporter$ScalafmtSbtError: scalafmt: missing setting 'version'. To fix this problem, add the following line to .scalafmt.conf: 'version=3.3.0'. [/home/me/scala-projects/open-electrons/oscp-scala/.scalafmt.conf]
[error] org.scalafmt.sbt.ScalafmtSbtReporter$ScalafmtSbtError: scalafmt: missing setting 'version'. To fix this problem, add the following line to .scalafmt.conf: 'version=3.3.0'. [/home/me/scala-projects/open-electrons/oscp-scala/.scalafmt.conf]
[error] (oscp-messages / Compile / scalafmt) org.scalafmt.sbt.ScalafmtSbtReporter$ScalafmtSbtError: scalafmt: missing setting 'version'. To fix this problem, add the following line to .scalafmt.conf: 'version=3.3.0'. [/home/me/scala-projects/open-electrons/oscp-scala/.scalafmt.conf]
[error] (oscp-j-api / Compile / scalafmt) org.scalafmt.sbt.ScalafmtSbtReporter$ScalafmtSbtError: scalafmt: missing setting 'version'. To fix this problem, add the following line to .scalafmt.conf: 'version=3.3.0'. [/home/me/scala-projects/open-electrons/oscp-scala/.scalafmt.conf]
[error] Total time: 0 s, completed Dec 30, 2022 10:10:21 PM

Here is the generated .scalafmt-common.conf which I can see in my plugin:

version = 3.6.1
maxColumn = 120

Solution

  • Here is how I managed to get this fixed:

    I originally had the implementation located under project, but after moving it into src/main/scala/my/package ensured that when I built the plugin JAR, it was indeed packaged into the JAR file.

    I then had to add this plugin in the build.sbt of my project where I'm using it like:

    .enablePlugins(MyScalafmtPlugin)
    

    This made sure that the PlugIn was referenced and it used the common scalafmt.conf file and started to format all the .scala files using the base conf.