К основному содержанию
скилл GitHub Copilot официальный · GitHub

roslyn-analyzers — скилл для GitHub Copilot: установка

Скилл помогает создавать, проверять, отлаживать, тестировать и упаковывать Roslyn-анализаторы, code fix providers и incremental source generators.

Установить в GitHub Copilot · Через Skills CLI (Vercel) Все способы и платформы ↓
npx skills add github/awesome-copilot --skill roslyn-analyzers
Открыть на GitHub

Что делает roslyn-analyzers

Скилл задаёт рабочий процесс для DiagnosticAnalyzer, CodeFixProvider и IIncrementalGenerator. Он помогает выбрать анализ через IOperation, настроить diagnostic ID, документацию, ссылки на справку и проверку символов через Compilation и SymbolEqualityComparer.Default.

Внутри описаны правила для раздельных analyzer и code-fix сборок, безопасных для многопоточности callback’ов, поддержки C# и VB.NET, Roslyn test harnesses и тестов для анализаторов. Для source generators предусмотрены incremental-подход и SourceWriter, а для публикации — настройка версии сборки и NuGet packaging.

Ставьте его, если работаете с анализаторами или генераторами Roslyn и хотите держать единый процесс разработки, тестирования и упаковки. Для задач вне экосистемы Roslyn он лишний.

Какие задачи решает

  • создать DiagnosticAnalyzer для C# или VB.NET
  • добавить CodeFixProvider в отдельную сборку
  • проверить символы и операции через Roslyn API
  • написать тесты с Roslyn test harness
  • упаковать analyzer в NuGet-пакет

Примеры запросов агенту

  • Создай DiagnosticAnalyzer для проверки вызовов API через `IOperation` и добавь тесты для C# и VB.NET.
  • Проверь CodeFixProvider: выдели минимальный span диагностики, сохрани trivia и подготовь документацию для diagnostic ID.
  • Разбери incremental source generator с `IIncrementalGenerator`, `SourceWriter` и настройкой версии сборки.

Как установить roslyn-analyzers

1Через Skills CLI (Vercel)

npx skills add github/awesome-copilot --skill roslyn-analyzers

2GitHub CLI

gh skill install github/awesome-copilot roslyn-analyzers

3Вручную: положить файлы

.github/skills/roslyn-analyzers/SKILL.md

Из того же набора554

Весь набор →
git-commit официальный github Скилл разбирает изменения Git, выбирает связанные файлы и создаёт коммит в формате Conventional Commits. Пригодится, когда нужно закоммитить одну или несколько логических частей изменений. GitHub Copilot 47 165установок excalidraw-diagram-generator официальный github Скилл превращает описание на обычном языке в диаграмму Excalidraw. Подойдёт, когда нужно показать процесс, связи между объектами или устройство системы. GitHub Copilot 31 639установок multi-stage-dockerfile официальный github Скилл помогает создавать многоэтапные Dockerfile для разных языков и фреймворков. Пригодится, когда важны размер и состав runtime-образа. GitHub Copilot 29 688установок documentation-writer официальный github Скилл для создания технической документации по фреймворку Diátaxis: туториалы, гайды, справки и объяснения. Предлагает структуру перед написанием. GitHub Copilot 27 757установок prd официальный github Собирает из идеи структурированный PRD с пользовательскими историями, техническими требованиями и рисками. Подходит для планирования новых функций, в том числе с AI. GitHub Copilot 24 035установок refactor официальный github Скилл помогает менять структуру кода без изменения внешнего поведения. Подойдёт для постепенной уборки сложных функций, дублирования и кодовых запахов. GitHub Copilot 22 321установок java-springboot официальный github Задаёт практики разработки Spring Boot-приложений: от структуры проекта до тестов и безопасности. Пригодится, когда Copilot пишет или проверяет Java-код. GitHub Copilot 21 272установок create-readme официальный github Скилл изучает проект и создаёт структурированный README.md. Подойдёт, когда документацию нужно собрать по коду и файлам проекта. GitHub Copilot 19 081установок

