Skip to content

Commit

Permalink
Fix code review findings
Browse files Browse the repository at this point in the history
  • Loading branch information
tamasvajk committed Apr 22, 2024
1 parent 7b5f2c7 commit 05f3c64
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Sdk(IDotNet dotNet, ILogger logger)
this.dotNet = dotNet;
this.logger = logger;

newestSdkVersion = new Lazy<DotNetVersion?>(GetNewestSdk);
newestSdkVersion = new Lazy<DotNetVersion?>(GetNewestSdkVersion);
cscPath = new Lazy<string?>(GetCscPath);
}

Expand All @@ -46,7 +46,7 @@ private static HashSet<DotNetVersion> ParseSdks(IList<string> listed)
return sdks;
}

private DotNetVersion? GetNewestSdk()
private DotNetVersion? GetNewestSdkVersion()
{
var listed = dotNet.GetListedSdks();
var sdks = ParseSdks(listed);
Expand All @@ -55,14 +55,14 @@ private static HashSet<DotNetVersion> ParseSdks(IList<string> listed)

private string? GetCscPath()
{
var sdk = GetNewestSdk();
if (sdk is null)
var newestSdkVersion = GetNewestSdkVersion();

Check notice

Code scanning / CodeQL

Local scope variable shadows member Note

Local scope variable 'newestSdkVersion' shadows
Sdk.newestSdkVersion
.
if (newestSdkVersion is null)
{
logger.LogWarning("No dotnet SDK found.");
return null;
}

var path = Path.Combine(sdk.FullPath, "Roslyn", "bincore", "csc.dll");
var path = Path.Combine(newestSdkVersion.FullPath, "Roslyn", "bincore", "csc.dll");
logger.LogDebug($"Source generator CSC: '{path}'");
if (!File.Exists(path))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,7 @@ protected override IEnumerable<string> Run()
continue;
}

if (!groupedFiles.TryGetValue(project.File, out var files))
{
files = [];
groupedFiles[project.File] = files;
}

files.Add(additionalFile);
groupedFiles.AddAnother(project.File, additionalFile);
}

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,22 @@ public DotnetSourceGeneratorWrapper(

public IEnumerable<string> RunSourceGenerator(IEnumerable<string> additionalFiles, string csprojFile, IEnumerable<string> references, string targetDir)
{
var name = Guid.NewGuid().ToString("N").ToUpper();
var tempPath = FileUtils.GetTemporaryWorkingDirectory(out var shouldCleanUp);
var analyzerConfig = Path.Combine(tempPath, $"{name}.txt");
var dllPath = Path.Combine(tempPath, $"{name}.dll");
var cscArgsPath = Path.Combine(tempPath, $"{name}.rsp");
var outputFolder = Path.Combine(targetDir, name);
Directory.CreateDirectory(outputFolder);

try
{
var name = Guid.NewGuid().ToString("N").ToUpper();
using var tempDir = new TemporaryDirectory(Path.Join(FileUtils.GetTemporaryWorkingDirectory(out _), "source-generator"), "source generator temporary", logger);
var analyzerConfigPath = Path.Combine(tempDir.DirInfo.FullName, $"{name}.txt");
var dllPath = Path.Combine(tempDir.DirInfo.FullName, $"{name}.dll");
var cscArgsPath = Path.Combine(tempDir.DirInfo.FullName, $"{name}.rsp");
var outputFolder = Path.Combine(targetDir, name);
Directory.CreateDirectory(outputFolder);
logger.LogInfo("Producing analyzer config content.");
GenerateAnalyzerConfig(additionalFiles, csprojFile, analyzerConfig);
GenerateAnalyzerConfig(additionalFiles, csprojFile, analyzerConfigPath);

logger.LogDebug($"Analyzer config content: {File.ReadAllText(analyzerConfig)}");
logger.LogDebug($"Analyzer config content: {File.ReadAllText(analyzerConfigPath)}");

var args = new StringBuilder();
args.Append($"/target:exe /generatedfilesout:\"{outputFolder}\" /out:\"{dllPath}\" /analyzerconfig:\"{analyzerConfig}\" ");
args.Append($"/target:exe /generatedfilesout:\"{outputFolder}\" /out:\"{dllPath}\" /analyzerconfig:\"{analyzerConfigPath}\" ");

foreach (var f in Directory.GetFiles(SourceGeneratorFolder, "*.dll", new EnumerationOptions { RecurseSubdirectories = false, MatchCasing = MatchCasing.CaseInsensitive }))
{
Expand Down Expand Up @@ -91,27 +90,6 @@ public IEnumerable<string> RunSourceGenerator(IEnumerable<string> additionalFile
logger.LogInfo($"Failed to generate source files from {FileType} files: {ex.Message}");
return [];
}

Check notice

Code scanning / CodeQL

Generic catch clause Note

Generic catch clause.
finally
{
if (shouldCleanUp)
{
DeleteFile(analyzerConfig);
DeleteFile(dllPath);
DeleteFile(cscArgsPath);
}
}
}

private void DeleteFile(string path)
{
try
{
File.Delete(path);
}
catch (Exception exc)
{
logger.LogWarning($"Failed to delete file {path}: {exc}");
}
}
}
}
4 changes: 2 additions & 2 deletions csharp/extractor/Semmle.Util/TemporaryDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public sealed class TemporaryDirectory : IDisposable

public DirectoryInfo DirInfo { get; }

public TemporaryDirectory(string name, string userReportedDirectoryPurpose, ILogger logger)
public TemporaryDirectory(string path, string userReportedDirectoryPurpose, ILogger logger)
{
DirInfo = new DirectoryInfo(name);
DirInfo = new DirectoryInfo(path);
DirInfo.Create();
this.userReportedDirectoryPurpose = userReportedDirectoryPurpose;
this.logger = logger;
Expand Down

0 comments on commit 05f3c64

Please sign in to comment.