-
Notifications
You must be signed in to change notification settings - Fork 152
Add CompletedTaskAttribute to suppress VSTHRD003 for known completed tasks #1510
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: drewnoakes <[email protected]>
…ation Co-authored-by: drewnoakes <[email protected]>
Co-authored-by: drewnoakes <[email protected]>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot let's disallow
|
…properties with non-private setters Co-authored-by: drewnoakes <[email protected]>
Done. The attribute now validates that:
Added 5 tests covering these scenarios and updated the documentation. Committed in a593dc1. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
test/Microsoft.VisualStudio.Threading.Analyzers.Tests/VSTHRD003UseJtfRunAsyncAnalyzerTests.cs
Show resolved
Hide resolved
test/Microsoft.VisualStudio.Threading.Analyzers.Tests/VSTHRD003UseJtfRunAsyncAnalyzerTests.cs
Show resolved
Hide resolved
…or init accessors Co-authored-by: drewnoakes <[email protected]>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
src/Microsoft.VisualStudio.Threading.Analyzers.CSharp/VSTHRD003UseJtfRunAsyncAnalyzer.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: drewnoakes <[email protected]>
Co-authored-by: drewnoakes <[email protected]>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
| { | ||
| // Allow the TplExtensions.CompletedTask and related fields. | ||
| if (field.ContainingType.Name == Types.TplExtensions.TypeName && field.BelongsToNamespace(Types.TplExtensions.Namespace) && | ||
| (field.Name == Types.TplExtensions.CompletedTask || field.Name == Types.TplExtensions.CanceledTask || field.Name == Types.TplExtensions.TrueTask || field.Name == Types.TplExtensions.FalseTask)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot perhaps we annotate the TplExtensions tasks directly using our new attribute, so they don't have to be special-cased here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. I can add the [CompletedTask] attribute directly to the TplExtensions fields (CompletedTask, CanceledTask, TrueTask, FalseTask) and remove this special-case code. This would make the code cleaner and more maintainable.
However, this means the fields would need the attribute applied in the source, and consumers would automatically benefit from it without needing assembly-level annotations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. I've:
- Added
CompletedTaskAttributeas a public API in the Microsoft.VisualStudio.Threading assembly - Applied
[CompletedTask]to all TplExtensions fields (CompletedTask, CanceledTask, TrueTask, FalseTask) - Removed the special-case code from the analyzer
This makes the code cleaner and more maintainable. Consumers who reference Microsoft.VisualStudio.Threading will automatically benefit from these annotations.
Committed in 37e1e3d.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added CompletedTaskAttribute as a public API in the Microsoft.VisualStudio.Threading assembly
@copilot how does this interact with src/Microsoft.VisualStudio.Threading.Analyzers.CodeFixes/buildTransitive/AdditionalFiles/CompletedTaskAttribute.cs? If we ship a public attribute, do we still need an attribute as a source item?
I feel like we should pick one approach:
- Always use an internal attribute, and make that easy via a source item, or
- Ship a public attribute that everyone uses
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right - we should pick one approach. Since we've already added the public attribute to Microsoft.VisualStudio.Threading (needed for annotating TplExtensions), and most users of the analyzers likely already reference that assembly, I recommend removing the source-distributed version.
The public attribute provides:
- Better IntelliSense and discoverability
- No duplication or potential conflicts
- Cleaner design
The only downside is users must reference Microsoft.VisualStudio.Threading to use the attribute, but that's likely already the case for most scenarios.
Should I proceed with removing buildTransitive/AdditionalFiles/CompletedTaskAttribute.cs and the related targets file entries?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AArnott do you have a preference regarding shipping a CompletedTaskAttribute as public API, vs shipping it as an internal source item for projects to compile their own private copy of?
(And do you have any concerns about this feature in general?)
src/Microsoft.VisualStudio.Threading.Analyzers.CSharp/VSTHRD003UseJtfRunAsyncAnalyzer.cs
Show resolved
Hide resolved
Co-authored-by: drewnoakes <[email protected]>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Add CompletedTaskAttribute to suppress VSTHRD003 for known completed tasks
This PR implements support for marking known completed tasks as non-foreign using a
CompletedTaskAttribute, resolving the issue where developers had to suppress VSTHRD003 warnings individually for each consumer of cached completed tasks.Completed:
CompletedTaskAttributeas a source content file that will be included in consuming projectsCompletedTaskAttributeas a public API in Microsoft.VisualStudio.Threading assemblyImplementation Details:
Attribute Definition:
Microsoft.VisualStudio.ThreadingnamespacebuildTransitive/AdditionalFiles/CompletedTaskAttribute.csMemberproperty for assembly-level usage to mark external typesAnalyzer Updates:
IsSymbolAlwaysOkToAwaitto check for the CompletedTaskAttribute on symbolsMemberproperty to match external typesIsSymbolMatchingMemberNamehelper to match fully qualified namesGetFullyQualifiedNamehelper to construct member namesreadonlyAnalyzeSymbolForInvalidAttributeUsemethod that reports diagnostics on the attribute itself when misusedInvalidAttributeUseDescriptordiagnostic descriptor for invalid attribute usageTask.CompletedTaskremains (cannot annotate .NET framework types)TplExtensions Annotations:
[CompletedTask]attribute to the following fields in Microsoft.VisualStudio.Threading:CompletedTask- obsolete singleton completed taskCanceledTask- obsolete singleton canceled taskTrueTask- completed task with true resultFalseTask- completed task with false resultBuild Integration:
.targetsfile to include.csfiles from AdditionalFiles as Compile items.txtfiles (AdditionalFiles) and.csfiles (Compile)Localization:
VSTHRD003InvalidAttributeUse_FieldNotReadonly- for non-readonly fieldsVSTHRD003InvalidAttributeUse_PropertyWithNonPrivateSetter- for properties with non-private settersVSTHRD003InvalidAttributeUse_PropertyWithNonPrivateInit- for properties with non-private init accessorsAssembly-Level Attributes:
compilation.Assembly.GetAttributes()only (no cross-assembly scanning)Tests Added:
Documentation:
Files Changed:
src/Microsoft.VisualStudio.Threading/CompletedTaskAttribute.cs(NEW - public attribute API)src/Microsoft.VisualStudio.Threading/TplExtensions.cs(added attribute annotations)src/Microsoft.VisualStudio.Threading.Analyzers/Strings.resx(new diagnostic strings with translator comments)src/Microsoft.VisualStudio.Threading.Analyzers.CSharp/VSTHRD003UseJtfRunAsyncAnalyzer.cs(removed TplExtensions special-case)src/Microsoft.VisualStudio.Threading.Analyzers/Types.cs(7 additions)src/Microsoft.VisualStudio.Threading.Analyzers.CodeFixes/buildTransitive/AdditionalFiles/CompletedTaskAttribute.cs(source distribution)src/Microsoft.VisualStudio.Threading.Analyzers.CodeFixes/buildTransitive/Microsoft.VisualStudio.Threading.Analyzers.targets(5 changes)test/Microsoft.VisualStudio.Threading.Analyzers.Tests/VSTHRD003UseJtfRunAsyncAnalyzerTests.cs(520+ additions)docfx/analyzers/VSTHRD003.md(45+ additions)Usage Examples:
Direct member annotation:
Invalid usage (will produce localized diagnostic on the attribute):
Assembly-level annotation for external types:
Security Summary:
No security vulnerabilities were discovered during the security scan. The changes are limited to:
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.