Похожие

migrate-to-shoehorn Matt Pocock Переводит type assertions с as в тестах на функции из @total-typescript/shoehorn. Нужен для частичных объектов и намеренно неверных данных в проверках. Claude Code 475 388установок write-swift emilkowalski Скилл помогает писать, проверять и переносить код на Swift с учётом модели языка. Подойдёт, когда нужно разобраться с конкурентностью, ошибкой или циклом удержания объектов. Claude Code CX Codex Cursor +1 117 500установок megacave JuliusBrussee Переводит ответы ассистента на классический китайский язык (вэньянь) для экономии места на экране. Активируется командами /megacave или /caveman wenyan. Claude Code CX Codex Cursor +1 24 400установок find-docs upstash Тянет актуальную документацию, справочники API и примеры кода для любой библиотеки через Context7 CLI. Пригодится, когда агент отвечает по устаревшим данным обучения. Claude Code CX Codex Cursor +1 9 400установок typescript-advanced-types Seth Hobson Помогает агенту писать сложную типизацию TypeScript: generics, conditional types и преобразования типов. Нужен для библиотек, API-клиентов и строгих конфигураций. Claude Code CX Codex Cursor 92 169установок smart-explore thedotmack Структурно ищет функции и классы через tree-sitter. Пригодится, когда нужно разобраться в кодовой базе без чтения всех файлов. Claude Code CX Codex Cursor +1 7 300установок

Вопросы о roslyn-analyzers

Как установить roslyn-analyzers в GitHub Copilot?

Выполните npx skills add github/awesome-copilot --skill roslyn-analyzers, gh skill install github/awesome-copilot roslyn-analyzers или используйте путь .github/skills/roslyn-analyzers/SKILL.md.

Что делает roslyn-analyzers?

Скилл подсказывает, как разрабатывать и проверять Roslyn-анализаторы, code fix providers и incremental source generators. Он также охватывает зависимости Microsoft.CodeAnalysis, тесты и NuGet packaging.

Какие языки поддерживает roslyn-analyzers?

Для анализаторов обязательна поддержка C#. VB.NET добавляется по запросу или по правилам репозитория; при заявленной поддержке нужны отдельные тестовые примеры для C# и VB.NET.

Исходный текст

SKILL.md · MIT
---
name: roslyn-analyzers
description: 'Build, review, debug, package, and test Roslyn diagnostic analyzers, code fix providers, and incremental source generators. Use for DiagnosticAnalyzer, CodeFixProvider, IIncrementalGenerator, IOperation analysis, Microsoft.CodeAnalysis dependency pinning, Roslyn test harnesses, C#/VB tests, and analyzer NuGet packaging.'
---

# Roslyn Analyzers and Source Generators

Use this workflow when adding or changing a Roslyn analyzer, code fix, source generator, tests, dependencies, or package layout. First inspect the repository's target frameworks, central package management, test framework, analyzer and generator conventions, diagnostic ID allocation, localization, and packaging. Preserve established conventions unless they conflict with the compatibility rules below.

## Non-negotiable design rules

