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

Require explicit environment usage #11641

Merged
merged 6 commits into from
Mar 9, 2021

Conversation

stuhood
Copy link
Sponsor Member

@stuhood stuhood commented Mar 5, 2021

Problem

In order to allow concurrent runs of Pants in #11639 (part of #7654), all global mutable singletons must be either 1) thread-local, 2) replaced with explicitly passed values. os.environ and sys.argv are two of the last cases we're concerned with.

Solution

Although we could hypothetically redefine os.environ and os.getenv as thread-local, they seem to be a better fit to be explicitly passed. So this change:

  1. Renames (and moves) PantsEnvironment to CompleteEnvironment
  2. Adds Environment and EnvironmentRequest, which filter the CompleteEnvironment to a smaller subset, and should be used more frequently by @rules.
  3. Fixes a few cases of implicit os.environ usage in the PythonSetup subsystem by explicitly selecting interpreter-selection env vars (HOME/PATH/PYENV_ROOT)

Finally, we empty the environment for pantsd runs to enforce explicit usage of CompleteEnvironment/Environment.

Result

The last (?) global mutable variable blocking #7654 is removed.

[ci skip-build-wheels]

…ronment or a new filtered Environment type.

[ci skip-build-wheels]

[ci skip-rust]
@stuhood stuhood marked this pull request as ready for review March 6, 2021 01:05
@stuhood
Copy link
Sponsor Member Author

stuhood commented Mar 6, 2021

Commits are useful to review independently.

Copy link
Contributor

@jsirois jsirois left a comment

Choose a reason for hiding this comment

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

Commits are useful to review independently.

I'm hoping to save you typing. I think its safe to assume any PR initially posted with multiple commits has this true. If not, it was a sloppy post.

@rule
async def complete_environment(session_values: SessionValues) -> CompleteEnvironment:
return session_values[CompleteEnvironment]


@rule
async def environment_subset(
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be a non async function which begs the question of why not just a utility. Are you making room here?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Good point: can drop the async. But the reason to expose this as a @rule is that we want to discourage direct access to the CompleteEnvironment and expose an API that encourages requesting a filtered copy, because it should allow @rules to be cleaned with early-cutoff in more cases. When a @rule requests the entire environment, it will be invalidated for any environment change.

Copy link
Contributor

@jsirois jsirois Mar 6, 2021

Choose a reason for hiding this comment

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

Could you s/encourage/force/ by just changing the relevant Process field type?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Possibly...? I think that a majority of users of Process right now are constructing an environment for the process by hand, and only some cases are pulling it in from the outside world. Most users of InteractiveProcess will use the CompleteEnvironment, but not all, etc.

Copy link
Contributor

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

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

Thanks! Looks great.

src/python/pants/engine/environment.py Show resolved Hide resolved
src/rust/engine/src/scheduler.rs Show resolved Hide resolved
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
…ronment as deprecated.

[ci skip-rust]

[ci skip-build-wheels]
[ci skip-rust]

[ci skip-build-wheels]
stuhood added a commit to stuhood/pants that referenced this pull request Mar 9, 2021
…nvironment usage to unblock pantsbuild#11641.

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
stuhood added a commit that referenced this pull request Mar 9, 2021
…nvironment usage to unblock #11641. (#11654)

Bump to version 0.7.0 of toolchain.pants.plugin to pull in explicit environment usage, which unblocks #11641.

[ci skip-rust]
@stuhood stuhood merged commit 73e97c2 into pantsbuild:master Mar 9, 2021
@stuhood stuhood deleted the stuhood/explicit-env branch March 9, 2021 04:31
stuhood added a commit that referenced this pull request Mar 12, 2021
…caching (#11678)

### Problem

#11641 changed how `ExecutionOptions` are computed for the "bootstrap" `Scheduler` that is used for plugin resolution (in order to allow any local-affecting options to still be used, while only disabling remote execution). But the `local_only` flag was sloppily applied, and did not affect the boolean flags that actually enable or disable remote caching and execution.

### Solution

Apply the `ExecutionOptions.from_options(local_only)` flag more thoroughly, so that both caching and execution are disabled (in addition to skipping the auth plugin).

### Result

Repositories that use auth plugins and remote execution will not experience errors during plugin bootstrapping.

[ci skip-build-wheels]
[ci skip-rust]
stuhood added a commit that referenced this pull request Apr 16, 2021
Since #11641, `sys.argv` has not been the right way to get the args for a particular run of pants.

Explicitly pass in the args via the `OptionsBootstrapper`, and fix the test (which was actually testing its _own_ `sys.argv`).

Fixes #11930.

[ci skip-rust]
[ci skip-build-wheels]
stuhood added a commit to stuhood/pants that referenced this pull request Apr 16, 2021
)

Since pantsbuild#11641, `sys.argv` has not been the right way to get the args for a particular run of pants.

Explicitly pass in the args via the `OptionsBootstrapper`, and fix the test (which was actually testing its _own_ `sys.argv`).

Fixes pantsbuild#11930.

[ci skip-rust]
[ci skip-build-wheels]
stuhood added a commit that referenced this pull request Apr 16, 2021
…11931) (#11932)

Since #11641, `sys.argv` has not been the right way to get the args for a particular run of pants.

Explicitly pass in the args via the `OptionsBootstrapper`, and fix the test (which was actually testing its _own_ `sys.argv`).

Fixes #11930.

[ci skip-rust]
[ci skip-build-wheels]
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

Successfully merging this pull request may close these issues.

3 participants