From dfbef9d1619bc5a995d13ee2098ea3b4af3fdd80 Mon Sep 17 00:00:00 2001 From: David Nguyen Date: Thu, 22 Jan 2026 02:19:50 -0500 Subject: [PATCH] Fix placeholder proto namespace to use project's actual namespace The WriteProtoFileTask now receives RootNamespace and AssemblyName from MSBuild and uses them for the placeholder proto's csharp_namespace instead of hardcoded "Generated.Grpc". This ensures GrpcGenerator can find the service base types on first build, enabling gRPC service registration to work without requiring a second build. Co-Authored-By: Claude Opus 4.5 --- .../WriteProtoFileTask.cs | 21 +++++++++++++++++-- .../build/Svrnty.CQRS.Grpc.Generators.targets | 4 +++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Svrnty.CQRS.Grpc.Generators/WriteProtoFileTask.cs b/Svrnty.CQRS.Grpc.Generators/WriteProtoFileTask.cs index 70788ba..455999b 100644 --- a/Svrnty.CQRS.Grpc.Generators/WriteProtoFileTask.cs +++ b/Svrnty.CQRS.Grpc.Generators/WriteProtoFileTask.cs @@ -39,6 +39,16 @@ public class WriteProtoFileTask : Task [Required] public string ProtoFileName { get; set; } = string.Empty; + /// + /// The root namespace of the project (optional, falls back to AssemblyName) + /// + public string RootNamespace { get; set; } = string.Empty; + + /// + /// The assembly name of the project (used for proto namespace if RootNamespace not set) + /// + public string AssemblyName { get; set; } = string.Empty; + public override bool Execute() { try @@ -65,13 +75,20 @@ public class WriteProtoFileTask : Task // Write a minimal placeholder proto file so Grpc.Tools doesn't fail // The real content will be generated on the next build - var placeholderProto = @"syntax = ""proto3""; + // Use project's namespace so GrpcGenerator can find the service base types + var projectNamespace = !string.IsNullOrEmpty(RootNamespace) ? RootNamespace + : !string.IsNullOrEmpty(AssemblyName) ? AssemblyName + : "Generated"; + var grpcNamespace = $"{projectNamespace}.Grpc"; -option csharp_namespace = ""Generated.Grpc""; + var placeholderProto = $@"syntax = ""proto3""; + +option csharp_namespace = ""{grpcNamespace}""; package cqrs; // Placeholder proto file - will be regenerated on next build +// Using namespace: {grpcNamespace} "; var placeholderOutputPath = Path.Combine(ProjectDirectory, OutputDirectory); Directory.CreateDirectory(placeholderOutputPath); diff --git a/Svrnty.CQRS.Grpc.Generators/build/Svrnty.CQRS.Grpc.Generators.targets b/Svrnty.CQRS.Grpc.Generators/build/Svrnty.CQRS.Grpc.Generators.targets index e31cc0b..e4584c1 100644 --- a/Svrnty.CQRS.Grpc.Generators/build/Svrnty.CQRS.Grpc.Generators.targets +++ b/Svrnty.CQRS.Grpc.Generators/build/Svrnty.CQRS.Grpc.Generators.targets @@ -32,6 +32,8 @@ ProjectDirectory="$(MSBuildProjectDirectory)" IntermediateOutputPath="$(IntermediateOutputPath)" OutputDirectory="$(ProtoOutputDirectory)" - ProtoFileName="$(GeneratedProtoFileName)" /> + ProtoFileName="$(GeneratedProtoFileName)" + RootNamespace="$(RootNamespace)" + AssemblyName="$(AssemblyName)" />