Skip to content

chore: format samples from sbt - #871

Merged
octonato merged 3 commits into
mainfrom
octonato/run-prettier-formatter-from-sbt
Aug 11, 2025
Merged

octonato merged 3 commits into
mainfrom
octonato/run-prettier-formatter-from-sbt

Conversation

@octonato

@octonato octonato commented Aug 8, 2025

Copy link
Copy Markdown
Contributor

This will run the formatter whenever there are files to compile in the samples.

Comment thread project/SamplesCompilationProject.scala Outdated
formatIfNeeded := {
val srcs = (Compile / sources).value
val targetDir = (Compile / compile / streams).value.cacheDirectory
val markerFile = targetDir / "prettier-last-run"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want sbt to detect automatically what needs to be formatted or not, but couldn't make it work.

One drawback is that when compile a new checkout, it will always run a formatting task. It slows down the build a bit.

if (markerFile.exists) {
srcs.exists(src => src.lastModified() > markerFile.lastModified())
} else {
srcs.nonEmpty

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One drawback is that when compile a new checkout, it will always run a formatting task. It slows down the build a bit.

How about something like the following to check a git diff

Suggested change
srcs.nonEmpty
Process(Seq("git", "diff", "--name-only","."), baseDirectory.value).!!.linesIterator.map(_.trim).exists(_.nonEmpty)

Maybe even look at --staged

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting.

Using the diff can be interesting to decide when to create the markerFile for the first time. For example, you do a clean compile, if there is not diff, not worth running the formatter at all.

But let's assume you change a file. Now you have a diff and the formatter is applied. After that, you still have a diff, but you don't need to format anymore since last change was made by the formatter.

Maybe a combination of both will be better, but I want to give another try and see if I can have sbt doing this trick for me. Sbt does know when there are files to compile, it's just that I couldn't find yet how to get this info.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I was thinking on those terms, too. Mixing them both but I see your comment below now.

@octonato octonato left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@franciscolopezsancho, I changed it a little bit now by removing the creation of a marker file.

scalafmt and javafmt are also on first compilation, so I don't think it's a big deal if we do it for the prettier formatter.

Once formatted and compiled, it will only run again if any java file is changed. That's also how scalafmt and javafmt` works.

@octonato
octonato force-pushed the octonato/run-prettier-formatter-from-sbt branch from 9e556a0 to 8b2de43 Compare August 11, 2025 10:50

@franciscolopezsancho franciscolopezsancho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scalafmt and javafmt are also on first compilation, so I don't think it's a big deal if we do it for the prettier formatter.

LGTM

@octonato
octonato merged commit 9b815f7 into main Aug 11, 2025
@octonato
octonato deleted the octonato/run-prettier-formatter-from-sbt branch August 11, 2025 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants