Skip to content

Commit

Permalink
Fix cache invalidation caused by unscoped lookup (#192)
Browse files Browse the repository at this point in the history
It seems that keys are not scoped when looked up from `Def.Initialize[]` referenced within `Def.taskDyn()`, resulting in `streams.value.cacheStoreFactory` defaulting to a directory shared across configurations.

Within a task referenced in a `taskDyn`, the enclosing scope is lost (not propagated), so we fall back to the global cache directory no matter on which config/task its evaluated.
bjaglin authored Dec 23, 2021
1 parent 2b98961 commit 912ba57
Showing 2 changed files with 11 additions and 13 deletions.
20 changes: 7 additions & 13 deletions plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala
Original file line number Diff line number Diff line change
@@ -138,6 +138,7 @@ object ScalafmtPlugin extends AutoPlugin {
private class FormatSession(
config: Path,
taskStreams: TaskStreams,
cacheStoreFactory: CacheStoreFactory,
resolvers: Seq[Resolver],
currentProject: ResolvedProject,
filterMode: String,
@@ -239,10 +240,7 @@ object ScalafmtPlugin extends AutoPlugin {
res
}

def formatTrackedSources(
cacheStoreFactory: CacheStoreFactory,
sources: Seq[File]
): Unit = {
def formatTrackedSources(sources: Seq[File]): Unit = {
val filteredSources = filterFiles(sources)
trackSourcesAndConfig(cacheStoreFactory, filteredSources) {
(outDiff, configChanged, prev) =>
@@ -272,10 +270,7 @@ object ScalafmtPlugin extends AutoPlugin {
if (cnt > 0) log.info(s"Reformatted $cnt Scala sources")
}

def checkTrackedSources(
cacheStoreFactory: CacheStoreFactory,
sources: Seq[File]
): ScalafmtAnalysis = {
def checkTrackedSources(sources: Seq[File]): ScalafmtAnalysis = {
val filteredSources = filterFiles(sources)
trackSourcesAndConfig(cacheStoreFactory, filteredSources) {
(outDiff, configChanged, prev) =>
@@ -393,15 +388,12 @@ object ScalafmtPlugin extends AutoPlugin {

private def scalafmtTask(sources: Seq[File], session: FormatSession) =
Def.task {
session.formatTrackedSources(streams.value.cacheStoreFactory, sources)
session.formatTrackedSources(sources)
} tag (ScalafmtTagPack: _*)

private def scalafmtCheckTask(sources: Seq[File], session: FormatSession) =
Def.task {
val analysis = session.checkTrackedSources(
(scalafmt / streams).value.cacheStoreFactory,
sources
)
val analysis = session.checkTrackedSources(sources)
throwOnFailure(analysis)
} tag (ScalafmtTagPack: _*)

@@ -451,6 +443,7 @@ object ScalafmtPlugin extends AutoPlugin {
val session = new FormatSession(
config,
streams.value,
(scalafmt / streams).value.cacheStoreFactory,
fullResolvers.value,
thisProject.value,
scalafmtFilter.value,
@@ -496,6 +489,7 @@ object ScalafmtPlugin extends AutoPlugin {
new FormatSession(
scalaConfig.value,
streams.value,
(scalafmt / streams).value.cacheStoreFactory,
fullResolvers.value,
thisProject.value,
"",
4 changes: 4 additions & 0 deletions plugin/src/sbt-test/scalafmt-sbt/sbt/test
Original file line number Diff line number Diff line change
@@ -142,6 +142,10 @@ $ delete p17/src/main/scala/Test2.scala
$ copy-file changes/good.scala p17/src/main/scala/Test2.scala
> p17/scalafmtCheck
> p17/scalafmt
######## formatting other config should not invalidate the cache
$ copy-file changes/bad.scala p17/src/test/scala/Test3.scala
> p17/test:scalafmt
> p17/scalafmtCheck

# set up git
$ exec git init -b main p18

0 comments on commit 912ba57

Please sign in to comment.