Switching sbt Sub-projects easily with sbt-project-switcher

When I went to Scala Days 2019, I met Shunsuke Tadokoro and he saw me his cool and useful sbt plugin called sbt-project-switcher.

It is an sbt plugin to help jump from one sub-project to another easily. It’s really useful if a project has many sub-projects. When I first saw it, I knew that it would be really useful as we used sbt sub-projects a lot at work.

We also use Git submodules and switching to submodule project is annoying. Here is an example.

Let’s say you have a project structured like this.

├── build.sbt
├── cli
├── core
├── data
├── web
└── submodule
   ├── just-fp
   └── scala-hedgehog
      ├── core
      ├── example
      ├── runner
      ├── sbt-test
      └── test

Except build.sbt, everything mentioned above is a directory. Each directory is a sub-project. The submodule directory contains other projects and each submodule project may or may not have other sub-projects as you can see.

The names above are the actual directory names but sbt project names might be different. This is the structure with the project names.

├── build.sbt
├── cli                - tpsCli
├── core               - tpsCore
├── data               - tpsData
├── web                - tpsWeb
└── submodule
   ├── just-fp         - justFp
   └── scala-hedgehog  - hedgehog
      ├── core         - core
      ├── example      - example
      ├── runner       - runner
      ├── sbt-test     - sbt-test
      └── test         - test

move to a sub-project is easy.

e.g.) To move to the cli project named tpsCli, just run project tpsCli

sbt:test-project-switcher> project tpsCli
[info] Set current project to tps-cli (in build file:/path/to/test-project-switcher/)
sbt:tps-cli>

For submodule projects, it’s different. By the way, if you’re interested in using Git submodules for source build instead of having libraries in binary, better check this out.

Moving to submodule project requires to use a canonical path. To get the path, run projects command first then project {file:/the/submodule/path} .

e.g.)

sbt:test-project-switcher> projects
[info] In file:/path/to/test-project-switcher/
[info] 	 * root
[info] 	   tpsCli
[info] 	   tpsCore
[info] 	   tpsData
[info] 	   tpsWeb
[info] In file:/path/to/test-project-switcher/submodule/just-fp/
[info] 	   justFp
[info] In file:/path/to/test-project-switcher/submodule/scala-hedgehog/
[info] 	   core
[info] 	   example
[info] 	   hedgehog
[info] 	   runner
[info] 	   sbt-test
[info] 	   test
sbt:test-project-switcher> project {file:/path/to/test-project-switcher/submodule/just-fp/}
[info] Set current project to just-fp (in build file:/path/to/test-project-switcher/submodule/just-fp/)
sbt:just-fp>

If the submodule project has sub-projects, one more step’s involved. It would be

  • projects
  • project {file:/the/submodule/path}
  • project subProjectName

e.g.)

sbt:test-project-switcher> projects
[info] In file:/path/to/test-project-switcher/
[info] 	 * root
[info] 	   tpsCli
[info] 	   tpsCore
[info] 	   tpsData
[info] 	   tpsWeb
[info] In file:/path/to/test-project-switcher/submodule/just-fp/
[info] 	   justFp
[info] In file:/path/to/test-project-switcher/submodule/scala-hedgehog/
[info] 	   core
[info] 	   example
[info] 	   hedgehog
[info] 	   runner
[info] 	   sbt-test
[info] 	   test
sbt:test-project-switcher> project {file:/path/to/test-project-switcher/submodule/scala-hedgehog/}
[info] Set current project to hedgehog (in build file:/path/to/test-project-switcher/submodule/scala-hedgehog/)
sbt:hedgehog> project runner
[info] Set current project to hedgehog-runner (in build file:/path/to/test-project-switcher/submodule/scala-hedgehog/)
sbt:hedgehog-runner>

So I was excited about sbt-project-switcher as it can make project switching quite easy. However, unfortunately, I found it not supporting submodule projects so I created an issue ticket and fixed it. Shunsuke merged the PR so now I can use it for submodule projects as well. Thanks to Shunsuke!

With sbt-project-switcher to switch to a submodule sub-project, it requires to run just pjs.

sbt:test-project-switcher> pjs 

Then select the sub-project you want.

  hedgehog
  example
  core
  tpsCore
  tpsCli
  justFp
  sbt-test
> runner
  tpsData
  tpsWeb
  root
  test
  12/12
>
[info] Set current project to hedgehog (in build file:/path/to/test-project-switcher/submodule/scala-hedgehog/)
[info] Set current project to hedgehog-runner (in build file:/path/to/test-project-switcher/submodule/scala-hedgehog/)
sbt:hedgehog-runner>

Voila!

Comments