Repository: pantsbuild/pants

Issue:
Twine env vars not exposed properly
**Describe the bug**

Using the `TWINE_` env vars without a repository suffix does not work without explicitly adding them to the subprocess env using a configuration like:
```toml
[subprocess-environment]
env_vars.add = ["TWINE_PASSWORD", "TWINE_USERNAME"]
```

**Pants version**
`main`

**OS**
Any.

**Additional info**
As reported by @Kludex on Slack: https://pantsbuild.slack.com/archives/C0D7TNJHL/p1667741059650449



Discussion:

Resolving patch:
diff --git a/build-support/bin/check_inits.py b/build-support/bin/check_inits.py
--- a/build-support/bin/check_inits.py
+++ b/build-support/bin/check_inits.py
@@ -19,6 +19,7 @@
 def main() -> None:
     exclude_check = {
         "src/python/pants/__init__.py",
+        "src/python/pants/testutil/__init__.py",
     }
     files = itertools.chain.from_iterable(
         [Path().glob(f"{d}/**/__init__.py") for d in DIRS_TO_CHECK]
diff --git a/src/python/pants/backend/python/goals/publish.py b/src/python/pants/backend/python/goals/publish.py
--- a/src/python/pants/backend/python/goals/publish.py
+++ b/src/python/pants/backend/python/goals/publish.py
@@ -114,25 +114,21 @@ def twine_env_suffix(repo: str) -> str:
 
 def twine_env_request(repo: str) -> EnvironmentVarsRequest:
     suffix = twine_env_suffix(repo)
-    req = EnvironmentVarsRequest(
-        [
-            f"{var}{suffix}"
-            for var in [
-                "TWINE_USERNAME",
-                "TWINE_PASSWORD",
-                "TWINE_REPOSITORY_URL",
-            ]
-        ]
-    )
+    env_vars = [
+        "TWINE_USERNAME",
+        "TWINE_PASSWORD",
+        "TWINE_REPOSITORY_URL",
+    ]
+    req = EnvironmentVarsRequest(env_vars + [f"{var}{suffix}" for var in env_vars])
     return req
 
 
 def twine_env(env: EnvironmentVars, repo: str) -> EnvironmentVars:
     suffix = twine_env_suffix(repo)
-    if not suffix:
-        return env
-
-    return EnvironmentVars({key.rsplit(suffix, maxsplit=1)[0]: value for key, value in env.items()})
+    print(env)
+    return EnvironmentVars(
+        {key.rsplit(suffix, maxsplit=1)[0] if suffix else key: value for key, value in env.items()}
+    )
 
 
 @rule
diff --git a/src/python/pants/testutil/__init__.py b/src/python/pants/testutil/__init__.py
--- a/src/python/pants/testutil/__init__.py
+++ b/src/python/pants/testutil/__init__.py
@@ -0,0 +1,4 @@
+import pytest
+
+# See: https://docs.pytest.org/en/latest/how-to/writing_plugins.html#assertion-rewriting
+pytest.register_assert_rewrite("pants.testutil.pytest_util", "pants.testutil.process_util")


Test patch:
diff --git a/src/python/pants/backend/python/goals/publish_test.py b/src/python/pants/backend/python/goals/publish_test.py
--- a/src/python/pants/backend/python/goals/publish_test.py
+++ b/src/python/pants/backend/python/goals/publish_test.py
@@ -47,7 +47,11 @@ def set_options(rule_runner: RuleRunner, options: list | None = None) -> RuleRun
     rule_runner.set_options(
         options or [],
         env_inherit={"PATH", "PYENV_ROOT", "HOME"},
-        env={"TWINE_PASSWORD_PYPI": "secret"},
+        env={
+            "TWINE_USERNAME": "whoami",
+            "TWINE_USERNAME_PYPI": "whoareyou",
+            "TWINE_PASSWORD_PYPI": "secret",
+        },
     )
     return rule_runner
 
@@ -131,7 +135,7 @@ def test_twine_upload(rule_runner, packages) -> None:
                 "my-package-0.1.0.tar.gz",
                 "my_package-0.1.0-py3-none-any.whl",
             ),
-            env=FrozenDict({"TWINE_PASSWORD": "secret"}),
+            env=FrozenDict({"TWINE_USERNAME": "whoareyou", "TWINE_PASSWORD": "secret"}),
         ),
     )
     assert_package(
@@ -151,7 +155,7 @@ def test_twine_upload(rule_runner, packages) -> None:
                 "my-package-0.1.0.tar.gz",
                 "my_package-0.1.0-py3-none-any.whl",
             ),
-            env=FrozenDict(),
+            env=FrozenDict({"TWINE_USERNAME": "whoami"}),
         ),
     )