Skip to content

Commit 7b1ecd1

Browse files
authored
gazelle: Resolve the Runfiles library (#243)
1 parent 306e4da commit 7b1ecd1

5 files changed

Lines changed: 42 additions & 0 deletions

File tree

java/gazelle/resolve.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,16 @@ func (jr *Resolver) resolveSinglePackage(c *config.Config, pc *javaconfig.Config
199199
return label.NoLabel, nil
200200
}
201201

202+
// As per https://github.com/bazelbuild/bazel/blob/347407a88fd480fc5e0fbd42cc8196e4356a690b/tools/java/runfiles/Runfiles.java#L41
203+
if imp.Name == "com.google.devtools.build.runfiles" {
204+
runfilesLabel := "@bazel_tools//tools/java/runfiles"
205+
l, err := label.Parse(runfilesLabel)
206+
if err != nil {
207+
return label.NoLabel, fmt.Errorf("failed to parse known-good runfiles label %s: %w", runfilesLabel, err)
208+
}
209+
return l, nil
210+
}
211+
202212
if l, err := jr.lang.mavenResolver.Resolve(imp, pc.ExcludedArtifacts(), pc.MavenRepositoryName()); err != nil {
203213
var noExternal *maven.NoExternalImportsError
204214
var multipleExternal *maven.MultipleExternalImportsError
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
workspace(name = "runfiles_example")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("@rules_java//java:defs.bzl", "java_library")
2+
3+
java_library(
4+
name = "hello",
5+
srcs = ["Hello.java"],
6+
visibility = ["//:__subpackages__"],
7+
deps = ["@bazel_tools//tools/java/runfiles"],
8+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.hello;
2+
3+
import com.google.devtools.build.runfiles.AutoBazelRepository;
4+
import com.google.devtools.build.runfiles.Runfiles;
5+
6+
import java.io.IOException;
7+
import java.nio.charset.StandardCharsets;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
11+
@AutoBazelRepository
12+
public class Hello {
13+
public static String sayHi() throws IOException {
14+
Runfiles.Preloaded runfiles = Runfiles.preload();
15+
String path = runfiles
16+
.withSourceRepository(AutoBazelRepository_Hello.NAME)
17+
.rlocation("runfiles_example/src/main/java/com/example/hello/data.txt");
18+
String fileContents = Files.readString(Path.of(path), StandardCharsets.UTF_8);
19+
20+
return String.format("Hello %s", fileContents);
21+
}
22+
}

0 commit comments

Comments
 (0)