Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException when trying to read resources in bloop #111

Open
kubukoz opened this issue Apr 6, 2021 · 1 comment
Open

NullPointerException when trying to read resources in bloop #111

kubukoz opened this issue Apr 6, 2021 · 1 comment

Comments

@kubukoz
Copy link

kubukoz commented Apr 6, 2021

Evaluating Resources.inferRootDir is throwing a NPE when it's run in bloop - I believe this is because (in sbt) the initial new File(getClass.getResource("/").toURI) call returns something like

(project root)/target/scala-2.12/test-classes,

but in Bloop it's

(project root)/src/test/resources - the actual source directory.

So in the latter case, the loop:

while (current.ne(null) && current.getName != "target") {
  current = current.getParentFile
}

basically runs until it finds the root directory (/) and its parent, null (because there's usually no target in the path).

It would be great to support the case of running these tests through bloop, as it's often used by users of Metals to avoid duplicate compilations with sbt.

Maybe instead of the fragile classpath-based logic, we could have something compile-time, getting the path of the current file in a sourcecode-like fashion and moving upwards until we hit test, then just adding /resources?

@kubukoz
Copy link
Author

kubukoz commented Apr 7, 2021

As a POC, this seems to work well in both environments:

def resourceDirectory(implicit file: sourcecode.File): File =
  Monad[cats.Id]
    .tailRecM(Paths.get(file.value)) {
      case p if p.endsWith("test") => Right(p)
      case p                       => Left(p.getParent())
    }
    .resolve("resources")
    .toFile()

println(resourceDirectory)

If this sounds good, I'll be happy to provide a PR (presumably with a forked implementation of sourcecode.File).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant