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

Document how to run tools on the command line #26

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,30 @@ Tools may then be accessed using `@multitool//tools/tool-name`.
### Workspace Usage

Instructions for using with WORKSPACE may be found in [release notes](https://github.com/theoremlp/rules_multitool/releases).

### Creating convenience scripts

When users need to execute tools directly, Bazel does not provide very good support for this,
because it sets the working directory to the root of the runfiles tree, which breaks the ability of the tool to resolve configuration files and other relative paths.

The typical workarounds are all bad:

1. Change all paths the tool interacts with to be absolute, so that the working directory is inconsequential.
2. Wrap the tool in a trivial `sh_binary` that can read `$BUILD_WORKING_DIRECTORY`.
3. Use `--run_under="cd $PWD &&` however this [discards the analysis cache] slowing down this and the subsequent build.

Instead, the authors recommend the following technique. Create a script like `tools/_multitool_run_under_cwd.sh` containing the following shell code:

```sh
#!/bin/bash
# Workaround https://github.com/bazelbuild/bazel/issues/3325
target="@multitool//tools/$(basename "$0")"
bazel build "$target" && exec $(bazel 2>/dev/null cquery --output=files "$target") "$@"
```

Now just create symlinks such as `tools/mytool -> ./_multitool_run_under_cwd.sh`.
This will build `@multitool//tools/mytool` and then execute the resulting binary in the current working directory.

Developers can now just naively run `./tools/mytool --arg my/file` and don't need to worry about where the tool comes from.

[discards the analysis cache]: https://github.com/bazelbuild/bazel/issues/10782
Loading