From c73d06b1404a538701f665fb2dbf294d6df412b4 Mon Sep 17 00:00:00 2001 From: Jordan Bonser Date: Tue, 14 Jan 2025 15:39:36 +0000 Subject: [PATCH 01/17] test: add a runfiles `toolchain_info` test --- e2e/.bazelrc | 3 ++ e2e/MODULE.bazel | 24 +++++++++++- e2e/MODULE.bazel.lock | 59 +++++++++++++++++++++++++++++- e2e/toolchain/runfiles/BUILD.bazel | 56 ++++++++++++++++++++++++++++ e2e/toolchain/runfiles/binary.py | 16 ++++++++ e2e/toolchain/runfiles/fixture.txt | 3 ++ 6 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 e2e/toolchain/runfiles/BUILD.bazel create mode 100644 e2e/toolchain/runfiles/binary.py create mode 100644 e2e/toolchain/runfiles/fixture.txt diff --git a/e2e/.bazelrc b/e2e/.bazelrc index 19b7a92..d422b08 100644 --- a/e2e/.bazelrc +++ b/e2e/.bazelrc @@ -1,3 +1,6 @@ +# Enable hermetic `rules_python` +common --@rules_python//python/config_settings:bootstrap_impl=script + # Build cache build --experimental_guard_against_concurrent_changes diff --git a/e2e/MODULE.bazel b/e2e/MODULE.bazel index 045bc4e..52aa722 100644 --- a/e2e/MODULE.bazel +++ b/e2e/MODULE.bazel @@ -5,13 +5,28 @@ module( ], ) +bazel_dep(name = "rules_python", version = "1.0.0") bazel_dep(name = "bazel_skylib", version = "1.4.2") -bazel_dep(name = "toolchain_utils", version = "0.0.0") + +bazel_dep(name = "hermetic_cc_toolchain", version = "3.1.0", dev_dependency = True) + +bazel_dep(name = "toolchain_utils") local_path_override( module_name = "toolchain_utils", path = "..", ) +# We have to avoid the `chmod`/`chown`/`id` unhermetic-ness +# TODO: remove this when `ignore_root_user_error` is hermetic +# https://github.com/bazelbuild/rules_python/issues/2016 +dev = use_extension("@rules_python//python/extensions:python.bzl", "python", dev_dependency = True) +dev.toolchain( + configure_coverage_tool = True, + # TODO: change the code to use Python for `chmod`/`id` + ignore_root_user_error = True, + python_version = "3.11", +) + which = use_repo_rule("@toolchain_utils//toolchain/local/which:defs.bzl", "toolchain_local_which") which( @@ -25,7 +40,12 @@ resolved( toolchain_type = "//toolchain/echo:type", ) -register_toolchains("//toolchain/echo:all") +resolved( + name = "resolved-runfiles", + toolchain_type = "//toolchain/runfiles:type", +) + +register_toolchains("//toolchain/...") fixture = use_repo_rule("//test/fixture:repository.bzl", "fixture") diff --git a/e2e/MODULE.bazel.lock b/e2e/MODULE.bazel.lock index 2b278bd..a7dc0f6 100644 --- a/e2e/MODULE.bazel.lock +++ b/e2e/MODULE.bazel.lock @@ -40,6 +40,8 @@ "https://bcr.bazel.build/modules/googletest/1.14.0.bcr.1/MODULE.bazel": "22c31a561553727960057361aa33bf20fb2e98584bc4fec007906e27053f80c6", "https://bcr.bazel.build/modules/googletest/1.14.0.bcr.1/source.json": "41e9e129f80d8c8bf103a7acc337b76e54fad1214ac0a7084bf24f4cd924b8b4", "https://bcr.bazel.build/modules/googletest/1.14.0/MODULE.bazel": "cfbcbf3e6eac06ef9d85900f64424708cc08687d1b527f0ef65aa7517af8118f", + "https://bcr.bazel.build/modules/hermetic_cc_toolchain/3.1.0/MODULE.bazel": "ea4b3a25a9417a7db57a8a2f9ebdee91d679823c6274b482b817ed128d81c594", + "https://bcr.bazel.build/modules/hermetic_cc_toolchain/3.1.0/source.json": "9d1df0459caefdf41052d360469922a73e219f67c8ce4da0628cc604469822b9", "https://bcr.bazel.build/modules/jsoncpp/1.9.5/MODULE.bazel": "31271aedc59e815656f5736f282bb7509a97c7ecb43e927ac1a37966e0578075", "https://bcr.bazel.build/modules/jsoncpp/1.9.5/source.json": "4108ee5085dd2885a341c7fab149429db457b3169b86eb081fa245eadf69169d", "https://bcr.bazel.build/modules/libpfm/4.11.0/MODULE.bazel": "45061ff025b301940f1e30d2c16bea596c25b176c8b6b3087e92615adbd52902", @@ -54,6 +56,7 @@ "https://bcr.bazel.build/modules/protobuf/27.0/MODULE.bazel": "7873b60be88844a0a1d8f80b9d5d20cfbd8495a689b8763e76c6372998d3f64c", "https://bcr.bazel.build/modules/protobuf/27.1/MODULE.bazel": "703a7b614728bb06647f965264967a8ef1c39e09e8f167b3ca0bb1fd80449c0d", "https://bcr.bazel.build/modules/protobuf/29.0-rc2/MODULE.bazel": "6241d35983510143049943fc0d57937937122baf1b287862f9dc8590fc4c37df", + "https://bcr.bazel.build/modules/protobuf/29.0-rc3/MODULE.bazel": "33c2dfa286578573afc55a7acaea3cada4122b9631007c594bf0729f41c8de92", "https://bcr.bazel.build/modules/protobuf/29.0/MODULE.bazel": "319dc8bf4c679ff87e71b1ccfb5a6e90a6dbc4693501d471f48662ac46d04e4e", "https://bcr.bazel.build/modules/protobuf/29.0/source.json": "b857f93c796750eef95f0d61ee378f3420d00ee1dd38627b27193aa482f4f981", "https://bcr.bazel.build/modules/protobuf/3.19.0/MODULE.bazel": "6b5fbb433f760a99a22b18b6850ed5784ef0e9928a72668b66e4d7ccd47db9b0", @@ -87,6 +90,8 @@ "https://bcr.bazel.build/modules/rules_java/7.2.0/MODULE.bazel": "06c0334c9be61e6cef2c8c84a7800cef502063269a5af25ceb100b192453d4ab", "https://bcr.bazel.build/modules/rules_java/7.3.2/MODULE.bazel": "50dece891cfdf1741ea230d001aa9c14398062f2b7c066470accace78e412bc2", "https://bcr.bazel.build/modules/rules_java/7.6.1/MODULE.bazel": "2f14b7e8a1aa2f67ae92bc69d1ec0fa8d9f827c4e17ff5e5f02e91caa3b2d0fe", + "https://bcr.bazel.build/modules/rules_java/8.3.2/MODULE.bazel": "7336d5511ad5af0b8615fdc7477535a2e4e723a357b6713af439fe8cf0195017", + "https://bcr.bazel.build/modules/rules_java/8.5.1/MODULE.bazel": "d8a9e38cc5228881f7055a6079f6f7821a073df3744d441978e7a43e20226939", "https://bcr.bazel.build/modules/rules_java/8.6.1/MODULE.bazel": "f4808e2ab5b0197f094cabce9f4b006a27766beb6a9975931da07099560ca9c2", "https://bcr.bazel.build/modules/rules_java/8.6.1/source.json": "f18d9ad3c4c54945bf422ad584fa6c5ca5b3116ff55a5b1bc77e5c1210be5960", "https://bcr.bazel.build/modules/rules_jvm_external/4.4.2/MODULE.bazel": "a56b85e418c83eb1839819f0b515c431010160383306d13ec21959ac412d2fe7", @@ -118,7 +123,8 @@ "https://bcr.bazel.build/modules/rules_python/0.31.0/MODULE.bazel": "93a43dc47ee570e6ec9f5779b2e64c1476a6ce921c48cc9a1678a91dd5f8fd58", "https://bcr.bazel.build/modules/rules_python/0.4.0/MODULE.bazel": "9208ee05fd48bf09ac60ed269791cf17fb343db56c8226a720fbb1cdf467166c", "https://bcr.bazel.build/modules/rules_python/0.40.0/MODULE.bazel": "9d1a3cd88ed7d8e39583d9ffe56ae8a244f67783ae89b60caafc9f5cf318ada7", - "https://bcr.bazel.build/modules/rules_python/0.40.0/source.json": "939d4bd2e3110f27bfb360292986bb79fd8dcefb874358ccd6cdaa7bda029320", + "https://bcr.bazel.build/modules/rules_python/1.0.0/MODULE.bazel": "898a3d999c22caa585eb062b600f88654bf92efb204fa346fb55f6f8edffca43", + "https://bcr.bazel.build/modules/rules_python/1.0.0/source.json": "b0162a65c6312e45e7912e39abd1a7f8856c2c7e41ecc9b6dc688a6f6400a917", "https://bcr.bazel.build/modules/rules_shell/0.2.0/MODULE.bazel": "fda8a652ab3c7d8fee214de05e7a9916d8b28082234e8d2c0094505c5268ed3c", "https://bcr.bazel.build/modules/rules_shell/0.2.0/source.json": "7f27af3c28037d9701487c4744b5448d26537cc66cdef0d8df7ae85411f8de95", "https://bcr.bazel.build/modules/stardoc/0.5.1/MODULE.bazel": "1a05d92974d0c122f5ccf09291442580317cdd859f07a8655f1db9a60374f9f8", @@ -126,7 +132,8 @@ "https://bcr.bazel.build/modules/stardoc/0.5.6/MODULE.bazel": "c43dabc564990eeab55e25ed61c07a1aadafe9ece96a4efabb3f8bf9063b71ef", "https://bcr.bazel.build/modules/stardoc/0.7.0/MODULE.bazel": "05e3d6d30c099b6770e97da986c53bd31844d7f13d41412480ea265ac9e8079c", "https://bcr.bazel.build/modules/stardoc/0.7.1/MODULE.bazel": "3548faea4ee5dda5580f9af150e79d0f6aea934fc60c1cc50f4efdd9420759e7", - "https://bcr.bazel.build/modules/stardoc/0.7.1/source.json": "b6500ffcd7b48cd72c29bb67bcac781e12701cc0d6d55d266a652583cfcdab01", + "https://bcr.bazel.build/modules/stardoc/0.7.2/MODULE.bazel": "fc152419aa2ea0f51c29583fab1e8c99ddefd5b3778421845606ee628629e0e5", + "https://bcr.bazel.build/modules/stardoc/0.7.2/source.json": "58b029e5e901d6802967754adf0a9056747e8176f017cfe3607c0851f4d42216", "https://bcr.bazel.build/modules/upb/0.0.0-20220923-a547704/MODULE.bazel": "7298990c00040a0e2f121f6c32544bab27d4452f80d9ce51349b1a28f3005c43", "https://bcr.bazel.build/modules/zlib/1.2.11/MODULE.bazel": "07b389abc85fdbca459b69e2ec656ae5622873af3f845e1c9d80fe179f3effa0", "https://bcr.bazel.build/modules/zlib/1.3.1.bcr.3/MODULE.bazel": "af322bc08976524477c79d1e45e241b6efbeb918c497e8840b8ab116802dda79", @@ -135,6 +142,54 @@ }, "selectedYankedVersions": {}, "moduleExtensions": { + "@@hermetic_cc_toolchain+//toolchain:ext.bzl%toolchains": { + "general": { + "bzlTransitiveDigest": "QE9mmKKNnRnnIzR5esB4//YvnfXT+tjg6NPgrdOQDnM=", + "usagesDigest": "JojEsGQu7csSPZpzBH/MSNWup5s1PPrDxn1Yeizs3n4=", + "recordedFileInputs": {}, + "recordedDirentsInputs": {}, + "envVariables": {}, + "generatedRepoSpecs": { + "zig_sdk": { + "repoRuleId": "@@hermetic_cc_toolchain+//toolchain:defs.bzl%zig_repository", + "attributes": { + "version": "0.12.0", + "url_formats": [ + "https://mirror.bazel.build/ziglang.org/download/{version}/zig-{host_platform}-{version}.{_ext}", + "https://ziglang.org/download/{version}/zig-{host_platform}-{version}.{_ext}" + ], + "host_platform_sha256": { + "linux-aarch64": "754f1029484079b7e0ca3b913a0a2f2a6afd5a28990cb224fe8845e72f09de63", + "linux-x86_64": "c7ae866b8a76a568e2d5cfd31fe89cdb629bdd161fdd5018b29a4a0a17045cad", + "macos-aarch64": "294e224c14fd0822cfb15a35cf39aa14bd9967867999bf8bdfe3db7ddec2a27f", + "macos-x86_64": "4d411bf413e7667821324da248e8589278180dbc197f4f282b7dbb599a689311", + "windows-aarch64": "04c6b92689241ca7a8a59b5f12d2ca2820c09d5043c3c4808b7e93e41c7bf97b", + "windows-x86_64": "2199eb4c2000ddb1fba85ba78f1fcf9c1fb8b3e57658f6a627a8e513131893f5" + }, + "host_platform_ext": { + "linux-aarch64": "tar.xz", + "linux-x86_64": "tar.xz", + "macos-aarch64": "tar.xz", + "macos-x86_64": "tar.xz", + "windows-x86_64": "zip" + } + } + } + }, + "recordedRepoMappingEntries": [ + [ + "hermetic_cc_toolchain+", + "bazel_tools", + "bazel_tools" + ], + [ + "hermetic_cc_toolchain+", + "hermetic_cc_toolchain", + "hermetic_cc_toolchain+" + ] + ] + } + }, "@@platforms//host:extension.bzl%host_platform": { "general": { "bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=", diff --git a/e2e/toolchain/runfiles/BUILD.bazel b/e2e/toolchain/runfiles/BUILD.bazel new file mode 100644 index 0000000..b1c102c --- /dev/null +++ b/e2e/toolchain/runfiles/BUILD.bazel @@ -0,0 +1,56 @@ +load("@rules_python//python:defs.bzl", "py_binary") +load("@toolchain_utils//toolchain/info:defs.bzl", "toolchain_info") +load("@toolchain_utils//toolchain/test:defs.bzl", "toolchain_test") +load("@bazel_skylib//rules:build_test.bzl", "build_test") + +toolchain_type( + name = "type", + visibility = ["//visibility:public"], +) + +py_binary( + name = "binary", + srcs = ["binary.py"], + data = [":fixture.txt"], + deps = ["@rules_python//python/runfiles"], +) + +toolchain_info( + name = "info", + target = ":binary", + variable = "BINARY", +) + +toolchain( + name = "hermetic", + toolchain = ":info", + toolchain_type = ":type", +) + +alias( + name = "resolved", + actual = "@resolved-runfiles", +) + +toolchain_test( + name = "fixture", + size = "small", + stdout = ":fixture.txt", + toolchains = [":resolved"], +) + +genrule( + name = "generate", + outs = ["stdout.log"], + cmd = "$(BINARY) > $@", + toolchains = [":resolved"], +) + +# FIXME: this fails to find runfiles +toolchain_test( + name = "stdout", + size = "small", + stdout = ":stdout.log", + tags = ["manual"], + toolchains = [":resolved"], +) diff --git a/e2e/toolchain/runfiles/binary.py b/e2e/toolchain/runfiles/binary.py new file mode 100644 index 0000000..ba464f0 --- /dev/null +++ b/e2e/toolchain/runfiles/binary.py @@ -0,0 +1,16 @@ +#! /usr/bin/env python3 + +from sys import argv, stdout + +from python.runfiles import Runfiles + +first, *rest = argv +assert not rest, "No arguments, please" + +runfiles = Runfiles.Create() +resolved = runfiles.Rlocation("_main/toolchain/runfiles/fixture.txt") +assert resolved, "No runfile found" + +with open(resolved) as stream: + for line in stream: + stdout.write(line) diff --git a/e2e/toolchain/runfiles/fixture.txt b/e2e/toolchain/runfiles/fixture.txt new file mode 100644 index 0000000..3e5f372 --- /dev/null +++ b/e2e/toolchain/runfiles/fixture.txt @@ -0,0 +1,3 @@ +Hello, world! + +This is a runfile 😀 -- GitLab From 29fc75086a102722eb46719f0772faf0cb85b3b9 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 15 Jan 2025 11:43:03 +0000 Subject: [PATCH 02/17] test: add a symlink `toolchain_symlink_target` test through a toolchain This is a common use-case when using multi-tool binaries. --- e2e/MODULE.bazel | 6 ++++ e2e/MODULE.bazel.lock | 18 +++++++++- e2e/toolchain/symlink/BUILD.bazel | 59 +++++++++++++++++++++++++++++++ e2e/toolchain/symlink/binary.go | 11 ++++++ e2e/toolchain/symlink/fixture.txt | 1 + 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 e2e/toolchain/symlink/BUILD.bazel create mode 100755 e2e/toolchain/symlink/binary.go create mode 100644 e2e/toolchain/symlink/fixture.txt diff --git a/e2e/MODULE.bazel b/e2e/MODULE.bazel index 52aa722..b1b1c3d 100644 --- a/e2e/MODULE.bazel +++ b/e2e/MODULE.bazel @@ -5,6 +5,7 @@ module( ], ) +bazel_dep(name = "rules_go", version = "0.52.0") bazel_dep(name = "rules_python", version = "1.0.0") bazel_dep(name = "bazel_skylib", version = "1.4.2") @@ -45,6 +46,11 @@ resolved( toolchain_type = "//toolchain/runfiles:type", ) +resolved( + name = "resolved-symlink", + toolchain_type = "//toolchain/symlink:type", +) + register_toolchains("//toolchain/...") fixture = use_repo_rule("//test/fixture:repository.bzl", "fixture") diff --git a/e2e/MODULE.bazel.lock b/e2e/MODULE.bazel.lock index a7dc0f6..86d1397 100644 --- a/e2e/MODULE.bazel.lock +++ b/e2e/MODULE.bazel.lock @@ -10,6 +10,7 @@ "https://bcr.bazel.build/modules/abseil-cpp/20230802.1/MODULE.bazel": "fa92e2eb41a04df73cdabeec37107316f7e5272650f81d6cc096418fe647b915", "https://bcr.bazel.build/modules/abseil-cpp/20240116.1/MODULE.bazel": "37bcdb4440fbb61df6a1c296ae01b327f19e9bb521f9b8e26ec854b6f97309ed", "https://bcr.bazel.build/modules/abseil-cpp/20240116.1/source.json": "9be551b8d4e3ef76875c0d744b5d6a504a27e3ae67bc6b28f46415fd2d2957da", + "https://bcr.bazel.build/modules/bazel_features/1.1.0/MODULE.bazel": "cfd42ff3b815a5f39554d97182657f8c4b9719568eb7fded2b9135f084bf760b", "https://bcr.bazel.build/modules/bazel_features/1.1.1/MODULE.bazel": "27b8c79ef57efe08efccbd9dd6ef70d61b4798320b8d3c134fd571f78963dbcd", "https://bcr.bazel.build/modules/bazel_features/1.11.0/MODULE.bazel": "f9382337dd5a474c3b7d334c2f83e50b6eaedc284253334cf823044a26de03e8", "https://bcr.bazel.build/modules/bazel_features/1.13.0/MODULE.bazel": "c14c33c7c3c730612bdbe14ebbb5e61936b6f11322ea95a6e91cd1ba962f94df", @@ -35,6 +36,11 @@ "https://bcr.bazel.build/modules/bazel_skylib/1.7.1/source.json": "f121b43eeefc7c29efbd51b83d08631e2347297c95aac9764a701f2a6a2bb953", "https://bcr.bazel.build/modules/buildozer/7.1.2/MODULE.bazel": "2e8dd40ede9c454042645fd8d8d0cd1527966aa5c919de86661e62953cd73d84", "https://bcr.bazel.build/modules/buildozer/7.1.2/source.json": "c9028a501d2db85793a6996205c8de120944f50a0d570438fcae0457a5f9d1f8", + "https://bcr.bazel.build/modules/gazelle/0.32.0/MODULE.bazel": "b499f58a5d0d3537f3cf5b76d8ada18242f64ec474d8391247438bf04f58c7b8", + "https://bcr.bazel.build/modules/gazelle/0.33.0/MODULE.bazel": "a13a0f279b462b784fb8dd52a4074526c4a2afe70e114c7d09066097a46b3350", + "https://bcr.bazel.build/modules/gazelle/0.34.0/MODULE.bazel": "abdd8ce4d70978933209db92e436deb3a8b737859e9354fb5fd11fb5c2004c8a", + "https://bcr.bazel.build/modules/gazelle/0.36.0/MODULE.bazel": "e375d5d6e9a6ca59b0cb38b0540bc9a05b6aa926d322f2de268ad267a2ee74c0", + "https://bcr.bazel.build/modules/gazelle/0.36.0/source.json": "0823f097b127e0201ae55d85647c94095edfe27db0431a7ae880dcab08dfaa04", "https://bcr.bazel.build/modules/google_benchmark/1.8.2/MODULE.bazel": "a70cf1bba851000ba93b58ae2f6d76490a9feb74192e57ab8e8ff13c34ec50cb", "https://bcr.bazel.build/modules/googletest/1.11.0/MODULE.bazel": "3a83f095183f66345ca86aa13c58b59f9f94a2f81999c093d4eeaa2d262d12f4", "https://bcr.bazel.build/modules/googletest/1.14.0.bcr.1/MODULE.bazel": "22c31a561553727960057361aa33bf20fb2e98584bc4fec007906e27053f80c6", @@ -60,6 +66,8 @@ "https://bcr.bazel.build/modules/protobuf/29.0/MODULE.bazel": "319dc8bf4c679ff87e71b1ccfb5a6e90a6dbc4693501d471f48662ac46d04e4e", "https://bcr.bazel.build/modules/protobuf/29.0/source.json": "b857f93c796750eef95f0d61ee378f3420d00ee1dd38627b27193aa482f4f981", "https://bcr.bazel.build/modules/protobuf/3.19.0/MODULE.bazel": "6b5fbb433f760a99a22b18b6850ed5784ef0e9928a72668b66e4d7ccd47db9b0", + "https://bcr.bazel.build/modules/protobuf/3.19.2/MODULE.bazel": "532ffe5f2186b69fdde039efe6df13ba726ff338c6bc82275ad433013fa10573", + "https://bcr.bazel.build/modules/protobuf/3.19.6/MODULE.bazel": "9233edc5e1f2ee276a60de3eaa47ac4132302ef9643238f23128fea53ea12858", "https://bcr.bazel.build/modules/pybind11_bazel/2.11.1/MODULE.bazel": "88af1c246226d87e65be78ed49ecd1e6f5e98648558c14ce99176da041dc378e", "https://bcr.bazel.build/modules/pybind11_bazel/2.11.1/source.json": "be4789e951dd5301282729fe3d4938995dc4c1a81c2ff150afc9f1b0504c6022", "https://bcr.bazel.build/modules/re2/2023-09-01/MODULE.bazel": "cb3d511531b16cfc78a225a9e2136007a48cf8a677e4264baeab57fe78a80206", @@ -80,6 +88,11 @@ "https://bcr.bazel.build/modules/rules_foreign_cc/0.9.0/MODULE.bazel": "c9e8c682bf75b0e7c704166d79b599f93b72cfca5ad7477df596947891feeef6", "https://bcr.bazel.build/modules/rules_fuzzing/0.5.2/MODULE.bazel": "40c97d1144356f52905566c55811f13b299453a14ac7769dfba2ac38192337a8", "https://bcr.bazel.build/modules/rules_fuzzing/0.5.2/source.json": "c8b1e2c717646f1702290959a3302a178fb639d987ab61d548105019f11e527e", + "https://bcr.bazel.build/modules/rules_go/0.41.0/MODULE.bazel": "55861d8e8bb0e62cbd2896f60ff303f62ffcb0eddb74ecb0e5c0cbe36fc292c8", + "https://bcr.bazel.build/modules/rules_go/0.42.0/MODULE.bazel": "8cfa875b9aa8c6fce2b2e5925e73c1388173ea3c32a0db4d2b4804b453c14270", + "https://bcr.bazel.build/modules/rules_go/0.46.0/MODULE.bazel": "3477df8bdcc49e698b9d25f734c4f3a9f5931ff34ee48a2c662be168f5f2d3fd", + "https://bcr.bazel.build/modules/rules_go/0.52.0/MODULE.bazel": "0cf080a2706aa8fc9abf64286cee60fdf0238db37b7f1793b0f7d550d59ea3ae", + "https://bcr.bazel.build/modules/rules_go/0.52.0/source.json": "441bc7591044993dce9fb0377fcadf3086d6afac621b909d17d53858a4a1b8d4", "https://bcr.bazel.build/modules/rules_java/4.0.0/MODULE.bazel": "5a78a7ae82cd1a33cef56dc578c7d2a46ed0dca12643ee45edbb8417899e6f74", "https://bcr.bazel.build/modules/rules_java/5.3.5/MODULE.bazel": "a4ec4f2db570171e3e5eb753276ee4b389bae16b96207e9d3230895c99644b86", "https://bcr.bazel.build/modules/rules_java/6.0.0/MODULE.bazel": "8a43b7df601a7ec1af61d79345c17b31ea1fedc6711fd4abfd013ea612978e39", @@ -113,6 +126,7 @@ "https://bcr.bazel.build/modules/rules_pkg/1.0.1/source.json": "bd82e5d7b9ce2d31e380dd9f50c111d678c3bdaca190cb76b0e1c71b05e1ba8a", "https://bcr.bazel.build/modules/rules_proto/4.0.0/MODULE.bazel": "a7a7b6ce9bee418c1a760b3d84f83a299ad6952f9903c67f19e4edd964894e06", "https://bcr.bazel.build/modules/rules_proto/5.3.0-21.7/MODULE.bazel": "e8dff86b0971688790ae75528fe1813f71809b5afd57facb44dad9e8eca631b7", + "https://bcr.bazel.build/modules/rules_proto/6.0.0/MODULE.bazel": "b531d7f09f58dce456cd61b4579ce8c86b38544da75184eadaf0a7cb7966453f", "https://bcr.bazel.build/modules/rules_proto/6.0.2/MODULE.bazel": "ce916b775a62b90b61888052a416ccdda405212b6aaeb39522f7dc53431a5e73", "https://bcr.bazel.build/modules/rules_proto/7.0.2/MODULE.bazel": "bf81793bd6d2ad89a37a40693e56c61b0ee30f7a7fdbaf3eabbf5f39de47dea2", "https://bcr.bazel.build/modules/rules_proto/7.0.2/source.json": "1e5e7260ae32ef4f2b52fd1d0de8d03b606a44c91b694d2f1afb1d3b28a48ce1", @@ -126,7 +140,8 @@ "https://bcr.bazel.build/modules/rules_python/1.0.0/MODULE.bazel": "898a3d999c22caa585eb062b600f88654bf92efb204fa346fb55f6f8edffca43", "https://bcr.bazel.build/modules/rules_python/1.0.0/source.json": "b0162a65c6312e45e7912e39abd1a7f8856c2c7e41ecc9b6dc688a6f6400a917", "https://bcr.bazel.build/modules/rules_shell/0.2.0/MODULE.bazel": "fda8a652ab3c7d8fee214de05e7a9916d8b28082234e8d2c0094505c5268ed3c", - "https://bcr.bazel.build/modules/rules_shell/0.2.0/source.json": "7f27af3c28037d9701487c4744b5448d26537cc66cdef0d8df7ae85411f8de95", + "https://bcr.bazel.build/modules/rules_shell/0.3.0/MODULE.bazel": "de4402cd12f4cc8fda2354fce179fdb068c0b9ca1ec2d2b17b3e21b24c1a937b", + "https://bcr.bazel.build/modules/rules_shell/0.3.0/source.json": "c55ed591aa5009401ddf80ded9762ac32c358d2517ee7820be981e2de9756cf3", "https://bcr.bazel.build/modules/stardoc/0.5.1/MODULE.bazel": "1a05d92974d0c122f5ccf09291442580317cdd859f07a8655f1db9a60374f9f8", "https://bcr.bazel.build/modules/stardoc/0.5.3/MODULE.bazel": "c7f6948dae6999bf0db32c1858ae345f112cacf98f174c7a8bb707e41b974f1c", "https://bcr.bazel.build/modules/stardoc/0.5.6/MODULE.bazel": "c43dabc564990eeab55e25ed61c07a1aadafe9ece96a4efabb3f8bf9063b71ef", @@ -136,6 +151,7 @@ "https://bcr.bazel.build/modules/stardoc/0.7.2/source.json": "58b029e5e901d6802967754adf0a9056747e8176f017cfe3607c0851f4d42216", "https://bcr.bazel.build/modules/upb/0.0.0-20220923-a547704/MODULE.bazel": "7298990c00040a0e2f121f6c32544bab27d4452f80d9ce51349b1a28f3005c43", "https://bcr.bazel.build/modules/zlib/1.2.11/MODULE.bazel": "07b389abc85fdbca459b69e2ec656ae5622873af3f845e1c9d80fe179f3effa0", + "https://bcr.bazel.build/modules/zlib/1.2.12/MODULE.bazel": "3b1a8834ada2a883674be8cbd36ede1b6ec481477ada359cd2d3ddc562340b27", "https://bcr.bazel.build/modules/zlib/1.3.1.bcr.3/MODULE.bazel": "af322bc08976524477c79d1e45e241b6efbeb918c497e8840b8ab116802dda79", "https://bcr.bazel.build/modules/zlib/1.3.1.bcr.3/source.json": "2be409ac3c7601245958cd4fcdff4288be79ed23bd690b4b951f500d54ee6e7d", "https://bcr.bazel.build/modules/zlib/1.3.1/MODULE.bazel": "751c9940dcfe869f5f7274e1295422a34623555916eb98c174c1e945594bf198" diff --git a/e2e/toolchain/symlink/BUILD.bazel b/e2e/toolchain/symlink/BUILD.bazel new file mode 100644 index 0000000..31763a0 --- /dev/null +++ b/e2e/toolchain/symlink/BUILD.bazel @@ -0,0 +1,59 @@ +load("@rules_go//go:def.bzl", "go_binary") +load("@toolchain_utils//toolchain/symlink/target:defs.bzl", "toolchain_symlink_target") +load("@toolchain_utils//toolchain/info:defs.bzl", "toolchain_info") +load("@toolchain_utils//toolchain/test:defs.bzl", "toolchain_test") +load("@bazel_skylib//rules:build_test.bzl", "build_test") + +toolchain_type( + name = "type", + visibility = ["//visibility:public"], +) + +go_binary( + name = "binary", + srcs = ["binary.go"], + pure = "on", +) + +toolchain_symlink_target( + name = "basename", + target = ":binary", +) + +toolchain_info( + name = "info", + target = ":basename", + variable = "BINARY", +) + +toolchain( + name = "hermetic", + toolchain = ":info", + toolchain_type = ":type", +) + +alias( + name = "resolved", + actual = "@resolved-symlink", +) + +toolchain_test( + name = "fixture", + size = "small", + stdout = ":fixture.txt", + toolchains = [":resolved"], +) + +genrule( + name = "generate", + outs = ["stdout.log"], + cmd = "$(BINARY) > $@", + toolchains = [":resolved"], +) + +toolchain_test( + name = "stdout", + size = "small", + stdout = ":stdout.log", + toolchains = [":resolved"], +) diff --git a/e2e/toolchain/symlink/binary.go b/e2e/toolchain/symlink/binary.go new file mode 100755 index 0000000..3762a59 --- /dev/null +++ b/e2e/toolchain/symlink/binary.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" + "os" + "path" +) + +func main() { + fmt.Fprintf(os.Stdout, "Argument zero: %s\n", path.Base(os.Args[0])) +} diff --git a/e2e/toolchain/symlink/fixture.txt b/e2e/toolchain/symlink/fixture.txt new file mode 100644 index 0000000..19c3909 --- /dev/null +++ b/e2e/toolchain/symlink/fixture.txt @@ -0,0 +1 @@ +Argument zero: basename -- GitLab From 2b1924ca698827e6ed7b8eba84be7027a8922e9d Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 15 Jan 2025 12:09:57 +0000 Subject: [PATCH 03/17] fix: forward basename through `toolchain_info` Previously, we used the `ctx.label.name` to determine the basename of the symlink. However, this can mess up usage for multitool binaries. Use the current executable basename as the symlink basename. --- toolchain/info/rule.bzl | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/toolchain/info/rule.bzl b/toolchain/info/rule.bzl index da20b94..9cf58c6 100644 --- a/toolchain/info/rule.bzl +++ b/toolchain/info/rule.bzl @@ -45,30 +45,16 @@ ATTRS = { "variable": attr.string( doc = "The variable name for Make or the execution environment. Defaults to `name.upper()`", ), - "_windows": attr.label( - providers = [platform_common.ConstraintValueInfo], - default = "//toolchain/constraint/os:windows", - ), } def implementation(ctx): - basename = ctx.label.name - variable = ctx.attr.variable or basename.upper() - windows = ctx.attr._windows[platform_common.ConstraintValueInfo] - - target = ctx.executable.target - extension = target.extension - if extension in (".bat", ".cmd"): - basename = basename + extension - elif extension in ("bat", "cmd"): - basename = "{}.{}".format(basename, extension) - elif not extension and "." not in basename and ctx.target_platform_has_constraint(windows): - basename = "{}.exe".format(basename) + basename = ctx.executable.target.basename + variable = ctx.attr.variable or ctx.label.name.upper() executable = ctx.actions.declare_file("{}/{}".format(ctx.label.name, basename)) ctx.actions.symlink( output = executable, - target_file = target, + target_file = ctx.executable.target, is_executable = True, ) -- GitLab From f51317b81fe3ffd2a30b214891ed45313b06ec1a Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 15 Jan 2025 13:04:58 +0000 Subject: [PATCH 04/17] feat(info): add `variable` to `ToolchainInfo` Allows redirecting the variable later, if needed. --- toolchain/info/rule.bzl | 1 + 1 file changed, 1 insertion(+) diff --git a/toolchain/info/rule.bzl b/toolchain/info/rule.bzl index 9cf58c6..fcc579f 100644 --- a/toolchain/info/rule.bzl +++ b/toolchain/info/rule.bzl @@ -76,6 +76,7 @@ def implementation(ctx): env = ctx.attr.target[RunEnvironmentInfo].environment toolchain = platform_common.ToolchainInfo( + variable = variable, variables = variables, default = ctx.attr.target[DefaultInfo], executable = ctx.executable.target, -- GitLab From dc423f6bf4b593ecd4f648815dae3ef0e9c0a308 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 15 Jan 2025 13:26:51 +0000 Subject: [PATCH 05/17] fix(resolved): correctly forward on providers The resolved rule incorrectly rebuilt and forwarded on providers in such a way that the variable was not able to be used in a `genrule` because runfiles were not correctly found. This patch resolves this issue by correctly constructing the providers. BREAKING CHANGE: The `resolved` repository rule has removed the `basename` attribute in favour or symlinking the binary directly. This change prevents unwanted symlink basename changes which can prevent multi-tool binaries from functioning correctly. To set the basename of a symlink use the `toolchain_symlink_target` rule. --- e2e/toolchain/runfiles/BUILD.bazel | 2 -- toolchain/resolved.bzl | 30 +++++++++-------------------- toolchain/resolved/BUILD.tmpl.bazel | 1 - toolchain/resolved/repository.bzl | 2 -- 4 files changed, 9 insertions(+), 26 deletions(-) diff --git a/e2e/toolchain/runfiles/BUILD.bazel b/e2e/toolchain/runfiles/BUILD.bazel index b1c102c..46fd03e 100644 --- a/e2e/toolchain/runfiles/BUILD.bazel +++ b/e2e/toolchain/runfiles/BUILD.bazel @@ -46,11 +46,9 @@ genrule( toolchains = [":resolved"], ) -# FIXME: this fails to find runfiles toolchain_test( name = "stdout", size = "small", stdout = ":stdout.log", - tags = ["manual"], toolchains = [":resolved"], ) diff --git a/toolchain/resolved.bzl b/toolchain/resolved.bzl index 6aa0042..bc1c619 100644 --- a/toolchain/resolved.bzl +++ b/toolchain/resolved.bzl @@ -14,51 +14,39 @@ PROVIDES = ( ) ATTRS = { - "basename": attr.string( - doc = "The basename for the symlink, which defaults to `name`", - ), "toolchain_type": attr.label( doc = "The toolchain type to resolve and forward on providers.", mandatory = True, ), - "_windows": attr.label( - providers = [platform_common.ConstraintValueInfo], - default = "//toolchain/constraint/os:windows", - ), } def implementation(ctx): - basename = ctx.attr.basename or ctx.label.name toolchain = ctx.toolchains[ctx.attr.toolchain_type.label] - windows = ctx.attr._windows[platform_common.ConstraintValueInfo] - - target = toolchain.executable - extension = target.extension - if extension in ("bat", "cmd"): - basename = "{}.{}".format(basename, extension) - elif not extension and "." not in basename and ctx.target_platform_has_constraint(windows): - basename = "{}.exe".format(basename) + basename = toolchain.executable.basename executable = ctx.actions.declare_file("{}/{}".format(ctx.label.name, basename)) ctx.actions.symlink( output = executable, - target_file = target, + target_file = toolchain.executable, is_executable = True, ) - files = depset([executable], transitive = [toolchain.default.files]) - runfiles = ctx.runfiles() + variables = platform_common.TemplateVariableInfo({ + toolchain.variable: executable.path, + }) + + runfiles = ctx.runfiles([executable, toolchain.executable]) runfiles = runfiles.merge(toolchain.default.default_runfiles) default = DefaultInfo( executable = executable, - files = files, + files = depset([executable]), runfiles = runfiles, ) return [ toolchain, - toolchain.variables, + variables, RunEnvironmentInfo(toolchain.env), default, ] diff --git a/toolchain/resolved/BUILD.tmpl.bazel b/toolchain/resolved/BUILD.tmpl.bazel index 65f2e05..28778b6 100644 --- a/toolchain/resolved/BUILD.tmpl.bazel +++ b/toolchain/resolved/BUILD.tmpl.bazel @@ -2,7 +2,6 @@ load("//:resolved.bzl", "resolved") resolved( name = "{{target}}", - basename = "{{basename}}", toolchain_type = "{{toolchain_type}}", visibility = ["//visibility:public"], ) diff --git a/toolchain/resolved/repository.bzl b/toolchain/resolved/repository.bzl index 02800e3..92b1a8a 100644 --- a/toolchain/resolved/repository.bzl +++ b/toolchain/resolved/repository.bzl @@ -35,10 +35,8 @@ ATTRS = _ATTRS | { def implementation(rctx): target = rctx.attr.target or rctx.attr.name.rsplit(SEPARATOR, 1)[1] - basename = rctx.attr.basename or target.removeprefix("resolved-") substitutions = { "{{toolchain_type}}": str(rctx.attr.toolchain_type), - "{{basename}}": basename, "{{target}}": target, } rctx.template("resolved.bzl", rctx.attr.resolved, substitutions, executable = False) -- GitLab From 4001e7d4fae977f98827df024ad9b4a9d8695fac Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 15 Jan 2025 13:06:01 +0000 Subject: [PATCH 06/17] fix(info): correct variable name The variable must point to an executable generated from this rule to work in `genrule`' This reverts commit f82eb1082dc5858231ab937d4cec4698678e5f6e. --- toolchain/info/rule.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toolchain/info/rule.bzl b/toolchain/info/rule.bzl index fcc579f..2038270 100644 --- a/toolchain/info/rule.bzl +++ b/toolchain/info/rule.bzl @@ -59,7 +59,7 @@ def implementation(ctx): ) variables = platform_common.TemplateVariableInfo({ - variable: ctx.executable.target.path, + variable: executable.path, }) runfiles = ctx.runfiles([executable, ctx.executable.target]) -- GitLab From 21edd3486770c124d51d3107b3095e2923f3bee1 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 15 Jan 2025 13:31:42 +0000 Subject: [PATCH 07/17] fix(info): remove `variables` from `ToolchainInfo` BREAKING CHANGE: `variables` is no longer available on the `ToolchainInfo` generated by `toolchain_info`. The `TemplateVariableInfo` is created in the scope of the `toolchain_info` rule. Using the provider in downstream rules that access the `ToolchainInfo` will no longer be relevant. A `ToolchainInfo#variable` attribute is provided to reconstruct a `TemplateVariableInfo` in the scope of a downstream rule. --- toolchain/info/rule.bzl | 1 - 1 file changed, 1 deletion(-) diff --git a/toolchain/info/rule.bzl b/toolchain/info/rule.bzl index 2038270..7dd61a1 100644 --- a/toolchain/info/rule.bzl +++ b/toolchain/info/rule.bzl @@ -77,7 +77,6 @@ def implementation(ctx): toolchain = platform_common.ToolchainInfo( variable = variable, - variables = variables, default = ctx.attr.target[DefaultInfo], executable = ctx.executable.target, run = ctx.attr.target.files_to_run or ctx.executable.target, -- GitLab From 1114069697bca434a6dc650fb936901c31c5c75f Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 15 Jan 2025 14:23:47 +0000 Subject: [PATCH 08/17] docs: update `rules_download` module name It is renamed to `download_utils` --- toolchain/info/rule.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toolchain/info/rule.bzl b/toolchain/info/rule.bzl index 7dd61a1..51979a7 100644 --- a/toolchain/info/rule.bzl +++ b/toolchain/info/rule.bzl @@ -31,7 +31,7 @@ toolchain_type( ] ``` -`rules_download` has a `download.archive` and `download.file` extension that can help with retrieving remote binaries. +`download_utils` has a `download.archive` and `download.file` extension that can help with retrieving remote binaries. """ ATTRS = { -- GitLab From 06e206176cac5836a774c78dfebb3cadceb55b9c Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 15 Jan 2025 15:02:26 +0000 Subject: [PATCH 09/17] fix(info): remove `ToolchainInfo#env` BREAKING CHANGE: `toolchain_info` removes `ToochainInfo#env`. Allowing access to the original `env` attribute of the `toolchain_info` target can result in unexpected errors. `env` is usual subject to Make variable and expand location expansion. The expansion is done in the scope of the original rule. Re-using the environment results in errors with miscalculated paths, etc. If it is required to capture the environment variables for a binary, it is much better to use a rule that bakes the environment variables into the execution of the binary. This can be done with launcher scripts that write out the environment variables for later use. --- toolchain/info/rule.bzl | 5 ----- toolchain/resolved.bzl | 1 - 2 files changed, 6 deletions(-) diff --git a/toolchain/info/rule.bzl b/toolchain/info/rule.bzl index 51979a7..19c6160 100644 --- a/toolchain/info/rule.bzl +++ b/toolchain/info/rule.bzl @@ -71,16 +71,11 @@ def implementation(ctx): runfiles = runfiles, ) - env = {} - if RunEnvironmentInfo in ctx.attr.target: - env = ctx.attr.target[RunEnvironmentInfo].environment - toolchain = platform_common.ToolchainInfo( variable = variable, default = ctx.attr.target[DefaultInfo], executable = ctx.executable.target, run = ctx.attr.target.files_to_run or ctx.executable.target, - env = env, ) return [variables, toolchain, default] diff --git a/toolchain/resolved.bzl b/toolchain/resolved.bzl index bc1c619..50106b6 100644 --- a/toolchain/resolved.bzl +++ b/toolchain/resolved.bzl @@ -47,7 +47,6 @@ def implementation(ctx): return [ toolchain, variables, - RunEnvironmentInfo(toolchain.env), default, ] -- GitLab From 7233458aafc9af37d49f195ec495b4e9ef078004 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 15 Jan 2025 17:22:31 +0000 Subject: [PATCH 10/17] test: executing Python binraries is borked ``` INFO: Analyzed target //toolchain/runfiles:binary (1 packages loaded, 39 targets configured). INFO: Found 1 target... Target //toolchain/runfiles:binary up-to-date: bazel-bin/toolchain/runfiles/binary.zip bazel-bin/toolchain/runfiles/binary.exe INFO: Elapsed time: 3.179s, Critical Path: 0.37s INFO: 3 processes: 11 action cache hit, 3 internal. INFO: Build completed successfully, 3 total actions INFO: Running command line: bazel-bin/toolchain/runfiles/binary.exe Traceback (most recent call last): File "C:\Users\matcla01\_bazel_matcla01\ki4xcn37\execroot\_main\bazel-out\x64_windows-fastbuild\bin\toolchain\runfiles\binary.zip\__main__.py", line 289, in main OSError: [WinError 1314] A required privilege is not held by the client: 'rules_python++python+python_3_11_x86_64-pc-windows-msvc/python.exe' -> 'C:\\Users\\matcla01\\AppData\\Local\\Temp\\Bazel.runfiles_8d64nm_h\\runfiles\\_main/toolchain/runfiles/_binary.venv/bin/python.exe' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 198, in _run_module_as_main File "", line 88, in _run_code File "C:\Users\matcla01\_bazel_matcla01\ki4xcn37\execroot\_main\bazel-out\x64_windows-fastbuild\bin\toolchain\runfiles\binary.zip\__main__.py", line 323, in File "C:\Users\matcla01\_bazel_matcla01\ki4xcn37\execroot\_main\bazel-out\x64_windows-fastbuild\bin\toolchain\runfiles\binary.zip\__main__.py", line 292, in main NameError: name 'PYTHON_BINARY_ACTUAL' is not defined. Did you mean: '_PYTHON_BINARY_ACTUAL'? ``` --- e2e/MODULE.bazel | 1 + e2e/toolchain/runfiles/BUILD.bazel | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/e2e/MODULE.bazel b/e2e/MODULE.bazel index b1b1c3d..1ba1a26 100644 --- a/e2e/MODULE.bazel +++ b/e2e/MODULE.bazel @@ -5,6 +5,7 @@ module( ], ) +bazel_dep(name = "platforms", version = "0.0.10") bazel_dep(name = "rules_go", version = "0.52.0") bazel_dep(name = "rules_python", version = "1.0.0") bazel_dep(name = "bazel_skylib", version = "1.4.2") diff --git a/e2e/toolchain/runfiles/BUILD.bazel b/e2e/toolchain/runfiles/BUILD.bazel index 46fd03e..c5810d2 100644 --- a/e2e/toolchain/runfiles/BUILD.bazel +++ b/e2e/toolchain/runfiles/BUILD.bazel @@ -12,6 +12,11 @@ py_binary( name = "binary", srcs = ["binary.py"], data = [":fixture.txt"], + # FIXME: `rules_python` binary building on Windows seems borked + target_compatible_with = select({ + "@platforms//os:windows": ["@platforms//:incompatible"], + "//conditions:default": [], + }), deps = ["@rules_python//python/runfiles"], ) -- GitLab From 44af1e8e25d3670445467aa0e911a51714f211b0 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 15 Jan 2025 17:26:23 +0000 Subject: [PATCH 11/17] test: make echo entry point compatible with everything but Windows --- e2e/toolchain/echo/BUILD.bazel | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/e2e/toolchain/echo/BUILD.bazel b/e2e/toolchain/echo/BUILD.bazel index bcf4380..682566c 100644 --- a/e2e/toolchain/echo/BUILD.bazel +++ b/e2e/toolchain/echo/BUILD.bazel @@ -43,8 +43,9 @@ toolchain_test( build_test( name = "entrypoint", size = "small", - target_compatible_with = [ - "@toolchain_utils//toolchain/constraint/os:linux", - ], + target_compatible_with = select({ + "@platforms//os:windows": ["@platforms//:incompatible"], + "//conditions:default": [], + }), targets = ["@which-echo//:entrypoint"], ) -- GitLab From 550d3bd63c99390d6630174bb1f7a87717a607bd Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 15 Jan 2025 17:27:02 +0000 Subject: [PATCH 12/17] test: use `cmd_{bash,bat}` in `genrule` to avoid Bash requirement on Windows --- e2e/toolchain/runfiles/BUILD.bazel | 3 ++- e2e/toolchain/symlink/BUILD.bazel | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/e2e/toolchain/runfiles/BUILD.bazel b/e2e/toolchain/runfiles/BUILD.bazel index c5810d2..6df2458 100644 --- a/e2e/toolchain/runfiles/BUILD.bazel +++ b/e2e/toolchain/runfiles/BUILD.bazel @@ -47,7 +47,8 @@ toolchain_test( genrule( name = "generate", outs = ["stdout.log"], - cmd = "$(BINARY) > $@", + cmd_bash = "$(BINARY) > $@", + cmd_bat = "$(BINARY) > $@", toolchains = [":resolved"], ) diff --git a/e2e/toolchain/symlink/BUILD.bazel b/e2e/toolchain/symlink/BUILD.bazel index 31763a0..8218313 100644 --- a/e2e/toolchain/symlink/BUILD.bazel +++ b/e2e/toolchain/symlink/BUILD.bazel @@ -47,7 +47,8 @@ toolchain_test( genrule( name = "generate", outs = ["stdout.log"], - cmd = "$(BINARY) > $@", + cmd_bash = "$(BINARY) > $@", + cmd_bat = "$(BINARY) > $@", toolchains = [":resolved"], ) -- GitLab From f406e6b17339d88274deaacc264d7c8ab0d46859 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 15 Jan 2025 17:27:20 +0000 Subject: [PATCH 13/17] test: make `genrule` manual targets Avoids building during `bazel build ...` --- e2e/toolchain/runfiles/BUILD.bazel | 1 + e2e/toolchain/symlink/BUILD.bazel | 1 + 2 files changed, 2 insertions(+) diff --git a/e2e/toolchain/runfiles/BUILD.bazel b/e2e/toolchain/runfiles/BUILD.bazel index 6df2458..d08d0d0 100644 --- a/e2e/toolchain/runfiles/BUILD.bazel +++ b/e2e/toolchain/runfiles/BUILD.bazel @@ -49,6 +49,7 @@ genrule( outs = ["stdout.log"], cmd_bash = "$(BINARY) > $@", cmd_bat = "$(BINARY) > $@", + tags = ["manual"], toolchains = [":resolved"], ) diff --git a/e2e/toolchain/symlink/BUILD.bazel b/e2e/toolchain/symlink/BUILD.bazel index 8218313..401a02e 100644 --- a/e2e/toolchain/symlink/BUILD.bazel +++ b/e2e/toolchain/symlink/BUILD.bazel @@ -49,6 +49,7 @@ genrule( outs = ["stdout.log"], cmd_bash = "$(BINARY) > $@", cmd_bat = "$(BINARY) > $@", + tags = ["manual"], toolchains = [":resolved"], ) -- GitLab From 6587ffdb79990b9f186612ae9fd9bff14b97c520 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 15 Jan 2025 17:28:12 +0000 Subject: [PATCH 14/17] fix(windows): add `.exe` extension to symlinks when necessary --- toolchain/symlink/path/rule.bzl | 2 +- toolchain/symlink/target/rule.bzl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/toolchain/symlink/path/rule.bzl b/toolchain/symlink/path/rule.bzl index d88297a..cee4030 100644 --- a/toolchain/symlink/path/rule.bzl +++ b/toolchain/symlink/path/rule.bzl @@ -60,7 +60,7 @@ def implementation(ctx): windows = ctx.attr._windows[platform_common.ConstraintValueInfo] _, extension = paths.split_extension(ctx.attr.path) - if extension in (".bat", ".cmd"): + if extension in (".bat", ".cmd", ".exe"): basename = basename + extension elif not extension and "." not in basename and ctx.target_platform_has_constraint(windows): basename = "{}.exe".format(basename) diff --git a/toolchain/symlink/target/rule.bzl b/toolchain/symlink/target/rule.bzl index 9c136ff..5416ffb 100644 --- a/toolchain/symlink/target/rule.bzl +++ b/toolchain/symlink/target/rule.bzl @@ -38,9 +38,9 @@ def implementation(ctx): target = ctx.executable.target extension = target.extension - if extension in (".bat", ".cmd"): + if extension in (".bat", ".cmd", ".exe"): basename = basename + extension - elif extension in ("bat", "cmd"): + elif extension in ("bat", "cmd", "exe"): basename = "{}.{}".format(basename, extension) elif not extension and "." not in basename and ctx.target_platform_has_constraint(windows): basename = "{}.exe".format(basename) -- GitLab From f2a8725acf98020af7f536a016048c98ecfcda26 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Thu, 16 Jan 2025 12:57:21 +0000 Subject: [PATCH 15/17] fix(info): default variable to binary basename BREAKING CHANGE: `toolchain_info#variable` now defaults to the basename of the target executable. Previously the variable defaulted to the `name` attribute: ```py go_binary( name = "abc, ) toolchain_info( name = "def", target = ":abc", # variable = "DEF", ) ``` Now the rule defaults the basename of the target executable: ```py go_binary( name = "abc, ) toolchain_info( name = "def", target = ":abc", # variable = "ABC", ) ``` In most situations, this is the prefered default. In cases where the name is the variable name wanted, the `variable` now needs to be added: ```py go_binary( name = "abc, ) toolchain_info( name = "def", target = ":abc", variable = "ABC", ) ``` --- toolchain/info/rule.bzl | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/toolchain/info/rule.bzl b/toolchain/info/rule.bzl index 19c6160..9b69141 100644 --- a/toolchain/info/rule.bzl +++ b/toolchain/info/rule.bzl @@ -43,13 +43,26 @@ ATTRS = { cfg = "exec", ), "variable": attr.string( - doc = "The variable name for Make or the execution environment. Defaults to `name.upper()`", + doc = "The variable name for Make or the execution environment. Defaults to a sanitized `target.basename.upper()`", ), } +def sanitize(value): + if value.endswith(".bat"): + value = value.removesuffix(".bat") + elif value.endswith(".cmd"): + value = value.removesuffix(".cmd") + elif value.endswith(".exe"): + value = value.removesuffix(".exe") + + sanitized = "".join([c if c.isalnum() else "_" for c in value.elems()]) + reduced = "_".join([c for c in sanitized.split("_") if c]) + + return reduced + def implementation(ctx): basename = ctx.executable.target.basename - variable = ctx.attr.variable or ctx.label.name.upper() + variable = ctx.attr.variable or sanitize(basename).upper() executable = ctx.actions.declare_file("{}/{}".format(ctx.label.name, basename)) ctx.actions.symlink( -- GitLab From 444e5add89cd49a1884c761a33d48f751d4d6fc2 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Thu, 16 Jan 2025 13:03:15 +0000 Subject: [PATCH 16/17] test(e2e): add test using `toolchain_info` in a `genrule` --- e2e/toolchain/runfiles/BUILD.bazel | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/e2e/toolchain/runfiles/BUILD.bazel b/e2e/toolchain/runfiles/BUILD.bazel index d08d0d0..ca44515 100644 --- a/e2e/toolchain/runfiles/BUILD.bazel +++ b/e2e/toolchain/runfiles/BUILD.bazel @@ -38,15 +38,15 @@ alias( ) toolchain_test( - name = "fixture", + name = "fixture-txt", size = "small", stdout = ":fixture.txt", toolchains = [":resolved"], ) genrule( - name = "generate", - outs = ["stdout.log"], + name = "generate-resolved", + outs = ["resolved.log"], cmd_bash = "$(BINARY) > $@", cmd_bat = "$(BINARY) > $@", tags = ["manual"], @@ -54,8 +54,24 @@ genrule( ) toolchain_test( - name = "stdout", + name = "resolved-log", size = "small", - stdout = ":stdout.log", + stdout = ":resolved.log", + toolchains = [":resolved"], +) + +genrule( + name = "generate-info", + outs = ["info.log"], + cmd_bash = "$(BINARY) > $@", + cmd_bat = "$(BINARY) > $@", + tags = ["manual"], + toolchains = [":info"], +) + +toolchain_test( + name = "info-log", + size = "small", + stdout = ":info.log", toolchains = [":resolved"], ) -- GitLab From d9c1b1536fe391399028b22ccc53eb42bc38b0ff Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Thu, 16 Jan 2025 13:07:18 +0000 Subject: [PATCH 17/17] test(e2e): default value for `ToolchainInfo#variable` test --- e2e/toolchain/symlink/BUILD.bazel | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/e2e/toolchain/symlink/BUILD.bazel b/e2e/toolchain/symlink/BUILD.bazel index 401a02e..138c03e 100644 --- a/e2e/toolchain/symlink/BUILD.bazel +++ b/e2e/toolchain/symlink/BUILD.bazel @@ -26,6 +26,11 @@ toolchain_info( variable = "BINARY", ) +toolchain_info( + name = "default", + target = ":basename", +) + toolchain( name = "hermetic", toolchain = ":info", @@ -38,15 +43,15 @@ alias( ) toolchain_test( - name = "fixture", + name = "fixture-txt", size = "small", stdout = ":fixture.txt", toolchains = [":resolved"], ) genrule( - name = "generate", - outs = ["stdout.log"], + name = "generate-resolved", + outs = ["generate.log"], cmd_bash = "$(BINARY) > $@", cmd_bat = "$(BINARY) > $@", tags = ["manual"], @@ -54,8 +59,24 @@ genrule( ) toolchain_test( - name = "stdout", + name = "generate-log", + size = "small", + stdout = ":generate.log", + toolchains = [":resolved"], +) + +genrule( + name = "generate-default", + outs = ["default.log"], + cmd_bash = "$(BASENAME) > $@", + cmd_bat = "$(BASENAME) > $@", + tags = ["manual"], + toolchains = [":default"], +) + +toolchain_test( + name = "default-log", size = "small", - stdout = ":stdout.log", + stdout = ":default.log", toolchains = [":resolved"], ) -- GitLab