diff --git a/compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala b/compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala index 30a437e9b800..29026bd4d175 100644 --- a/compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala +++ b/compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala @@ -12,6 +12,7 @@ import Names.Name import StdNames.nme import util.Spans.Span import util.{SourceFile, SourcePosition} +import scala.jdk.CollectionConverters._ import collection.mutable import java.nio.file.Paths @@ -594,8 +595,15 @@ object ExtractSemanticDB: def write(source: SourceFile, occurrences: List[SymbolOccurrence], symbolInfos: List[SymbolInformation])(using Context): Unit = def absolutePath(path: Path): Path = path.toAbsolutePath.normalize + def commonPrefix[T](z: T)(i1: Iterable[T], i2: Iterable[T])(app: (T, T) => T): T = + (i1 lazyZip i2).takeWhile(p => p(0) == p(1)).map(_(0)).foldLeft(z)(app) val sourcePath = absolutePath(source.file.jpath) - val sourceRoot = absolutePath(Paths.get(ctx.settings.sourceroot.value)) + val sourceRoot = + // Here if `sourceRoot` and `sourcePath` do not share a common prefix then `relPath` will not be normalised, + // containing ../.. etc, which is problematic when appending to `/META-INF/semanticdb/` and will not be accepted + // by Files.createDirectories on JDK 11. + val sourceRoot0 = absolutePath(Paths.get(ctx.settings.sourceroot.value)) + commonPrefix(sourcePath.getRoot)(sourcePath.asScala, sourceRoot0.asScala)(_ resolve _) val semanticdbTarget = val semanticdbTargetSetting = ctx.settings.semanticdbTarget.value absolutePath( diff --git a/compiler/test/dotty/tools/dotc/core/tasty/CommentPicklingTest.scala b/compiler/test/dotty/tools/dotc/core/tasty/CommentPicklingTest.scala index 3120f0071133..a14c4d1aa4ee 100644 --- a/compiler/test/dotty/tools/dotc/core/tasty/CommentPicklingTest.scala +++ b/compiler/test/dotty/tools/dotc/core/tasty/CommentPicklingTest.scala @@ -82,7 +82,7 @@ class CommentPicklingTest { Directory.inTempDirectory { tmp => val sourceFiles = sources.zipWithIndex.map { case (src, id) => - val path = tmp./(File("Src$id.scala")).toAbsolute + val path = tmp./(File(s"Src$id.scala")).toAbsolute path.writeAll(src) path.toString }