From ccd13bafdb3a2e90fd98815b4f856af2f2e75f75 Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Wed, 27 Dec 2023 16:19:34 +0100 Subject: [PATCH] bugfix: Fix StacktraceAnalyser in case of empty package --- .../internal/metals/StacktraceAnalyzer.scala | 7 ++- .../feature/CrossAnalyzeStacktraceSuite.scala | 45 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/metals/src/main/scala/scala/meta/internal/metals/StacktraceAnalyzer.scala b/metals/src/main/scala/scala/meta/internal/metals/StacktraceAnalyzer.scala index 1d267f96855..3337498fad8 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/StacktraceAnalyzer.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/StacktraceAnalyzer.scala @@ -210,7 +210,7 @@ object StacktraceAnalyzer { /* Symbol containing `$package$` is a toplevel method and we only need to * find any method contained in the same file even if overloaded */ - if (symbol.contains("$package$")) { + val symbolToFind = if (symbol.contains("$package$")) { symbolIn.split("\\$package\\$") match { case Array(filePath, symbol) => val re = filePath.replace('.', '/') + "$package" + symbol @@ -232,6 +232,11 @@ object StacktraceAnalyzer { } else { List(symbol :+ '#') } + symbolToFind.map { + // empty package needs to be added if no package is present + case sym if !sym.contains("/") => "_empty_/" + sym + case sym => sym + } } } diff --git a/tests/slow/src/test/scala/tests/feature/CrossAnalyzeStacktraceSuite.scala b/tests/slow/src/test/scala/tests/feature/CrossAnalyzeStacktraceSuite.scala index 3ecee08fa84..0461ecdef99 100644 --- a/tests/slow/src/test/scala/tests/feature/CrossAnalyzeStacktraceSuite.scala +++ b/tests/slow/src/test/scala/tests/feature/CrossAnalyzeStacktraceSuite.scala @@ -29,4 +29,49 @@ class CrossAnalyzeStacktraceSuite scalaVersion = V.scala3, ) + check( + "no-package", + """| + |def fetch() = + | println("Fetching!") + |<<1>> throw new Exception("") + | + |@main + |def main() = + |<<2>> fetch() + | + |def fetch(a: Int) = a + 2 + |""".stripMargin, + """|Exception in thread "main" java.lang.Exception: + |at other$package$.fetch(other.scala:4) + |at other$package$.main(other.scala:8) + |at main.main(other.scala:6) + |""".stripMargin, + filename = "other.scala", + scalaVersion = V.scala3, + ) + + check( + "no-package-type", + """| + | + |object O: + | def fetch() = + | println("Fetching!") + |<<1>> throw new Exception("") + | + |@main + |def main() = + |<<2>> fetch() + | + |def fetch(a: Int) = a + 2 + |""".stripMargin, + """|Exception in thread "main" java.lang.Exception: + |at O$.fetch(Main.scala:6) + |at other$package$.main(other.scala:10) + |at main.main(other.scala:8) + |""".stripMargin, + filename = "other.scala", + scalaVersion = V.scala3, + ) }