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

Let Label#debugPrint emit label strings in display form #21963

Closed
wants to merge 4 commits into from

Conversation

fmeum
Copy link
Collaborator

@fmeum fmeum commented Apr 10, 2024

This is a reland of 30b95e3 with a different approach to emitting display form labels that avoids adding a new to_display_form() method to Label:

  • In action command lines, which are the most frequent use of labels in rule implementation functions, labels are automatically emitted in display form since 9d3a8b0.
  • In module extensions and repository rules, if labels can be turned into display form, the inverse of the repository mapping would need to be tracked in lockfiles and marker files for correct incrementality. Furthermore, allowing implementation functions to access apparent names would allow them to "discriminate" against them, thus possibly breaking the user's capability to map repos at will via use_repo and repo_name. Similar to how providers on a target can't be enumerated, it is thus safer to not provide this information to the implementation functions directly.

This change thus implements StarlarkValue#debugPrint for Label to allow ruleset authors to emit labels in display form in warnings and error messages while ensuring that Starlark logic doesn't have access to this information. print("My message", label) degrades gracefully with older Bazel versions (it just prints a canonical label literal) and can thus be adopted immediately without a need for feature detection.

This requires changing the signature of StarlarkValue#debugPrint to receive the StarlarkThread instead of just the StarlarkSemantics. Since debugPrint is meant for emitting potentially non-deterministic information, it is safe to give it access to StarlarkThread.

Also improves the Bzlmod cycle reporter so that it prints helpful information on a cycle encountered in a previous iteration of this PR.

Fixes #20486

RELNOTES: Label instances passed to print or fail as positional arguments are now formatted with apparent repository names (optimized for human readability).

@fmeum fmeum requested review from aranguyen and removed request for a team April 10, 2024 20:37
@github-actions github-actions bot added team-Configurability Issues for Configurability team team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. team-Rules-CPP Issues for C++ rules awaiting-review PR is awaiting review from an assigned reviewer labels Apr 10, 2024
@fmeum
Copy link
Collaborator Author

fmeum commented Apr 10, 2024

@tetromino I have one more, but it's purely mechanical. :-)

Copy link
Contributor

@tetromino tetromino left a comment

Choose a reason for hiding this comment

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

Could you please make the motivating change (for label printing) in the same PR?

@fmeum fmeum changed the title Give StarlarkValue#debugPrint access to the StarlarkThread Make Label#debugPrint emit label strings in display form Apr 10, 2024
@fmeum fmeum marked this pull request as draft April 10, 2024 22:51
@fmeum fmeum changed the title Make Label#debugPrint emit label strings in display form Let Label#debugPrint emit label strings in display form Apr 11, 2024
@fmeum fmeum marked this pull request as ready for review April 11, 2024 11:30
@fmeum fmeum removed team-Configurability Issues for Configurability team team-Rules-CPP Issues for C++ rules labels Apr 11, 2024
@github-actions github-actions bot added team-Performance Issues for Performance teams team-Configurability Issues for Configurability team team-Rules-CPP Issues for C++ rules labels Apr 11, 2024
@fmeum
Copy link
Collaborator Author

fmeum commented May 1, 2024

@Wyverald Friendly ping

Copy link
Member

@Wyverald Wyverald left a comment

Choose a reason for hiding this comment

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

sorry for the delay -- looks good to me! I'd still like @tetromino to take a look before we merge.

Copy link
Contributor

@tetromino tetromino left a comment

Choose a reason for hiding this comment

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

Overall LGTM with the exception of a question about getMainRepositoryMapping

