From 994d866fe6ca395b708add9e01354faafaf11be8 Mon Sep 17 00:00:00 2001 From: Alex Trotta Date: Mon, 16 Nov 2026 02:52:00 -0500 Subject: [PATCH] fix: Give a best-effort guess if repos are the same This is a _terrible_ hack. I really hope there's a better way with 8.x. If the main repo uses `@foo//:bar` as a target, using `Label` here tries to resolve `@foo` relative to `@rules_mojo`, which usually would fail to match (I suppose it's super spooky if it doesn't fail). Now, we look at the repo names and as long as one is a subset of the other (avoid things like `+`s or slight `@` vs `@@` mismatches), then we count it. There could be cases this doesn't catch, but if it does we'll handle it then. --- mojo/private/utils.bzl | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/mojo/private/utils.bzl b/mojo/private/utils.bzl index 043590c..4db6f73 100644 --- a/mojo/private/utils.bzl +++ b/mojo/private/utils.bzl @@ -75,6 +75,8 @@ def _package_relative_label(ctx, label): label = "@@" + ctx.label.repo_name + "//" + ctx.label.package + label elif label.startswith("//"): label = "@@" + ctx.label.repo_name + label + elif label.startswith("@") and not label.startswith("@@"): + label = "@" + label return Label(label) def _extract_label(ctx, copt): @@ -111,6 +113,19 @@ def _extract_label(ctx, copt): return format_string, label +def _labels_similar(label1, label2): + """Without the full repo mapping, determine if two labels are close enough to count them as equal. + + Also replacable when `ctx.package_relative_label` is available.""" + + if label1.name != label2.name: + return False + if label1.package != label2.package: + return False + if label1.repo_name not in label2.repo_name and label2.repo_name not in label1.repo_name: + return False + return True + def handle_copt(ctx, copt, args): # type: (ctx, string, Args) -> None """Handles make variable expansion in copts @@ -126,7 +141,7 @@ def handle_copt(ctx, copt, args): if "$(" in copt: format_string, label = _extract_label(ctx, copt) for input in ctx.attr.additional_compiler_inputs: - if label != input.label: + if label != input.label and not _labels_similar(label, input.label): continue files = input.files.to_list() if len(files) != 1: