bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
23.21k stars 4.06k forks source link

Inconsistency between sh_binary docs and implementation #20345

Open chrisbrown2050 opened 11 months ago

chrisbrown2050 commented 11 months ago

The sh_binary docs say:

We recommend that you name your sh_binary() rules after the name of the script minus the extension (e.g. .sh); the rule name and the file name must be distinct

But on windows, if the sh_binary is written without extension, this error is thrown:

throw ruleContext.throwWithRuleError(
            "Source file is a Windows executable file,"
                + " target name extension should match source file extension");

https://github.com/bazelbuild/bazel/blob/b074ddb86eb0cdb7ca96110d6c3c8b602dde08d8/src/main/java/com/google/devtools/build/lib/bazel/rules/sh/ShBinary.java#L132C27-L132C27

This makes it impossible to declare sh_binaries that wrap pre-compiled windows targets and use them in rules or genrules.

Errors are produced both when an extension is used in target name:

sh_binary(
    name = "mico-cpp.exe",
    srcs = ["mico-cpp.exe"],
)
used in rule attr:
        "_mico_cpp": attr.label(default = Label("@MICO//:mico-cpp.exe"), cfg = "host", executable=True),
results in:
ERROR: D:/udu/b/3dsz6nhl/external/MICO/BUILD.bazel:8:10: in sh_binary rule @MICO//:mico-cpp.exe: cycle in dependency graph:
    //src/server/icctskel:iccsadmin (9a08aa5e3d90fb8851bc398dec9ff1911a24c740b15279b4060412bc62040da3)
.-> @MICO//:mico-cpp.exe (929e5caa53c9bca4d9772686a4099333b8897f5d50a002376b67885bbefaa012) [self-edge]

And without extension:

sh_binary(
    name = "mico-cpp",
    srcs = ["mico-cpp.exe"],
)
used in rule attr:
        "_mico_cpp": attr.label(default = Label("@MICO//:mico-cpp"), cfg = "host", executable=True),
gives this:
ERROR: D:/udu/b/3dsz6nhl/external/MICO/BUILD.bazel:8:10: in sh_binary rule @MICO//:mico-cpp: Source file is a Windows executable file, target name extension should match source file extension
iancha1992 commented 11 months ago

@chrisbrown2050 What is the bazel version that you're using?

peakschris commented 6 months ago

sorry for the delay, new github ID.

We are using bazel 7.1.1, and the error is still emitted. This is causing us significant complexity in our cross-platform build, because we need to use different tool labels on windows vs linux.

sh_binary(
    name = "flatc",
    srcs = ["bin/flatc.exe"],
    visibility = ["//visibility:public"],
)
ERROR: D:/udu/b/kjvohmfq/external/flatbuffers/BUILD.bazel:37:10: in sh_binary rule @@flatbuffers//:flatc: Source file is a Windows executable file, target name extension should match source file extension

The desired fix is to change the implementation to match documentation; relax the check in sh_binary so that the label can be the executable name minus the '.exe'