BzlLoadValue.Key key, StarlarkSemantics starlarkSemantics, Environment env)
throws InterruptedException {
if (!starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_BZLMOD)) {
return Optional.empty();
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand why return empty here; you can have a repo map in legacy WORKSPACE mode.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This has always been broken in numerous ways, @Wyverald can probably say more about it. As far as I know you also couldn't define a non-trivial repo mapping for the main repo, only for other repos.

Copy link
Contributor

Choose a reason for hiding this comment

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

One undesirable result of a null main repo map though is that in legacy WORKSPACE mode, all non-main-repo labels will get printed in @@ form.

Copy link
Contributor

@tetromino tetromino May 8, 2024

Choose a reason for hiding this comment

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

@fmeum - observe that the weird corner cases for legacy WORKSPACE, bazel_tools etc. are already well-handled by getRepositoryMapping. So how about the following implementation of getMainRepositoryMapping, which falls back to getRepositoryMapping for the edge case logic:

  @Nullable
  private static RepositoryMapping getMainRepositoryMapping(
      BzlLoadValue.Key key, StarlarkSemantics starlarkSemantics, Environment env)
      throws InterruptedException {
    boolean bzlmod = starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_BZLMOD);
    RepositoryMappingValue.Key repoMappingKey;
    if (key instanceof BzlLoadValue.KeyForBuild) {
      repoMappingKey = RepositoryMappingValue.key(RepositoryName.MAIN);
    } else if ((key instanceof BzlLoadValue.KeyForBzlmod
            && !(key instanceof BzlLoadValue.KeyForBzlmodBootstrap))
        || (bzlmod && key instanceof BzlLoadValue.KeyForWorkspace)) {
      // Since the main repo mapping requires evaluating WORKSPACE, but WORKSPACE can load from
      // extension repos, requesting the full main repo mapping would cause a cycle.
      repoMappingKey = RepositoryMappingValue.KEY_FOR_ROOT_MODULE_WITHOUT_WORKSPACE_REPOS;
    } else {
      // For builtins, @bazel_tools, and legacy WORKSPACE, the key's local repo mapping can be used
      // as the main repo mapping.
      return getRepositoryMapping(key, starlarkSemantics, env);
    }
    RepositoryMappingValue mainRepositoryMappingValue =
        (RepositoryMappingValue) env.getValue(repoMappingKey);
    if (mainRepositoryMappingValue == null) {
      return null;
    }
    return mainRepositoryMappingValue.getRepositoryMapping();
  }

Note that this will also let you get rid of the Optional wrapper as a side effect

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, @brandjon - do you have any comments on putting the main repo mapping in module context? Or maybe we can rather stick it into StarlarkThread somehow (I don't see any clean way to do it, but maybe you do)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, this looks much better. I also added a test for the --noenable_bzlmod case.

Copy link
Contributor

Choose a reason for hiding this comment

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

I talked to @brandjon about this PR in our daily sync; he suggested we try to put the main repository mapping pointer in BazelStarlarkContext rather than in module context (mainly for memory usage reasons; there may be a vast number of module context objects in flight in memory at any one time, but only a small number of starlark threads).

Copy link
Collaborator Author

@fmeum fmeum May 10, 2024

Choose a reason for hiding this comment

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

BazelStarlarkContext isn't available when a module extension is evaluated. Should I introduce a new Phase for that or reuse LOADING?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am now using BazelStarlarkContext and the existing WORKSPACE phase for module extension evaluation, PTAL.

@Wyverald Could you verify that adding BazelStarlarkContext to module extensions' Starlark threads doesn't give extensions access to functions they shouldn't have access to?

@fmeum fmeum force-pushed the 20486-debug-print-thread branch from 6586341 to 2c03231 Compare May 9, 2024 14:18
@fmeum fmeum requested a review from tetromino May 9, 2024 14:18
@iancha1992
Copy link
Member

@tetromino friendly ping :)
Also 7.2.0 RC1 is scheduled for next Monday, 5/13/2024 FYI

@fmeum fmeum force-pushed the 20486-debug-print-thread branch 4 times, most recently from 5426f16 to c902c41 Compare May 15, 2024 08:39
fmeum added 3 commits May 15, 2024 10:39
Since `debugPrint` is meant for emitting potentially non-deterministic, it is safe to give it access to `StarlarkThread` information.

This will be used in a follow-up to let `print` and `fail` emit `Label`s in display form without having to track the reverse repo mapping for the sake of invalidation of repo rules or module extensions.

Work towards bazelbuild#20486
@fmeum fmeum force-pushed the 20486-debug-print-thread branch from c902c41 to 182568c Compare May 15, 2024 08:39
@fmeum fmeum requested a review from Wyverald May 15, 2024 10:35
Copy link
Contributor

@tetromino tetromino left a comment

Choose a reason for hiding this comment

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

LGTM and I apologize for the wait!

Thank you for getting this complex PR to a polished state!

@tetromino tetromino added awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally and removed awaiting-review PR is awaiting review from an assigned reviewer labels May 20, 2024
@github-actions github-actions bot removed the awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally label May 20, 2024
fmeum added a commit to fmeum/bazel that referenced this pull request May 21, 2024
This is a reland of 30b95e3 with a different approach to emitting display form labels that avoids adding a new `to_display_form()` method to `Label`:
* In action command lines, which are the most frequent use of labels in rule implementation functions, labels are automatically emitted in display form since 9d3a8b0.
* In module extensions and repository rules, if labels can be turned into display form, the inverse of the repository mapping would need to be tracked in lockfiles and marker files for correct incrementality. Furthermore, allowing implementation functions to access apparent names would allow them to "discriminate" against them, thus possibly breaking the user's capability to map repos at will via `use_repo` and `repo_name`. Similar to how providers on a target can't be enumerated, it is thus safer to not provide this information to the implementation functions directly.