- Prefer `IOperation`-based analysis wherever possible. Register the narrowest applicable `OperationKind` and inspect typed operations such as `IInvocationOperation`, `IAwaitOperation`, or `IObjectCreationOperation`. This usually supports C# and VB with one analyzer and gives direct access to symbols and conversions.
- Use syntax analysis only for inherently syntactic rules or syntax not represented adequately by `IOperation`. A syntax-node callback already has a `SemanticModel`; do not call `Compilation.GetSemanticModel` or fetch another semantic model from it. Repeated semantic-model creation is expensive and often indicates that an operation or symbol action is the better abstraction.
- Every analyzer must support C#. VB.NET support is optional until requested or established repository precedent requires it. A language-neutral operation analyzer may declare both languages. If VB support is promised, include equivalent C# and VB snippets in tests; do not infer VB correctness from shared implementation alone.
- Keep analyzer and code-fix providers in distinct assemblies. The analyzer project must never reference Roslyn Workspaces packages. Workspaces dependencies belong only in the code-fix and test projects.
- Analyzer callbacks must be stateless or concurrency-safe. Call `EnableConcurrentExecution()`. Make an explicit generated-code choice with `ConfigureGeneratedCodeAnalysis(...)`; follow repository policy rather than silently accepting the default.
- Respect cancellation where APIs expose a token. Do not retain compilations, operations, syntax trees, symbols, or semantic models in static state.
- Avoid `InternalsVisibleTo`. Test analyzers through their public `DiagnosticAnalyzer` and `CodeFixProvider` APIs and the Roslyn test harness. A small analyzer helper may be public when direct testing is genuinely useful, but most behavior should be tested end to end.
- Centralize metadata names and member names instead of propagating magic strings. Use one shared static catalog for fully qualified type names, namespaces, and API member names.
- Never identify a symbol, including an attribute class, by calling `ToDisplayString()` and comparing the result with a string literal. Resolve an expected named type from the `Compilation` once by metadata name. To resolve a method, property, field, or event, first resolve its containing type, then select the expected member by name and signature. Compare encountered symbols with `SymbolEqualityComparer.Default.Equals(actual, expected)`. When generic substitutions are irrelevant, compare named types, properties, fields, and events by `OriginalDefinition`; compare methods after replacing a reduced extension method with `ReducedFrom ?? method` and then taking `OriginalDefinition`. Normalize both expected and encountered symbols, and inspect type arguments separately when they are part of the match. Display strings are for presentation, not identity, and can vary with formatting, aliases, qualification, nullability, and other context.
- Document every diagnostic ID. When the repository uses Docfx, put analyzer documentation under its Docfx tree, typically `docfx/analyzers`, and include each page in the relevant table of contents.
- Set every analyzer assembly's version precisely enough that each commit produces a unique assembly version. When using Nerdbank.GitVersioning, give each analyzer project its own `version.json` with `assemblyVersion.precision` set to `revision` and ensure that repository-wide MSBuild properties do not prevent that file from being discovered.
- Source generators must implement `IIncrementalGenerator`, not `ISourceGenerator`. Design the provider graph so unchanged inputs remain cached and do not regenerate output.
- Source generators must use a small `SourceWriter` abstraction for deterministic newlines, indentation, encoding, and balanced output. Start from [SourceWriter.cs](./references/SourceWriter.cs) and tailor its namespace and target-framework details to the receiving repository.

## Implementation workflow

1. Write down examples that must report and near-misses that must not report. Decide the exact diagnostic span and message arguments before implementation.
2. Determine whether the rule is semantic. Prefer, in order, operation actions, operation-block actions, symbol actions, compilation-start actions that register one of those actions, and finally syntax actions.
3. Resolve well-known named types once in a compilation-start action with `Compilation.GetTypeByMetadataName` when needed. Resolve expected members from the resulting containing type by name and full signature; `GetTypeByMetadataName` does not resolve methods, properties, fields, or events. If a required type or member is absent, register no inner action. Capture the resolved `ISymbol` in the registered callback and compare encountered symbols with `SymbolEqualityComparer.Default.Equals`. When generic substitutions are irrelevant, normalize both expected and encountered named types, properties, fields, and events with `OriginalDefinition` before comparing them; this includes comparing `AttributeData.AttributeClass` with a resolved generic attribute definition. For methods, normalize each `IMethodSymbol` with `(method.ReducedFrom ?? method).OriginalDefinition` so constructed generic and reduced extension methods match their declaration. Inspect type arguments separately when they matter. Never use `ToDisplayString()` plus a string literal as a substitute for symbol identity. Check metadata names, arity, containing type, and signature as appropriate when selecting members; do not identify APIs by simple method name alone.
4. Define a stable diagnostic ID and descriptor. Follow repository practices for category, severity, localization, help links, telemetry tags, and release tracking.
5. Add or update the diagnostic's documentation page and wire the descriptor's `HelpLinkUri` to its published URL.
6. Report the smallest useful source location. Avoid diagnostics on generated code unless that is intentional.
7. Add or update the code fix in the separate code-fix assembly. Preserve trivia, use syntax generators or typed syntax APIs, annotate simplifiable/formattable nodes where appropriate, provide stable equivalence keys for distinct actions, and offer `FixAllProvider` only when batch application is correct.
8. Add positive, negative, edge, and code-fix tests in one test class for the rule.
9. Verify that every analyzer project has per-commit assembly versioning and that its project-local version configuration is actually honored.
10. Run the narrow test class or method first, then the test project, then the repository's normal build or analyzer validation. Honor the repository's test-runner syntax; do not assume VSTest `--filter` is supported.

