3

I have a set of Scala projects and for all of those projects, I would like to introduce some scala source code formatting for which purpose, I'm using the scamafmt sbt pliugin. I have compiled the config file and this config file is in a separate project repo. I would now like to reuse this in all of the other Scala projects. I see two possibilities:

  1. Use the repo where the conf file is located as a git submodule in all the other 10 projects where I want to run the scala formatter

  2. Do not do anything, just add a README documentation that every user who is working on the codebase should download the scalafmt conf file to the project (I will pre add a .gitignore to all projects to ignore the local conf file)

Is there any other approach? I definitely do not want the conf file to diverge if I leave it as is in all the projects.

joesan
  • 13,963
  • 27
  • 95
  • 232
  • Note that you probably don't want option 2 anyway because it means you can't assert on the formatting in your CI. – Gaël J Dec 30 '22 at 18:33
  • So you mean option 1 is a good idea? – joesan Dec 30 '22 at 18:37
  • With Option 1, what if out of the 10 projects, 5 of them change the formatting? The rest of the 5 should pull the latest changes. Correct? Is using git submodules a good practice? – joesan Dec 30 '22 at 18:37
  • 1
    I'm always against git submodules. Don't think it's a good option either – Gaël J Dec 30 '22 at 18:40

1 Answers1

2

As per the documentation, one option is to build (and publish in your org) a SBT plugin with your configuration:

https://scalameta.org/scalafmt/docs/installation.html#share-configuration-between-builds

To share configuration across different sbt builds, create a custom sbt plugin that generates .scalafmt-common.conf on build reload, then include the generated file from .scalafmt.conf

// project/MyScalafmtPlugin.scala
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"),
        "maxColumn = 100".stripMargin.getBytes("UTF-8")
      )
  }
}
// .scalafmt.conf
include ".scalafmt-common.conf"
Gaël J
  • 11,274
  • 4
  • 17
  • 32
  • I'm now stuck with calling this plugin. I have it published locally, but now sure how to get it triggered. I have raised a new question here: https://stackoverflow.com/questions/74965732/sbt-plugin-published-locally-fails-with-an-error-when-included-in-a-project – joesan Dec 30 '22 at 20:54