From 7b1ecd1c55178430efeb837cb9c46c484b9544a6 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Thu, 18 Jan 2024 17:38:33 +0000 Subject: [PATCH] gazelle: Resolve the Runfiles library (#243) --- java/gazelle/resolve.go | 10 +++++++++ java/gazelle/testdata/runfiles/WORKSPACE | 1 + .../testdata/runfiles/expectedStderr.txt | 1 + .../src/main/java/com/example/hello/BUILD.out | 8 +++++++ .../main/java/com/example/hello/Hello.java | 22 +++++++++++++++++++ 5 files changed, 42 insertions(+) create mode 100644 java/gazelle/testdata/runfiles/WORKSPACE create mode 100644 java/gazelle/testdata/runfiles/expectedStderr.txt create mode 100644 java/gazelle/testdata/runfiles/src/main/java/com/example/hello/BUILD.out create mode 100644 java/gazelle/testdata/runfiles/src/main/java/com/example/hello/Hello.java diff --git a/java/gazelle/resolve.go b/java/gazelle/resolve.go index 509727b8..ff9434d5 100644 --- a/java/gazelle/resolve.go +++ b/java/gazelle/resolve.go @@ -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 diff --git a/java/gazelle/testdata/runfiles/WORKSPACE b/java/gazelle/testdata/runfiles/WORKSPACE new file mode 100644 index 00000000..97587523 --- /dev/null +++ b/java/gazelle/testdata/runfiles/WORKSPACE @@ -0,0 +1 @@ +workspace(name = "runfiles_example") diff --git a/java/gazelle/testdata/runfiles/expectedStderr.txt b/java/gazelle/testdata/runfiles/expectedStderr.txt new file mode 100644 index 00000000..b3cb3407 --- /dev/null +++ b/java/gazelle/testdata/runfiles/expectedStderr.txt @@ -0,0 +1 @@ +{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"} diff --git a/java/gazelle/testdata/runfiles/src/main/java/com/example/hello/BUILD.out b/java/gazelle/testdata/runfiles/src/main/java/com/example/hello/BUILD.out new file mode 100644 index 00000000..d6e2a06c --- /dev/null +++ b/java/gazelle/testdata/runfiles/src/main/java/com/example/hello/BUILD.out @@ -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"], +) diff --git a/java/gazelle/testdata/runfiles/src/main/java/com/example/hello/Hello.java b/java/gazelle/testdata/runfiles/src/main/java/com/example/hello/Hello.java new file mode 100644 index 00000000..e9868003 --- /dev/null +++ b/java/gazelle/testdata/runfiles/src/main/java/com/example/hello/Hello.java @@ -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); + } +}