For a source generator, replace steps 1-8 with: define generated API examples and invalid-input diagnostics; model each independent input; build an incremental provider graph; render with `SourceWriter`; and test output, diagnostics, determinism, and incremental caching before continuing with versioning, packaging, and repository validation.

## Analyzer pattern

This is the preferred shape for a semantic rule. Adapt descriptor construction and generated-code policy to local conventions.

```csharp
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class AvoidBlockingAnalyzer : DiagnosticAnalyzer
{
    public const string DiagnosticId = "LIB0001";

    private static readonly DiagnosticDescriptor Rule = new(
        DiagnosticId,
        "Avoid blocking calls",
        "Do not call '{0}'",
        "Usage",
        DiagnosticSeverity.Warning,
        isEnabledByDefault: true);

    public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Rule];

    public override void Initialize(AnalysisContext context)
    {
        context.EnableConcurrentExecution();
        context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
        context.RegisterCompilationStartAction(static context =>
        {
            INamedTypeSymbol? taskType = context.Compilation.GetTypeByMetadataName(KnownApis.Task.FullName);
            if (taskType is null)
            {
                return;
            }

            context.RegisterOperationAction(
                operationContext => AnalyzeInvocation(operationContext, taskType),
                OperationKind.Invocation);
        });
    }

    private static void AnalyzeInvocation(OperationAnalysisContext context, INamedTypeSymbol taskType)
    {
        var invocation = (IInvocationOperation)context.Operation;
        IMethodSymbol method = invocation.TargetMethod;
        if (method.Name == KnownApis.Task.Wait &&
            SymbolEqualityComparer.Default.Equals(method.ContainingType, taskType))
        {
            context.ReportDiagnostic(Diagnostic.Create(
                Rule,
                invocation.Syntax.GetLocation(),
                method.Name));
        }
    }
}

public static class KnownApis
{
    public static class Task
    {
        public const string FullName = "System.Threading.Tasks.Task";
        public const string Wait = "Wait";
    }
}
```

When supporting both languages, use `[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]` only if all analysis and diagnostic locations are actually language-neutral or both languages are separately handled and tested.

## Syntax-analysis exception

Use syntax callbacks only when syntax itself controls the rule. Use the callback's existing semantic model if semantic information is unavoidable:

```csharp
private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
    var invocation = (InvocationExpressionSyntax)context.Node;
    ISymbol? symbol = context.SemanticModel.GetSymbolInfo(invocation, context.CancellationToken).Symbol;
    // Do not call context.Compilation.GetSemanticModel(invocation.SyntaxTree).
}
```

Before choosing this pattern, check whether `IInvocationOperation.TargetMethod`, `IArgumentOperation.Parameter`, `IConversionOperation`, or another operation supplies the same information.

## Incremental source generators

Always implement `IIncrementalGenerator`. Do not wrap an `ISourceGenerator` with `AsSourceGenerator`; that preserves the old execution model rather than designing for incrementality.

Build a pipeline with these properties:

