Skip to content

Commit

Permalink
Log the args diff when re-running the smithy4s codegen (#1559)
Browse files Browse the repository at this point in the history
This PR introduces a change in Smithy4s sbt codegen plugin that would allow users to see what caused the codegen to re-execute. It is done by replacing `sbt.util.Tracked.inputChanged` with a similar implementation that keeps the entire codegen args serialized in cache, and when codegen needs to be rerun, plugin will print the diff of the values to debug log. The diff is calculated using [munit-diff](https://scalameta.org/munit/blog/2024/05/22/release-1.0.0.html#diff-module-extracted-to-a-separate-module).

When running `sbt` with `set logLevel := Level.Debug`, user will notice a diff like this when codegen needs to rerun:
  • Loading branch information
majk-p committed Jun 10, 2024
1 parent 19acfb2 commit 72a9503
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ lazy val codegenPlugin = (projectMatrix in file("modules/codegen-plugin"))
Compile / unmanagedSources / excludeFilter := { f =>
Glob("**/sbt-test/**").matches(f.toPath)
},
libraryDependencies += Dependencies.MunitV1.diff.value,
publishLocal := {
// make sure that core and codegen are published before the
// plugin is published
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# check if smithy4sCodegen works
> set logLevel := Level.Debug
> compile
$ exists target/scala-2.13/src_managed/main/smithy4s/smithy4s/example/ObjectService.scala
$ exists target/scala-2.13/resource_managed/main/smithy4s.example.ObjectService.json
Expand Down
83 changes: 83 additions & 0 deletions modules/codegen-plugin/src/smithy4s/codegen/CachedTask.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2021-2024 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package smithy4s.codegen

import munit.diff.Diff
import sbt._
import sbt.util.CacheImplicits._
import sbt.util.CacheStore
import sbt.util.Logger
import sjsonnew._
import sjsonnew.support.murmurhash.Hasher
import sjsonnew.support.scalajson.unsafe.Converter
import sjsonnew.support.scalajson.unsafe.{PrettyPrinter => prettify}

import scala.util.Try

private[codegen] object CachedTask {

// This implementation is inspired by sbt.util.Tracked.inputChanged
// The main difference is that when the values don't match, the difference is calculated
// using munit-diff and recorded to debug log
def inputChanged[I: JsonFormat, O](store: CacheStore, logger: Logger)(
f: (Boolean, I) => O
): I => O = { in =>
def debug(str: String): Unit = logger.debug(s"[smithy4s] $str")

val previousValue = Try(store.read[ValueAndHash[I]]()).toOption
val newValueHash = hash(in)
store.write[ValueAndHash[I]]((in, newValueHash))

previousValue match {
case None =>
debug("Could not read previous inputs value from cache.")
f(true, in)

case Some((oldValue, previousHash)) =>
(toJson(oldValue), toJson(in)) match {
case (Some(oldArgs), Some(newArgs)) if !oldArgs.equals(newArgs) =>
val diff = new Diff(prettify(oldArgs), prettify(newArgs))
val report = diff.createReport(
"Arguments changed between smithy4s codegen invocations, diff:",
printObtainedAsStripMargin = false
)
debug(report)
f(true, in)

case (_, _) if (previousHash != newValueHash) =>
debug(
"Codegen arguments didn't change, but their hashes didn't match. " +
"This means file change on paths provided as codegen arguments."
)
f(true, in)

case _ =>
debug("Input didn't change between codegen invocations")
f(false, in)
}

}
}

private type ValueAndHash[I] = (I, Int)

private def toJson[I: JsonFormat](args: I) =
Converter.toJson(args).toOption

private def hash[I: JsonFormat](in: I) =
Hasher.hash(in).toOption.getOrElse(-1)
}
Original file line number Diff line number Diff line change
Expand Up @@ -451,21 +451,24 @@ object Smithy4sCodegenPlugin extends AutoPlugin {
)

val cached =
Tracked.inputChanged[CodegenArgs, Seq[File]](
s.cacheStoreFactory.make("input")
CachedTask.inputChanged[CodegenArgs, Seq[File]](
s.cacheStoreFactory.make("input"),
s.log
) {
Function.untupled {
Tracked.lastOutput[(Boolean, CodegenArgs), Seq[File]](
s.cacheStoreFactory.make("output")
) { case ((inputChanged, args), outputs) =>
if (inputChanged || outputs.isEmpty) {
s.log.debug("Regenerating managed sources")
s.log.debug(s"[smithy4s] Input changed: $inputChanged")
s.log.debug(s"[smithy4s] Outputs empty: ${outputs.isEmpty}")
s.log.debug("[smithy4s] Sources will be regenerated")
val resPaths = smithy4s.codegen.Codegen
.generateToDisk(args)
.toList
resPaths.map(path => new File(path.toString))
} else {
s.log.debug("Using cached version of outputs")
s.log.debug("[smithy4s] Using cached version of outputs")
outputs.getOrElse(Seq.empty)
}
}
Expand All @@ -474,4 +477,5 @@ object Smithy4sCodegenPlugin extends AutoPlugin {

cached(codegenArgs)
}

}
6 changes: 5 additions & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,18 @@ object Dependencies {
)
}

class MunitCross(munitVersion: String) {
class MunitCross(val munitVersion: String) {
val core: Def.Initialize[ModuleID] =
Def.setting("org.scalameta" %%% "munit" % munitVersion)
val scalacheck: Def.Initialize[ModuleID] =
Def.setting("org.scalameta" %%% "munit-scalacheck" % munitVersion)
}
object Munit extends MunitCross("0.7.29")
object MunitMilestone extends MunitCross("1.0.0-M6")
object MunitV1 extends MunitCross("1.0.0") {
val diff: Def.Initialize[ModuleID] =
Def.setting("org.scalameta" %%% "munit-diff" % munitVersion)
}

val Scalacheck = new {
val scalacheckVersion = "1.16.0"
Expand Down

0 comments on commit 72a9503

Please sign in to comment.