Skip to content

Commit

Permalink
Do not choke on missing lockfiles. (pantsbuild#18940)
Browse files Browse the repository at this point in the history
Fixes pantsbuild#18933 (and pantsbuild#18404). Original attempt at fixing this but failed
was in pantsbuild#18406 due to an oversight that it was the `_lockfiles` target
generator with a `sources` field that was being synthesized, not the
`_lockfile` target with a single `source` file only.

Either use should work equally well, and now they do.
  • Loading branch information
kaos authored May 9, 2023
1 parent 3f7bea4 commit fa7ea81
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/python/pants/core/target_types.py
Original file line number Diff line number Diff line change
@@ -903,8 +903,17 @@ class LockfileTarget(Target):


class LockfilesGeneratorSourcesField(MultipleSourcesField):
"""Sources field for synthesized `_lockfiles` targets.
It is special in that it always ignores any missing files, regardless of the global
`--unmatched-build-file-globs` option.
"""

help = generate_multiple_sources_field_help_message("Example: `sources=['example.lock']`")

def path_globs(self, unmatched_build_file_globs: UnmatchedBuildFileGlobs) -> PathGlobs: # type: ignore[misc]
return super().path_globs(UnmatchedBuildFileGlobs.ignore())


class LockfilesGeneratorTarget(TargetFilesGenerator):
alias = "_lockfiles"
18 changes: 18 additions & 0 deletions src/python/pants/core/target_types_test.py
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@
FilesGeneratorTarget,
FileSourceField,
FileTarget,
LockfilesGeneratorSourcesField,
LockfileSourceField,
RelocatedFiles,
RelocateFilesViaCodegenRequest,
@@ -448,3 +449,20 @@ def test_lockfile_glob_match_error_behavior(
UnmatchedBuildFileGlobs(error_behavior)
).glob_match_error_behavior
)


@pytest.mark.parametrize(
"error_behavior", [GlobMatchErrorBehavior.warn, GlobMatchErrorBehavior.error]
)
def test_lockfiles_glob_match_error_behavior(
error_behavior: GlobMatchErrorBehavior,
) -> None:
lockfile_sources = LockfilesGeneratorSourcesField(
["test.lock"], Address("", target_name="lockfiles-test")
)
assert (
GlobMatchErrorBehavior.ignore
== lockfile_sources.path_globs(
UnmatchedBuildFileGlobs(error_behavior)
).glob_match_error_behavior
)

0 comments on commit fa7ea81

Please sign in to comment.