Skip to content

C#: NuGet dependency workspace for template type attribution#7061

Merged
knutwannheden merged 3 commits intomainfrom
csharp/dependency-workspace
Mar 19, 2026
Merged

C#: NuGet dependency workspace for template type attribution#7061
knutwannheden merged 3 commits intomainfrom
csharp/dependency-workspace

Conversation

@knutwannheden
Copy link
Contributor

Summary

  • Add DependencyWorkspace that resolves NuGet packages into Roslyn MetadataReferences using Microsoft.CodeAnalysis.Testing.ReferenceAssemblies — no subprocess or temp .csproj needed
  • When dependencies are provided to CSharpTemplate.Create or CSharpPattern.Create, the scaffold is parsed with a SemanticModel backed by resolved package references, enabling type attribution from external packages
  • Resolved references are cached by dependency set hash for performance
  • Includes CaptureKind enum and position-specific factories (Capture.Expression, Capture.Type, Capture.Name) from C#: Add CaptureKind to template captures for position-aware scaffolding #7059

Test plan

  • Template with Newtonsoft.Json dependency has full type attribution (MethodType.DeclaringType == "Newtonsoft.Json.JsonConvert")
  • Same template without dependencies has MethodType == null (confirms attribution comes from the dependency)
  • Pattern with typed capture + dependencies gets type attribution
  • Pattern with untyped capture + dependencies parses successfully
  • Templates without dependencies continue to work as before
  • All 141 template tests pass

Captures now carry an internal `CaptureKind` (Expression, Type, Name)
that tells the template engine what syntactic position the placeholder
occupies. This allows `BuildTypePreamble` to dispatch on kind and
generate appropriate scaffold code for each position.

New factory methods `Capture.Type()` and `Capture.Name()` create
captures with the correct kind pre-set while preserving compile-time
type safety via the generic parameter.
- Thread `dependencies` (IReadOnlyDictionary<string, string>) through
  CSharpTemplate.Create, CSharpPattern.Create, and TemplateEngine.Parse
  for future NuGet-based type attribution during scaffold parsing
- Include dependencies in the template cache key
- Add Capture.Expression() factory with explicit CaptureKind.Expression
- Update Of<T> docs to steer toward position-specific factories
…lates

When templates or patterns specify NuGet dependencies, the scaffold is
now parsed with a SemanticModel backed by resolved package references.
This enables Roslyn to attribute types from external packages (e.g.,
Newtonsoft.Json.JsonConvert) during template parsing.

Uses Microsoft.CodeAnalysis.Testing.ReferenceAssemblies for NuGet
resolution — no subprocess or temp .csproj needed. Resolved references
are cached by dependency set hash.
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Mar 19, 2026
@knutwannheden knutwannheden merged commit 662c5cc into main Mar 19, 2026
1 check passed
@knutwannheden knutwannheden deleted the csharp/dependency-workspace branch March 19, 2026 16:43
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Mar 19, 2026
knutwannheden added a commit that referenced this pull request Mar 19, 2026
* C#: Add CaptureKind to template captures for position-aware scaffolding

Captures now carry an internal `CaptureKind` (Expression, Type, Name)
that tells the template engine what syntactic position the placeholder
occupies. This allows `BuildTypePreamble` to dispatch on kind and
generate appropriate scaffold code for each position.

New factory methods `Capture.Type()` and `Capture.Name()` create
captures with the correct kind pre-set while preserving compile-time
type safety via the generic parameter.

* C#: Add dependencies parameter and Capture.Expression factory

- Thread `dependencies` (IReadOnlyDictionary<string, string>) through
  CSharpTemplate.Create, CSharpPattern.Create, and TemplateEngine.Parse
  for future NuGet-based type attribution during scaffold parsing
- Include dependencies in the template cache key
- Add Capture.Expression() factory with explicit CaptureKind.Expression
- Update Of<T> docs to steer toward position-specific factories

* C#: Add DependencyWorkspace for NuGet-backed type attribution in templates

When templates or patterns specify NuGet dependencies, the scaffold is
now parsed with a SemanticModel backed by resolved package references.
This enables Roslyn to attribute types from external packages (e.g.,
Newtonsoft.Json.JsonConvert) during template parsing.

Uses Microsoft.CodeAnalysis.Testing.ReferenceAssemblies for NuGet
resolution — no subprocess or temp .csproj needed. Resolved references
are cached by dependency set hash.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant