JetBrains/bazel-bsp

Source roots approximation is incorrect

lukaszwawrzyk opened this issue · 0 comments

I had a root directory in my workspace called java_hello. WORKSPACE_ROOT/java_hello/src/main/java/com/hello/Hello.java.
The heuristics for detecting source roots found java in java_hello and inferred root to be WORKSPACE_ROOT/java - a directory which doesn't exist. Which resolved in package prefix ..java_hello.src.main.java.com.hello instead of expected com.hello.
In fastpass we've been using following logic, which seemed to work fine:

  private val sourceRootPattern = FileSystems.getDefault.getPathMatcher(
    "glob:**/{main,test,tests,src,3rdparty,3rd_party,thirdparty,third_party}/{resources,scala,java,jvm,proto,python,protobuf,py}"
  )
  private val defaultTestRootPattern = FileSystems.getDefault.getPathMatcher(
    "glob:**/{test,tests}"
  )

  private def approximateSourceRoot(dir: Path): Option[Path] = {
    @tailrec def loop(d: Path): Option[Path] = {
      if (sourceRootPattern.matches(d)) Some(d)
      else if (defaultTestRootPattern.matches(d)) Some(d)
      else {
        Option(d.getParent) match {
          case Some(parent) => loop(parent)
          case None => None
        }
      }
    }
    loop(dir)
  }

Maybe it might be configurable. But actually it would be the best if bazel could provide this information.