- Use `SyntaxProvider.ForAttributeWithMetadataName` for attribute-driven discovery. Otherwise, use `CreateSyntaxProvider` with a very cheap syntactic predicate and perform semantic work only in its transform.
- Transform syntax and symbols immediately into small immutable, value-equatable models containing only information needed for generation. Do not carry `SyntaxNode`, `ISymbol`, `SemanticModel`, or `Compilation` into later stages because their identity changes defeat caching.
- Keep independent inputs in independent providers. Combine providers as late as possible. Avoid combining every item with `CompilationProvider` merely for convenience.
- Avoid `Collect()` unless one output truly depends on the complete set. Prefer one output per model so editing one declaration invalidates only that output.
- Use stable equality. Records and immutable value types are good models; add `WithComparer` when normal equality does not represent semantic equivalence.
- Keep transforms and output callbacks static, pure, deterministic, and cancellation-aware. Do not read ambient files, clocks, environment state, or mutable static state. Model configuration, additional files, analyzer config options, and parse options as explicit providers.
- Give providers `WithTrackingName` labels while developing or testing incremental behavior.
- Generate stable, collision-resistant hint names. Sort any aggregated inputs explicitly before emitting output.
- Report invalid user input with generator diagnostics instead of throwing. Generator exceptions surface as compiler failures.
- Use `SourceText` with an explicit encoding when calling `AddSource`.

A compact pipeline should look like this:

```csharp
[Generator(LanguageNames.CSharp)]
public sealed class FactoryGenerator : IIncrementalGenerator
{
    public void Initialize(IncrementalGeneratorInitializationContext context)
    {
        IncrementalValuesProvider<TypeModel> models = context.SyntaxProvider
            .ForAttributeWithMetadataName(
                "Product.GenerateFactoryAttribute",
                static (node, _) => node is TypeDeclarationSyntax,
                static (attributeContext, cancellationToken) =>
                    TypeModel.Create(attributeContext, cancellationToken))
            .WithTrackingName("FactoryModels");

        context.RegisterSourceOutput(models, static (productionContext, model) =>
        {
            var writer = new SourceWriter();
            writer.WriteLine("// <auto-generated/>");
            writer.WriteLine($"namespace {model.Namespace};");
            writer.WriteLine();
            writer.WriteLine($"partial class {model.TypeName}");
            writer.WriteLine('{');
            writer.Indentation++;
            writer.WriteLine($"public static {model.TypeName} Create() => new();");
            writer.Indentation--;
            writer.WriteLine('}');
            productionContext.AddSource(model.HintName, writer.ToSourceText());
        });
    }
}
```

The model creation step must normalize symbol data into strings, booleans, enums, and other immutable values, validate unsupported declarations, escape identifiers, and derive a stable hint name. The output callback should only render the model. Use the complete [SourceWriter.cs](./references/SourceWriter.cs) example rather than ad hoc `StringBuilder` concatenation, then adapt indentation style and framework compatibility to the repository.

### Generator tests

Test source generators with `GeneratorDriver` or the repository's generator test harness. Cover:

- exact generated hint names and source text;
- multiple and nested declarations, namespaces, generics, escaped identifiers, and partial types;
- malformed or unsupported inputs and their diagnostic locations;
- additional files, analyzer config options, and parse options when consumed;
- deterministic output independent of input enumeration and operating-system newlines;
- a second driver run with identical inputs that caches all expected tracked steps;
- a narrowly changed input that reruns only the affected pipeline and output;
- compilation of generated output without unexpected diagnostics.

Enable incremental step tracking in tests and assert run reasons such as cached, unchanged, modified, or newly produced according to the Roslyn version in use. Do not settle for snapshot-only tests: identical generated text can hide a pipeline that recomputes everything.

## Diagnostic documentation and help links

Create one discoverable documentation page for every diagnostic code. A useful page includes:

- the diagnostic ID, title, category, and default severity;
- the exact condition that triggers the diagnostic;
- why the reported pattern is problematic;
- bad and corrected C# examples;
- VB examples when VB support is promised;
- exceptions, configuration, suppression guidance, and code-fix behavior when relevant.

If the repository has a Docfx site, place these pages under its existing analyzer documentation area, typically:

```text
docfx/
    analyzers/
        LIB0001.md
    toc.yml
```

