From 17a530f61d7c7608f13a757059994aa71986cd8c Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 10 Dec 2024 08:45:19 -0800 Subject: [PATCH] Ensure we don't have any broken links to external repository labels https://github.com/bazelbuild/bazel/commit/f27cfb8e88e497afeed4208047246cba7db20f6d made it possible to generate docs for rules from other repos but unless `src/main/java/com/google/devtools/build/docgen/bazel_link_map.json` is also updated appropriately, we can end up with broken links to rule sources. PiperOrigin-RevId: 704725413 Change-Id: I9eb5c490a1bfe9aa1c4325d394bc4840dc574aab --- .../com/google/devtools/build/docgen/SourceUrlMapper.java | 6 ++---- .../google/devtools/build/docgen/BuildDocCollectorTest.java | 4 +++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/google/devtools/build/docgen/SourceUrlMapper.java b/src/main/java/com/google/devtools/build/docgen/SourceUrlMapper.java index db5f580b184772..3170bea424bde0 100644 --- a/src/main/java/com/google/devtools/build/docgen/SourceUrlMapper.java +++ b/src/main/java/com/google/devtools/build/docgen/SourceUrlMapper.java @@ -95,11 +95,9 @@ private String urlOfLabel(Label label) { + label.toPathFragment().getPathString(); for (Map.Entry entry : repoPathsRewrites.entrySet()) { if (path.startsWith(entry.getKey())) { - path = entry.getValue() + path.substring(entry.getKey().length()); - break; + return entry.getValue() + path.substring(entry.getKey().length()); } } - - return path; + throw new IllegalStateException("Label URL left untransformed: " + path); } } diff --git a/src/test/java/com/google/devtools/build/docgen/BuildDocCollectorTest.java b/src/test/java/com/google/devtools/build/docgen/BuildDocCollectorTest.java index df3566f0f7d071..130d1a6a15732a 100644 --- a/src/test/java/com/google/devtools/build/docgen/BuildDocCollectorTest.java +++ b/src/test/java/com/google/devtools/build/docgen/BuildDocCollectorTest.java @@ -48,7 +48,9 @@ public final class BuildDocCollectorTest { new SourceUrlMapper( /* sourceUrlRoot= */ "https://example.com/", /* inputRoot= */ "/tmp/io_bazel/", - ImmutableMap.of("@_builtins//", "https://example.com/src/main/starlark/builtins_bzl/")); + ImmutableMap.of( + "@//", "https://main-repo.com/", + "@_builtins//", "https://example.com/src/main/starlark/builtins_bzl/")); @Before public void setUpCollectorState() {