Skip to content

Refactor Microsoft.IO usage#13062

Merged
AR-May merged 2 commits intodotnet:mainfrom
AR-May:refactor-redist-usage
Jan 28, 2026
Merged

Refactor Microsoft.IO usage#13062
AR-May merged 2 commits intodotnet:mainfrom
AR-May:refactor-redist-usage

Conversation

@AR-May
Copy link
Copy Markdown
Member

@AR-May AR-May commented Jan 20, 2026

Fixes #13041

Context

At this moment we have multiple different patterns of using Microsoft.IO namespace from Microsoft.IO.Redist.

Changes Made

Refactored Microsoft.IO namespace usage.

Testing

unit tests

@AR-May AR-May requested review from YuliiaKovalova and Copilot and removed request for Copilot January 20, 2026 09:21
@AR-May AR-May self-assigned this Jan 20, 2026
Copilot AI review requested due to automatic review settings January 27, 2026 09:25
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR standardizes repository usage of Microsoft.IO (from Microsoft.IO.Redist) by moving from framework-based compilation guards to a single feature flag and by using explicit type aliases (e.g., Path = Microsoft.IO.Path) to reduce ambiguity and unify call sites.

Changes:

  • Replace assorted NETFRAMEWORK / NET* conditional patterns with FEATURE_MSIOREDIST.
  • Introduce/adjust Microsoft.IO type aliases (Path, File, Directory, etc.) to make opt-in usage explicit.
  • Clean up/normalize System.IO usings in a few test files.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/Tasks/XamlTaskFactory/CommandLineGenerator.cs Removes conditional Microsoft.IO import where it’s not needed.
src/Tasks.UnitTests/RoslynCodeTaskFactory_Tests.cs Normalizes System.IO using placement and removes unused conditional aliasing.
src/Shared/TaskFactoryUtilities.cs Switches to FEATURE_MSIOREDIST and aliases Microsoft.IO.File/Path for consistent usage.
src/Shared/FileSystem/WindowsFileSystem.cs Uses FEATURE_MSIOREDIST to opt into Microsoft.IO.File.Exists.
src/Shared/FileMatcher.cs Uses FEATURE_MSIOREDIST to alias Microsoft.IO.Path consistently.
src/MSBuild/XMake.cs Uses FEATURE_MSIOREDIST and explicit aliases for Microsoft.IO filesystem types.
src/MSBuild.UnitTests/MSBuildServer_Tests.cs Simplifies conditional IO usings to System.IO.
src/Framework/PathHelpers/AbsolutePath.cs Uses FEATURE_MSIOREDIST to alias Microsoft.IO.Path for path helper functionality.
src/Build/Utilities/Utilities.cs Moves Path selection under FEATURE_MSIOREDIST for consistency.
src/Build/Logging/TerminalLogger/TerminalLogger.cs Uses FEATURE_MSIOREDIST to select Path implementation consistently.
src/Build/Instance/TaskFactories/AssemblyTaskFactory.cs Uses FEATURE_MSIOREDIST to alias Microsoft.IO.Path.
src/Build/Evaluation/Expander/ArgumentParser.cs Updates conditional compilation to FEATURE_MSIOREDIST for Microsoft.IO namespace import.
src/Build/Evaluation/Expander.cs Uses FEATURE_MSIOREDIST to alias Microsoft.IO.Directory/Path while keeping System.IO baseline.
src/Build/BuildCheck/Checks/ExecCliBuildCheck.cs Uses FEATURE_MSIOREDIST to alias Microsoft.IO.Path where enabled.
src/Build/BuildCheck/Checks/DoubleWritesCheck.cs Uses FEATURE_MSIOREDIST to alias Microsoft.IO.Path where enabled.
src/Build/BackEnd/Shared/BuildRequestConfiguration.cs Uses FEATURE_MSIOREDIST for span-based Microsoft.IO.Path operations on supported builds.
Comments suppressed due to low confidence (1)