Follow the site's existing URL and navigation conventions. Add each page to the applicable `toc.yml` so it is rendered and discoverable. The descriptor's help link must be the public GitHub Pages URL that Docfx will produce, not a source-tree path. Centralize construction when all diagnostics share a route:

```csharp
private static string GetHelpLink(string diagnosticId)
        => $"https://example.github.io/product/analyzers/{diagnosticId}.html";

private static readonly DiagnosticDescriptor Rule = new(
        id: DiagnosticId,
        title: "Avoid blocking calls",
        messageFormat: "Do not call '{0}'",
        category: "Usage",
        defaultSeverity: DiagnosticSeverity.Warning,
        isEnabledByDefault: true,
        helpLinkUri: GetHelpLink(DiagnosticId));
```

Derive the exact hostname, base path, output extension, casing, and route from the repository's Docfx configuration and deployment setup. Confirm that the generated site's route matches `HelpLinkUri`; do not merely assume that a Markdown path maps to the expected URL. When no Docfx folder exists, follow the repository's established documentation system or create an equivalent durable page and public help URL.

## Project and dependency boundaries

A typical solution has three projects:

```text
Product.Analyzers          -> Microsoft.CodeAnalysis.Common/CSharp only
Product.CodeFixes          -> Product.Analyzers + Roslyn Workspaces/Features APIs
Product.Analyzers.Tests    -> analyzer + code-fix projects + current testing packages
```

Target analyzer assemblies conservatively, commonly `netstandard2.0`, unless host requirements dictate otherwise. Treat the analyzer's Roslyn version as a host-compatibility floor, not a routine dependency to float.

Choose exactly one Roslyn reference-assembly version for shipping analyzer projects. Pin it independently from the latest Roslyn version used by code-fix tests or the rest of the repository. Pin the analyzer dependency's entire transitive closure to mutually compatible versions, including packages such as `System.Collections.Immutable`, `System.Memory`, `System.Reflection.Metadata`, `System.Runtime.CompilerServices.Unsafe`, and `System.Threading.Tasks.Extensions` when they appear in restore. Directly pinning only `Microsoft.CodeAnalysis.*` is insufficient: a newer centrally managed transitive dependency can make the analyzer fail in an older compiler host.

With central package management, create a dedicated `Directory.Packages.Analyzers.props` at the repository root and import it only for shipping analyzer, source-generator, and code-fix projects. Do not fold these overrides into the general `Directory.Packages.props`: the separate file makes the compatibility boundary visible and lets dependency automation treat it specially.

Use this three-part pattern.

First, have every shipping analyzer, source-generator, and code-fix project import one shared props file such as `src/AnalyzerCompatibility.props`. That file should identify the project:

```xml
<Project>
    <PropertyGroup>
        <!-- This is set so that Directory.Packages.Analyzers.props can apply version overrides specific to analyzer projects. -->
        <IsAnalyzerCompatibilityProject>true</IsAnalyzerCompatibilityProject>
    </PropertyGroup>
</Project>
```

Second, conditionally import the dedicated package file from a repository-wide props or targets file after central package versions are available:

```xml
<Import Project="$(MSBuildThisFileDirectory)Directory.Packages.Analyzers.props"
                Condition="'$(IsAnalyzerCompatibilityProject)' == 'true'" />
```

Third, copy this `Directory.Packages.Analyzers.props` structure and tailor the baseline comment, Roslyn version, and transitive versions to the oldest supported compiler or SDK host. Use `Update` only for a package already declared by the repository's central package file; use `Include` to add a pin for a package that is otherwise transitive-only. The example assumes the Roslyn packages are already declared and the listed runtime dependencies are new pins:

