From 017113937304505ed62da76fb0c4fb29343a07bd Mon Sep 17 00:00:00 2001 From: Alex Trotta Date: Mon, 27 Jul 2026 11:31:14 -0400 Subject: [PATCH] Fix mojo_library with generated sources with path stripping enabled Specifically in this set of circumstances this would cause an error. Fix by doing the normal path mapping workaround, and add a regression test. --- .github/workflows/ci.yaml | 5 ++++- mojo/mojo_library.bzl | 5 ++++- tests/generated/BUILD.bazel | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 tests/generated/BUILD.bazel diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 415f552..fda10a1 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -35,4 +35,7 @@ jobs: - name: CPU Info run: | lscpu || true - - run: bazel test //... + # Test twice, once with path stripping + path mapping, and once without + - run: | + bazel test //... + bazel test --experimental_output_paths=strip //... diff --git a/mojo/mojo_library.bzl b/mojo/mojo_library.bzl index 524cbc5..deb4d46 100644 --- a/mojo/mojo_library.bzl +++ b/mojo/mojo_library.bzl @@ -8,6 +8,9 @@ load("//mojo/private:utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "format_ def _format_include(arg): return ["-I", arg.dirname] +def _format_root(arg): + return arg.dirname + def _mojo_library_implementation(ctx): mojo_toolchain = ctx.toolchains["//:toolchain_type"].mojo_toolchain_info build_env = getattr(ctx.toolchains["//:toolchain_type"], "build_env", {}) @@ -43,7 +46,7 @@ def _mojo_library_implementation(ctx): args.add("--experimental-export-fixit", fixits_file) file_args.add_all(import_paths, map_each = format_import) - file_args.add(root_directory) + file_args.add_all([ctx.files.srcs[0]], map_each = _format_root) ctx.actions.run( executable = mojo_toolchain.mojo, inputs = depset(ctx.files.srcs + ctx.files.additional_compiler_inputs, transitive = [transitive_mojodeps]), diff --git a/tests/generated/BUILD.bazel b/tests/generated/BUILD.bazel new file mode 100644 index 0000000..cb3dfbb --- /dev/null +++ b/tests/generated/BUILD.bazel @@ -0,0 +1,37 @@ +# Regression test to ensure mojo_library and mojo_binary targets work with generated sources + +load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("//mojo:mojo_binary.bzl", "mojo_binary") +load("//mojo:mojo_library.bzl", "mojo_library") + +genrule( + name = "srcs", + outs = [ + "__init__.mojo", + "main.mojo", + ], + cmd = """cat << 'EOF' > $(location main.mojo) +def main(): + pass +EOF +touch $(location __init__.mojo) +""", +) + +mojo_library( + name = "lib", + srcs = [":__init__.mojo"], +) + +mojo_binary( + name = "bin", + srcs = [":main.mojo"], +) + +build_test( + name = "build_targets", + targets = [ + ":lib", + ":bin", + ], +)