Skip to content

Commit

Permalink
gazelle: Resolve the Runfiles library (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
illicitonion authored Jan 18, 2024
1 parent 306e4da commit 7b1ecd1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions java/gazelle/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ func (jr *Resolver) resolveSinglePackage(c *config.Config, pc *javaconfig.Config
return label.NoLabel, nil
}

// As per https://github.com/bazelbuild/bazel/blob/347407a88fd480fc5e0fbd42cc8196e4356a690b/tools/java/runfiles/Runfiles.java#L41
if imp.Name == "com.google.devtools.build.runfiles" {
runfilesLabel := "@bazel_tools//tools/java/runfiles"
l, err := label.Parse(runfilesLabel)
if err != nil {
return label.NoLabel, fmt.Errorf("failed to parse known-good runfiles label %s: %w", runfilesLabel, err)
}
return l, nil
}

if l, err := jr.lang.mavenResolver.Resolve(imp, pc.ExcludedArtifacts(), pc.MavenRepositoryName()); err != nil {
var noExternal *maven.NoExternalImportsError
var multipleExternal *maven.MultipleExternalImportsError
Expand Down
1 change: 1 addition & 0 deletions java/gazelle/testdata/runfiles/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
workspace(name = "runfiles_example")
1 change: 1 addition & 0 deletions java/gazelle/testdata/runfiles/expectedStderr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@rules_java//java:defs.bzl", "java_library")

java_library(
name = "hello",
srcs = ["Hello.java"],
visibility = ["//:__subpackages__"],
deps = ["@bazel_tools//tools/java/runfiles"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.hello;

import com.google.devtools.build.runfiles.AutoBazelRepository;
import com.google.devtools.build.runfiles.Runfiles;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

@AutoBazelRepository
public class Hello {
public static String sayHi() throws IOException {
Runfiles.Preloaded runfiles = Runfiles.preload();
String path = runfiles
.withSourceRepository(AutoBazelRepository_Hello.NAME)
.rlocation("runfiles_example/src/main/java/com/example/hello/data.txt");
String fileContents = Files.readString(Path.of(path), StandardCharsets.UTF_8);

return String.format("Hello %s", fileContents);
}
}

0 comments on commit 7b1ecd1

Please sign in to comment.