Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using as a native_binary / native_test #53

Open
novas0x2a opened this issue Oct 2, 2024 · 2 comments
Open

Using as a native_binary / native_test #53

novas0x2a opened this issue Oct 2, 2024 · 2 comments

Comments

@novas0x2a
Copy link

novas0x2a commented Oct 2, 2024

This is honestly probably a doc suggestion, I think, though I'm not quite sure how to phrase it. If you want to use the multitool binary as a tool, i.e. as part of the build, it seems like:

  • If you want it to be safe under cross-compilation, you must use the :cwd target to ensure that there is a transition to cfg=exec (otherwise, when you run with --platforms <incompatible>, it'll download the wrong binary)
  • If you want it to work on windows, you have to use a file extension that matches the original one (in other words, .bat).

I think this means that this is exactly what you need to invoke (for, let's say, using ruff as a test):

load("@bazel_skylib//rules:native_binary.bzl", "native_test")

native_test(
    name = "ruff_lint",
    # use cwd to run the binary through a cfg=exec label, so things don't break when target != exec
    src = "@multitool//tools/ruff:cwd",
    # use .bat because unix doesn't care about the extension, but windows won't work otherwise
    out = "ruff_lint.bat", 
    args = [
        "--version",
    ],
    data = [
        "@multitool//tools/ruff:cwd",
    ]
)

I'm curious if you know if a better way to handle this.

@mark-thm
Copy link
Contributor

mark-thm commented Oct 2, 2024

When using multitool binaries in our builds we typically make a custom rule where we can specify the configuration.

Our ruff wrapper is an aspect that sets cfg=“exec”, for instance. The specifics around producing a .bat file is generally true of windows and not something specific to the executables we make here.

Absent a custom rule, I think you need a transition, but frankly I’m not quite literate enough to give you an example.

@novas0x2a
Copy link
Author

I was largely trying to avoid needing a custom rule to handle it, instead trying to make it work with just a custom macro to avoid needing to reimplement stuff like native_test. I mostly mention this as a doc fix because it took me a few (distracted) hours to figure out what was happening in our windows build.

No additional transition is needed if you use the :cwd target, because the transition is built into that target already. (The thing I put into the description is a complete, working example if all you want to do is invoke ruff --version. Obviously, if you want to run ruff check with a custom config, you need a few more things)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants