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

Fix #1074 #1075

Closed
wants to merge 1 commit into from
Closed

Fix #1074 #1075

wants to merge 1 commit into from

Conversation

tom-englert
Copy link
Contributor

@tom-englert tom-englert commented Mar 25, 2023

Fix #1074: AnalyzerTesting: Make the generated syntax trees of source generators available

@tom-englert tom-englert requested a review from a team as a code owner March 25, 2023 07:15
Copy link
Member

@sharwell sharwell left a comment

Choose a reason for hiding this comment

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

/// <summary>
/// Gets the syntax trees generated by the source generators during the test run.
/// </summary>
public ImmutableArray<SyntaxTree> GeneratedTrees { get; private set; } = ImmutableArray<SyntaxTree>.Empty;
Copy link
Member

Choose a reason for hiding this comment

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

📝 I have quite a few concerns about this. However, since no changes are required to implement the original feature, it might be easiest to just close this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See my comments in #1074. It should not be necessary to rewrite half of the test framework again and again, just to get hold of this simple thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BTW, what are your concerns here?

/// <summary>
/// Gets the syntax trees generated by the source generators during the test run.
/// </summary>
public ImmutableArray<SyntaxTree> GeneratedTrees { get; private set; } = ImmutableArray<SyntaxTree>.Empty;
Copy link
Member

Choose a reason for hiding this comment

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

❗ Source generators can run more than once during a test. It's not clear if/when/how this property would be valid during the life of a test. I would expect the following conceptual changes:

  1. The state of AnalyzerTest<T> should not change during the life of a call to RunAsync.
  2. The GeneratedTrees should not appear as a property on AnalyzerTest<T>, since that would make initializing the test instance more confusing (the output property would be showing up as an input property).
  3. The manner in which generated trees are provided accounts for the fact that generators may run multiple times during a test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

about 1:
it already does:
private readonly ConcurrentBag _workspaces = new ConcurrentBag()

about 2:
It won't, because the setter is private

about 3:
The sample you provided overwrites GetProjectCompilationAsync() to get access to the generated sources - but isn't that called also many times, so the problem is exactly the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe wrapping the call to GenerateSources into a new virtual method could be a good solution?
It would simplify extracting the sources a lot

Copy link
Member

Choose a reason for hiding this comment

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

about 1:
it already does:
private readonly ConcurrentBag _workspaces = new ConcurrentBag()

This is true, but also specifically designed to account for an unknown number of workspace creations. This was a bit clumsy in its own addition, and hopefully it could be avoided for future cases.

about 2: It won't, because the setter is private

TestState doesn't have a setter at all. The syntax used to initialize TestState is also valid for properties that do not have a public setter.

about 3: The sample you provided overwrites GetProjectCompilationAsync() to get access to the generated sources - but isn't that called also many times, so the problem is exactly the same?

It is, but the path that accesses the generated sources is only executed during specific record cases.

Maybe wrapping the call to GenerateSources into a new virtual method could be a good solution?
It would simplify extracting the sources a lot

ApplySourceGeneratorAsync might be the method to expose. The part I don't like is it's a difficult method to override.

@tom-englert
Copy link
Contributor Author

tom-englert commented Apr 2, 2023

Suggested solution works with acceptable effort

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AnalyzerTesting: Make the generated syntax trees of source generators available
2 participants