Skip to content
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

Update the error/warning messages for stale/invalid lockfiles #12618

Merged
Merged
45 changes: 27 additions & 18 deletions src/python/pants/backend/python/util_rules/pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,15 +436,7 @@ async def build_pex(

requirements_file_digest_contents = await Get(DigestContents, PathGlobs, globs)
metadata = LockfileMetadata.from_lockfile(requirements_file_digest_contents[0].content)
if not metadata.is_valid_for(
request.requirements.lockfile_hex_digest,
request.interpreter_constraints,
python_setup.interpreter_universe,
):
if python_setup.invalid_lockfile_behavior == InvalidLockfileBehavior.error:
raise ValueError("Invalid lockfile provided. [TODO(#12314): Improve message]")
elif python_setup.invalid_lockfile_behavior == InvalidLockfileBehavior.warn:
logger.warning("%s", "Invalid lockfile provided. [TODO(#12314): Improve message]")
_validate_metadata(metadata, request, python_setup)

requirements_file_digest = await Get(Digest, PathGlobs, globs)

Expand All @@ -453,15 +445,7 @@ async def build_pex(
argv.extend(["--requirement", file_content.path])

metadata = LockfileMetadata.from_lockfile(file_content.content)
if not metadata.is_valid_for(
request.requirements.lockfile_hex_digest,
request.interpreter_constraints,
python_setup.interpreter_universe,
):
if python_setup.invalid_lockfile_behavior == InvalidLockfileBehavior.error:
raise ValueError("Invalid lockfile provided. [TODO(#12314): Improve message]")
elif python_setup.invalid_lockfile_behavior == InvalidLockfileBehavior.warn:
logger.warning("%s", "Invalid lockfile provided. [TODO(#12314): Improve message]")
_validate_metadata(metadata, request, python_setup)

requirements_file_digest = await Get(Digest, CreateDigest([file_content]))
else:
Expand Down Expand Up @@ -515,6 +499,31 @@ async def build_pex(
)


def _validate_metadata(
metadata: LockfileMetadata, request: PexRequest, python_setup: PythonSetup
) -> None:
if metadata.is_valid_for(
request.requirements.lockfile_hex_digest,
request.interpreter_constraints,
python_setup.interpreter_universe,
):
return None

message = (
f"Invalid lockfile for PEX request `{request.output_filename}`."
"\n\n"
"If your requirements or your project's interpreter constraints have changed since the "
"lockfile was generated, you can follow the instructions in the header of the lockfile to "
"regenerate it. Otherwise, ensure your interpreter constraints setting is compatible with "
"the constraints specified in the lockfile."
)
Eric-Arellano marked this conversation as resolved.
Show resolved Hide resolved

if python_setup.invalid_lockfile_behavior == InvalidLockfileBehavior.error:
raise ValueError(message)
elif python_setup.invalid_lockfile_behavior == InvalidLockfileBehavior.warn:
logger.warning("%s", message)


def _build_pex_description(request: PexRequest) -> str:
if request.description:
return request.description
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/backend/python/util_rules/pex_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ def test_error_on_invalid_lockfile_with_path(rule_runner: RuleRunner) -> None:

def test_warn_on_invalid_lockfile_with_path(rule_runner: RuleRunner, caplog) -> None:
_run_pex_for_lockfile_test(rule_runner, True, actual="1bad", expected="900d", behavior="warn")
assert "Invalid lockfile provided." in caplog.text
assert "Invalid lockfile for PEX request" in caplog.text


def test_ignore_on_invalid_lockfile_with_path(rule_runner: RuleRunner, caplog) -> None:
Expand All @@ -569,7 +569,7 @@ def test_error_on_invalid_lockfile_with_content(rule_runner: RuleRunner) -> None

def test_warn_on_invalid_lockfile_with_content(rule_runner: RuleRunner, caplog) -> None:
_run_pex_for_lockfile_test(rule_runner, False, actual="1bad", expected="900d", behavior="warn")
assert "Invalid lockfile provided." in caplog.text
assert "Invalid lockfile for PEX request" in caplog.text


def test_no_warning_on_valid_lockfile_with_content(rule_runner: RuleRunner, caplog) -> None:
Expand Down