Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug mixing .NET SDKs #73

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/Shared/DotnetUtil.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Build.Logging.StructuredLogger;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration.Internal;
using System.Diagnostics;
Expand All @@ -12,11 +14,34 @@ namespace Basic.CompilerLog;

internal static class DotnetUtil
{
private static readonly Lazy<Dictionary<string, string>> _lazyDotnetEnvironmentVariables = new(CreateDotnetEnvironmentVariables);

private static Dictionary<string, string> CreateDotnetEnvironmentVariables()
{
// The CLI, particularly when run from dotnet test, will set the MSBuildSDKsPath environment variable
// to point to the current SDK. That could be an SDK that is higher than the version that our tests
// are executing under. For example `dotnet test` could spawn an 8.0 process but we end up testing
// the 7.0.400 SDK. This environment variable though will point to 8.0 and end up causing load
// issues. Clear it out here so that the `dotnet` commands have a fresh context.
var map = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (DictionaryEntry entry in Environment.GetEnvironmentVariables())
{
var key = (string)entry.Key;
if (!string.Equals(key, "MSBuildSDKsPath", StringComparison.OrdinalIgnoreCase))
{
map.Add(key, (string)entry.Value!);

}
}
return map;
}

internal static ProcessResult Command(string args, string? workingDirectory = null) =>
ProcessUtil.Run(
"dotnet",
args,
workingDirectory: workingDirectory);
workingDirectory: workingDirectory,
environment: _lazyDotnetEnvironmentVariables.Value);

internal static void CommandOrThrow(string args, string? workingDirectory = null)
{
Expand Down
16 changes: 14 additions & 2 deletions src/Shared/ProcessUtil.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MessagePack.Formatters;

namespace Basic.CompilerLog;

Expand All @@ -28,7 +30,8 @@ internal static class ProcessUtil
internal static ProcessResult Run(
string fileName,
string args,
string? workingDirectory = null)
string? workingDirectory = null,
Dictionary<string, string>? environment = null)
{
var info = new ProcessStartInfo()
{
Expand All @@ -40,11 +43,20 @@ internal static ProcessResult Run(
RedirectStandardError = true,
};

if (environment is not null)
{
info.Environment.Clear();
foreach (var tuple in environment)
{
info.Environment.Add(tuple.Key, tuple.Value);
}
}

var process = Process.Start(info)!;
var standardOut = process.StandardOutput.ReadToEnd();
var standardError = process.StandardError.ReadToEnd();

process.WaitForExit();

return new ProcessResult(
process.ExitCode,
standardOut,
Expand Down