```xml
<Project>
    <!-- These versions are chosen to support the oldest compiler/SDK host named here. -->
  <PropertyGroup>
        <CodeAnalysisVersionForAnalyzers>4.11.0</CodeAnalysisVersionForAnalyzers>
      <!-- Apply analyzer compatibility pins to packages referenced only transitively. -->
      <CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
  </PropertyGroup>
  <ItemGroup>
        <PackageVersion Update="Microsoft.CodeAnalysis" Version="$(CodeAnalysisVersionForAnalyzers)" />
        <PackageVersion Update="Microsoft.CodeAnalysis.Common" Version="$(CodeAnalysisVersionForAnalyzers)" />
        <PackageVersion Update="Microsoft.CodeAnalysis.CSharp" Version="$(CodeAnalysisVersionForAnalyzers)" />
        <PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(CodeAnalysisVersionForAnalyzers)" />
        <PackageVersion Update="Microsoft.CodeAnalysis.VisualBasic" Version="$(CodeAnalysisVersionForAnalyzers)" />
        <PackageVersion Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="$(CodeAnalysisVersionForAnalyzers)" />
        <PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
        <PackageVersion Include="System.Collections.Immutable" Version="8.0.0" />
        <PackageVersion Include="System.Memory" Version="4.5.5" />
        <PackageVersion Include="System.Reflection.Metadata" Version="8.0.0" />
        <PackageVersion Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
        <PackageVersion Include="System.Text.Json" Version="8.0.0" />
        <PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
  </ItemGroup>
</Project>
```

The XML comments in these samples are required, not decorative. Keep equivalent comments in the receiving repository explaining why `IsAnalyzerCompatibilityProject` exists, why the conditional import exists when its purpose is not obvious, which oldest host the pinned versions support, and why these versions intentionally differ from the repository defaults. Include the Roslyn version-support documentation URL in or next to the baseline comment when it helps maintainers choose the correct compiler baseline. Future dependency updates must preserve or update these comments so users do not mistake deliberate compatibility pins for stale packages.

The versions above are a structural example, not universal recommendations. Recalculate the complete package set for the selected oldest host by inspecting the resolved dependency graph. Retain each package that Roslyn or the generator loads in the compiler process, even when the repository does not reference it directly. `Workspaces` entries centrally pin versions for code-fix projects but must not be referenced by analyzer or source-generator projects. The test project should remain outside `IsAnalyzerCompatibilityProject` and use the latest repository-approved Roslyn and analyzer-testing packages so tests also catch compatibility with current APIs and compilers.

Always prefer the latest available version of `Microsoft.CodeAnalysis.Analyzers`. It is a development-time analyzer package, not a compiler-host runtime dependency, so it does not need to match `Microsoft.CodeAnalysis`, `Microsoft.CodeAnalysis.Common`, C#/VB, or Workspaces package versions and must not be held back to the analyzer compatibility baseline. Keep it on the repository's normal latest-version update path and reference it without flowing it to consumers:

```xml
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="all" />
```

Do not set its central version to `$(CodeAnalysisVersionForAnalyzers)` and do not include it among packages excluded from minor or major dependency updates. This exception applies specifically to `Microsoft.CodeAnalysis.Analyzers`; packages loaded with the shipping analyzer or generator still belong to the carefully pinned compatibility closure.

Configure dependency automation so the analyzer baseline is not upgraded across Roslyn versions. For Renovate, isolate the analyzer version file or package group and disallow updates:

```json
{
  "packageRules": [
    {
      "matchFileNames": ["Directory.Packages.Analyzers.props"],
      "enabled": false
    }
  ]
}
```

For Dependabot, use an `ignore` entry for each analyzer-baseline package with `update-types` containing `version-update:semver-major`, `version-update:semver-minor`, and `version-update:semver-patch`. Include the pinned transitive packages, not only `Microsoft.CodeAnalysis.*`. If the tool cannot distinguish analyzer-only central versions from repository-wide versions, place the analyzer pins in a dedicated file/directory or make those upgrades manual.

Verify dependency isolation with a restore graph or assets file and, when practical, load the built analyzer in the oldest supported compiler/IDE host. A successful build on the newest SDK does not prove host compatibility.

## Code-fix boundaries