This change thus implements `StarlarkValue#debugPrint` for `Label` to allow ruleset authors to emit labels in display form in warnings and error messages while ensuring that Starlark logic doesn't have access to this information. `print("My message", label)` degrades gracefully with older Bazel versions (it just prints a canonical label literal) and can thus be adopted immediately without a need for feature detection.

This requires changing the signature of `StarlarkValue#debugPrint` to receive the `StarlarkThread` instead of just the `StarlarkSemantics`. Since `debugPrint` is meant for emitting potentially non-deterministic information, it is safe to give it access to `StarlarkThread`.

Also improves the Bzlmod cycle reporter so that it prints helpful information on a cycle encountered in a previous iteration of this PR.

Fixes bazelbuild#20486

RELNOTES: `Label` instances passed to `print` or `fail` as positional arguments are now formatted with apparent repository names (optimized for human readability).

Closes bazelbuild#21963.

PiperOrigin-RevId: 635589078
Change-Id: If60fdc632a59f19dad0cb02312690c15a0540c8e
fmeum added a commit to fmeum/bazel that referenced this pull request May 21, 2024
This is a reland of 30b95e3 with a different approach to emitting display form labels that avoids adding a new `to_display_form()` method to `Label`:
* In action command lines, which are the most frequent use of labels in rule implementation functions, labels are automatically emitted in display form since 9d3a8b0.
* In module extensions and repository rules, if labels can be turned into display form, the inverse of the repository mapping would need to be tracked in lockfiles and marker files for correct incrementality. Furthermore, allowing implementation functions to access apparent names would allow them to "discriminate" against them, thus possibly breaking the user's capability to map repos at will via `use_repo` and `repo_name`. Similar to how providers on a target can't be enumerated, it is thus safer to not provide this information to the implementation functions directly.

This change thus implements `StarlarkValue#debugPrint` for `Label` to allow ruleset authors to emit labels in display form in warnings and error messages while ensuring that Starlark logic doesn't have access to this information. `print("My message", label)` degrades gracefully with older Bazel versions (it just prints a canonical label literal) and can thus be adopted immediately without a need for feature detection.

This requires changing the signature of `StarlarkValue#debugPrint` to receive the `StarlarkThread` instead of just the `StarlarkSemantics`. Since `debugPrint` is meant for emitting potentially non-deterministic information, it is safe to give it access to `StarlarkThread`.

Also improves the Bzlmod cycle reporter so that it prints helpful information on a cycle encountered in a previous iteration of this PR.

Fixes bazelbuild#20486

RELNOTES: `Label` instances passed to `print` or `fail` as positional arguments are now formatted with apparent repository names (optimized for human readability).

Closes bazelbuild#21963.

PiperOrigin-RevId: 635589078
Change-Id: If60fdc632a59f19dad0cb02312690c15a0540c8e
@fmeum fmeum deleted the 20486-debug-print-thread branch May 21, 2024 12:57
github-merge-queue bot pushed a commit that referenced this pull request May 21, 2024
…2460)

This is a reland of 30b95e3 with a
different approach to emitting display form labels that avoids adding a
new `to_display_form()` method to `Label`:
* In action command lines, which are the most frequent use of labels in
rule implementation functions, labels are automatically emitted in
display form since 9d3a8b0.
* In module extensions and repository rules, if labels can be turned
into display form, the inverse of the repository mapping would need to
be tracked in lockfiles and marker files for correct incrementality.
Furthermore, allowing implementation functions to access apparent names
would allow them to "discriminate" against them, thus possibly breaking
the user's capability to map repos at will via `use_repo` and
`repo_name`. Similar to how providers on a target can't be enumerated,
it is thus safer to not provide this information to the implementation
functions directly.

This change thus implements `StarlarkValue#debugPrint` for `Label` to
allow ruleset authors to emit labels in display form in warnings and
error messages while ensuring that Starlark logic doesn't have access to
this information. `print("My message", label)` degrades gracefully with
older Bazel versions (it just prints a canonical label literal) and can
thus be adopted immediately without a need for feature detection.

This requires changing the signature of `StarlarkValue#debugPrint` to
receive the `StarlarkThread` instead of just the `StarlarkSemantics`.
Since `debugPrint` is meant for emitting potentially non-deterministic
information, it is safe to give it access to `StarlarkThread`.

Also improves the Bzlmod cycle reporter so that it prints helpful
information on a cycle encountered in a previous iteration of this PR.

Fixes #20486

RELNOTES: `Label` instances passed to `print` or `fail` as positional
arguments are now formatted with apparent repository names (optimized
for human readability).

Closes #21963.

PiperOrigin-RevId: 635589078
Change-Id: If60fdc632a59f19dad0cb02312690c15a0540c8e

Closes #22136
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose "human usable" workspace names for labels
5 participants