Repository navigation
Add recipe to upgrade Gatling 3.x to 3.16 - #1259
Conversation
1ec0696 to
398a531
Compare
Upgrade Gatling simulations written in Scala, Java or Kotlin from 3.0 onward
to 3.16: bump the Gatling dependencies and the Maven and Gradle plugins,
replace the `${}` Expression Language with `#{}`, and migrate the DSL methods
removed along the way.
Scala sources are not reliably type attributed against the Gatling Scala DSL,
so Scala methods are matched by name in sources importing Gatling, skipping
names the source declares itself.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
398a531 to
540cd77
Compare
The DSL is matched by name, so `Map("a" -> 1).transform((k, v) => v + 1)` in a
simulation was renamed to `transformWithSession`, which does not compile. The
Scala standard library is type attributed even where Gatling is not, so skip
calls whose method type is declared in `scala.collection`.
There was a problem hiding this comment.
Thanks @sullis! Nice to see Scala, Java and Kotlin all covered, and the notes on where the upgrade guides overstate the Java scope helped a lot. I've pushed a few commits myself, so there's nothing left for you to change. For any next PRs:
-
Merged
main, which also triggered a fresh CGP run. The earlier failure wasn't caused by this PR: CGP's Maven cache answered 503 for therecipe-libraryplugin. -
Applied
OpenRewriteRecipeBestPractices(f0299ac):reduce(..)instead ofvisit(..)with a captured set indeclaredNames, explicit imports, and oneclasspathFromResourcesentry per line. -
MigrateGatlingScalaDslrenamedMap("a" -> 1).transform((k, v) => v + 1)in a simulation totransformWithSession, which doesn't compile (5756bce). This is now covered by the type guard in item 8. -
Trimmed the
UpgradeToGatling3_16description to its overall effect plus what it leaves alone (dc99d5f). The included recipes already describe themselves, and a composite description tends to go stale as entries get added. -
ReplaceGatlingDollarExpressionLanguagealso rewrote${}placeholders meant for something else, as inSystem.getProperty("baseUrl", "${BASE_URL}")(748a4d8). It now skips a string when every call it passes through, up to the enclosing statement, is typed and declared outsideio.gatling. Strings that reach Gatling through other calls, such asheaders(Map.of("X-Token", "${token}")), are still rewritten, as are constants and untyped Scala DSL calls. -
ElFileBodytemplates use the same Expression Language, so their${}also stops working in 3.11. The composite description now says those files are left alone (a206578); rewriting them could be a follow-up. -
The Scala recipes now also cover
.scscripts, as used to run simulations with scala-cli (3948b26).ScalaParserparses those too, but the precondition only matched.scala. -
With openrewrite/rewrite#9914, the Scala parser can read the Gatling type tables. The Scala tests now parse against Gatling 3.9, like the Java and Kotlin ones (451f6d3). The DSL renames still match by name, because the declaring types moved between Gatling versions:
WhiteListis aCoreDslmethod in 3.9 but a case class in 3.0. They now skip anything typed outsideio.gatling, such as the source's own declarations or the Scala library. That one guard replaces both the scan for names the source declares and thescala.collectionskip. -
openrewrite/rewrite#9915 now parses a typed call without parentheses, such as
5.seconds, as a method invocation.ReplaceScalaPostfixDurationsproduces that same shape, and matches typed units throughDurationConversions(21e2c17). Renamed calls also get renamed method types now, and the test callsignoreDefaultCheckson a request, where Gatling defines it. Code that stays untyped still gets matched by name, because the parser keeps modelling it the old way. -
The recipes found Gatling sources by matching import package names, because Scala imports used to be untyped. Since openrewrite/rewrite#9983 they are typed, so
UsesType<>("io.gatling..*", false)works for Scala, Java and Kotlin, andGatlingSourcesis gone (84e1716). The Scala-only recipes need no file filter either (ad22129): they can't change Java or Kotlin code, whichkeepJavaSdkTransformchecks. One difference: unlike the old import check,UsesTypeskips a Java or Kotlin source whose only Gatling imports are unresolved wildcards, so such a source is now left alone. -
A fully qualified
io.gatling.javaapi.core.CoreDsl.exitBlockOnFail(chain)becameCoreDsl.exitBlockOnFail().on(chain), without an import ofCoreDsl, so it didn't compile (e5e67cb). The recipe now keeps the call's own qualifier, andfullyQualifiedStaticMethodcovers it. -
Removed the
recipeListcomments ingatling.yml(cc3882e). They repeated what each recipe's own description says, and comments there are best kept for where ordering matters.
openrewrite/rewrite#9914 and #9915 have both landed, and the 33 Gatling tests pass against today's rewrite snapshot.
A simulation may hold `${}` placeholders meant for something other than Gatling,
as in `System.getProperty("baseUrl", "${BASE_URL}")`. Skip a string when every
call it is passed through up to the enclosing statement is typed and declared
outside `io.gatling`. Strings reaching Gatling through other calls, such as
`headers(Map.of("X-Token", "${token}"))`, and constants are still rewritten, as
are untyped calls, which Gatling's Scala DSL calls are.
`ScalaParser` also parses `.sc` scripts, as used to run Gatling simulations with scala-cli, but `GatlingSources.isScala` only matched `.scala` files.
|
I'd noticed that we had to work around some missing type information here that I think is best addressed upstream first: We'll see what we can do to improve the situation there, and then come back here to strip out the string based matching. Thanks for bringing this to our attention! |
# Conflicts: # build.gradle.kts # src/main/resources/META-INF/rewrite/classpath.tsv.gz
With openrewrite/rewrite#9914, type table stubs of Scala libraries resolve in the Scala parser, so the Scala tests now parse against the Gatling 3.9 type tables, like the Java and Kotlin tests already did. Typed, the source's own declarations and the Scala library are recognised by their types, so the `declaredNames` scan and the `scala.collection` special case give way to one guard: rename only what is untyped or attributed to `io.gatling`. Methods are still matched by name: their declaring types moved between Gatling versions, so `WhiteList` is a `CoreDsl` method in 3.9 but a case class in 3.0, and calls without parentheses carry no method type yet (openrewrite/rewrite#9913). The three no-change Scala tests fail until openrewrite/rewrite#9914 is in a snapshot, as without it the Gatling import leaves the whole source untyped.
|
One final PR to land before we can retry here: |
… types in sync openrewrite/rewrite#9915 parses a typed call without parentheses, such as `5.seconds`, as a `J.MethodInvocation` whose arguments carry `OmitParentheses`. `ReplaceScalaPostfixDurations` now produces that instead of a `J.FieldAccess`, and matches typed units by `DurationConversions`, falling back to their names where the enclosing Gatling call leaves them untyped. The field access branch of `MigrateGatlingScalaDsl` stays for untyped sources, which are still parsed that way. As Scala calls into Gatling are now typed, the renamed calls get renamed method types, and `maxConnectionsPerHost` takes the `int` it is passed. `ignoreDefaultChecks` and `ignoreDefaultHeaders` are request builder methods, so the test now calls them on a request rather than on the protocol.
The precondition matched imports by package name because Scala imports used to be untyped. Since openrewrite/rewrite#9983 they are typed like Java, and `UsesType` also counts unresolved single-type imports, so it covers Scala, Java and Kotlin alike. The Scala recipes narrow it to `.scala` and `.sc` files with `FindSourceFiles`. Unlike the import check, `UsesType` skips a Java or Kotlin source whose only Gatling imports are unresolved wildcards, so such a source is now left alone.
`UsesType` alone suffices. Outside Scala, postfix durations never carry the `InfixNotation` marker, and the only DSL names the Java SDK shares are `stopInjector`, which gets the same rename there, and the single-parameter `transform`, which `transformWithSession` never matches.
A fully qualified `io.gatling.javaapi.core.CoreDsl.exitBlockOnFail(chain)` became `CoreDsl.exitBlockOnFail().on(chain)` without an import of `CoreDsl`, which does not compile. Reuse the call's own qualifier instead.
A qualified static call's select is typed with its class, just like an
instance receiver, so both go through the same `#{any(..)}` parameter instead of
printing the qualifier into the template code.
…l/logHttp options Found by running the recipe on real Gatling projects and compiling against 3.16.1: - FeederBuilder.Batchable was removed after 3.11, so it becomes FileBased - the io.gatling.gradle plugin dropped logLevel and logHttp in 3.11, which breaks build evaluation - document the plugin ordering and Engine/Recorder launcher changes that are left alone
timtebeek
left a comment
There was a problem hiding this comment.
Thanks a lot again! Took a little while longer after spotting the upstream improvements to be made for Scala, but that's now in a better shape as well. I've also applied these at scale against OSS repositories, which led to the last recipe added.
Hope these recipes help you there!
What's changed
Adds
org.openrewrite.java.testing.gatling.UpgradeToGatling3_16. It upgrades Gatling simulations written in Scala, Java or Kotlin from 3.0 onward to 3.16, following the official upgrade guides.io.gatling.highcharts:gatling-charts-highchartsandio.gatling:gatling-*→ 3.16.x,gatling-maven-plugin→ 4.x,io.gatling.gradle→ 3.16.x.ReplaceGatlingDollarExpressionLanguage(Java, Kotlin, Scala): rewrites${…}→#{…}. Gatling deprecated${}in 3.7 and stopped interpreting it in 3.11. Kotlin's escaped"\${id}"becomes"#{id}". Scala interpolated strings and Kotlin string templates are left alone. Only sources importingio.gatlingare touched, so other${…}placeholders (Spring, Maven filtering) are safe.MigrateGatlingScalaDsl(Scala):heavisideUsers→stressPeakUsersWhiteList/BlackList→AllowList/DenyListpolling→pollignoreDefaultChecks/ignoreDefaultHeaders→ignoreProtocolChecks/ignoreProtocolHeadersstopInjector(If)→stopLoadGenerator(If)maxConnectionsPerHostLikeXXX→maxConnectionsPerHost(n), with counts taken from the Gatling bytecodeconvert→transformtransform/transformOptiontaking theSession→…WithSession(3.4)ReplaceScalaPostfixDurations(Scala):5 seconds→5.secondsfor thescala.concurrent.durationunits, as recommended by the 3.5 upgrade guide. Postfix notation needs-language:postfixOpsand is dropped in Scala 3. Only argument-less infix calls with a duration unit name are rewritten, soa max borx plus yare left alone.MigrateGatlingExitBlockOnFail(Java SDK, so Java and Kotlin):exitBlockOnFail(chain)→exitBlockOnFail().on(chain)(3.10). Handles both the static and the instance form.MigrateGatlingChoice(Java SDK):Choice.withKey/withWeight→onCase(..).then(..)/percent(..).then(..)(3.11).ChangeMethodNamefor the Java SDK'sstopInjector/stopInjectorIf(3.12).Notes for reviewers
rewrite-scaladoes attribute types, but they are misplaced: for example,applywrappers carry the enclosing call's type. Type-based matching would be fragile.MigrateGatlingScalaDslandReplaceScalaPostfixDurationstherefore only apply in.scalasources importingio.gatling, and the DSL renames skip names the source declares itself. The shared precondition lives inGatlingSources. The main code does not depend onrewrite-scala; it is added as a test dependency only.MigrateGatlingExitBlockOnFail,MigrateGatlingChoiceand thestopInjectorrenames use method matchers againstio.gatling.javaapi.core, so they also cover Kotlin sources.polling,WhiteList/BlackList,maxConnectionsPerHostLikeXXXandignoreDefaultChecksas Java/Scala. The jars show the Java SDK (since 3.7) never had them, so those renames are Scala-only.gatling-core/gatling-core-java3.16.0 for the templates. The Java SDK's builders are bounded by the Scala ones, so both are needed.gatling-core-javaandgatling-http-java3.9.5, plus the Scalagatling-coreandgatling-httpjars they are bounded by. 3.9.5 is the last version withexitBlockOnFail(ChainBuilder).scala-maven-pluginfor Maven Scala projects (plugin 4.x no longer compiles Scala), andjavax.jms→jakarta.jms(3.14, vendor dependent).com.github.lkishalmi.gatlingGradle plugin. Only the officialio.gatling.gradleplugin is upgraded.Testing
main.Related work
https://github.com/sullis/sullis/blob/main/hacktoberfest-2026-10-31.md
🤖 Generated with Claude Code