Search code examples
scalacommand-linesbtscalafmt

How to invoke plugin from command line (sbt beginner)?


Following https://scalameta.org/scalafmt/docs/installation.html I'm trying to reformat my Scala code via command line.

How to execute the scalafmt command ?

Loading sbt from the project can I invoke scalafmt ?

Using below returns error :

sbt:skeleton> scalafmt
[error] Not a valid command: scalafmt (similar: last)
[error] Not a valid project ID: scalafmt
[error] Expected ':'
[error] Not a valid key: scalafmt (similar: scalaHome, scalaArtifacts, scalaInstance)
[error] scalafmt
[error]         ^
sbt:skeleton> my-project/scalafmt
[error] Expected ':'
[error] Not a valid key: scalafmt (similar: scalaHome, scalaArtifacts, scalaInstance)
[error] my-project/scalafmt
[error]                    ^
sbt:skeleton>

Solution

  • First I would suggest checking your project/plugins.sbt if it includes

    addSbtPlugin("org.scalameta" % "sbt-scalafmt" % scalaMetaVersion) 
    

    looks like it can't find scalafmt command.

    Second run sbt plugins to check if it's enabled - you should have something like this: org.scalafmt.sbt.ScalafmtPlugin: enabled in skeleton