From 9c541799ffd48cf00b3bc4c6b79ce2e8ea1707a8 Mon Sep 17 00:00:00 2001 From: Eric Arellano Date: Fri, 16 Jul 2021 15:58:06 -0700 Subject: [PATCH] Revert "Prefix the entire setup.py chroot. (#12359)" This reverts commit 36026dfb509ecfac54da7425c331c9e51a5eb182. [ci skip-rust] [ci skip-build-wheels] --- .../pants/backend/python/goals/setup_py.py | 43 ++++++++----------- .../backend/python/goals/setup_py_test.py | 18 ++++---- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/python/pants/backend/python/goals/setup_py.py b/src/python/pants/backend/python/goals/setup_py.py index f7018d216c3..bc593fb7944 100644 --- a/src/python/pants/backend/python/goals/setup_py.py +++ b/src/python/pants/backend/python/goals/setup_py.py @@ -405,6 +405,10 @@ async def package_python_dist( return BuiltPackage(rel_chroot, (BuiltPackageArtifact(dirname),)) +# We write .py sources into the chroot under this dir. +CHROOT_SOURCE_ROOT = "src" + + SETUP_BOILERPLATE = """ # DO NOT EDIT THIS FILE -- AUTOGENERATED BY PANTS # Target: {target_address_spec} @@ -429,42 +433,23 @@ async def run_setup_py(req: RunSetupPyRequest, setuptools: Setuptools) -> RunSet interpreter_constraints=req.interpreter_constraints, ), ) - - # We prefix the entire chroot, and run with this prefix as the cwd, so that we can capture any - # changes setup made within it (e.g., when running 'develop') without also capturing other - # artifacts of the pex process invocation. - chroot_prefix = "chroot" - # The setuptools dist dir, created by it under the chroot (not to be confused with # pants's own dist dir, at the buildroot). - dist_dir = "dist" - - prefixed_chroot = await Get(Digest, AddPrefix(req.chroot.digest, chroot_prefix)) - - # setup.py basically always expects to be run with the cwd as its own directory - # (e.g., paths in it are relative to that directory). This is true of the setup.py - # we generate and is overwhelmingly likely to be true for existing setup.py files, - # as there is no robust way to run them otherwise. - setup_script_reldir, setup_script_name = os.path.split(req.chroot.setup_script) - working_directory = os.path.join(chroot_prefix, setup_script_reldir) - + dist_dir = "dist/" result = await Get( ProcessResult, VenvPexProcess( setuptools_pex, - argv=(setup_script_name, *req.args), - input_digest=prefixed_chroot, - working_directory=working_directory, + argv=(req.chroot.setup_script, *req.args), + input_digest=req.chroot.digest, # setuptools commands that create dists write them to the distdir. # TODO: Could there be other useful files to capture? - output_directories=(dist_dir,), # Relative to the working_directory. + output_directories=(dist_dir,), description=f"Run setuptools for {req.exported_target.target.address}", level=LogLevel.DEBUG, ), ) - output_digest = await Get( - Digest, RemovePrefix(result.output_digest, os.path.join(chroot_prefix, dist_dir)) - ) + output_digest = await Get(Digest, RemovePrefix(result.output_digest, dist_dir)) return RunSetupPyResult(output_digest) @@ -550,6 +535,7 @@ async def generate_chroot(request: SetupPyChrootRequest) -> SetupPyChroot: # specified `SetupKwargs(_allow_banned_keys=True)`. setup_kwargs.update( { + "package_dir": {"": CHROOT_SOURCE_ROOT, **setup_kwargs.get("package_dir", {})}, "packages": (*sources.packages, *(setup_kwargs.get("packages", []))), "namespace_packages": ( *sources.namespace_packages, @@ -609,8 +595,13 @@ async def generate_chroot(request: SetupPyChrootRequest) -> SetupPyChroot: FileContent("setup.py", setup_py_content), FileContent("MANIFEST.in", "include *.py".encode()), ] - extra_files_digest = await Get(Digest, CreateDigest(files_to_create)) - chroot_digest = await Get(Digest, MergeDigests((sources.digest, extra_files_digest))) + extra_files_digest, src_digest = await MultiGet( + Get(Digest, CreateDigest(files_to_create)), + # Nest the sources under the src/ prefix. + Get(Digest, AddPrefix(sources.digest, CHROOT_SOURCE_ROOT)), + ) + + chroot_digest = await Get(Digest, MergeDigests((src_digest, extra_files_digest))) return SetupPyChroot( chroot_digest, "setup.py", FinalizedSetupKwargs(setup_kwargs, address=target.address) ) diff --git a/src/python/pants/backend/python/goals/setup_py_test.py b/src/python/pants/backend/python/goals/setup_py_test.py index d3c17eec9bb..292df4debd3 100644 --- a/src/python/pants/backend/python/goals/setup_py_test.py +++ b/src/python/pants/backend/python/goals/setup_py_test.py @@ -285,13 +285,13 @@ def test_generate_chroot(chroot_rule_runner: RuleRunner) -> None: assert_chroot( chroot_rule_runner, [ - "files/README.txt", - "foo/qux/__init__.py", - "foo/qux/qux.py", - "foo/qux/qux.pyi", - "foo/resources/js/code.js", - "foo/__init__.py", - "foo/foo.py", + "src/files/README.txt", + "src/foo/qux/__init__.py", + "src/foo/qux/qux.py", + "src/foo/qux/qux.pyi", + "src/foo/resources/js/code.js", + "src/foo/__init__.py", + "src/foo/foo.py", "setup.py", "MANIFEST.in", ], @@ -300,6 +300,7 @@ def test_generate_chroot(chroot_rule_runner: RuleRunner) -> None: "name": "foo", "version": "1.2.3", "plugin_demo": "hello world", + "package_dir": {"": "src"}, "packages": ("foo", "foo.qux"), "namespace_packages": ("foo",), "package_data": {"foo": ("resources/js/code.js",)}, @@ -377,12 +378,13 @@ def test_binary_shorthand(chroot_rule_runner: RuleRunner) -> None: ) assert_chroot( chroot_rule_runner, - ["project/app.py", "setup.py", "MANIFEST.in"], + ["src/project/app.py", "setup.py", "MANIFEST.in"], "setup.py", { "name": "bin", "version": "1.1.1", "plugin_demo": "hello world", + "package_dir": {"": "src"}, "packages": ("project",), "namespace_packages": (), "install_requires": (),