Skip to content

Commit 4a7f108

Browse files
authored
better workaround for the scala reflect bug (#1100)
* upgrade ammonite and cats * doesn't seem to fix anything but doesn't harm either * better workaround for the scala reflect bug it only seems to happen with ammonite scrits, so now we trigger scala reflect once, which is a workaround for the bug - it only happens the first time... * fmt
1 parent a2428f8 commit 4a7f108

4 files changed

Lines changed: 38 additions & 5 deletions

File tree

console/build.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ scalacOptions -= "-Xfatal-warnings"
4848
val ScoptVersion = "3.7.1"
4949
val BetterFilesVersion = "3.8.0"
5050
val CaskVersion = "0.7.8"
51-
val CatsVersion = "2.0.0"
51+
val CatsVersion = "2.3.1"
5252
val CirceVersion = "0.12.2"
53-
val AmmoniteVersion = "2.3.8-4-88785969"
53+
val AmmoniteVersion = "2.3.8-32-64308dc3"
5454
val ZeroturnaroundVersion = "1.13"
5555

5656
dependsOn(Projects.fuzzyc2cpg % Test)

console/src/main/scala/io/shiftleft/console/scripting/AmmoniteExecutor.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import java.nio.file.{Files, Path}
1717
* All scripts are compiled in-memory and no caching is performed.
1818
*/
1919
trait AmmoniteExecutor {
20+
ScalaReflectWorkaround.workaroundScalaReflectBugByTriggeringReflection()
21+
2022
protected def predef: String
2123

2224
protected lazy val ammoniteMain: Main = ammonite.Main(predefCode = predef,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.shiftleft.console.scripting
2+
3+
import scala.reflect.runtime.currentMirror
4+
import com.google.protobuf.DescriptorProtos.FileDescriptorProto
5+
6+
/** a workaround for a scala reflect bug, where the first invocation of scala reflect fails
7+
* idea: invoke this at the very start to trigger the bug. future invocations should be fine
8+
* i asked to move the retry logic into scala reflect as a better way to handle this
9+
* https://github.com/scala/bug/issues/12038#issuecomment-760629585
10+
*/
11+
object ScalaReflectWorkaround {
12+
var applied = false
13+
14+
def fromJava(t: FileDescriptorProto): Unit = ()
15+
16+
def workaroundScalaReflectBugByTriggeringReflection() = {
17+
if (!applied) {
18+
try {
19+
currentMirror
20+
.reflectModule(currentMirror.staticModule("io.shiftleft.console.scripting.ScalaReflectWorkaround$"))
21+
.instance
22+
} catch {
23+
case t: Throwable => // that's what we want to trigger - it happens the first time, then works - all good
24+
}
25+
applied = true
26+
}
27+
}
28+
}

console/src/test/scala/io/shiftleft/console/QueryDatabaseTests.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ object TestBundle extends QueryBundle {
1313
title = "a-title",
1414
description = s"a-description $n",
1515
score = 2.0,
16-
traversal = { cpg => cpg.method }
16+
traversal = { cpg =>
17+
cpg.method
18+
}
1719
)
1820
}
1921

@@ -42,8 +44,9 @@ class QueryDatabaseTests extends AnyWordSpec with should.Matchers {
4244
"an-author",
4345
"a-title",
4446
"a-description",
45-
2.0,
46-
{ cpg: Cpg => cpg.method }
47+
2.0, { cpg: Cpg =>
48+
cpg.method
49+
}
4750
)
4851
query.title shouldBe "a-title"
4952
query.traversalAsString shouldBe "cpg: Cpg => cpg.method"

0 commit comments

Comments
 (0)