Skip to content

Commit b858170

Browse files
authored
fix: Location not working for cross-assembly compilations (#20)
* fix: Location not working for cross-assembly compilatins * Remove unneeded using statement
1 parent 65e2def commit b858170

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/Splat.DependencyInjection.SourceGenerator/MetadataDependencyChecker.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77

88
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.CSharp.Syntax;
910

1011
using ReactiveMarbles.RoslynHelpers;
1112

@@ -43,10 +44,12 @@ public static List<MethodMetadata> CheckMetadata(GeneratorExecutionContext conte
4344
{
4445
if (childConstructor.TypeName == metadataMethod.InterfaceTypeName)
4546
{
47+
var location = childConstructor.Parameter.GetLocation(metadataMethod.MethodInvocation);
48+
4649
context.ReportDiagnostic(
4750
Diagnostic.Create(
4851
DiagnosticWarnings.ConstructorsMustNotHaveCircularDependency,
49-
childConstructor.Parameter.Locations.FirstOrDefault(x => x is not null) ?? metadataMethod.MethodInvocation.GetLocation()));
52+
location));
5053
isError = true;
5154
}
5255
}
@@ -65,10 +68,12 @@ public static List<MethodMetadata> CheckMetadata(GeneratorExecutionContext conte
6568

6669
if (metadataDependencies.TryGetValue(lazyType.ToDisplayString(RoslynCommonHelpers.TypeFormat), out dependencyMethod) && !dependencyMethod.IsLazy)
6770
{
71+
var location = constructorDependency.Parameter.GetLocation(metadataMethod.MethodInvocation);
72+
6873
context.ReportDiagnostic(
6974
Diagnostic.Create(
7075
DiagnosticWarnings.LazyParameterNotRegisteredLazy,
71-
constructorDependency.Parameter.Locations.FirstOrDefault(x => x is not null) ?? metadataMethod.MethodInvocation.GetLocation(),
76+
location,
7277
metadataMethod.ConcreteTypeName,
7378
constructorDependency.Parameter.Name));
7479
isError = true;
@@ -84,5 +89,17 @@ public static List<MethodMetadata> CheckMetadata(GeneratorExecutionContext conte
8489

8590
return methods;
8691
}
92+
93+
private static Location GetLocation(this ISymbol symbol, InvocationExpressionSyntax backupInvocation)
94+
{
95+
var location = symbol.Locations.FirstOrDefault();
96+
97+
if (location?.Kind != LocationKind.SourceFile)
98+
{
99+
location = backupInvocation.GetLocation();
100+
}
101+
102+
return location;
103+
}
87104
}
88105
}
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>netstandard2.0</TargetFramework>
44
<DevelopmentDependency>true</DevelopmentDependency>
5-
<IncludeBuildOutput>false</IncludeBuildOutput>
6-
<!-- forces SDK to copy dependencies into build output to make packing easier -->
7-
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
85
<PackageDescription>Produces DI registration for both property and constructor injection using the Splat locators.</PackageDescription>
9-
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);PackBuildOutputs</TargetsForTfmSpecificContentInPackage>
106
<NoWarn>$(NoWarn);AD0001</NoWarn>
7+
<BuildOutputTargetFolder>analyzers</BuildOutputTargetFolder>
8+
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
119
</PropertyGroup>
1210

1311
<ItemGroup>
1412
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" PrivateAssets="all" />
1513
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
16-
<PackageReference Include="ReactiveMarbles.RoslynHelpers" Version="1.0.9" PrivateAssets="all" />
14+
<PackageReference Include="ReactiveMarbles.RoslynHelpers" GeneratePathProperty="true" Version="1.0.9" PrivateAssets="all" />
1715
</ItemGroup>
1816

19-
<Target Name="PackBuildOutputs">
17+
<PropertyGroup>
18+
</PropertyGroup>
19+
20+
<Target Name="GetDependencyTargetPaths">
2021
<ItemGroup>
21-
<TfmSpecificPackageFile Include="$(OutputPath)\**\*" PackagePath="analyzers/dotnet/cs" Visible="false" />
22+
<TargetPathWithTargetPlatformMoniker Include="$(PKGReactiveMarbles_RoslynHelpers)\lib\netstandard2.0\ReactiveMarbles.RoslynHelpers.dll" IncludeRuntimeDependency="false" />
2223
</ItemGroup>
2324
</Target>
2425
</Project>

0 commit comments

Comments
 (0)