diff --git a/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java b/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java index b43e9d71478124..0c11cec4570586 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java @@ -1004,15 +1004,7 @@ public Label label(String labelString, StarlarkThread thread) throws EvalExcepti BazelModuleContext moduleContext = BazelModuleContext.of(Module.ofInnermostEnclosingStarlarkFunction(thread)); try { - Label label = Label.parseWithRepoContext(labelString, moduleContext.packageContext()); - if (!label.getRepository().isVisible()) { - throw Starlark.errorf( - "Invalid label string '%s': no repository visible as '@%s' from %s", - labelString, - label.getRepository().getName(), - label.getRepository().getOwnerRepoDisplayString()); - } - return label; + return Label.parseWithRepoContext(labelString, moduleContext.packageContext()); } catch (LabelSyntaxException e) { throw Starlark.errorf("Illegal absolute label syntax: %s", e.getMessage()); } diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java index 7105afce3f9d42..438d1a50f1d8ec 100644 --- a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java +++ b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java @@ -338,7 +338,8 @@ public String getPackageName() { + " \"external/repo\"", useStarlarkSemantics = true) @Deprecated - public String getWorkspaceRootForStarlarkOnly(StarlarkSemantics semantics) { + public String getWorkspaceRootForStarlarkOnly(StarlarkSemantics semantics) throws EvalException { + checkRepoVisibilityForStarlark("workspace_root"); return packageIdentifier .getRepository() .getExecPath(semantics.getBool(BuildLanguageOptions.EXPERIMENTAL_SIBLING_REPOSITORY_LAYOUT)) @@ -431,7 +432,8 @@ public String getUnambiguousCanonicalForm() { "The repository part of this label. For instance, " + "
Label(\"@foo//bar:baz\").workspace_name" + " == \"foo\"") - public String getWorkspaceName() { + public String getWorkspaceName() throws EvalException { + checkRepoVisibilityForStarlark("workspace_name"); return packageIdentifier.getRepository().getName(); } @@ -504,21 +506,10 @@ public Label getLocalTargetLabel(String targetName) throws LabelSyntaxException @Param(name = "relName", doc = "The label that will be resolved relative to this one.") }, useStarlarkThread = true) - public Label getRelative(String relName, StarlarkThread thread) - throws LabelSyntaxException, EvalException { - Label label = - getRelativeWithRemapping( - relName, - BazelModuleContext.of(Module.ofInnermostEnclosingStarlarkFunction(thread)) - .repoMapping()); - if (!label.getRepository().isVisible()) { - throw Starlark.errorf( - "Invalid label string '%s': no repository visible as '@%s' from %s", - relName, - label.getRepository().getName(), - label.getRepository().getOwnerRepoDisplayString()); - } - return label; + public Label getRelative(String relName, StarlarkThread thread) throws LabelSyntaxException { + return getRelativeWithRemapping( + relName, + BazelModuleContext.of(Module.ofInnermostEnclosingStarlarkFunction(thread)).repoMapping()); } /** @@ -676,4 +667,10 @@ public String expandToCommandLine() { // TODO(wyv): Consider using StarlarkSemantics here too for optional unambiguity. return getCanonicalForm(); } + + private void checkRepoVisibilityForStarlark(String method) throws EvalException { + if (!getRepository().isVisible()) { + throw Starlark.errorf("'%s' is not allowed on invalid Label %s", method, this); + } + } } diff --git a/src/main/java/com/google/devtools/build/skydoc/SkydocMain.java b/src/main/java/com/google/devtools/build/skydoc/SkydocMain.java index 573eb849c541e5..af7b186d477816 100644 --- a/src/main/java/com/google/devtools/build/skydoc/SkydocMain.java +++ b/src/main/java/com/google/devtools/build/skydoc/SkydocMain.java @@ -163,7 +163,7 @@ public static void main(String[] args) providerInfoMap, userDefinedFunctions, aspectInfoMap); - } catch (StarlarkEvaluationException exception) { + } catch (StarlarkEvaluationException | EvalException exception) { exception.printStackTrace(); System.err.println("Stardoc documentation generation failed: " + exception.getMessage()); System.exit(1); @@ -386,7 +386,8 @@ private Module recursiveEval( List