@@ -4,7 +4,7 @@ import java.io.OutputStreamWriter
44import java .nio .file .{Files , Path }
55
66import sbt .Keys .*
7- import sbt .librarymanagement . MavenRepository
7+ import sbt .librarymanagement as lm
88import sbt .util .CacheImplicits .*
99import sbt .util .{CacheStoreFactory , FileInfo , Level }
1010import sbt .{given , * }
@@ -102,7 +102,7 @@ object ScalafmtPlugin extends AutoPlugin {
102102 private type Output = String
103103 private type InitTask = Def .Initialize [Task [Unit ]]
104104
105- val globalInstance = Scalafmt .create(this .getClass.getClassLoader)
105+ private val globalInstance = Scalafmt .create(this .getClass.getClassLoader)
106106 .withRespectProjectFilters(true )
107107
108108 private object FilterMode {
@@ -133,6 +133,8 @@ object ScalafmtPlugin extends AutoPlugin {
133133 currentProject : ResolvedProject ,
134134 filterMode : String ,
135135 errorHandling : ErrorHandling ,
136+ dependencyResolution : lm.DependencyResolution ,
137+ updateConfiguration : lm.UpdateConfiguration ,
136138 ) {
137139 locally {
138140 def cmp [A : Ordering ](x : Iterable [A ], y : Iterable [A ]): Int = Ordering
@@ -162,8 +164,10 @@ object ScalafmtPlugin extends AutoPlugin {
162164 errorHandling,
163165 )
164166
165- private val scalafmtSession = {
166- val repositories = resolvers.collect { case r : MavenRepository => r.root }
167+ private def getFallbackInstance = {
168+ val repositories = resolvers.collect { case r : lm.MavenRepository =>
169+ r.root
170+ }
167171 val repoCredentials = credentials.flatMap { c =>
168172 Try (CredentialsUtils .toDirect(c)).toOption
169173 .map(dc => new RepositoryCredential (dc.host, dc.userName, dc.passwd))
@@ -175,10 +179,21 @@ object ScalafmtPlugin extends AutoPlugin {
175179 info.mkString(" Adding credentials [" , " ," , " ]" )
176180 }
177181
178- val scalafmtSession = globalInstance.withReporter(reporter)
179- .withMavenRepositories(repositories* )
182+ globalInstance.withMavenRepositories(repositories* )
180183 .withRepositoryCredentials(repoCredentials* )
181- .createSession(config.toAbsolutePath)
184+ }
185+
186+ private val scalafmtSession = {
187+ val factory = new ScalafmtSbtDependencyDownloader (
188+ taskStreams,
189+ dependencyResolution,
190+ updateConfiguration,
191+ )
192+
193+ val withFactory = globalInstance.withRepositoryPackageDownloader(factory)
194+ val scalafmtSession = {
195+ if (withFactory ne globalInstance) withFactory else getFallbackInstance
196+ }.withReporter(reporter).createSession(config.toAbsolutePath)
182197 if (scalafmtSession == null ) throw messageException(
183198 " failed to create formatting session. Please report bug to https://github.com/scalameta/sbt-scalafmt" ,
184199 )
@@ -473,6 +488,8 @@ object ScalafmtPlugin extends AutoPlugin {
473488 ! noThrow && scalafmtFailOnErrors.value,
474489 scalafmtDetailedError.value,
475490 ),
491+ dependencyResolution.value,
492+ updateConfiguration.value,
476493 )
477494 func(files, dirs, session)
478495 }
@@ -519,6 +536,8 @@ object ScalafmtPlugin extends AutoPlugin {
519536 scalafmtFailOnErrors.value,
520537 scalafmtDetailedError.value,
521538 ),
539+ dependencyResolution.value,
540+ updateConfiguration.value,
522541 ).formatSources(absFiles, Nil )
523542 },
524543 ))
0 commit comments