Skip to content

Commit d34f9f2

Browse files
dbortfacebook-github-bot
authored andcommitted
Only unset HOME when running cmake as root (#3507)
Summary: 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. Mitigates #3502 Pull Request resolved: #3507 Test Plan: `./install_requirements.sh` succeeded locally. The build-wheels jobs for this PR do not break during the buck2 phase, and show that they're unsetting HOME. https://github.com/pytorch/executorch/actions/runs/8946028682/job/24575999986?pr=3507#step:14:118 ``` 2024-05-03T23:32:08.9616508Z temporarily unsetting HOME while running as root ``` https://github.com/pytorch/executorch/actions/runs/8946028682/job/24575999986?pr=3507#step:14:465 ``` 2024-05-03T23:32:08.9914557Z restored HOME ``` Reviewed By: larryliu0820 Differential Revision: D56958571 Pulled By: dbort fbshipit-source-id: c7c6abdd52361af8253ce068002e3c23dee16f6b
1 parent 0909c5a commit d34f9f2

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
@@ -382,6 +383,31 @@ def run(self):
382383
self.copy_file(src, dst, preserve_mode=False)
383384

384385

386+
class Buck2EnvironmentFixer(contextlib.AbstractContextManager):
387+
"""Removes HOME from the environment when running as root.
388+
389+
This script is sometimes run as root in docker containers. buck2 doesn't
390+
allow running as root unless $HOME is owned by root or is not set.
391+
392+
TODO(pytorch/test-infra#5091): Remove this once the CI jobs stop running as
393+
root.
394+
"""
395+
396+
def __init__(self):
397+
self.saved_env = {}
398+
399+
def __enter__(self):
400+
if os.geteuid() == 0 and "HOME" in os.environ:
401+
log.info("temporarily unsetting HOME while running as root")
402+
self.saved_env["HOME"] = os.environ.pop("HOME")
403+
return self
404+
405+
def __exit__(self, *args, **kwargs):
406+
if "HOME" in self.saved_env:
407+
log.info("restored HOME")
408+
os.environ["HOME"] = self.saved_env["HOME"]
409+
410+
385411
# TODO(dbort): For editable wheels, may need to update get_source_files(),
386412
# get_outputs(), and get_output_mapping() to satisfy
387413
# https://setuptools.pypa.io/en/latest/userguide/extension.html#setuptools.command.build.SubCommand.get_output_mapping
@@ -490,17 +516,13 @@ def run(self):
490516
if not self.dry_run:
491517
# Dry run should log the command but not actually run it.
492518
(Path(cmake_cache_dir) / "CMakeCache.txt").unlink(missing_ok=True)
493-
try:
494-
# This script is sometimes run as root in docker containers. buck2
495-
# doesn't allow running as root unless $HOME is owned by root or
496-
# does not exist. So temporarily undefine it while configuring
497-
# cmake, which runs buck2 to get some source lists.
498-
old_home = os.environ.pop("HOME", None)
519+
with Buck2EnvironmentFixer():
520+
# The context manager may patch the environment while running this
521+
# cmake command, which happens to run buck2 to get some source
522+
# lists.
523+
499524
# Generate the build system files.
500525
self.spawn(["cmake", "-S", repo_root, "-B", cmake_cache_dir, *cmake_args])
501-
finally:
502-
if old_home is not None:
503-
os.environ["HOME"] = old_home
504526

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

0 commit comments

Comments
 (0)