src/Build/BackEnd/Shared/BuildRequestConfiguration.cs:308

  • Switching this guard to FEATURE_MSIOREDIST changes the compilation behavior for DotNetBuildSourceOnly .NETFramework builds (where FEATURE_MSIOREDIST is not defined). In that configuration, the #else branch uses System.IO.Path.GetFileName(ProjectFullPath.AsSpan()), but the span overloads are not available on .NET Framework, causing a compile failure. Add an explicit #elif NETFRAMEWORK fallback that uses the string-based overload (or otherwise avoid span overloads when building netfx without Microsoft.IO.Redist).
#if FEATURE_MSIOREDIST
                    if (MemoryExtensions.Equals(Microsoft.IO.Path.GetFileName(ProjectFullPath.AsSpan()), "dirs.proj".AsSpan(), StringComparison.OrdinalIgnoreCase))
#else
                    if (MemoryExtensions.Equals(Path.GetFileName(ProjectFullPath.AsSpan()), "dirs.proj", StringComparison.OrdinalIgnoreCase))
#endif

Comment on lines +10 to 14
#if FEATURE_MSIOREDIST
using File = Microsoft.IO.File;
using Path = Microsoft.IO.Path;
#else
using System.IO;
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

FEATURE_MSIOREDIST is not defined for DotNetBuildSourceOnly .NETFramework builds, so this file will fall back to System.IO.Path. However, later in this file you call Path.IsPathFullyQualified(...) under #if NETFRAMEWORK || NET, which will not compile on .NET Framework when Path is System.IO.Path (no IsPathFullyQualified there). Update the conditional compilation around the IsPathFullyQualified usage (or provide a .NET Framework fallback implementation) so source-only .NETFramework builds compile without Microsoft.IO.Redist.

Copilot uses AI. Check for mistakes.
Comment on lines +6 to 10
#if FEATURE_MSIOREDIST
using Path = Microsoft.IO.Path;
#else
using System.IO;
#endif
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

When DotNetBuildSourceOnly is true on .NETFramework, FEATURE_MSIOREDIST is not defined and this file will use System.IO.Path. But later, ValidatePath calls Path.IsPathFullyQualified(...) under #if NETFRAMEWORK || NET, which won't compile on .NET Framework without Microsoft.IO.Redist. Narrow the guard to FEATURE_MSIOREDIST || NET (or add a .NET Framework fallback) so source-only netfx builds compile.

Copilot uses AI. Check for mistakes.
Comment on lines +19 to 23
#if FEATURE_MSIOREDIST
using Path = Microsoft.IO.Path;
#else
using System.IO;
#endif
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

With DotNetBuildSourceOnly on .NETFramework, FEATURE_MSIOREDIST is not defined so Path resolves to System.IO.Path. This file uses APIs like Path.GetRelativePath(...) (not available on .NET Framework), so the source-only .NETFramework build will fail to compile. Please either guard those API usages with #if FEATURE_MSIOREDIST and provide a fallback (e.g., via existing FileUtilities.MakeRelative), or keep Microsoft.IO.Path available for all .NETFramework builds where these APIs are needed.

Copilot uses AI. Check for mistakes.
Comment on lines 10 to 14
#if FEATURE_MSIOREDIST
using Path = Microsoft.IO.Path;
#else
using System.IO;
#endif
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

FEATURE_MSIOREDIST is disabled for DotNetBuildSourceOnly .NETFramework builds, which means this file will use System.IO.Path. However, later in this file Path.GetFullPath(fileBeingWritten, context.Data.ProjectFileDirectory) is used, and that overload does not exist on .NET Framework's System.IO.Path. Provide a netfx fallback when FEATURE_MSIOREDIST is off (e.g., route through FileUtilities.GetFullPath(..., currentDirectory, ...) or equivalent) so source-only netfx builds compile.

Copilot uses AI. Check for mistakes.
@AR-May AR-May merged commit 6b7a28c into dotnet:main Jan 28, 2026
15 checks passed
YuliiaKovalova added a commit that referenced this pull request Feb 3, 2026
YuliiaKovalova added a commit that referenced this pull request Feb 4, 2026
YuliiaKovalova added a commit that referenced this pull request Feb 4, 2026
JanProvaznik pushed a commit to JanProvaznik/msbuild that referenced this pull request Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor Microsoft.IO APIs usage.

3 participants