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

Updates publish to USES_ENVIRONMENTS #17313

Merged
merged 4 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions src/python/pants/core/goals/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ def value_or_default(self, *, file_ending: str | None) -> str:
return os.path.join(self.address.spec_path.replace(os.sep, "."), file_name)


@dataclass(frozen=True)
class EnvironmentAwarePackageRequest:
"""Request class to request a `BuiltPackage` in an environment-aware fashion."""

field_set: PackageFieldSet


@rule
async def environment_aware_package(request: EnvironmentAwarePackageRequest) -> BuiltPackage:
environment_name = await Get(
EnvironmentName,
EnvironmentNameRequest,
EnvironmentNameRequest.from_field_set(request.field_set),
)
package = await Get(
BuiltPackage, {request.field_set: PackageFieldSet, environment_name: EnvironmentName}
)
return package


class PackageSubsystem(GoalSubsystem):
name = "package"
help = "Create a distributable package."
Expand Down Expand Up @@ -134,19 +154,9 @@ async def package_asset(workspace: Workspace, dist_dir: DistDir) -> Package:
if not target_roots_to_field_sets.field_sets:
return Package(exit_code=0)

environment_names_per_field_set = await MultiGet(
Get(
EnvironmentName,
EnvironmentNameRequest,
EnvironmentNameRequest.from_field_set(field_set),
)
for field_set in target_roots_to_field_sets.field_sets
)
packages = await MultiGet(
Get(BuiltPackage, {field_set: PackageFieldSet, environment_name: EnvironmentName})
for field_set, environment_name in zip(
target_roots_to_field_sets.field_sets, environment_names_per_field_set
)
Get(BuiltPackage, EnvironmentAwarePackageRequest(field_set))
for field_set in target_roots_to_field_sets.field_sets
)

merged_digest = await Get(Digest, MergeDigests(pkg.digest for pkg in packages))
Expand Down
28 changes: 19 additions & 9 deletions src/python/pants/core/goals/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ async def publish_example(request: PublishToMyRepoRequest, ...) -> PublishProces

from typing_extensions import final

from pants.core.goals.package import BuiltPackage, PackageFieldSet
from pants.core.goals.package import BuiltPackage, EnvironmentAwarePackageRequest, PackageFieldSet
from pants.engine.addresses import Address
from pants.engine.collection import Collection
from pants.engine.console import Console
from pants.engine.environment import EnvironmentName
from pants.engine.environment import ChosenLocalEnvironmentName, EnvironmentName
from pants.engine.goal import Goal, GoalSubsystem
from pants.engine.process import InteractiveProcess, InteractiveProcessResult
from pants.engine.rules import Effect, Get, MultiGet, collect_rules, goal_rule, rule
Expand Down Expand Up @@ -180,11 +180,13 @@ def activated(cls, union_membership: UnionMembership) -> bool:

class Publish(Goal):
subsystem_cls = PublishSubsystem
environment_behavior = Goal.EnvironmentBehavior.LOCAL_ONLY # TODO(#17129) — Migrate this.
environment_behavior = Goal.EnvironmentBehavior.USES_ENVIRONMENTS


@goal_rule
async def run_publish(console: Console, publish: PublishSubsystem) -> Publish:
async def run_publish(
console: Console, publish: PublishSubsystem, local_environment: ChosenLocalEnvironmentName
) -> Publish:
target_roots_to_package_field_sets, target_roots_to_publish_field_sets = await MultiGet(
Get(
TargetRootsToFieldSets,
Expand Down Expand Up @@ -242,7 +244,10 @@ async def run_publish(console: Console, publish: PublishSubsystem) -> Publish:
continue

logger.debug(f"Execute {pub.process}")
res = await Effect(InteractiveProcessResult, InteractiveProcess, pub.process)
res = await Effect(
InteractiveProcessResult,
{pub.process: InteractiveProcess, local_environment.val: EnvironmentName},
)
Comment on lines +247 to +250
Copy link
Member

@stuhood stuhood Oct 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's likely that some publish implementations will need to pin the construction of the actual publish command to use back to the local environment... because otherwise they might consume environment variables / etc to construct it "for" a remote environment?

i.e.: Do packaging/etc in some environment, but then construct a publish command for the local environment.

I'm not sure if that suggests that they should each need to figure out their own environment internally instead... but ... maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that what's going on here? The packaging request is using the environment that's needed to successfully construct the package, but the publish command is run locally.

if res.exit_code == 0:
sigil = console.sigil_succeeded()
status = "published"
Expand Down Expand Up @@ -305,9 +310,12 @@ def default(self, o):


@rule
async def package_for_publish(request: PublishProcessesRequest) -> PublishProcesses:
async def package_for_publish(
request: PublishProcessesRequest, local_environment: ChosenLocalEnvironmentName
) -> PublishProcesses:
packages = await MultiGet(
Get(BuiltPackage, PackageFieldSet, field_set) for field_set in request.package_field_sets
Get(BuiltPackage, EnvironmentAwarePackageRequest(field_set))
for field_set in request.package_field_sets
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stuhood this is where the package operation is potentially performed in a different environment.

)

for pkg in packages:
Expand All @@ -320,8 +328,10 @@ async def package_for_publish(request: PublishProcessesRequest) -> PublishProces
publish = await MultiGet(
Get(
PublishProcesses,
PublishRequest,
field_set._request(packages),
{
field_set._request(packages): PublishRequest,
local_environment.val: EnvironmentName,
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stuhood This is where the publish processes are constructed, pinned to the local environment.

Copy link
Member

@stuhood stuhood Oct 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, you're right! I forgot that PublishProcesses doesn't actually do any of the building, and that that happens via BuiltPackage.

)
for field_set in request.publish_field_sets
)
Expand Down