Skip to content

Commit 9904f73

Browse files
committed
Only unset HOME when running cmake as root
This hack is required to work around pytorch/test-infra#5091, which runs some CI jobs as root, which buck2 doesn't like. But we saw in #3502 that this can break things for some normal users. Reduce the blast radius of this hack, only modifying HOME when actually running as root.
1 parent 18dfd6a commit 9904f73

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

setup.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
# other computer software, distribute, and sublicense such enhancements or
4646
# derivative works thereof, in binary and source code form.
4747

48+
import contextlib
4849
import os
4950
import re
5051
import sys
@@ -377,6 +378,31 @@ def run(self):
377378
self.copy_file(src, dst, preserve_mode=False)
378379

379380

381+
class Buck2EnvironmentFixer(contextlib.AbstractContextManager):
382+
"""Removes HOME from the environment when running as root.
383+
384+
This script is sometimes run as root in docker containers. buck2 doesn't
385+
allow running as root unless $HOME is owned by root or is not set.
386+
387+
TODO(pytorch/test-infra#5091): Remove this once the CI jobs stop running as
388+
root.
389+
"""
390+
391+
def __init__(self):
392+
self.saved_env = {}
393+
394+
def __enter__(self):
395+
if os.geteuid() == 0 and "HOME" in os.environ:
396+
log.info("temporarily unsetting HOME while running as root")
397+
self.saved_env["HOME"] = os.environ.pop("HOME")
398+
return self
399+
400+
def __exit__(self, *args, **kwargs):
401+
if "HOME" in self.saved_env:
402+
log.info("restored HOME")
403+
os.environ["HOME"] = self.saved_env["HOME"]
404+
405+
380406
# TODO(dbort): For editable wheels, may need to update get_source_files(),
381407
# get_outputs(), and get_output_mapping() to satisfy
382408
# https://setuptools.pypa.io/en/latest/userguide/extension.html#setuptools.command.build.SubCommand.get_output_mapping
@@ -470,17 +496,13 @@ def run(self):
470496
if not self.dry_run:
471497
# Dry run should log the command but not actually run it.
472498
(Path(cmake_cache_dir) / "CMakeCache.txt").unlink(missing_ok=True)
473-
try:
474-
# This script is sometimes run as root in docker containers. buck2
475-
# doesn't allow running as root unless $HOME is owned by root or
476-
# does not exist. So temporarily undefine it while configuring
477-
# cmake, which runs buck2 to get some source lists.
478-
old_home = os.environ.pop("HOME", None)
499+
with Buck2EnvironmentFixer():
500+
# The context manager may patch the environment while running this
501+
# cmake command, which happens to run buck2 to get some source
502+
# lists.
503+
479504
# Generate the build system files.
480505
self.spawn(["cmake", "-S", repo_root, "-B", cmake_cache_dir, *cmake_args])
481-
finally:
482-
if old_home is not None:
483-
os.environ["HOME"] = old_home
484506

485507
# Build the system.
486508
self.spawn(["cmake", "--build", cmake_cache_dir, *build_args])

0 commit comments

Comments
 (0)