From c0e3a18cb7be123c2dc1c8ab86db51cb90629f09 Mon Sep 17 00:00:00 2001 From: Alex Archambault Date: Mon, 20 Jan 2025 12:38:50 +0100 Subject: [PATCH 1/4] Ignore non-standard repositories --- .../com/goyeau/mill/scalafix/CoursierUtils.scala | 13 ++++++++----- .../com/goyeau/mill/scalafix/ScalafixCache.scala | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/mill-scalafix/src/com/goyeau/mill/scalafix/CoursierUtils.scala b/mill-scalafix/src/com/goyeau/mill/scalafix/CoursierUtils.scala index 2a4c703..1641907 100644 --- a/mill-scalafix/src/com/goyeau/mill/scalafix/CoursierUtils.scala +++ b/mill-scalafix/src/com/goyeau/mill/scalafix/CoursierUtils.scala @@ -7,22 +7,25 @@ import coursier.core.Authentication import mill.scalalib.{CrossVersion, Dep} object CoursierUtils { - def toApiRepository(repo: Repository): coursierapi.Repository = + def toApiRepository(repo: Repository): Option[coursierapi.Repository] = repo match { case mvn: MavenRepository => val credentialsOpt = mvn.authentication.map(toApiCredentials) - coursierapi.MavenRepository + val apiRepo = coursierapi.MavenRepository .of(mvn.root) .withCredentials(credentialsOpt.orNull) + Some(apiRepo) case ivy: IvyRepository => val credentialsOpt = ivy.authentication.map(toApiCredentials) val mdPatternOpt = ivy.metadataPatternOpt.map(_.string) - coursierapi.IvyRepository + val apiRepo = coursierapi.IvyRepository .of(ivy.pattern.string) .withMetadataPattern(mdPatternOpt.orNull) .withCredentials(credentialsOpt.orNull) - case other => - throw new Exception(s"Unrecognized repository: " + other) + Some(apiRepo) + case _ => + // non-standard repository, ignoring it + None } def toApiCredentials(auth: Authentication): coursierapi.Credentials = diff --git a/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala b/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala index 58aeebc..62d4980 100644 --- a/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala +++ b/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala @@ -21,7 +21,7 @@ private[scalafix] object ScalafixCache { private val scalafixArgumentsCache = new Cache[(String, Seq[Repository], Agg[Dep]), ScalafixArguments](createFunction = { case (scalaVersion, repositories, scalafixIvyDeps) => - val repos = repositories.map(CoursierUtils.toApiRepository).asJava + val repos = repositories.flatMap(CoursierUtils.toApiRepository).asJava val deps = scalafixIvyDeps.map(CoursierUtils.toCoordinates).iterator.toSeq.asJava scalafixCache .getOrElseCreate((scalaVersion, repos)) From 734cf49fd18b64391d4c96cbc2a548b60f150785 Mon Sep 17 00:00:00 2001 From: Alex Archambault Date: Mon, 20 Jan 2025 13:14:44 +0100 Subject: [PATCH 2/4] Revert "Ignore non-standard repositories" This reverts commit c0e3a18cb7be123c2dc1c8ab86db51cb90629f09. --- .../com/goyeau/mill/scalafix/CoursierUtils.scala | 13 +++++-------- .../com/goyeau/mill/scalafix/ScalafixCache.scala | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/mill-scalafix/src/com/goyeau/mill/scalafix/CoursierUtils.scala b/mill-scalafix/src/com/goyeau/mill/scalafix/CoursierUtils.scala index 1641907..2a4c703 100644 --- a/mill-scalafix/src/com/goyeau/mill/scalafix/CoursierUtils.scala +++ b/mill-scalafix/src/com/goyeau/mill/scalafix/CoursierUtils.scala @@ -7,25 +7,22 @@ import coursier.core.Authentication import mill.scalalib.{CrossVersion, Dep} object CoursierUtils { - def toApiRepository(repo: Repository): Option[coursierapi.Repository] = + def toApiRepository(repo: Repository): coursierapi.Repository = repo match { case mvn: MavenRepository => val credentialsOpt = mvn.authentication.map(toApiCredentials) - val apiRepo = coursierapi.MavenRepository + coursierapi.MavenRepository .of(mvn.root) .withCredentials(credentialsOpt.orNull) - Some(apiRepo) case ivy: IvyRepository => val credentialsOpt = ivy.authentication.map(toApiCredentials) val mdPatternOpt = ivy.metadataPatternOpt.map(_.string) - val apiRepo = coursierapi.IvyRepository + coursierapi.IvyRepository .of(ivy.pattern.string) .withMetadataPattern(mdPatternOpt.orNull) .withCredentials(credentialsOpt.orNull) - Some(apiRepo) - case _ => - // non-standard repository, ignoring it - None + case other => + throw new Exception(s"Unrecognized repository: " + other) } def toApiCredentials(auth: Authentication): coursierapi.Credentials = diff --git a/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala b/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala index 62d4980..58aeebc 100644 --- a/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala +++ b/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala @@ -21,7 +21,7 @@ private[scalafix] object ScalafixCache { private val scalafixArgumentsCache = new Cache[(String, Seq[Repository], Agg[Dep]), ScalafixArguments](createFunction = { case (scalaVersion, repositories, scalafixIvyDeps) => - val repos = repositories.flatMap(CoursierUtils.toApiRepository).asJava + val repos = repositories.map(CoursierUtils.toApiRepository).asJava val deps = scalafixIvyDeps.map(CoursierUtils.toCoordinates).iterator.toSeq.asJava scalafixCache .getOrElseCreate((scalaVersion, repos)) From 67c0c3cf32695af2c850d003523d0514e721fad9 Mon Sep 17 00:00:00 2001 From: Alex Archambault Date: Mon, 20 Jan 2025 13:13:38 +0100 Subject: [PATCH 3/4] Add ScalafixModule#scalafixRepositories Allowing users to filter out repositories for scalafix --- .../goyeau/mill/scalafix/ScalafixModule.scala | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixModule.scala b/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixModule.scala index db2a6c2..8454ec9 100644 --- a/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixModule.scala +++ b/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixModule.scala @@ -2,7 +2,7 @@ package com.goyeau.mill.scalafix import com.goyeau.mill.scalafix.ScalafixModule.{filesToFix, fixAction} import coursier.Repository -import mill.{Agg, T} +import mill.{Agg, T, Task} import mill.api.{Logger, PathRef, Result} import mill.scalalib.{Dep, ScalaModule} import mill.define.Command @@ -15,6 +15,23 @@ import scala.jdk.CollectionConverters.* trait ScalafixModule extends ScalaModule { def scalafixConfig: T[Option[os.Path]] = T(None) def scalafixIvyDeps: T[Agg[Dep]] = Agg.empty[Dep] + + /** Override this to filter out repositories that don't need to be passed to scalafix + * + * Repositories passed to scalafix need to be converted to the coursier-interface API (coursierapi.*). This can be an + * issue for non-Maven or Ivy repositories. Overriding this task and filtering those repositories out allows to work + * around that. + */ + def scalafixRepositories: Task[Seq[Repository]] = Task.Anon { + repositoriesTask().filter { + case repo if repo.getClass.getName == "mill.scalalib.JavaModule$InternalRepo" => + // Change to this when bumping to Mill > 0.12.5: + // case _: mill.scalalib.JavaModule.InternalRepo => + // no need to pass Mill's internal repository to scalafix + false + case _ => true + } + } @deprecated("Scalafix now follows scalaVersion", since = "0.4.2") def scalafixScalaBinaryVersion: T[String] = "2.12" @@ -24,7 +41,7 @@ trait ScalafixModule extends ScalaModule { T.command { fixAction( T.ctx().log, - repositoriesTask(), + scalafixRepositories(), filesToFix(sources()).map(_.path), classpath = (compileClasspath() ++ localClasspath() ++ Seq(semanticDbData())).iterator.toSeq.map(_.path), scalaVersion(), From a82f8971211ea9edc9c11eabbabd0eb95b22020d Mon Sep 17 00:00:00 2001 From: Alex Archambault Date: Tue, 21 Jan 2025 01:12:07 +0100 Subject: [PATCH 4/4] fixup --- .../src/com/goyeau/mill/scalafix/ScalafixModule.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixModule.scala b/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixModule.scala index 8454ec9..a04fe28 100644 --- a/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixModule.scala +++ b/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixModule.scala @@ -25,13 +25,14 @@ trait ScalafixModule extends ScalaModule { def scalafixRepositories: Task[Seq[Repository]] = Task.Anon { repositoriesTask().filter { case repo if repo.getClass.getName == "mill.scalalib.JavaModule$InternalRepo" => - // Change to this when bumping to Mill > 0.12.5: + // Change to this when bumping to Mill 0.13.x: // case _: mill.scalalib.JavaModule.InternalRepo => // no need to pass Mill's internal repository to scalafix false case _ => true } } + @deprecated("Scalafix now follows scalaVersion", since = "0.4.2") def scalafixScalaBinaryVersion: T[String] = "2.12"