3

SBT scalariform plugin is formatting files more than once in multi project setup. Here is example https://github.com/Seetaramayya/sbt-scalariform-example

if you compile the project you can see something like this in the console

[info] Formatting 5 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "root")(compile) ...
[info] Formatting 7 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "module1")(compile) ...
[info] Formatting 7 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "module2")(compile) ...
[info] Formatting 7 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "main")(compile) ...

There are 5 scala files and 2 sbt files exist in the code base. Neither in SBT nor in scalariform documentation I could not find how to execute the task only once in multi module setup.

I would like to execute the task only once. Only option I see is enable plugin at root project level and disable at sub-projects but I did not like the option (I need to add disablePlugins in 15 sub-projects)

1 Answers1

1

The issue seems to be that

Each subproject's scalariformFormat task in their different scopes have the project root in their sourceDirectories setting. This just leads to the whole project being crawled for scala sources and formatted as many times as there are subprojects.

and has been addressed via scalariformWithBaseDirectory setting

val scalariformWithBaseDirectory = settingKey[Boolean]("Whether or not to format sources in project root (default: false)")

Thus setting withBaseDirectory=false in .scalariform.conf should give output

[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "main")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module1")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module2")(compile) ...

Note, however, this will not format *.scala sources in the root project/ directory. To address that we could try keeping withBaseDirectory=false in .conf but switch it on for the root project like so

lazy val root = (project in file("."))
  .aggregate(module1, module2, main)
  .settings(scalariformWithBaseDirectory := true)

which outputs

[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module2")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module1")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "main")(compile) ...
[info] Formatting 5 Scala sources ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "root")(compile) ...

where we still get duplication but not as much as initially.

Mario Galic
  • 47,285
  • 6
  • 56
  • 98