Skip to content

Commit 8d0c9e1

Browse files
authored
Merge pull request #176 from leancodepl/fix/embedded-version-conflicts
Process each compilation separately when searching for embedded contracts
2 parents 796b016 + 8dcf674 commit 8d0c9e1

1 file changed

Lines changed: 26 additions & 22 deletions

File tree

src/LeanCode.ContractsGenerator/Compilation/ContractsCompiler.cs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -133,35 +133,39 @@ private static CSharpCompilationOptions PrepareCompilationOptions() =>
133133
)]
134134
private static List<Export> TryLoadEmbeddedContracts(IReadOnlyCollection<CSharpCompilation> compilations)
135135
{
136-
var externalReferences = compilations
137-
.SelectMany(c => c.ExternalReferences)
138-
.OfType<PortableExecutableReference>()
139-
.Where(per => !string.IsNullOrEmpty(per.FilePath) && File.Exists(per.FilePath))
140-
.Select(per => per.FilePath!)
141-
.Distinct()
142-
.ToList();
143-
144-
using var context = new MetadataLoadContext(new PathAssemblyResolver(externalReferences));
145136
var exports = new List<Export>();
137+
var processed = new HashSet<string>();
146138

147-
foreach (var assemblyPath in externalReferences)
139+
foreach (var compilation in compilations)
148140
{
149-
try
141+
var externalReferences = compilation
142+
.ExternalReferences.OfType<PortableExecutableReference>()
143+
.Where(per => !string.IsNullOrEmpty(per.FilePath) && File.Exists(per.FilePath))
144+
.Select(per => per.FilePath!)
145+
.Distinct()
146+
.ToList();
147+
148+
using var context = new MetadataLoadContext(new PathAssemblyResolver(externalReferences));
149+
150+
foreach (var assemblyPath in externalReferences.Where(p => !processed.Contains(p)))
150151
{
151-
var assembly = context.LoadFromAssemblyPath(assemblyPath);
152-
using var stream = assembly.GetManifestResourceStream("LeanCode.Contracts.pb");
152+
try
153+
{
154+
var assembly = context.LoadFromAssemblyPath(assemblyPath);
155+
using var stream = assembly.GetManifestResourceStream("LeanCode.Contracts.pb");
156+
157+
if (stream is not null)
158+
{
159+
exports.Add(Export.Parser.ParseFrom(stream));
160+
}
153161

154-
if (stream is null)
162+
processed.Add(assemblyPath);
163+
}
164+
catch (Exception e)
155165
{
156-
continue;
166+
Console.Error.WriteLine($"Failed to search assembly at {assemblyPath} for embedded contracts.");
167+
Console.Error.WriteLine(e.ToString());
157168
}
158-
159-
exports.Add(Export.Parser.ParseFrom(stream));
160-
}
161-
catch (Exception e)
162-
{
163-
Console.Error.WriteLine($"Failed to search assembly at {assemblyPath} for embedded contracts.");
164-
Console.Error.WriteLine(e.ToString());
165169
}
166170
}
167171

0 commit comments

Comments
 (0)