Microsoft.Unity.Analyzers
Copilot instructions815 starsChanged 3 months agoMITView on GitHub
This is a Roslyn-based analyzer project providing Unity-specific diagnostics and suppressors for Visual Studio. The analyzers help Unity developers by: Every source file must include the Microsoft copyright header: Use the scaffolding tool instead of manually creating files: This automatically: - Creates source files with proper structure - Adds resource entries to Strings.resx - Creates corresponding test files - Use Run Custom Tool on Microsoft.Unity.Analyzers\Resources\String.resx to re-generate Microsoft.Unity.Analyzers\src\Microsoft.Unity.Analyzers\Resources\Strings.Designer.cs using ResXFileCodeGenerator Use constants from DiagnosticCategory: - Performance - Performance-related issues -…
The file
Show its rules and text
We split this copilot-instructions.md into the rules it gives an agent
14 rules. About 0 are common, 6 are distinctive, and 8 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈6
Distinctive · estimate
This author's own choices. Worth a look.
8
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 37
All rules are located in src\.editorconfig
- Very specific: line 38
Use current OS newlines, except for specific rules in .gitattributes
- Distinctive: line 43
Every source file must include the Microsoft copyright header:
- Distinctive: line 53
Use the scaffolding tool instead of manually creating files:
- Very specific: line 66
Use Run Custom Tool on Microsoft.Unity.Analyzers\Resources\String.resx to re-generate …
- Very specific: line 75
Use constants from DiagnosticCategory:
- Very specific: line 97
When testing, Unity is required and the test infrastructure will use the real UnityEngine.dll with effective types.
- Distinctive: line 98
When you need to search the unity API, you can use :
- Very specific: line 121
If you hit CS0649, make sure you initialize fields with null
- Very specific: line 124
When using DiagnosticMessageFormat parameters, you need to use the method .WithArguments(...) on ExpectDiagnostic
- Very specific: line 148
Update documentation in doc/ folder
- Distinctive: line 149
Ensure build and all tests pass
- Distinctive: line 150
When adding tests, try to find edge cases as well
- Distinctive: line 151
Follow the existing code style conventions
≈ “Common” is an estimate: at least 1 in 5 of the 1,644 Copilot instructions files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Copilot Instructions for Microsoft.Unity.Analyzers
Project Overview
This is a Roslyn-based analyzer project providing Unity-specific diagnostics and suppressors for Visual Studio. The analyzers help Unity developers by:
- Adding Unity-specific diagnostics (UNTxxxx) - detect Unity-specific issues like performance problems, incorrect API usage, and type safety violations
- Suppressing false-positive C# diagnostics (USPxxxx) - suppress general C# warnings that don't apply to Unity projects (e.g., unused Unity messages, serialized fields)
Technology Stack
- Language: C# with nullable reference types enabled
- Target Framework:
netstandard2.0for analyzers,net10.0for tests - Roslyn Version: Microsoft.CodeAnalysis 3.11.0 (locked for compatibility for analyzers, but latest version is used in tests)
- Testing Framework: xUnit
- Build System: .NET SDK with
slnxsolution files - Package Management: Central Package Management (
Directory.Packages.props)
Project Structure
src/
├── Microsoft.Unity.Analyzers/ # Main analyzer library
│ ├── Resources/ # Localized strings (Strings.resx)
│ └── *.cs # Analyzer, CodeFix, and Suppressor implementations
├── Microsoft.Unity.Analyzers.Tests/ # Unit tests
│ └── Infrastructure/ # Test helpers and verifiers
├── Microsoft.Unity.Analyzers.Vsix/ # VS extension packaging
└── new-analyzer/ # Tool to scaffold new analyzers
doc/ # Documentation for each diagnosticKey Conventions
Code Style
- All rules are located in
src\.editorconfig - Use current OS newlines, except for specific rules in
.gitattributes - You can use
dotnet format src/Microsoft.Unity.Analyzers.slnx whitespace --verbosity quiet --include <FILE>to format a specific<FILE>(compatible with wildcards)
File Headers
Every source file must include the Microsoft copyright header:
/*--------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*-------------------------------------------------------------------------------------------*/Creating New Analyzers
Use the scaffolding tool instead of manually creating files:
# Create analyzer + codefix:
dotnet run --project .\src\new-analyzer MyNewAnalyzer
# Create suppressor:
dotnet run --project .\src\new-analyzer MyNewSuppressorThis automatically:
- Creates source files with proper structure
- Adds resource entries to Strings.resx
- Creates corresponding test files
- Use
Run Custom ToolonMicrosoft.Unity.Analyzers\Resources\String.resxto re-generateMicrosoft.Unity.Analyzers\src\Microsoft.Unity.Analyzers\Resources\Strings.Designer.csusingResXFileCodeGenerator
Diagnostic IDs
- UNT0001-UNT9999: Analyzers (Unity diagnostics)
- USP0001-USP9999: Suppressors (suppress other analyzer diagnostics)
Diagnostic Categories
Use constants from DiagnosticCategory:
Performance- Performance-related issuesCorrectness- Bugs and incorrect behaviorTypeSafety- Type-related issuesReadability- Code clarity issues
Key Helper Classes
ScriptInfo
Determines if a type derives from Unity base classes and identifies Unity messages:
var scriptInfo = new ScriptInfo(typeSymbol);
if (scriptInfo.HasMessages && scriptInfo.IsMessage(methodSymbol))
{
// This is a Unity message like Update(), Start(), etc.
}UnityStubs
Contains stub definitions of Unity types. Located in UnityStubs.cs. This is only used for compiling, when we need to know specific method signatures.
When testing, Unity is required and the test infrastructure will use the real UnityEngine.dll with effective types.
When you need to search the unity API, you can use :
- https://docs.unity3d.com/ScriptReference/30_search.html?q=QUERY where
QUERYis your search item - or if you know the Class/Method name you can use https://docs.unity3d.com/ScriptReference/CLASS.METHOD.html like https://docs.unity3d.com/ScriptReference/MonoBehaviour.Update.html
Extension Methods
TypeSymbolExtensions- Type matching and inheritance checks (Extends(),Matches())MethodSymbolExtensions- Method signature matchingSyntaxNodeExtensions- Syntax tree navigation helpersSuppressionAnalysisContextExtensions- Helper for getting suppressible nodes
Testing Patterns
Test Base Classes
BaseDiagnosticVerifierTest<TAnalyzer>- For analyzers without code fixesBaseCodeFixVerifierTest<TAnalyzer, TCodeFix>- For analyzers with code fixesBaseSuppressorVerifierTest<TAnalyzer>- For suppressors
Test Requirements
- Unity installation required: Tests use actual Unity assemblies for reference resolution
- Tests automatically inherit
DoNotFailWithInterfaceMemberstest to verify interface member handling - If you hit
CS0649, make sure you initialize fields withnull - For advanced tests scenarios, use
AnalyzerVerificationContextto set a specific analyzer option or to filter a diagnostic - The line numbers in the tests are off by 1, given we are using a verbatim string starting with
@", the first line is empty, so the actual code starts on line 2. - When using DiagnosticMessageFormat parameters, you need to use the method
.WithArguments(...)onExpectDiagnostic
Build and Test Commands
# Build
dotnet build .\src\Microsoft.Unity.Analyzers.slnx
# Run tests with Microsoft Testing Platform
dotnet .\src\Microsoft.Unity.Analyzers.Tests\bin\Debug\net10.0\Microsoft.Unity.Analyzers.Tests.dll --retry-failed-tests 3 --filter-not-class Microsoft.Unity.Analyzers.Tests.ConsistencyTestsIndeed we want to skip ConsistencyTests when developping new diagnostics because those tests are doing http-requests on documentation.
Documentation
Each diagnostic must have a corresponding documentation file in doc/:
doc/UNT00XX.md- Description, examples, and fix guidancedoc/index.md- Master list of all diagnostics
New Diagnostic Guidelines
- Use the scaffolding tool instead of manually creating files
- Include unit tests for all new functionality
- Update documentation in
doc/folder - Ensure build and all tests pass
- When adding tests, try to find edge cases as well
- Follow the existing code style conventions
- Mimic the style, architecture, organization that you can see in existing analyzers and suppressors for consistency
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

