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

gazelle: Resolve the Runfiles library #243

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading