Skip to content

Commit

Permalink
refactor pex files warning
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd committed Mar 30, 2023
1 parent 0805da8 commit a3cb6fe
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions src/python/pants/backend/python/goals/package_pex_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ class PexBinaryFieldSet(PackageFieldSet, RunFieldSet):

required_fields = (PexEntryPointField,)

dependencies: PexBinaryDependenciesField

entry_point: PexEntryPointField
script: PexScriptField
args: PexArgsField
Expand Down Expand Up @@ -130,41 +128,37 @@ async def package_pex_binary(
ResolvedPexEntryPoint, ResolvePexEntryPointRequest(field_set.entry_point)
)

if field_set.include_sources.value:
# If sources are included, then we need to check dependencies for `files` targets.
if not field_set.include_sources.value:
resolved_entry_point = await resolved_pex_entry_point_get
else:
resolved_entry_point, transitive_targets = await MultiGet(
resolved_pex_entry_point_get,
Get(TransitiveTargets, TransitiveTargetsRequest([field_set.address])),
)
dep_targets = tuple(transitive_targets.dependencies)
depends = "transitively depends"
else:
resolved_entry_point, dependencies_targets = await MultiGet(
resolved_pex_entry_point_get,
Get(Targets, DependenciesRequest(field_set.dependencies)),

# Warn if users depend on `files` targets, which won't be included in the PEX and is a common
# gotcha.
file_tgts = targets_with_sources_types(
[FileSourceField], transitive_targets.dependencies, union_membership
)
dep_targets = tuple(dependencies_targets)
depends = "depends"

# Warn if users depend on `files` targets, which won't be included in the PEX and is a common
# gotcha.
file_tgts = targets_with_sources_types([FileSourceField], dep_targets, union_membership)
if file_tgts:
files_addresses = sorted(tgt.address.spec for tgt in file_tgts)
logger.warning(
softwrap(
f"""
The `pex_binary` target {field_set.address} {depends} on the below `files`
targets, but Pants will not include them in the PEX. Filesystem APIs like `open()`
are not able to load files within the binary itself; instead, they read from the
current working directory.
Instead, use `resources` targets or wrap this `pex_binary` in an `archive`.
See {doc_url('resources')}.
Files targets dependencies: {files_addresses}
"""
if file_tgts:
files_addresses = sorted(tgt.address.spec for tgt in file_tgts)
logger.warning(
softwrap(
f"""
The `pex_binary` target {field_set.address} transitively depends on the below `files`
targets, but Pants will not include them in the PEX. Filesystem APIs like `open()`
are not able to load files within the binary itself; instead, they read from the
current working directory.
Instead, use `resources` targets or wrap this `pex_binary` in an `archive`.
See {doc_url('resources')}.
Files targets dependencies: {files_addresses}
"""
)
)
)

output_filename = field_set.output_path.value_or_default(file_ending="pex")

Expand Down

0 comments on commit a3cb6fe

Please sign in to comment.