Skip to content

--incompatible_existing_rules_immutable_view breaks json encoding of existing_rule/s objects #16256

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

Closed
tetromino opened this issue Sep 12, 2022 · 7 comments
Assignees
Labels
P1 I'll work on this now. (Assignee required) type: bug

Comments

@tetromino
Copy link
Contributor

Originally posted by @dws in #13907 (comment)

If --incompatible_existing_rules_immutable_view is set, json.encode (or encode_indent) will encode an immutable native.existing_rule or existing_rules object as an array of field names, instead of a dict (because our DictLikeView object is a StarlarkIterable).

proto.encode_text is worse: it will fail with Error in encode_text: [...] got ExistingRulesView, want string, int, bool, or struct when trying to encode a struct one of whose fields has an existing_rule object as value.

Although there is a workaround (encode dict(native.existing_rule("foo").items())), this is obviously a regression compared to the situation before the flag flip.

@tetromino tetromino added type: bug P2 We'll consider working on this in future. (Assignee optional) team-Build-Language labels Sep 12, 2022
@tetromino tetromino self-assigned this Sep 12, 2022
@tetromino
Copy link
Contributor Author

I think a solution would have the following shape:

An alternative would be for DictLikeView to implement Json.Encodable (whose interface we'd need to change a bit), and the corresponding ProtoModule.Encodable (which we'd need to add) - but IMHO that would be quite ugly. Especially the duplication between the two encodable interfaces.

@tetromino
Copy link
Contributor Author

@brandjon fyi

@tetromino tetromino added this to the 6.0.0 release blockers milestone Sep 13, 2022
@tetromino tetromino added P1 I'll work on this now. (Assignee required) and removed P2 We'll consider working on this in future. (Assignee optional) labels Sep 13, 2022
@tetromino
Copy link
Contributor Author

After looking at this a bit, proto.encode_text cannot reasonably support the output of native.existing_rule(): the encoder cannot produce any reasonable schema for a dict (or dict-like entity) whose values may be lists/tuples, as opposed to scalars. Which is a reasonable limitation, considering the proto format.

We do still want to fix json encoding, however.

@tetromino tetromino changed the title --incompatible_existing_rules_immutable_view breaks json and proto encoding of existing_rule/s objects --incompatible_existing_rules_immutable_view breaks json encoding of existing_rule/s objects Sep 15, 2022
@meteorcloudy
Copy link
Member

@comius Is this something you can look into? This is also blocking the 6.0 release.

@tetromino
Copy link
Contributor Author

@meteorcloudy - I have a fix in review internally

@meteorcloudy
Copy link
Member

Bazel 6.0 cut is scheduled on Oct 10th, do you think we can fix this in time?

@tetromino
Copy link
Contributor Author

I hope to submit the fix this week

aiuto pushed a commit to aiuto/bazel that referenced this issue Oct 12, 2022
…utable_view to be encoded as json and passed as **kwargs

To do so, we make ExistingRuleView and ExistingRulesView implement Map. This
requires getting rid of NotRepresentableException in starlarkifyValue (see
discussion in bazelbuild#13829) so that the view object's accessors don't throw. And
that is, arguably, the correct thing to do: the distinction between returning
null for some non-representable attributes vs. throwing an exception for others
was entirely artificial.

This in turn implies a small change in behavior: now, `existing_rule(x).keys()`
will omit all attributes whose values cannot be represented in Starlark. (The
old behavior was to allow some such attributes, and throw an exception when
their value was accessed.)

Fixes bazelbuild#13829
Fixes bazelbuild#16256

PiperOrigin-RevId: 478511532
Change-Id: If0c580a4e4eee1739136ea30eddce9eca71995e3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P1 I'll work on this now. (Assignee required) type: bug
Projects
None yet
Development

No branches or pull requests

2 participants