From fa9254de7d31ef035933a2ca3e4ed67872d66a69 Mon Sep 17 00:00:00 2001 From: Alex Trotta Date: Tue, 1 Dec 2026 12:51:10 -0500 Subject: [PATCH 1/5] feat: Expand locations in environment variables --- mojo/mojo_library.bzl | 4 +- mojo/private/mojo_binary_test.bzl | 4 +- mojo/private/utils.bzl | 79 +++++++++++++++++++++++++++---- mojo/providers.bzl | 3 ++ mojo/toolchain.bzl | 24 ++++++++-- 5 files changed, 98 insertions(+), 16 deletions(-) diff --git a/mojo/mojo_library.bzl b/mojo/mojo_library.bzl index d19b56c..3751d86 100644 --- a/mojo/mojo_library.bzl +++ b/mojo/mojo_library.bzl @@ -5,7 +5,7 @@ load("@bazel_skylib//lib:paths.bzl", "paths") load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") load("//mojo:providers.bzl", "MojoInfo") -load("//mojo/private:utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "format_import", "handle_copt", "is_exec_config") +load("//mojo/private:utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "format_import", "handle_copt", "is_exec_config", "resolve_env") def _format_include(arg): return ["-I", arg.dirname] @@ -97,7 +97,7 @@ def _mojo_library_implementation(ctx): "MODULAR_CRASH_REPORTING_ENABLED": "false", "PATH": "/dev/null", # Avoid using the host's PATH "TEST_TMPDIR": ".", # Make sure any cache files are written to somewhere bazel will cleanup - } | mojo_toolchain.build_env, + } | resolve_env(mojo_toolchain.build_env), # TODO use_default_shell_env = True, toolchain = "//:toolchain_type", execution_requirements = { diff --git a/mojo/private/mojo_binary_test.bzl b/mojo/private/mojo_binary_test.bzl index 06bba5c..2b1f657 100644 --- a/mojo/private/mojo_binary_test.bzl +++ b/mojo/private/mojo_binary_test.bzl @@ -9,7 +9,7 @@ load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") load("@rules_python//python:py_info.bzl", "PyInfo") load("//mojo:providers.bzl", "MojoInfo") load(":transitions.bzl", "python_version_transition") -load(":utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "format_import", "handle_copt", "is_exec_config") +load(":utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "format_import", "handle_copt", "is_exec_config", "resolve_env") _PYTHON_TOOLCHAIN_TYPE = "@rules_python//python:toolchain_type" _ATTRS = { @@ -157,7 +157,7 @@ def _mojo_binary_test_implementation(ctx, *, shared_library = False): "MODULAR_MOJO_MAX_LLD_PATH": "/dev/null", # Make sure this fails if accessed "PATH": "/dev/null", # Avoid using the host's PATH "TEST_TMPDIR": ".", - } | mojo_toolchain.build_env, + } | resolve_env(mojo_toolchain.build_env), # TODO use_default_shell_env = True, exec_group = "mojo_compile", toolchain = "//:toolchain_type", diff --git a/mojo/private/utils.bzl b/mojo/private/utils.bzl index 4db6f73..602f8ba 100644 --- a/mojo/private/utils.bzl +++ b/mojo/private/utils.bzl @@ -126,6 +126,18 @@ def _labels_similar(label1, label2): return False return True +def _find_file(label, targets): + # type: (Label, list[Target]) -> File + """Finds a label in a list of targets. Asserts that it exists and maps to exactly one file.""" + for target in targets: + if label != target.label and not _labels_similar(label, target.label): + continue + files = target.files.to_list() + if len(files) != 1: + fail("expected {} to expand to exactly one file, got {}".format(label, len(files))) + return files[0] + fail("unable to map {} to an input".format(label)) + def handle_copt(ctx, copt, args): # type: (ctx, string, Args) -> None """Handles make variable expansion in copts @@ -140,14 +152,63 @@ 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 and not _labels_similar(label, input.label): - continue - files = input.files.to_list() - if len(files) != 1: - fail("expected exactly one file") - args.add(files[0], format = format_string) - return - fail("input {} not found in additional_compiler_inputs".format(label)) + input_file = _find_file(label, ctx.attr.additional_compiler_inputs) + args.add(input_file, format = format_string) else: args.add(copt) + +def parse_env(ctx, env, targets): + # type: (ctx, dict[string, string], list[Target]) -> dict[string, tuple[string, File | None]] + """Parses the values of an env dict without expanding paths. + + ctx.expand_location can't be used for this since it doesn't support path + mapping, so $(location), $(execpath) and $(rootpath) are instead turned into + File references that get expanded by resolve_env, or by an Args object, once + the action is being constructed. + + Args: + ctx: The rule context, used to resolve labels. + env: Dict of str -> str, where values may contain $(location