Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
0bae975
Bump Microsoft.Build to 18.7.1 and align System.* band to 10.0.8
JanProvaznik Jul 9, 2026
c5509d6
Mark ShowMessageForImplicitlySkipAnalyzers multithreadable; fix test …
JanProvaznik Jul 9, 2026
f39df64
migrate csc
OvesN Jul 14, 2026
b64e6b3
Remove redundant server environment clear
OvesN Jul 15, 2026
3ad846e
Merge branch 'main' into dev/veronikao/migrate-csc
OvesN Jul 27, 2026
0255db0
Require callers to explicitly provide environment lookup for runtime …
OvesN Jul 29, 2026
fb98f60
Pass explicit environment snapshots into BuildServerConnection instea…
OvesN Jul 29, 2026
ec9fe01
Merge branch 'main' of https://github.com/dotnet/roslyn into dev/vero…
OvesN Jul 29, 2026
c175fa7
Clean up, revert unnecessary changes
OvesN Jul 29, 2026
b941367
formatting
OvesN Jul 29, 2026
a15813d
Use BannedSymbols
jaredpar Jul 30, 2026
fab0b1c
fixes
jaredpar Jul 30, 2026
c533da2
fixes
jaredpar Jul 30, 2026
f3288fb
fixes
jaredpar Jul 30, 2026
bd3f2d0
new sdk
jaredpar Jul 30, 2026
1a92c26
Merge remote-tracking branch 'origin/features/msbuildmt' into fix-csc…
jaredpar Jul 30, 2026
9448129
fixes
jaredpar Jul 30, 2026
e47dd8c
new sdk version
jaredpar Jul 31, 2026
da0fc32
fixes
jaredpar Aug 4, 2026
80d8ad3
Merge origin/main into fix-csc-task
jaredpar Aug 11, 2026
5258959
PR feedback
jaredpar Aug 11, 2026
e2033b5
Pr feedback
jaredpar Aug 11, 2026
d282f14
more
jaredpar Aug 12, 2026
fa0cf21
more
jaredpar Aug 12, 2026
3ba8a2f
build error
jaredpar Aug 12, 2026
7b28515
Apply suggestion from @jjonescz
jaredpar Aug 13, 2026
dab569d
pr feedback
jaredpar Aug 13, 2026
2f7cafc
Fix MSBuild task path tests
jaredpar Aug 13, 2026
5fac7bd
Document multithreaded MSBuild task helpers
jaredpar Aug 13, 2026
52b773a
pr feedback
jaredpar Aug 14, 2026
305eb0c
Merge remote-tracking branch 'jaredpar/fix-csc-task' into fix-csc-task
jaredpar Aug 14, 2026
d29f6d5
pr feedback
jaredpar Aug 15, 2026
58b9fc3
Apply suggestion from @jaredpar
jaredpar Aug 17, 2026
6e0c557
fix
jaredpar Aug 17, 2026
f8ddfd6
fixes
jaredpar Aug 19, 2026
fd30eaf
more
jaredpar Aug 19, 2026
775c916
pr feedback
jaredpar Aug 19, 2026
259bfda
Move to interface instead of conditional code
jaredpar Aug 21, 2026
6e50163
PR Feedback
jaredpar Aug 25, 2026
ce86426
Fix empty temp path handling
jaredpar Aug 25, 2026
627bbdc
PR feedback
jaredpar Aug 25, 2026
3b1378e
more
jaredpar Aug 25, 2026
9feef99
more
jaredpar Aug 25, 2026
cb14d46
more
jaredpar Aug 25, 2026
77108f1
more
jaredpar Aug 26, 2026
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
14 changes: 14 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@
"problemMatcher": "$msCompile",
"group": "build"
},
{
"label": "build Compilers.slnf with analyzers",
"command": "dotnet",
"type": "shell",
"args": [
"build",
"-p:RunAnalyzersDuringBuild=true",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we set /p:RoslynEnforceCodeStyle=true too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just following the other patterns in this file. Got to be honest ... I'm surprised that these are separate settings. I would assume that enabling analyzers turned on code style enforcement. Is that not the case?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why is that but I think they are separate. Our CI checks set both:

- script: ./eng/build.sh --solution Roslyn.slnx --restore --build --configuration Debug --prepareMachine --ci --binaryLog --runanalyzers --warnaserror /p:RoslynEnforceCodeStyle=true

"-p:GenerateFullPaths=true",
"-tl:off",
"Compilers.slnf"
],
"problemMatcher": "$msCompile",
"group": "build"
},
{
"label": "build Roslyn.slnx",
"command": "dotnet",
Expand Down
8 changes: 8 additions & 0 deletions src/Compilers/Core/MSBuildTask/BannedSymbols.MSBuildMT.txt
Comment thread
jaredpar marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
P:System.Environment.CurrentDirectory
M:System.Environment.GetEnvironmentVariable(System.String);
M:System.Environment.GetEnvironmentVariables();
M:System.Environment.GetCommandLineArgs();
M:System.Environment.GetFolderPath();
M:System.Environment.GetLogicalDrives();
M:System.Environment.SetEnvironmentVariable();
M:System.IO.Directory.GetCurrentDirectory();
1 change: 1 addition & 0 deletions src/Compilers/Core/MSBuildTask/Csc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Microsoft.CodeAnalysis.BuildTasks
/// should be significantly faster with larger projects and have a smaller memory
/// footprint.
/// </summary>
[MSBuildMultiThreadableTask]
public class Csc : ManagedCompiler
{
#region Properties
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/Core/MSBuildTask/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<Compile Include="$(MSBuildThisFileDirectory)CopyRefAssembly.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Csc.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Csi.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)GenerateMSBuildEditorConfig.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IAnalyzerConfigFilesHostObject.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ICompilerOptionsHostObject.cs" />
Expand Down
22 changes: 22 additions & 0 deletions src/Compilers/Core/MSBuildTask/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Build.Framework;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CommandLine;

internal static class Extensions
{
extension(TaskEnvironment taskEnvironment)
{
internal string GetFullPath(string path)
{
var fullPath = taskEnvironment.GetAbsolutePath(path).Value;
return Path.GetFullPath(fullPath);
Comment thread
jaredpar marked this conversation as resolved.
Resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<OutputType>Library</OutputType>
<RootNamespace>Microsoft.CodeAnalysis.BuildTasks</RootNamespace>
<TargetFrameworks>$(NetRoslynSourceBuild);net472</TargetFrameworks>
<DefineConstants>$(DefineConstants);MICROSOFT_CODEANALYSIS_CONTRACTS_NO_VALUE_TASK</DefineConstants>
<DefineConstants>$(DefineConstants);MICROSOFT_CODEANALYSIS_CONTRACTS_NO_VALUE_TASK;MICROSOFT_CODEANALYSIS_MSBUILD_TASK</DefineConstants>
<!--
A .NET copy of this task assembly is included in the .NET SDK's Native AOT CLI, so it must be
clean for trimming, AOT, and single-file publishing. IsAotCompatible (enabled only for the .NET
Expand All @@ -15,6 +15,7 @@
</PropertyGroup>

<ItemGroup>
<AdditionalFiles Include="..\BannedSymbols.MSBuildMT.txt" />
Comment thread
jaredpar marked this conversation as resolved.
Resolved
<InternalsVisibleTo Include="Microsoft.Build.Tasks.CodeAnalysis.UnitTests" />
</ItemGroup>

Expand Down
34 changes: 22 additions & 12 deletions src/Compilers/Core/MSBuildTask/ManagedCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,11 @@ public string GeneratePathToTool()

protected override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands)
{
using var innerLogger = new CompilerServerLogger($"MSBuild {Process.GetCurrentProcess().Id}");
using var innerLogger = new CompilerServerLogger(
$"MSBuild {Process.GetCurrentProcess().Id}",
loggingFilePath: null,
TaskEnvironment.GetEnvironmentVariable,
TaskEnvironment.GetFullPath);
var logger = new TaskCompilerServerLogger(Log, innerLogger);
return ExecuteTool(pathToTool, responseFileCommands, commandLineCommands, logger);
}
Expand Down Expand Up @@ -553,7 +557,10 @@ internal int ExecuteTool(string pathToTool, string responseFileCommands, string
// commandLineCommands (the parameter) may have been mucked with
// (to support using the dotnet cli)
var buildRequestArguments = GenerateCommandLineArgsList(responseFileCommands);
CompilerOptionParseUtilities.PrependFeatureFlagFromEnvironment(buildRequestArguments, logger.Log);
CompilerOptionParseUtilities.PrependFeatureFlagFromEnvironment(
buildRequestArguments,
TaskEnvironment.GetEnvironmentVariable,
logger.Log);
var buildRequest = BuildServerConnection.CreateBuildRequest(
requestId,
Language,
Expand All @@ -571,8 +578,9 @@ internal int ExecuteTool(string pathToTool, string responseFileCommands, string
buildRequest,
pipeName,
clientDirectory,
logger: logger,
cancellationToken: _sharedCompileCts.Token);
TaskEnvironment.GetEnvironmentVariables(),
logger,
_sharedCompileCts.Token);

responseTask.Wait(_sharedCompileCts.Token);

Expand Down Expand Up @@ -628,12 +636,12 @@ public override void Cancel()
/// </summary>
private string CurrentDirectoryToUse()
{
// ToolTask has a method for this. But it may return null. Use the process directory
// if ToolTask didn't override. MSBuild uses the process directory.
// ToolTask has a method for this. But it may return null. Use the task's project
// directory if ToolTask didn't override. This resolves against the task's execution environment.
string workingDirectory = GetWorkingDirectory();
if (string.IsNullOrEmpty(workingDirectory))
{
workingDirectory = Directory.GetCurrentDirectory();
workingDirectory = TaskEnvironment.ProjectDirectory;
}
return workingDirectory;
}
Expand All @@ -643,8 +651,8 @@ private string CurrentDirectoryToUse()
/// </summary>
private string? LibDirectoryToUse()
{
// First check the real environment.
string? libDirectory = Environment.GetEnvironmentVariable("LIB");
// First check the task environment.
string? libDirectory = TaskEnvironment.GetEnvironmentVariable("LIB");

// Now go through additional environment variables.
string[] additionalVariables = EnvironmentVariables;
Expand Down Expand Up @@ -1098,7 +1106,7 @@ private void NormalizePaths(ITaskItem[]? taskItems)

foreach (var item in taskItems)
{
item.ItemSpec = Utilities.GetFullPathNoThrow(item.ItemSpec);
item.ItemSpec = Utilities.GetFullPathNoThrow(item.ItemSpec, TaskEnvironment);
}
}

Expand Down Expand Up @@ -1219,10 +1227,12 @@ protected bool CheckAllReferencesExistOnDisk()

foreach (ITaskItem reference in References)
{
if (!File.Exists(reference.ItemSpec))
var itemSpec = reference.ItemSpec;

if (string.IsNullOrEmpty(itemSpec) || !File.Exists(TaskEnvironment.GetFullPath(itemSpec)))
Comment thread
jaredpar marked this conversation as resolved.
Resolved
{
success = false;
Comment thread
jaredpar marked this conversation as resolved.
Resolved
Log.LogErrorWithCodeFromResources("General_ReferenceDoesNotExist", reference.ItemSpec);
Log.LogErrorWithCodeFromResources("General_ReferenceDoesNotExist", itemSpec);
}
}

Expand Down
19 changes: 13 additions & 6 deletions src/Compilers/Core/MSBuildTask/ManagedToolTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.CodeAnalysis.CommandLine;
Comment thread
jaredpar marked this conversation as resolved.
Resolved
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.BuildTasks
Expand Down Expand Up @@ -167,7 +168,9 @@ protected sealed override string GenerateFullPathToTool()
// which means `ToolExe` is not really overridden by user (yes, the user sets it but basically to its default value).
ToolExe = null;

return UseAppHost ? PathToBuiltInTool : RuntimeHostInfo.GetDotNetPathOrDefault();
return UseAppHost
? PathToBuiltInTool
: RuntimeHostInfo.GetDotNetPathOrDefault(TaskEnvironment.GetEnvironmentVariable);
}
Comment thread
jaredpar marked this conversation as resolved.
Resolved

return Path.Combine(ToolPath ?? "", ToolExe);
Expand Down Expand Up @@ -319,20 +322,24 @@ protected override bool ValidateParameters()
{
// Set DOTNET_ROOT so that the apphost executables launch properly.
// Unset all other DOTNET_ROOT* variables so for example DOTNET_ROOT_X64 does not override ours.
if (IsBuiltinToolRunningOnCoreClr && RuntimeHostInfo.GetToolDotNetRoot(Log.LogMessage) is { } dotNetRoot)
if (IsBuiltinToolRunningOnCoreClr &&
RuntimeHostInfo.GetToolDotNetRoot(
TaskEnvironment.GetEnvironmentVariable,
Log.LogMessage) is { } dotNetRoot)
{
Log.LogMessage("Setting {0} to '{1}'", RuntimeHostInfo.DotNetRootEnvironmentName, dotNetRoot);
EnvironmentVariables =
[
.. EnvironmentVariables?.Where(static e => !e.StartsWith(RuntimeHostInfo.DotNetRootEnvironmentName, StringComparison.OrdinalIgnoreCase)) ?? [],
.. Environment.GetEnvironmentVariables().Cast<System.Collections.DictionaryEntry>()
.Where(e => ((string)e.Key).StartsWith(RuntimeHostInfo.DotNetRootEnvironmentName, StringComparison.OrdinalIgnoreCase))
.. EnvironmentVariables?.Where(static e => !e.StartsWith(RuntimeHostInfo.DotNetRootEnvironmentName, Environment.EnvironmentVariableComparison)) ?? [],
.. TaskEnvironment.GetEnvironmentVariables()
.Where(e => e.Key.StartsWith(RuntimeHostInfo.DotNetRootEnvironmentName, Environment.EnvironmentVariableComparison))
.Select(e => $"{e.Key}="),
$"{RuntimeHostInfo.DotNetRootEnvironmentName}={dotNetRoot}",
];
}

if (RuntimeHostInfo.ShouldDisableTieredCompilation && Environment.GetEnvironmentVariable(RuntimeHostInfo.DotNetTieredCompilationEnvironmentName) == null)
if (RuntimeHostInfo.ShouldDisableTieredCompilation &&
this.TaskEnvironment.GetEnvironmentVariable(RuntimeHostInfo.DotNetTieredCompilationEnvironmentName) == null)
{
var value = "0";
Log.LogMessage("Setting {0} to '{1}'", RuntimeHostInfo.DotNetTieredCompilationEnvironmentName, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<OutputType>Library</OutputType>
<RootNamespace>Microsoft.CodeAnalysis.BuildTasks</RootNamespace>
<TargetFramework>net472</TargetFramework>
<DefineConstants>$(DefineConstants);SDK_TASK;MICROSOFT_CODEANALYSIS_CONTRACTS_NO_VALUE_TASK</DefineConstants>
<DefineConstants>$(DefineConstants);SDK_TASK;MICROSOFT_CODEANALYSIS_CONTRACTS_NO_VALUE_TASK;MICROSOFT_CODEANALYSIS_MSBUILD_TASK</DefineConstants>

<!--
This task is the same as the MSBuild version but for net472 only. Our NRT setup does
Expand All @@ -20,9 +20,7 @@

<ItemGroup>
<InternalsVisibleTo Include="Microsoft.Build.Tasks.CodeAnalysis.Sdk.UnitTests" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="..\BannedSymbols.MSBuildMT.txt" />
<None Include="$(MSBuildThisFileDirectory)..\..\..\CSharp\csc\csc.rsp" CopyToOutputDirectory="PreserveNewest" />
<None Include="$(MSBuildThisFileDirectory)..\..\..\VisualBasic\vbc\vbc.rsp" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
Expand Down
11 changes: 11 additions & 0 deletions src/Compilers/Core/MSBuildTask/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.CodeAnalysis.CommandLine;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -119,6 +120,16 @@ internal static string GetFullPathNoThrow(string path)
return path;
}

internal static string GetFullPathNoThrow(string path, TaskEnvironment taskEnvironment)
Comment thread
jaredpar marked this conversation as resolved.
Resolved
{
try
{
path = taskEnvironment.GetFullPath(path);
}
catch (Exception e) when (IsIoRelatedException(e)) { }
return path;
}

internal static bool TryCombine(string path1, string path2, [NotNullWhen(returnValue: true)] out string? combined)
{
try
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/Core/MSBuildTask/Vbc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Microsoft.CodeAnalysis.BuildTasks
/// should be significantly faster with larger projects and have a smaller memory
/// footprint.
/// </summary>
[MSBuildMultiThreadableTask]
public class Vbc : ManagedCompiler
{
private bool _useHostCompilerIfAvailable;
Expand Down
93 changes: 92 additions & 1 deletion src/Compilers/Core/MSBuildTaskTests/CscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.CodeAnalysis.BuildTasks.UnitTests.TestUtilities;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
Expand Down Expand Up @@ -513,11 +515,30 @@ public void BuiltInToolExe(bool useAppHost, bool setToolExe)
}
else
{
AssertEx.Equal(RuntimeHostInfo.GetDotNetPathOrDefault(), csc.GeneratePathToTool());
AssertEx.Equal(RuntimeHostInfo.GetDotNetPathOrDefault(Environment.GetEnvironmentVariable), csc.GeneratePathToTool());
AssertEx.Equal(RuntimeHostInfo.GetDotNetExecCommandLine(csc.PathToBuiltInTool, ""), csc.GenerateCommandLineContents());
}
}

[Fact]
public void BuiltInToolUsesTaskEnvironmentDotNetHostPath()
{
var projectDirectory = Temp.CreateDirectory();
var dotNetHost = projectDirectory.CreateFile("dotnet-host");
var csc = new Csc
{
UseAppHost_TestOnly = false,
TaskEnvironment = TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(
projectDirectory.Path,
new Dictionary<string, string>
{
[RuntimeHostInfo.DotNetHostPathEnvironmentName] = Path.GetFileName(dotNetHost.Path),
}),
};

Assert.Equal(Path.GetFileName(dotNetHost.Path), csc.GeneratePathToTool());
}

[Fact]
public void EditorConfig()
{
Expand Down Expand Up @@ -673,6 +694,76 @@ void parseRef(string refText, string alias)
}
}

/// <summary>
/// The multithreaded task migration requires relative references to be resolved against the
/// task's <see cref="TaskEnvironment.ProjectDirectory"/> rather than the shared process
/// working directory. Two task instances pointed at different project directories must resolve
/// the same relative reference independently.
/// </summary>
[Fact]
public void ReferenceExistenceResolvesAgainstTaskProjectDirectory()
{
var directoryWithReference = Temp.CreateDirectory();
var directoryWithoutReference = Temp.CreateDirectory();
directoryWithReference.CreateFile("ref.dll");

var taskInDirectoryWithReference = new TestableCsc()
{
BuildEngine = new MockEngine(TestOutputHelper),
TaskEnvironment = TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(directoryWithReference.Path),
References = MSBuildUtil.CreateTaskItems("ref.dll"),
};
var taskInDirectoryWithoutReference = new TestableCsc()
{
BuildEngine = new MockEngine(TestOutputHelper),
TaskEnvironment = TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(directoryWithoutReference.Path),
References = MSBuildUtil.CreateTaskItems("ref.dll"),
};

Assert.True(taskInDirectoryWithReference.CheckReferences());
Assert.False(taskInDirectoryWithoutReference.CheckReferences());
}

private sealed class TestableCsc : Csc
{
public bool CheckReferences() => CheckAllReferencesExistOnDisk();
}

/// <summary>
/// An empty reference <see cref="ITaskItem.ItemSpec"/> must be reported as a missing reference
/// (matching the pre-migration <c>File.Exists</c> behavior) rather than throwing from
/// <see cref="TaskEnvironment.GetAbsolutePath"/>.
/// </summary>
[Fact]
public void EmptyReferenceItemSpecIsReportedAsMissingWithoutThrowing()
{
var engine = new MockEngine(TestOutputHelper);
var task = new TestableCsc()
{
BuildEngine = engine,
References = MSBuildUtil.CreateTaskItems(""),
};

Assert.False(task.CheckReferences());
Assert.Contains("MSB3104", engine.Log);
}

[Fact]
public void InvalidReferencePathIsReportedAsMissingWithoutThrowing()
{
var projectDirectory = Temp.CreateDirectory();
var engine = new MockEngine(TestOutputHelper);
var task = new TestableCsc()
{
BuildEngine = engine,
TaskEnvironment = TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(projectDirectory.Path),
References = MSBuildUtil.CreateTaskItems("bad|ref.dll"),
};

Assert.False(task.CheckReferences());
Assert.Contains("MSB3104", engine.Log);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79907")]
public void StdLib()
{
Expand Down
Loading
Loading