Skip to content

Commit

Permalink
Fix output directory naming (#186)
Browse files Browse the repository at this point in the history
The output directory naming scheme didn't take into account satellite
assemblies. That is a case where many different `CompilerCall` will have
the same project name. To generate a unique name need to consider all
the names at the same time, vs individually. After this change can
successfully export logs that have many satellite compilations.
  • Loading branch information
jaredpar authored Dec 3, 2024
1 parent 479c898 commit 52b1013
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Features" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Features" />
<PackageReference Include="System.Buffers" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" >
Expand Down
23 changes: 0 additions & 23 deletions src/Basic.CompilerLog.UnitTests/app.config

This file was deleted.

76 changes: 54 additions & 22 deletions src/Basic.CompilerLog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ int RunReferences(IEnumerable<string> args)

using var reader = GetCompilerCallReader(extra, BasicAnalyzerKind.None);
var compilerCalls = reader.ReadAllCompilerCalls(options.FilterCompilerCalls);
var compilerCallNames = GetCompilerCallNames(compilerCalls);

baseOutputPath = GetBaseOutputPath(baseOutputPath, "refs");
WriteLine($"Copying references to {baseOutputPath}");
Expand All @@ -253,15 +254,15 @@ int RunReferences(IEnumerable<string> args)
for (int i = 0; i < compilerCalls.Count; i++)
{
var compilerCall = compilerCalls[i];
var refDirPath = Path.Combine(GetOutputPath(baseOutputPath, compilerCalls, i), "refs");
var refDirPath = Path.Combine(baseOutputPath, compilerCallNames[i], "refs");
Directory.CreateDirectory(refDirPath);
foreach (var data in reader.ReadAllReferenceData(compilerCall))
{
var filePath = Path.Combine(refDirPath, data.FileName);
WriteTo(data.AssemblyData, filePath);
}

var analyzerDirPath = Path.Combine(GetOutputPath(baseOutputPath, compilerCalls, i), "analyzers");
var analyzerDirPath = Path.Combine(baseOutputPath, compilerCallNames[i], "analyzers");
var groupMap = new Dictionary<string, string>(PathUtil.Comparer);
foreach (var data in reader.ReadAllAnalyzerData(compilerCall))
{
Expand Down Expand Up @@ -335,6 +336,7 @@ int RunExport(IEnumerable<string> args)
using var compilerLogStream = GetOrCreateCompilerLogStream(extra);
using var reader = GetCompilerLogReader(compilerLogStream, leaveOpen: true, options.BasicAnalyzerKind);
var compilerCalls = reader.ReadAllCompilerCalls(options.FilterCompilerCalls);
var compilerCallNames = GetCompilerCallNames(compilerCalls);
var exportUtil = new ExportUtil(reader, includeAnalyzers: options.IncludeAnalyzers);

baseOutputPath = GetBaseOutputPath(baseOutputPath, "export");
Expand All @@ -345,7 +347,7 @@ int RunExport(IEnumerable<string> args)
for (int i = 0; i < compilerCalls.Count; i++)
{
var compilerCall = compilerCalls[i];
var exportDir = GetOutputPath(baseOutputPath, compilerCalls, i);
var exportDir = Path.Combine(baseOutputPath, compilerCallNames[i]);
exportUtil.Export(compilerCall, exportDir, sdkDirs);
}

Expand Down Expand Up @@ -405,12 +407,13 @@ int RunResponseFile(IEnumerable<string> args)
}

var compilerCalls = reader.ReadAllCompilerCalls(options.FilterCompilerCalls);
var compilerCallNames = GetCompilerCallNames(compilerCalls);
for (int i = 0; i < compilerCalls.Count; i++)
{
var compilerCall = compilerCalls[i];
var rspDirPath = inline
? compilerCall.ProjectDirectory
: GetOutputPath(baseOutputPath, compilerCalls, i);
: Path.Combine(baseOutputPath, compilerCallNames[i]);
Directory.CreateDirectory(rspDirPath);
var rspFilePath = Path.Combine(rspDirPath, GetRspFileName());
using var writer = new StreamWriter(rspFilePath, append: false, Encoding.UTF8);
Expand All @@ -420,7 +423,7 @@ string GetRspFileName()
{
if (inline)
{
return IsSingleTarget(compilerCalls, i)
return IsSingleTarget(compilerCall, compilerCalls)
? "build.rsp"
: $"build-{compilerCall.TargetFramework}.rsp";
}
Expand Down Expand Up @@ -478,6 +481,7 @@ int RunReplay(IEnumerable<string> args)
return ExitFailure;
}

var compilerCallNames = GetCompilerCallNames(compilerCalls);
var sdkDirs = SdkUtil.GetSdkDirectories();
var success = true;

Expand All @@ -493,7 +497,7 @@ int RunReplay(IEnumerable<string> args)
IEmitResult emitResult;
if (baseOutputPath is not null)
{
var path = GetOutputPath(baseOutputPath, compilerCalls, i);
var path = Path.Combine(baseOutputPath, compilerCallNames[i]);
Directory.CreateDirectory(path);
emitResult = compilationData.EmitToDisk(path);
}
Expand Down Expand Up @@ -556,6 +560,7 @@ int RunGenerated(IEnumerable<string> args)
return ExitFailure;
}

var compilerCallNames = GetCompilerCallNames(compilerCalls);
for (int i = 0; i < compilerCalls.Count; i++)
{
var compilerCall = compilerCalls[i];
Expand All @@ -579,7 +584,7 @@ int RunGenerated(IEnumerable<string> args)
var fileRelativePath = generatedTree.FilePath.StartsWith(compilerCall.ProjectDirectory, StringComparison.OrdinalIgnoreCase)
? generatedTree.FilePath.Substring(compilerCall.ProjectDirectory.Length + 1)
: Path.GetFileName(generatedTree.FilePath);
var outputPath = GetOutputPath(baseOutputPath, compilerCalls, i);
var outputPath = Path.Combine(baseOutputPath, compilerCallNames[i]);
var filePath = Path.Combine(outputPath, fileRelativePath);
Directory.CreateDirectory(Path.GetDirectoryName(filePath)!);
File.WriteAllText(filePath, generatedTree.ToString());
Expand Down Expand Up @@ -822,26 +827,53 @@ string GetBaseOutputPath(string? baseOutputPath, string? directoryName = null)
return baseOutputPath;
}

string GetOutputPath(string baseOutputPath, List<CompilerCall> compilerCalls, int index)
// Is the project for this <see cref="CompilerCall"/> only occur once in the list as a
// non-satellite assembly?
static bool IsSingleTarget(CompilerCall compilerCall, List<CompilerCall> compilerCalls)
{
var projectName = GetProjectUniqueName(compilerCalls, index);
return Path.Combine(baseOutputPath, projectName);
return compilerCalls.Count(x =>
x.ProjectFilePath == compilerCall.ProjectFilePath &&
x.Kind == CompilerCallKind.Regular) == 1;
}

bool IsSingleTarget(List<CompilerCall> compilerCalls, int index)
// Convert the CompilerCall instances into a list of unique names that are
// valid file names
List<string> GetCompilerCallNames(List<CompilerCall> compilerCalls)
{
var compilerCall = compilerCalls[index];
var name = Path.GetFileNameWithoutExtension(compilerCall.ProjectFileName);
return compilerCalls.Count(x => x.ProjectFilePath == compilerCall.ProjectFilePath) == 1;
}
var hashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var list = new List<string>();
foreach (var compilerCall in compilerCalls)
{
var name = GetName(compilerCall, compilerCalls);
if (!hashSet.Add(name))
{
var suffix = 1;
string newName;
do
{
newName = $"{name}-{suffix}";
suffix++;
} while (!hashSet.Add(newName));

string GetProjectUniqueName(List<CompilerCall> compilerCalls, int index)
{
var compilerCall = compilerCalls[index];
var name = Path.GetFileNameWithoutExtension(compilerCall.ProjectFileName);
return IsSingleTarget(compilerCalls, index)
? name
: $"{name}-{compilerCall.TargetFramework}";
name = newName;
}

list.Add(name);
}

return list;

string GetName(CompilerCall compilerCall, List<CompilerCall> compilerCalls)
{
var name = Path.GetFileNameWithoutExtension(compilerCall.ProjectFileName);
return compilerCall.Kind switch
{
CompilerCallKind.Regular => string.IsNullOrEmpty(compilerCall.TargetFramework) || IsSingleTarget(compilerCall, compilerCalls)
? name
: $"{name}-{compilerCall.TargetFramework}",
_ => $"{name}-{compilerCall.Kind.ToString().ToLowerInvariant()}",
};
}
}

static string GetResolvedPath(string baseDirectory, string path)
Expand Down
4 changes: 4 additions & 0 deletions src/Scratch/Scratch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
using TraceReloggerLib;

#pragma warning disable 8321

var temp = CompilerLogUtil.GetOrCreateCompilerLogStream(@"C:\Users\jaredpar\code\roslyn\src\Compilers\Core\Portable\msbuild.binlog");


Bing();
void Bing()
{
Expand Down

0 comments on commit 52b1013

Please sign in to comment.