Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #2906: Misleading warning when two deps reference the same val version #2907

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,44 @@ final class EditAlg[F[_]](implicit
update: Update.Single,
preCommit: F[Unit] = F.unit
): F[List[EditAttempt]] =
findUpdateReplacements(data.repo, data.config, update).flatMap {
case Nil => logger.warn(s"Unable to bump version for update ${update.show}").as(Nil)
case updateReplacements =>
for {
_ <- preCommit
(preMigrations, postMigrations) = scalafixMigrationsFinder.findMigrations(update)
preScalafixEdits <- runScalafixMigrations(data.repo, data.config, preMigrations)
// PreUpdate migrations could invalidate previously found replacements,
// so we find them again if the migrations produced any changes.
freshReplacements <-
if (preScalafixEdits.flatMap(_.commits).isEmpty) F.pure(updateReplacements)
else findUpdateReplacements(data.repo, data.config, update)
updateEdit <- applyUpdateReplacements(data, update, freshReplacements)
postScalafixEdits <- runScalafixMigrations(data.repo, data.config, postMigrations)
hooksEdits <- hookExecutor.execPostUpdateHooks(data, update)
} yield preScalafixEdits ++ updateEdit ++ postScalafixEdits ++ hooksEdits
{
findUpdateReplacements(data.repo, data.config, update).flatMap {
case Nil =>
logger.warn(s"Unable to bump version for update ${update.show}").as(Nil)
case updateReplacements =>
for {
_ <- preCommit
(preMigrations, postMigrations) = scalafixMigrationsFinder.findMigrations(update)
preScalafixEdits <- runScalafixMigrations(data.repo, data.config, preMigrations)
// PreUpdate migrations could invalidate previously found replacements,
// so we find them again if the migrations produced any changes.
freshReplacements <-
if (preScalafixEdits.flatMap(_.commits).isEmpty) F.pure(updateReplacements)
else findUpdateReplacements(data.repo, data.config, update)
updateEdit <- applyUpdateReplacements(data, update, freshReplacements)
postScalafixEdits <- runScalafixMigrations(data.repo, data.config, postMigrations)
hooksEdits <- hookExecutor.execPostUpdateHooks(data, update)
} yield {
preScalafixEdits ++ updateEdit ++ postScalafixEdits ++ hooksEdits
}
}
}

private def findUpdateReplacements(
repo: Repo,
config: RepoConfig,
update: Update.Single
): F[List[Substring.Replacement]] =
for {
versionPositions <- scannerAlg.findVersionPositions(repo, config, update.currentVersion)
modulePositions <- scannerAlg.findModulePositions(repo, config, update.dependencies)
} yield Selector.select(update, versionPositions, modulePositions)
{
println("findUpdateReplacements")
println(s" update = $update")
for {
versionPositions <- scannerAlg.findVersionPositions(repo, config, update.currentVersion)
_ = println(s" versionPositions = ${versionPositions}")
modulePositions <- scannerAlg.findModulePositions(repo, config, update.dependencies)
_ = println(s" modulePositions = ${modulePositions}")
} yield Selector.select(update, versionPositions, modulePositions)
}

private def runScalafixMigrations(
repo: Repo,
Expand All @@ -103,17 +114,23 @@ final class EditAlg[F[_]](implicit
update: Update.Single,
updateReplacements: List[Substring.Replacement]
): F[Option[EditAttempt]] =
for {
repoDir <- workspaceAlg.repoDir(data.repo)
replacementsByPath = updateReplacements.groupBy(_.position.path).toList
_ <- replacementsByPath.traverse { case (path, replacements) =>
fileAlg.editFile(repoDir / path, Substring.Replacement.applyAll(replacements))
}
_ <- reformatChangedFiles(data)
msgTemplate = data.config.commits.messageOrDefault
commitMsg = CommitMsg.replaceVariables(msgTemplate)(update, data.repo.branch)
maybeCommit <- gitAlg.commitAllIfDirty(data.repo, commitMsg)
} yield maybeCommit.map(UpdateEdit(update, _))
{
println("applyUpdateReplacements")
println(s" data = ${data}")
println(s" update = ${update}")
println(s" updateReplacements = ${updateReplacements}")
for {
repoDir <- workspaceAlg.repoDir(data.repo)
replacementsByPath = updateReplacements.groupBy(_.position.path).toList
_ <- replacementsByPath.traverse { case (path, replacements) =>
fileAlg.editFile(repoDir / path, Substring.Replacement.applyAll(replacements))
}
_ <- reformatChangedFiles(data)
msgTemplate = data.config.commits.messageOrDefault
commitMsg = CommitMsg.replaceVariables(msgTemplate)(update, data.repo.branch)
maybeCommit <- gitAlg.commitAllIfDirty(data.repo, commitMsg)
} yield maybeCommit.map(UpdateEdit(update, _))
}

private def reformatChangedFiles(data: RepoData): F[Unit] = {
val reformat =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ final class NurtureAlg[F[_]](config: VCSCfg)(implicit
_ <- logger.info(s"Process update ${data.update.show}")
head = vcs.listingBranch(config.tpe, data.fork, data.updateBranch)
pullRequests <- vcsApiAlg.listPullRequests(data.repo, head, data.baseBranch)
.map(_.take(0)) // Debug
result <- pullRequests.headOption match {
case Some(pr) if pr.state.isClosed && data.update.isInstanceOf[Update.Single] =>
logger.info(s"PR ${pr.html_url} is closed") >>
Expand Down Expand Up @@ -161,6 +162,8 @@ final class NurtureAlg[F[_]](config: VCSCfg)(implicit
gitAlg.returnToCurrentBranch(data.repo) {
val createBranch = logger.info(s"Create branch ${data.updateBranch.name}") >>
gitAlg.createBranch(data.repo, data.updateBranch)
println("applyNewUpdate")
println(s" data.update = ${data.update}")
data.update
.on(
update = editAlg.applyUpdate(data.repoData, _, createBranch),
Expand Down Expand Up @@ -295,6 +298,8 @@ final class NurtureAlg[F[_]](config: VCSCfg)(implicit
)
maybeRevertCommit <- gitAlg.revertChanges(data.repo, data.baseBranch)
maybeMergeCommit <- gitAlg.mergeTheirs(data.repo, data.baseBranch)
_ = println("mergeAndApplyAgain")
_ = println(s" data.update = ${data.update}")
edits <- data.update.on(
update = editAlg.applyUpdate(data.repoData, _),
grouped = _.updates.flatTraverse(editAlg.applyUpdate(data.repoData, _))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.scalasteward.core.edit

import cats.data.NonEmptyList
import cats.effect.unsafe.implicits.global
import munit.FunSuite
import org.scalasteward.core.TestInstances.dummyRepoCache
import org.scalasteward.core.TestSyntax._
import org.scalasteward.core.data.{RepoData, Update}
import org.scalasteward.core.data.Update.ForArtifactId
import org.scalasteward.core.data.{ArtifactId, CrossDependency, Dependency, GroupId, RepoData, Update, Version}
import org.scalasteward.core.mock.MockContext.context._
import org.scalasteward.core.mock.MockState
import org.scalasteward.core.mock.MockState.TraceEntry
import org.scalasteward.core.repoconfig.RepoConfig
import org.scalasteward.core.scalafmt.scalafmtConfName
import org.scalasteward.core.util.Nel
Expand Down Expand Up @@ -911,11 +914,86 @@ class RewriteTest extends FunSuite {
runApplyUpdate(update, original, expected)
}

test("issue 2906") {
val original = Map(
"build.sbt" ->
"""
|val FooLibraryVersion = "1.2.3"
|libraryDependencies ++= Seq(
| "com.foo" %% "library-1" % FooLibraryVersion,
| "com.foo" %% "library-2" % FooLibraryVersion
|)
|""".stripMargin
)
val expected = Map(
"build.sbt" ->
"""
|val FooLibraryVersion = "1.3.0"
|libraryDependencies ++= Seq(
| "com.foo" %% "library-1" % FooLibraryVersion,
| "com.foo" %% "library-2" % FooLibraryVersion
|)
|""".stripMargin
)

val update = ForArtifactId(
crossDependency = CrossDependency(
dependencies =
NonEmptyList(Dependency(GroupId("com.foo"), ArtifactId("library-1"), Version("1.2.3")), List.empty)
),
newerVersions = NonEmptyList(Version("1.3.0"), List.empty))

val trace = runApplyUpdate(update, original, expected)
trace.foreach(println)
}

// test("issue ...") {
// val update1: Update.ForArtifactId = ("com.foo".g % "library-1".a % "1.2.3" %> "1.3.0").single
// val update2: Update.ForArtifactId = ("com.foo".g % "library-2".a % "1.2.3" %> "1.3.0").single
// val update3 = Update.ForGroupId(NonEmptyList(update1, List(update2)))
// val original = Map(
// "build.sbt" ->
// """
// |val FooLibraryVersion = "1.2.3"
// |libraryDependencies ++= Seq(
// | "com.foo" %% "library-1" % FooLibraryVersion,
// | "com.foo" %% "library-2" % FooLibraryVersion
// |)
// |""".stripMargin
// )
// val expected = Map(
// "build.sbt" ->
// """
// |val FooLibraryVersion = "1.3.0"
// |libraryDependencies ++= Seq(
// | "com.foo" %% "library-1" % FooLibraryVersion,
// | "com.foo" %% "library-2" % FooLibraryVersion
// |)
// |""".stripMargin
// )
//
// println("update1")
// runApplyUpdate(update1, original, expected)
// require(
// !terribleMutableLogBufferDoNotMergeThis.exists(_.startsWith("Unable to bump version"))
// )
//
// // The second update prints a misleading warning.
// println("update2")
// runApplyUpdate(update2, expected, expected)
// require(
// terribleMutableLogBufferDoNotMergeThis.last == "Unable to bump version for update com.foo:library-2 : 1.2.3 -> 1.3.0"
// )
//
// println("update3")
// runApplyUpdate(update3, original, expected)
// }

private def runApplyUpdate(
update: Update.Single,
files: Map[String, String],
expected: Map[String, String]
): Unit = {
): Vector[TraceEntry] = {
val repo = Repo("edit-alg", s"runApplyUpdate-${nextInt()}")
val data = RepoData(repo, dummyRepoCache, RepoConfig.empty)
val repoDir = workspaceAlg.repoDir(repo).unsafeRunSync()
Expand All @@ -927,6 +1005,7 @@ class RewriteTest extends FunSuite {
val obtained = state.files
.map { case (file, content) => file.toString.replace(repoDir.toString + "/", "") -> content }
assertEquals(obtained, expected)
state.trace
}

private var counter = 0
Expand Down