Skip to content

Not clear if @visibleForTesting elements should be visible in other packages #60731

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

Open
sigurdm opened this issue May 15, 2025 · 3 comments
Open
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@sigurdm
Copy link
Contributor

sigurdm commented May 15, 2025

https://api.flutter.dev/flutter/meta/visibleForTesting-constant.html says

Tools, such as the analyzer, can provide feedback if

  • ...
  • the declaration is referenced outside of its defining library or a library which is in the test folder of the defining package.

But the analyzer doesn't warn if an element is used in a test from another package.

It is not clear if the documentation or the analyzer is wrong here. I think both interpretations are useful in some cases.

Maybe we even need two different annotations

@bwilkerson bwilkerson added P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. labels May 15, 2025
@bwilkerson
Copy link
Member

In this case I think the documentation is correct and that the analyzer is wrong. I don't know how big of a breaking change it would be to fix it.

It's an interesting question as to whether there is enough need to have a visibleForTestingAnywhere annotation that allows the current behavior. (Or a visibleForTestingInSamePackage annotation if we want to avoid a breaking change.) I suspect that it's rare for a package author to want to allow dependencies in other packages on non-public API, so I think in those cases it's better for authors of dependent packages to clearly mark that the use of the API is known to be prohibited and that they're risking having their code broken by changes to the defining package.

@sigurdm
Copy link
Contributor Author

sigurdm commented May 19, 2025

I suspect that it's rare for a package author to want to allow dependencies in other packages on non-public API

I think it make sense to have API that is "public, but for testing only" perhaps it uses reflection, or mocks setup in a way that is not suitable for deployment time, but useful for testing.

@sigurdm
Copy link
Contributor Author

sigurdm commented May 19, 2025

In the discussions around dart-lang/dartdoc#4048 several people commented (some in an internal chat thread) that they had or intended to use @visibleForTesting for exactly that purpose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

2 participants