The code-fix project may reference packages such as `Microsoft.CodeAnalysis.CSharp.Workspaces` or `Microsoft.CodeAnalysis.VisualBasic.Workspaces`; the analyzer project may not. Keep the code fix optional from the analyzer's perspective. A fix should:

- register only when the diagnostic can be fixed safely;
- find the target node from the diagnostic span defensively;
- preserve leading/trailing trivia and imports;
- use `Simplifier.Annotation` and `Formatter.Annotation` where appropriate;
- return a new document or solution without mutating shared state;
- support cancellation and avoid blocking async Roslyn APIs;
- use a meaningful, stable equivalence key, especially when multiple fixes share a title;
- return the batch fixer only when fixes do not conflict or depend on application order.

## Unique assembly versions

Analyzer hosts cache and identify assemblies by assembly identity. Every commit that can change an analyzer should therefore produce a unique, precise assembly version; relying only on package version or file version is insufficient.

With Nerdbank.GitVersioning, put a `version.json` directly in every analyzer project directory, including each language-specific analyzer assembly when those are separate:

```json
{
    "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
    "inherit": true,
    "assemblyVersion": {
        "precision": "revision"
    }
}
```

Then inspect all imported `Directory.Build.props`, `Directory.Build.targets`, and project SDK props for `GitVersionBaseDirectory`. A repository may set that property to a shared root so version calculation happens once; that can cause a project-local `version.json` to be ignored. In each analyzer project file, reset it to the analyzer project's own directory:

```xml
<PropertyGroup>
    <!-- Honor this analyzer project's version.json and produce a unique assembly identity per commit. -->
    <GitVersionBaseDirectory>$(MSBuildProjectDirectory)</GitVersionBaseDirectory>
</PropertyGroup>
```

Apply this override after any shared property import that sets the value, or otherwise ensure normal MSBuild precedence leaves the project-local value effective. Do not add the override blindly: first locate the controlling assignment and confirm which `version.json` the evaluated analyzer project uses.

Validate the built assembly rather than only inspecting MSBuild source. Build the same analyzer from two distinct commits when practical and confirm that `AssemblyName.Version` differs. At minimum, inspect the evaluated `GitVersionBaseDirectory`, the selected version file, and the resulting assembly version. Repeat this for every analyzer assembly, not merely the packaging or code-fix project.

## Test structure

Use `Microsoft.CodeAnalysis.Testing` or the repository's established equivalent. Keep one test class per diagnostic rule for analyzer and code-fix behavior together. Do not create a separate code-fix test class. Even when a diagnostic intentionally has no fix, instantiate the same analyzer-plus-code-fix harness with `EmptyCodeFixProvider`; this keeps all cases and configuration for the rule together.

Annotate every verifier/helper parameter that accepts C# source with `[StringSyntax("c#-test")]` from `System.Diagnostics.CodeAnalysis`. This enables syntax highlighting and language services at test call sites. Start from [CSharpCodeFixVerifier.cs](./references/CSharpCodeFixVerifier.cs) for a complete example and apply the attribute to analyzer-only and generator test helpers as well. If the target framework does not define `StringSyntaxAttribute`, use the repository's compatibility/polyfill pattern.

When C# source is assigned to a local variable instead of passed directly to an annotated parameter, add a language comment before the literal:

```csharp
const string source = /* lang=c#-test */ """
    // C# code here
    """;
```

Prefer inline markup to hard-coded line/column spans:

```csharp
using Verify = CSharpCodeFixVerifier<AvoidBlockingAnalyzer, AvoidBlockingCodeFix>;

public class AvoidBlockingTests
{
    [Fact]
    public async Task ReportsBlockingCallAndFixesIt()
    {
        const string source = /* lang=c#-test */ """
            using System.Threading.Tasks;
            class C
            {
                void M(Task task) => [|task.Wait()|];
            }
            """;
        const string fixedSource = /* lang=c#-test */ """
            using System.Th...

© github · MIT · github/awesome-copilot

Смотрите также

Спонсоры проекта

Компании, которые помогают держать аналитику открытой