-
-
Notifications
You must be signed in to change notification settings - Fork 652
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
Prefix the entire setup.py chroot. #12372
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -405,10 +405,6 @@ 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} | ||
|
@@ -433,22 +429,40 @@ 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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like we should be doing this more generally. Afaict no good ever comes of mixing binaries in with the sources they operate on at the top level in sandboxes. |
||
|
||
# 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/" | ||
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) | ||
|
||
result = await Get( | ||
ProcessResult, | ||
VenvPexProcess( | ||
setuptools_pex, | ||
argv=(req.chroot.setup_script, *req.args), | ||
input_digest=req.chroot.digest, | ||
argv=(setup_script_name, *req.args), | ||
input_digest=prefixed_chroot, | ||
working_directory=working_directory, | ||
# setuptools commands that create dists write them to the distdir. | ||
# TODO: Could there be other useful files to capture? | ||
output_directories=(dist_dir,), | ||
output_directories=(dist_dir,), # Relative to the working_directory. | ||
description=f"Run setuptools for {req.exported_target.target.address}", | ||
level=LogLevel.DEBUG, | ||
), | ||
) | ||
# Note that output_digest paths are relative to the working_directory. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is different from #12359 - the error there was that I tried to strip "chroot/dist" when it should have just been "dist" (turns out output files are not just specified relative to the working dir, but the output digest is as well). |
||
output_digest = await Get(Digest, RemovePrefix(result.output_digest, dist_dir)) | ||
return RunSetupPyResult(output_digest) | ||
|
||
|
@@ -535,7 +549,6 @@ 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, | ||
|
@@ -595,13 +608,8 @@ async def generate_chroot(request: SetupPyChrootRequest) -> SetupPyChroot: | |
FileContent("setup.py", setup_py_content), | ||
FileContent("MANIFEST.in", "include *.py".encode()), | ||
] | ||
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))) | ||
extra_files_digest = await Get(Digest, CreateDigest(files_to_create)) | ||
chroot_digest = await Get(Digest, MergeDigests((sources.digest, extra_files_digest))) | ||
return SetupPyChroot( | ||
chroot_digest, "setup.py", FinalizedSetupKwargs(setup_kwargs, address=target.address) | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,14 @@ | |
|
||
import pytest | ||
|
||
from pants.engine.fs import EMPTY_DIGEST, CreateDigest, Digest, DigestContents, FileContent | ||
from pants.engine.fs import ( | ||
EMPTY_DIGEST, | ||
CreateDigest, | ||
Digest, | ||
DigestContents, | ||
Directory, | ||
FileContent, | ||
) | ||
from pants.engine.internals.scheduler import ExecutionError | ||
from pants.engine.process import ( | ||
BinaryPathRequest, | ||
|
@@ -74,11 +81,24 @@ def test_env(rule_runner: RuleRunner) -> None: | |
assert b"VAR2=VAL" in result.stdout | ||
|
||
|
||
def test_output_digest(rule_runner: RuleRunner) -> None: | ||
@pytest.mark.parametrize("working_directory", ["", "subdir"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is different from #12359 - it tests that the behavior we rely on above (output digests are relative to the working directory) is in fact true. |
||
def test_output_digest(rule_runner: RuleRunner, working_directory) -> None: | ||
# Test that the output files are relative to the working directory, both in how | ||
# they're specified, and their paths in the output_digest. | ||
input_digest = ( | ||
rule_runner.request( | ||
Digest, | ||
[CreateDigest([Directory(working_directory)])], | ||
) | ||
if working_directory | ||
else EMPTY_DIGEST | ||
) | ||
process = Process( | ||
input_digest=input_digest, | ||
argv=("/bin/bash", "-c", "echo -n 'European Burmese' > roland"), | ||
description="echo roland", | ||
output_files=("roland",), | ||
working_directory=working_directory, | ||
) | ||
result = rule_runner.request(ProcessResult, [process]) | ||
assert result.output_digest == Digest( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is different from #12359