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

Reimplement the pants_bin_name functionality in Rust. #21872

Merged
merged 2 commits into from
Jan 23, 2025

Conversation

benjyw
Copy link
Contributor

@benjyw benjyw commented Jan 23, 2025

This feature allows users to set a custom binary name
for use in help messages and documentation. So that
if their org has a custom wrapper script or binary,
we can direct users to use that.

Reimplementing this in Rust allows us to further
simplify OptionsBootstrapper.

Note that the old implementation relied on setting
an env var on the current process by manipulating
os.environ. This served two purposes:

  1. Communicating the value within the process, and
  2. Communicating the value from the client to pantsd,
    since the latter inherits the environment of the former.

( 2) is important because pantsd uses bin_name() at
import time, and options (including the pants_bin_name
option) may not be available yet. )

But - setting an environment variable on the current
process is not thread-safe on POSIX, and there is no
way to make it so. Therefore we change the implementation
to use a static Mutex for 1), and an explicit pass-through
of that value when spawning pantsd for 2).

@benjyw benjyw added the category:internal CI, fixes for not-yet-released features, etc. label Jan 23, 2025
@benjyw benjyw requested review from tdyas and huonw January 23, 2025 02:27
Comment on lines +82 to +84
// Fortunately, this only applies in pantsd; The native client does not have this problem because
// it parses options without registration. So we use the __PANTS_BIN_NAME env var to propagate
// this value from the native client to its spawned pantsd process (see pants_daemon_client.py).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, just confirming my understanding, the initialisation process looks something like:

  1. the native client starts up, and parses options
  2. parsing these options includes detecting the https://www.pantsbuild.org/stable/reference/global-options#pants_bin_name value (if set) and setting the static
  3. pants_daemon_client.py then reads that option value via the static...
  4. ... to propagate into pantsd via the env var

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly right. This is in fact how it always worked, but the implementation obscured this. At a casual glance you could think that the daemon was reading the value and setting the env var. And this did happen, but too late. The reason it worked in practice was that the client read it first and then the spawned server received it via env var.

Copy link
Contributor Author

@benjyw benjyw Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and I should mention that all this is tested (both with and without pantsd) in tests/python/pants_test/integration/bin_name_integration_test.py

@benjyw benjyw merged commit 2c16710 into pantsbuild:main Jan 23, 2025
24 checks passed
@benjyw benjyw deleted the reimplement_bin_name branch January 23, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:internal CI, fixes for not-yet-released features, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants