This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
ReferenceProtector is a NuGet package that protects codebases from unwanted dependencies by validating project and package references during build time against user-defined rules. It consists of:
- MSBuild Task (
ReferenceProtector.Tasks) - Collects project and package references at build time - Roslyn Analyzer (
ReferenceProtector.Analyzers) - Validates references against dependency rules and reports diagnostics - Package Project (
ReferenceProtector.Package) - Bundles everything into a distributable NuGet package
# Build the entire solution
dotnet build ReferenceProtector.sln
# Build a specific project
dotnet build src/Package/ReferenceProtector.Package.csproj
# Build and create NuGet package (outputs to artifacts/ folder)
dotnet build src/Package/ReferenceProtector.Package.csproj -c Release
# Clean build artifacts
dotnet clean# Run all tests
dotnet test
# Run tests for a specific project
dotnet test src/Analyzers/ReferenceProtector.Analyzers.Tests/ReferenceProtector.Analyzers.Tests.csproj
dotnet test src/Tasks/ReferenceProtector.Tasks.UnitTests/ReferenceProtector.Tasks.UnitTests.csproj
dotnet test src/Tasks/ReferenceProtector.Tasks.IntegrationTests/ReferenceProtector.Tasks.IntegrationTests.csproj
dotnet test tests/ReferenceProtector.IntegrationTests/ReferenceProtector.IntegrationTests.csproj
# Run a single test by filter
dotnet test --filter "FullyQualifiedName~TestMethodName"The package uses a two-phase approach integrated into the MSBuild pipeline:
Phase 1: MSBuild Task (CollectAllReferences)
- Runs during
CoreCompileDependsOntarget before compilation - Declared in
src/Build/ReferenceProtector.targets - Task located at
src/Tasks/ReferenceProtector.Tasks/CollectAllReferences.cs - Collects all direct and transitive project references from MSBuild's
@(ProjectReference)items - Collects direct package references from
@(PackageReference)items - Uses
NuGetPackageIdmetadata to distinguish transitive project references - Outputs to
obj/Debug/_ReferenceProtector_DeclaredReferences.tsvfile - This file is then registered as an
AdditionalFilefor the analyzer
Phase 2: Roslyn Analyzer (ReferenceProtectorAnalyzer)
- Located at
src/Analyzers/ReferenceProtector.Analyzers/ReferenceProtector.Analyzers.cs - Reads the
_ReferenceProtector_DeclaredReferences.tsvfile viaAdditionalFiles - Loads dependency rules from the JSON file specified by
DependencyRulesFileMSBuild property - Matches each reference against rules using regex patterns (with
*expansion) - Reports diagnostics (RP0001-RP0005) for rule violations
Shared Models (src/Shared/)
ReferenceItem.cs- Represents a reference (source, target, link type)- Shared between MSBuild task and analyzer via linked files in project files
Dependency Rules Schema (src/Analyzers/ReferenceProtector.Analyzers/Models/DependencyRules.cs)
ProjectDependencies[]- Rules for project-to-project references with LinkType supportPackageDependencies[]- Rules for package references (only direct)- Each rule has: From (regex), To (regex), Policy (Allowed/Forbidden), Description, Exceptions[]
Configuration
EnableReferenceProtectorMSBuild property controls whether the feature runs (default: true)DependencyRulesFileMSBuild property specifies the path to the rules JSON file- These properties are made visible to the analyzer via
CompilerVisiblePropertyinsrc/Build/ReferenceProtector.props
src/Analyzers/- Roslyn analyzer that validates referencessrc/Tasks/- MSBuild task that collects referencessrc/Build/- MSBuild .props and .targets filessrc/BuildMultiTargeting/- Multi-targeting MSBuild supportsrc/Package/- NuGet package project (uses Microsoft.Build.NoTargets SDK)src/Shared/- Code shared between analyzer and tasks (linked as source)tests/- Integration testssamples/- Sample projects demonstrating usage (ClassA, ClassB, ClassC with DependencyRules.json)
- RP0001: DependencyRulesFile property not provided
- RP0002: Invalid dependency rules JSON format
- RP0003: No rules matched the current project
- RP0004: Project reference violates a rule
- RP0005: Package reference violates a rule
Pattern Matching Logic (ReferenceProtectorAnalyzer:255)
- Patterns are regex with
*replaced by.*and$appended - Case-insensitive matching
Rule Evaluation Logic (ReferenceProtectorAnalyzer:192-197)
- For
Forbiddenpolicies: violation if no exceptions match - For
Allowedpolicies: violation if any exception matches
LinkType Handling (ReferenceProtectorAnalyzer:180-181)
- Direct: Matches only
ProjectReferenceDirect - Transitive: Matches only
ProjectReferenceTransitive - DirectOrTransitive: Matches both
Central Package Management is enabled (Directory.Packages.props). Key dependencies:
- Microsoft.Build.Utilities.Core - for MSBuild task
- Microsoft.CodeAnalysis.CSharp - for Roslyn analyzer
- NuGet.ProjectModel - for NuGet assets parsing
- System.Text.Json - for parsing dependency rules (copied to analyzer output)
- xunit.v3 - for testing
Uses Nerdbank.GitVersioning (version.json in repo root) for semantic versioning.
- The analyzer copies System.Text.Json.dll to its output via a custom target (
CopyDependenciesToOutputin the .csproj) - Shared code uses linked compilation (
<Compile Include="..\..\Shared\*.cs" />) - Package outputs to
artifacts/folder - The sample projects in
samples/demonstrate the feature with a workingDependencyRules.json