Skip to content

Conversation

@fmeum
Copy link
Collaborator

@fmeum fmeum commented Sep 7, 2023

Previously, Starlark-defined aspects-on-aspects saw their own attribute values but the type declared by the underlying aspect in case of an attribute name collision.

As a side effect of this change, native aspects-on-aspects now see their own attribute values instead of those of the underlying aspect.

Previously, Starlark-defined aspects-on-aspects saw their own attribute
values but the type declared by the underlying aspect in case of an
attribute name collision.

As a side effect of this change, native aspects-on-aspects now see their
own attribute values instead of those of the underlying aspect.
@fmeum fmeum changed the title Update ConfiguredTargetFactory.java Fix type checking for colliding aspect-on-aspect attributes Sep 10, 2023
@fmeum fmeum requested a review from mai93 September 10, 2023 09:15
@fmeum fmeum marked this pull request as ready for review September 10, 2023 09:15
@fmeum fmeum requested a review from a team as a code owner September 10, 2023 09:15
@fmeum fmeum requested review from aiuto and removed request for a team and aiuto September 10, 2023 09:15
@github-actions github-actions bot added awaiting-review PR is awaiting review from an assigned reviewer team-Configurability platforms, toolchains, cquery, select(), config transitions labels Sep 10, 2023
@fmeum
Copy link
Collaborator Author

fmeum commented Sep 10, 2023

@mai93 I dug deeper and found 399beac. This change doesn't seem to change any existing Starlark aspect-on-aspect logic that wasn't already broken due to type mismatches, only native aspects should be able to observe a difference in precedence.

@mai93
Copy link
Contributor

mai93 commented Sep 11, 2023

another case that will have a different behaviour with this change is for aspect propagating to its underlying aspect dependency:

## defs.bzl
prov_a = provider()
prov_b = provider()

def _aspect_a_impl(target, ctx):
  print('aspect_a on target {}'.format(target.label))
  if prov_b in target:
    print('\tcan see: ', target[prov_b].val)
  if hasattr(ctx.rule.attr, '_tool') and ctx.rule.attr._tool[prov_a]:
    print('\tcan see: ', ctx.rule.attr._tool[prov_a].val)
  if hasattr(ctx.rule.attr, 'dep') and ctx.rule.attr.dep and ctx.rule.attr.dep[prov_a]:
    print('\tcan see: ', ctx.rule.attr.dep[prov_a].val)
  return [prov_a(val='aspect_a on target {}'.format(target.label))]

aspect_a = aspect(
    implementation = _aspect_a_impl,
    attr_aspects = ['dep', '_tool'],
    required_aspect_providers = [prov_b],
    attrs = {
     '_tool' : attr.label(default = "//:tool_a"),
   },
)

def _aspect_b_impl(target, ctx):
  print('aspect_b on target {}'.format(target.label))
  return [prov_b(val='aspect_b on target {}'.format(target.label))]

aspect_b = aspect(
    implementation = _aspect_b_impl,
    attr_aspects = ['dep', '_tool'],
    attrs = {
     '_tool' : attr.label(default = "//:tool_b"),
    },
    provides = [prov_b],
)

def _rule_impl(ctx):
  pass

r1 = rule(
   implementation = _rule_impl,
   attrs = {
     'dep' : attr.label(aspects=[aspect_b, aspect_a]),
   },
)

r2 = rule(
   implementation = _rule_impl,
   attrs = {
     'dep' : attr.label(),
   },
)

## BUILD
load('defs.bzl', 'r1', 'r2')
r1(
  name = 't1',
  dep = ':t2',
)
r2(
  name = 't2',
  dep = ':t3',
)

r2(
  name = 't3',
)

sh_binary(name = "tool_a", srcs = ["tool.sh"])
sh_binary(name = "tool_b", srcs = ["tool.sh"])

currently when building t1, aspect_a will be applied to _tool_b and it can inspect the result of its application but with this change the value of ctx.rule.attr._tool in aspect_a will be //:tool_a instead of //:tool_b . So a proper separation between the aspects attributes is needed, if this is not blocking #19442 I think we can submit it and flip the incompatible_visibility_private_attributes_at_definition flag, I plan to work on the separation of the aspects implicit dependency.

@fmeum
Copy link
Collaborator Author

fmeum commented Sep 12, 2023

Thanks for the interesting example! If I understand this correctly, does it mean that ctx.rule contains attributes of underlying aspects in addition to those of the underlying rule? I wasn't aware of that being possible.

@mai93 #19442 does require the current PR. Would you like to block it on implementing proper separation or do you see that as an independent effort? I am fine with both, it's just that our window to flip the flag will close for a somewhat long time when we don't get it into 7.0.0. But the flag is also not blocking anything else, so we are in no particular hurry to flip it.

@mai93
Copy link
Contributor

mai93 commented Sep 13, 2023

does it mean that ctx.rule contains attributes of underlying aspects in addition to those of the underlying rule? I wasn't aware of that being possible.

yes, the attributes of the rule and the underlying aspects are grouped under ctx.rule.attr (code).

#19442 does require the current PR

I thought the current PR is only needed for testAspectImplicitDependencyCheckedAtDefinition_visibleWithNameCollision in #19442, if that's the case we can merge #19442 and mark testAspectImplicitDependencyCheckedAtDefinition_visibleWithNameCollision ignored until the problem with same name attributes is fixed.

@fmeum
Copy link
Collaborator Author

fmeum commented Nov 6, 2023

@mai93 Is this still relevant with your recent work?

@mai93
Copy link
Contributor

mai93 commented Nov 6, 2023

I think we can close this, the test testAspectImplicitDependencyCheckedAtDefinition_visibleWithNameCollision is no longer ignored after 755579d

@fmeum fmeum closed this Nov 6, 2023
@fmeum fmeum deleted the patch-7 branch November 6, 2023 21:57
@github-actions github-actions bot removed the awaiting-review PR is awaiting review from an assigned reviewer label Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team-Configurability platforms, toolchains, cquery, select(), config transitions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants