Cache
Compiling and packaging are the slow parts of a build. Histos keeps their outputs so the next run can reuse them when the inputs have not changed. The store is a directory of blobs. A blob's name is the digest of its bytes, so two builds that produced the same bytes share one file, and a changed input cannot accidentally read the old output.
Each project remembers which blobs it still needs. histos cache clean removes entries
older than the retention that no project is keeping. histos cache path prints the
directory. histos cache list prints each digest and its size.
def put(digest: Digest, bytes: Chunk[Byte], at: Instant): IO[CacheError, Unit]val Retention: Duration = Duration.ofDays(30)What has to change before a module rebuilds
Histos keeps two digests, because "nothing changed" means two different things.
The API digest is the public signature: the names and types other modules compile against. Zinc computes it. A comment, or a change inside a method body, does not move the signature. A downstream module then reuses its previous compile. That is the skip you want when you fixed a typo in a comment.
The product digest is the class files as a later packager would see them. Line-number tables and the TASTY attribute are removed first, and the absolute path of your checkout is removed where it sits on a path boundary. Everything else stays. A change that would affect a published jar changes this digest. A change that would not, does not.
def of(analysis: sbt.internal.inc.Analysis): Sha256 =
digest(
analysis.apis.allInternalClasses.toList.sorted.map: name =>
s"$name ${analysis.apis.internalAPI(name).apiHash()}"
)def normalize(bytes: Array[Byte], checkout: Path): Either[ClassBytesError, Array[Byte]] =
val root = checkoutRoot(checkout)
rewrite(bytes): node =>
dropPositions(node)
dropPaths(node, root)
.flatMap(rewritten => rewritePool(rewritten, root))Sharing a cache with CI
A remote cache is optional. Set HISTOS_CACHE_URI to turn it on. http and https use
HTTP. grpc is plaintext and grpcs is TLS. Leave the variable unset and Histos stays
local. A value it does not understand fails the command. It does not quietly ignore the
variable.
HISTOS_CACHE_MODE is read unless you set write. A laptop that asks for write is
opened as read. Only CI stores results. That is so a developer's machine cannot publish
a bad output that every other machine then trusts.
HISTOS_CACHE_NAMESPACE separates those results. It is main unless you set pr/<n> for
a pull request or dev/<name> for a person. A pull request reads its own namespace
first, then main.
def decide(env: Map[String, String]): Either[RemoteError, Option[Selected]] =
blank(env.get(UriKey)) match
case None =>
for
_ <- modeOf(env)
_ <- namespaceOf(env)
yield None
case Some(uri) =>
for
target <- target(uri)
mode <- modeOf(env)
namespace <- namespaceOf(env)
yield Some(Selected(target, mode, namespace))/** `Off` is the process default. A configured cache is read-only until it is opened for write.
*/
enum RemoteModeWhere a lookup looks
When an action can be cached, Histos looks in this order: the namespace you named, then
main, then the local store. The first hit is used. If nothing hits, the action runs.
histos cache promote --from <namespace> --to <namespace> copies an HTTP index from one
namespace to another, which is how a merged pull request becomes the main cache.
/** Copy one HTTP index onto another. gRPC has no action-cache list, so promote names that limit and does not open a
* second port. Unset stays off.
*/
def promote(env: Map[String, String], from: CacheNamespace, to: CacheNamespace): ZIO[Scope, RemoteError, Unit] =
ZIO
.fromEither(decide(env))
.flatMap:
case None => ZIO.fail(RemoteError.Off)
case Some(Selected(Target.Grpc(_, _, _), _, _)) => ZIO.fail(RemoteError.HttpOnly("cache promote"))
case Some(Selected(Target.Http(url), _, _)) =>
client.flatMap(http => HttpCache.copy(url, from, to).provideEnvironment(ZEnvironment(http)))