Skip to content

Commit 90d3a14

Browse files
committed
LAMBJ-127 Transitive Project Reference Warnings (#363)
* LAMBJ-127 Transitive Project Reference Warnings * LAMBJ-127 Update Release Notes
1 parent ba85c5f commit 90d3a14

File tree

5 files changed

+60
-19
lines changed

5 files changed

+60
-19
lines changed

.github/releases/v0.8.1.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
## Bug Fixes
22

3-
- Fixed an issue where StyleCop and other analyzers were analyzing generated code and causing a bunch of warnings/errors depending on how you have them configured.
3+
- Fixed an issue where StyleCop and other analyzers were analyzing generated code and causing a bunch of warnings/errors depending on how you have them configured.
4+
- Fixed an issue where projects that had a transitive reference to Lambdajection would produce a warning about how the generator would not contribute any sources, along with not being able to find the "Microsoft.Extensions.Hosting" assembly.

src/Generator/Program.cs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ public void Initialize(GeneratorInitializationContext context)
2020
public void Execute(GeneratorExecutionContext context)
2121
{
2222
var buildTimeAssemblies = GetBuildTimeAssemblies(context);
23+
24+
// Generation will be impossible without the build time assemblies,
25+
// aka generator dependencies.
26+
if (!buildTimeAssemblies.Any())
27+
{
28+
return;
29+
}
30+
2331
foreach (var buildTimeAssembly in buildTimeAssemblies)
2432
{
2533
try
@@ -34,28 +42,33 @@ public void Execute(GeneratorExecutionContext context)
3442
var options = context.AnalyzerConfigOptions.GlobalOptions;
3543
options.TryGetValue("build_property.LambdajectionAdditionalProbingPath", out var additionalProbingPath);
3644

37-
if (additionalProbingPath != null)
45+
// Dependencies other than the ones provided explicitly as build time assemblies,
46+
// the generator may use other dependencies in the restore packages path.
47+
// If we cannot access those, there is no point attempting generation.
48+
if (string.IsNullOrEmpty(additionalProbingPath))
3849
{
39-
AssemblyLoadContext.Default.Resolving += (_, name) =>
40-
{
41-
var matchingFiles = from file in Directory.GetFiles(additionalProbingPath, $"{name.Name}.dll", SearchOption.AllDirectories)
42-
where file.Contains("netstandard") || file.Contains("net5.0")
43-
select file;
50+
return;
51+
}
4452

45-
foreach (var matchingFile in matchingFiles)
53+
AssemblyLoadContext.Default.Resolving += (_, name) =>
54+
{
55+
var matchingFiles = from file in Directory.GetFiles(additionalProbingPath, $"{name.Name}.dll", SearchOption.AllDirectories)
56+
where file.Contains("netstandard") || file.Contains("net5.0")
57+
select file;
58+
59+
foreach (var matchingFile in matchingFiles)
60+
{
61+
try
62+
{
63+
return Assembly.LoadFile(matchingFile);
64+
}
65+
catch (Exception)
4666
{
47-
try
48-
{
49-
return Assembly.LoadFile(matchingFile);
50-
}
51-
catch (Exception)
52-
{
53-
}
5467
}
68+
}
5569

56-
return null;
57-
};
58-
}
70+
return null;
71+
};
5972

6073
var host = new ProgramHost();
6174
host.Run(context);

tests/Compilation/Utils/MSBuildProjectExtensions.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
using Lambdajection.Generator;
77

88
using Microsoft.CodeAnalysis.CSharp;
9+
using Microsoft.CodeAnalysis.Diagnostics;
10+
11+
using NSubstitute;
12+
13+
using static NSubstitute.Arg;
914

1015
#pragma warning disable SA1009
1116
namespace Microsoft.CodeAnalysis.MSBuild
@@ -23,7 +28,23 @@ public static async Task<ImmutableArray<Diagnostic>> GetGeneratorDiagnostics(thi
2328
{
2429
var compilation = (await project.GetCompilationAsync())!;
2530
var generator = new Program();
26-
var driver = CSharpGeneratorDriver.Create(new[] { generator });
31+
var optionsProvider = Substitute.For<AnalyzerConfigOptionsProvider>();
32+
var options = Substitute.For<AnalyzerConfigOptions>();
33+
optionsProvider.GlobalOptions.Returns(options);
34+
35+
options.TryGetValue(Is("build_property.LambdajectionBuildTimeAssemblies"), out Any<string?>()).Returns(x =>
36+
{
37+
x[1] = TestMetadata.OutputPath + "/Lambdajection.Core.dll";
38+
return true;
39+
});
40+
41+
options.TryGetValue(Is("build_property.LambdajectionAdditionalProbingPath"), out Any<string?>()).Returns(x =>
42+
{
43+
x[1] = TestMetadata.RestorePackagesPath;
44+
return true;
45+
});
46+
47+
var driver = CSharpGeneratorDriver.Create(generators: new[] { generator }, optionsProvider: optionsProvider);
2748
driver.RunGeneratorsAndUpdateCompilation(compilation, out var _, out var diagnostics);
2849
return diagnostics;
2950
}

tests/TestMetadata.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ internal class TestMetadata
88

99
public static string BaseIntermediateOutputPath => @"__BaseIntermediateOutputPath__";
1010

11+
public static string OutputPath => @"__OutputPath__";
12+
13+
public static string RestorePackagesPath => @"__BaseIntermediateOutputPath__";
14+
1115
public static string TargetFramework => "__TargetFramework__";
1216

1317
public static string Configuration => "__Configuration__";

tests/Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
.Replace('__PackageVersion__', '$(PackageVersion)')
4848
.Replace('__BaseOutputPath__', '$(MSBuildThisFileDirectory)../bin')
4949
.Replace('__BaseIntermediateOutputPath__', '$(MSBuildThisFileDirectory)../obj')
50+
.Replace('__OutputPath__', '$(OutputPath)')
51+
.Replace('__RestorePackagesPath__', '$(RestorePackagesPath)')
5052
.Replace('__TargetFramework__', '$(TargetFramework)')
5153
.Replace('__Configuration__', '$(Configuration)')
5254
)

0 commit comments

Comments
 (0)