Skip to content

Fix removal and addition of analyzer in same batch#58024

Merged
jasonmalinowski merged 5 commits intodotnet:mainfrom
jasonmalinowski:fix-removal-and-addition-of-analyzer-in-same-batch
Dec 4, 2021
Merged

Fix removal and addition of analyzer in same batch#58024
jasonmalinowski merged 5 commits intodotnet:mainfrom
jasonmalinowski:fix-removal-and-addition-of-analyzer-in-same-batch

Conversation

@jasonmalinowski
Copy link
Copy Markdown
Member

If we had an analyzer reference being removed and re-added in the same batch, we would throw exceptions.

Fixes https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1264575

While doing this, discovered a few other issues so commit-at-a-time recommended.

@jasonmalinowski jasonmalinowski requested a review from a team as a code owner November 30, 2021 04:12
@ghost ghost added the Area-IDE label Nov 30, 2021
else
{
_workspace.ApplyChangeToWorkspace(w => w.OnAnalyzerReferenceRemoved(Id, visualStudioAnalyzer.GetReference()));
visualStudioAnalyzer.Dispose();
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I should get some tests for disposal too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Now with DivideByZeroException more tests!

@jasonmalinowski jasonmalinowski self-assigned this Nov 30, 2021
Id,
newSolution: solutionChanges.Solution.RemoveAnalyzerReference(Id, analyzerReference.GetReference()));

analyzerReference.Dispose();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should this either be a using or try/finally block to make sure it's always disposed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

So the object came from a previous addition which was "a long time ago" so using won't work. My first thought was your suggestion did make sense, but if we were to throw somewhere earlier it means the analyzer reference is still within _analyzersRemovedInBatch, so if a later call to close the batch works then maybe we'd dispose it then?

More generally we really don't expect applying a batch to ever really throw, since we're in a pretty funky state -- we don't have a good mechanism to roll back to the point "before the failure" since everything is very stateful here.

{
_analyzersAddedInBatch.Add(visualStudioAnalyzer);
_analyzersRemovedInBatch.Remove(analyzerPendingRemoval);
_analyzerPathsToAnalyzers.Add(fullPath, analyzerPendingRemoval);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we remove the path before the batch is complete? That seems odd... but maybe correct.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, it's a bit strange. The _analyzerPathsToAnalyzers is added/removed to represent the "current" state of the world at all points; this means that any further add/remove can first check it for it being a valid action, i.e. the throw you see happening back a few lines before all of this if you're adding a duplicate.

In the synchronous dispose path, we were asserting that the task
completed; if it completed due to a fault, we were silently catching
that exception. This ensures we propagate the exception.

Under no "normal" circumstance we do we expect an exception to throw
when a batch is applied, so this is only needed for diagnosing faults.
If we had an analyzer reference being removed and re-added in the same
batch, we would throw exceptions.

Fixes https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1264575
The VisualStudioAnalyzer also pushes load diagnostics to the workspace
so failing to do this would mean they stay around.
@dotnet dotnet deleted a comment from azure-pipelines bot Dec 3, 2021
@jasonmalinowski jasonmalinowski merged commit 5d5b3a3 into dotnet:main Dec 4, 2021
@jasonmalinowski jasonmalinowski deleted the fix-removal-and-addition-of-analyzer-in-same-batch branch December 4, 2021 00:18
@ghost ghost added this to the Next milestone Dec 4, 2021
dibarbet added a commit to dibarbet/roslyn that referenced this pull request Dec 9, 2021
…val-and-addition-of-analyzer-in-same-batch"

This reverts commit 5d5b3a3, reversing
changes made to 0b2c30d.
@Cosifne Cosifne modified the milestones: Next, 17.1.P3 Jan 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants