feat: add shebang attribute on py_console_script_binary (#2867)

# Background
Use case: user is setting up the environment for a docker image, and
needs a bash executable from the py_console_script (e.g. to run `ray`
from command line without full bazel bootstrapping). User is responsible
of setting up the right paths (and hermeticity concerns). There's no
change in default behavior per this diff.

Previously, prior to Bazel mod, this was possible and simple through the
use of `rules_python_wheel_entry_points` ([per
here](https://github.com/bazel-contrib/rules_python/blob/9dfa3abba293488a9a1899832a340f7b44525cad/python/private/pypi/whl_library.bzl#L507))
but these are not reachable now via Bazel mod.

# Approach
Add a shebang attribute that allows users of the console binary to use
it like a binary executable.

This is similar to the functionality that came with wheel entry points
here:

https://github.com/bazel-contrib/rules_python/blob/9dfa3abba293488a9a1899832a340f7b44525cad/python/private/pypi/whl_library.bzl#L507

With this change, one can specify a shebang like:
```starlark
py_console_script_binary(
    name = "yamllint",
    pkg = "@pip//yamllint",
    shebang = "#!/usr/bin/env python3",
)
```

Summary:
- Update tests
- Add test for this functionality
- Leave default to without shebang so this is a non-breaking change
- Documentation (want to hear more about the general approach first, and
also want to hear whether this warrants specific docs, or can just leave
it to API docs)

---------

Co-authored-by: Ignas Anikevicius <240938+aignas@users.noreply.github.com>
5 files changed
tree: bbe54ba0ce14c1b0ea2636220c26b508e4ff1e6f
  1. .bazelci/
  2. .bcr/
  3. .ci/
  4. .github/
  5. docs/
  6. examples/
  7. gazelle/
  8. private/
  9. python/
  10. sphinxdocs/
  11. tests/
  12. third_party/
  13. tools/
  14. .bazelignore
  15. .bazelrc
  16. .bazelversion
  17. .editorconfig
  18. .git-blame-ignore-revs
  19. .gitattributes
  20. .gitignore
  21. .pre-commit-config.yaml
  22. .python-version
  23. .readthedocs.yml
  24. addlicense.sh
  25. AUTHORS
  26. BUILD.bazel
  27. BZLMOD_SUPPORT.md
  28. CHANGELOG.md
  29. CONTRIBUTING.md
  30. CONTRIBUTORS
  31. internal_dev_deps.bzl
  32. internal_dev_setup.bzl
  33. LICENSE
  34. MODULE.bazel
  35. README.md
  36. RELEASING.md
  37. version.bzl
  38. WORKSPACE
  39. WORKSPACE.bzlmod
README.md

Python Rules for Bazel

Build status

Overview

This repository is the home of the core Python rules -- py_library, py_binary, py_test, py_proto_library, and related symbols that provide the basis for Python support in Bazel. It also contains package installation rules for integrating with PyPI and other indices.

Documentation for rules_python is at https://rules-python.readthedocs.io and in the Bazel Build Encyclopedia.

Examples live in the examples directory.

The core rules are stable. Their implementation is subject to Bazel's backward compatibility policy. This repository aims to follow semantic versioning.

The Bazel community maintains this repository. Neither Google nor the Bazel team provides support for the code. However, this repository is part of the test suite used to vet new Bazel releases. See How to contribute page for information on our development workflow.

Documentation

For detailed documentation, see https://rules-python.readthedocs.io

Bzlmod support

  • Status: Beta
  • Full Feature Parity: No

See Bzlmod support for more details.