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

StringIndexOutOfBoundsException thrown when trying to perform Substring.Replacement #3206

Closed
rtyley opened this issue Nov 8, 2023 · 4 comments · Fixed by #3207
Closed

StringIndexOutOfBoundsException thrown when trying to perform Substring.Replacement #3206

rtyley opened this issue Nov 8, 2023 · 4 comments · Fixed by #3207
Labels
bug Something isn't working cat:rewrite

Comments

@rtyley
Copy link
Contributor

rtyley commented Nov 8, 2023

This looks a little like #3016 maybe, but that fix was merged for Scala Steward 0.24.0, and this run is running with Scala Steward v0.26.0.

The full exception in the logs is:

  2023-11-08 16:01:30,316 ERROR Steward guardian/play-secret-rotation failed
  java.lang.Throwable: failed to edit /home/runner/scala-steward/workspace/repos/guardian/play-secret-rotation/build.sbt
  	at org.scalasteward.core.io.FileAlg$$anonfun$editFile$4.applyOrElse(FileAlg.scala:69)
  	at org.scalasteward.core.io.FileAlg$$anonfun$editFile$4.applyOrElse(FileAlg.scala:69)
  	at scala.PartialFunction$AndThen.applyOrElse(PartialFunction.scala:288)
  	at cats.ApplicativeError.$anonfun$recoverWith$1(ApplicativeError.scala:172)
  	at flatMap @ org.scalasteward.core.io.FileAlg.editFile(FileAlg.scala:68)
  	at adaptErr$extension @ org.scalasteward.core.io.process$.$anonfun$slurp$3(process.scala:68)
  	at flatTraverse @ org.scalasteward.core.update.artifact.ArtifactMigrationsLoader.loadAll(ArtifactMigrationsLoader.scala:40)
  	at flatTraverse @ org.scalasteward.core.update.artifact.ArtifactMigrationsLoader.loadAll(ArtifactMigrationsLoader.scala:40)
  	at flatTraverse @ org.scalasteward.core.update.artifact.ArtifactMigrationsLoader.loadAll(ArtifactMigrationsLoader.scala:40)
  	at flatMap @ org.scalasteward.core.edit.EditAlg.$anonfun$applyUpdateReplacements$3(EditAlg.scala:108)
  	at map @ org.scalasteward.core.edit.EditAlg.applyUpdateReplacements(EditAlg.scala:106)
  	at flatMap @ org.scalasteward.core.edit.EditAlg.applyUpdateReplacements(EditAlg.scala:106)
  	at flatMap @ org.scalasteward.core.edit.EditAlg.$anonfun$applyUpdate$7(EditAlg.scala:64)
  	at flatMap @ org.scalasteward.core.edit.EditAlg.$anonfun$applyUpdate$5(EditAlg.scala:61)
  	at flatMap @ org.scalasteward.core.edit.EditAlg.$anonfun$applyUpdate$4(EditAlg.scala:58)
  	at map @ org.scalasteward.core.edit.EditAlg.$anonfun$applyUpdate$1(EditAlg.scala:56)
  	at flatMap @ org.scalasteward.core.edit.EditAlg.$anonfun$applyUpdate$1(EditAlg.scala:56)
  	at flatMap @ fs2.Compiler$Target.flatMap(Compiler.scala:163)
  Caused by: java.lang.StringIndexOutOfBoundsException: begin 2326, end 2322, length 3903
  	at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)
  	at java.base/java.lang.String.substring(String.java:1874)
  	at org.scalasteward.core.edit.update.data.Substring$Replacement$.$anonfun$applyAll$3(Substring.scala:48)
  	at scala.collection.immutable.List.foreach(List.scala:333)
  	at org.scalasteward.core.edit.update.data.Substring$Replacement$.applyAll(Substring.scala:47)
  	at org.scalasteward.core.edit.EditAlg.$anonfun$applyUpdateReplacements$5(EditAlg.scala:109)
  	at org.scalasteward.core.io.FileAlg.$anonfun$editFile$3(FileAlg.scala:68)
  	at scala.Option.fold(Option.scala:263)
  	at org.scalasteward.core.io.FileAlg.$anonfun$editFile$1(FileAlg.scala:68)
  	... 14 common frames omitted
  2023-11-08 16:01:30,318 INFO  ──────────── Total time: Steward guardian/play-secret-rotation: 33s 573ms ────────────

Note that exception says the substring has begin 2326, end 2322 - which means that the requested end was 4 characters before the requested start.

This seems to be failing consistently since I merged guardian/play-secret-rotation#411 at 12:02pm today:

image

This is the current build.sbt file in that project:

https://github.com/guardian/play-secret-rotation/blob/07068e712ab5aa8a41cc6354c34b1eadd58dd206/build.sbt

I'll probably have to exclude this particular project from our Scala Steward run until this is fixed, otherwise we get some bad side-effects from consistent failure.

@fthomas
Copy link
Member

fthomas commented Nov 11, 2023

Thanks for the report, @rtyley!

I think the following test is a reproduction of this issue:

diff --git a/modules/core/src/test/scala/org/scalasteward/core/edit/RewriteTest.scala b/modules/core/src/test/scala/org/scalasteward/core/edit/RewriteTest.scala
index dc1f33d7..ad8d874c 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/edit/RewriteTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/edit/RewriteTest.scala
@@ -942,6 +942,15 @@ class RewriteTest extends FunSuite {
     runApplyUpdate(update, original, expected)
   }
 
+  // https://github.com/scala-steward-org/scala-steward/issues/3206
+  test("".only) {
+    val update = ("com.typesafe.play".g % "play".a % "2.9.0" %> "3.0.0").single
+      .copy(newerGroupId = Some("org.playframework".g), newerArtifactId = Some("play"))
+    val original = Map("build.sbt" -> """ "com.typesafe.play" %% "play" % "2.9.0" """)
+    val expected = Map("build.sbt" -> """ "org.playframework" %% "play" % "3.0.0" """)
+    runApplyUpdate(update, original, expected)
+  }
+
   private def runApplyUpdate(
       update: Update.Single,
       files: Map[String, String],

It looks suspicious that the newerArtifactId is the same as the old artifactId instead of None.

@fthomas
Copy link
Member

fthomas commented Nov 11, 2023

This should be fixed by #3207.

fthomas added a commit that referenced this issue Nov 13, 2023
* Test the root cause of #3206

* Reproduce #3206 as rewrite test

* Use correct start indices in ModulePositionScanner

`ModulePositionScanner` currently reports wrong substring positions if
an an artifactId is a proper substring of a groupId. With this change,
regex capturing groups are used to get the correct start indices of
substring positions of `ModulePositions`.

* Reformat test
@fthomas fthomas added bug Something isn't working cat:rewrite labels Nov 13, 2023
@rtyley
Copy link
Contributor Author

rtyley commented Nov 16, 2023

This should be fixed by #3207.

Thank you @fthomas ! I can see that this was released with v0.27.0, and running a Scala Steward job this morning with v0.27.0 and play-secret-rotation once again included in the run, I can confirm the job successfully processed the repo - great work, thank you!

@fthomas
Copy link
Member

fthomas commented Nov 16, 2023

Good to hear that it is working now and thanks for reporting back!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cat:rewrite
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants