Skip to content
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
42 changes: 42 additions & 0 deletions documentation/specs/custom-cultures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# MSBuild Custom Cultures Support

## Overview

The `EnableCustomCulture` property provides an opt-in mechanism for handling custom culture-specific resources in MSBuild projects. This feature allows for greater control over which directories are treated as culture-specific resources during the build process.

## Purpose

In some projects, directory names that match culture name patterns might not actually be culture resources. This can cause issues with resource compilation and deployment. This feature flag enables:

1. Control over whether custom culture detection is enabled
2. Fine-grained configuration of which directories should be excluded from culture-specific resource processing

## Usage

### Enabling the Feature

To enable the custom cultures feature, set the `EnableCustomCulture` property `true`.

```xml
<PropertyGroup>
<EnableCustomCulture>true</EnableCustomCulture>
</PropertyGroup>
```

### Excluding Specific Directories

When the feature is enabled, you can specify directories that should not be treated as culture-specific resources using the `NonCultureResourceDirectories` property:

```xml
<PropertyGroup>
<NonCultureResourceDirectories>long;hash;temp</NonCultureResourceDirectories>
</PropertyGroup>
```

In this example, directories named "long", "hash", or "temp" will not be processed as culture-specific resources and the assemblied inside of them will be skipped, even if their names match culture naming patterns. Globbing is not supported.

## Additional Notes

- This feature does not affect the standard resource handling for well-known cultures.
- The feature is designed to be backward compatible - existing projects without the feature flag will behave the same as before.
- Performance impact is minimal, as the exclusion check happens only during the resource discovery phase of the build.
2 changes: 1 addition & 1 deletion documentation/wiki/ChangeWaves.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ A wave of features is set to "rotate out" (i.e. become standard functionality) t

### 17.14
- ~[.SLNX support - use the new parser for .sln and .slnx](https://github.com/dotnet/msbuild/pull/10836)~ reverted after compat problems discovered
- [Support custom culture in RAR](https://github.com/dotnet/msbuild/pull/11000)
- ~~[Support custom culture in RAR](https://github.com/dotnet/msbuild/pull/11000)~~ - see [11607](https://github.com/dotnet/msbuild/pull/11607) for details
- [VS Telemetry](https://github.com/dotnet/msbuild/pull/11255)

### 17.12
Expand Down
15 changes: 14 additions & 1 deletion eng/BootStrapMsBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,27 @@
</ItemGroup>
</Target>

<!-- The task allows to find VS bits on machine. These files will be used as a source for patching on the top of them. -->
<UsingTask TaskName="LocateVisualStudioTask"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll"
TaskFactory="RoslynCodeTaskFactory">
<Task>
<Code Source="$(MSBuildThisFileDirectory)..\src\MSBuild.Bootstrap.Utils\Tasks\LocateVisualStudioTask.cs" Language="cs" />
</Task>
</UsingTask>
<Target Name="BootstrapFull" DependsOnTargets="CleanBootstrapFolder;SetBinPaths;GatherNuGetDependencies">

<LocateVisualStudioTask>
<Output TaskParameter="VsInstallPath" PropertyName="AvailableVsInstallPath" />
</LocateVisualStudioTask>

<ItemGroup>
<InstalledVersionedExtensions Include="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\**\*.targets" />
<InstalledVersionedExtensions Include="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\**\*.props" />
<InstalledVersionedExtensions Include="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\**\Tracker*.dll" />
<InstalledVersionedExtensions Include="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\**\Tracker*.exe" />
<InstalledVersionedExtensions Include="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\**\FileTracker*.dll" />
<SdkResolverFiles Include="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Bin\SdkResolvers\Microsoft.DotNet.MSBuildSdkResolver\**\*.*" />
<SdkResolverFiles Include="$(AvailableVsInstallPath)\MSBuild\Current\Bin\SdkResolvers\Microsoft.DotNet.MSBuildSdkResolver\**\*.*" />
<NuGetSdkResolverManifest Include="$(RepoRoot)src\MSBuild\SdkResolvers\VS\Microsoft.Build.NuGetSdkResolver.xml" />
<InstalledSdks Include="$(DOTNET_INSTALL_DIR)\sdk\$(DotNetCliVersion)\Sdks\**\*.*" />

Expand Down
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!-- Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the MIT license. See License.txt in the project root for full license information. -->
<Project>
<PropertyGroup>
<VersionPrefix>17.14.1</VersionPrefix><DotNetFinalVersionKind>release</DotNetFinalVersionKind>
<VersionPrefix>17.14.2</VersionPrefix><DotNetFinalVersionKind>release</DotNetFinalVersionKind>
<PackageValidationBaselineVersion>17.13.9</PackageValidationBaselineVersion>
<AssemblyVersion>15.1.0.0</AssemblyVersion>
<PreReleaseVersionLabel>preview</PreReleaseVersionLabel>
Expand Down
1 change: 0 additions & 1 deletion src/BuildCheck.UnitTests/EndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Microsoft.Build.Shared;
using Microsoft.Build.UnitTests;
using Microsoft.Build.UnitTests.Shared;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Shouldly;
using Xunit;
using Xunit.Abstractions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableCustomCulture>true</EnableCustomCulture>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableCustomCulture>true</EnableCustomCulture>
</PropertyGroup>

<PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Framework/Traits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
namespace Microsoft.Build.Framework
{
/// <summary>
/// Represents toggleable features of the MSBuild engine
/// Represents toggleable features of the MSBuild engine.
/// </summary>
internal class Traits
{
private static Traits _instance = new Traits();

public static Traits Instance
{
get
Expand Down Expand Up @@ -132,7 +133,6 @@ public Traits()

public readonly bool InProcNodeDisabled = Environment.GetEnvironmentVariable("MSBUILDNOINPROCNODE") == "1";


/// <summary>
/// Variables controlling opt out at the level of not initializing telemetry infrastructure. Set to "1" or "true" to opt out.
/// mirroring
Expand Down
62 changes: 62 additions & 0 deletions src/MSBuild.Bootstrap.Utils/Tasks/LocateVisualStudioTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace MSBuild.Bootstrap.Utils.Tasks
{
public class LocateVisualStudioTask : ToolTask
{
private readonly StringBuilder _standardOutput = new();

[Output]
public string VsInstallPath { get; set; }

protected override string ToolName => "vswhere.exe";

protected override string GenerateFullPathToTool()
{
string programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
string vsWherePath = Path.Combine(programFilesX86, "Microsoft Visual Studio", "Installer", ToolName);


return vsWherePath;
}

protected override string GenerateCommandLineCommands() => "-latest -prerelease -property installationPath";

public override bool Execute()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Log.LogMessage(MessageImportance.High, "Not running on Windows. Skipping Visual Studio detection.");
return true;
}

_ = ExecuteTool(GenerateFullPathToTool(), string.Empty, GenerateCommandLineCommands());

if (!Log.HasLoggedErrors)
{
VsInstallPath = _standardOutput.ToString().Trim();
}

return true;
}

// Override to capture standard output
protected override void LogEventsFromTextOutput(string singleLine, MessageImportance messageImportance)
{
if (!string.IsNullOrWhiteSpace(singleLine))
{
_ = _standardOutput.AppendLine(singleLine);
}

base.LogEventsFromTextOutput(singleLine, messageImportance);
}
}
}
1 change: 1 addition & 0 deletions src/MSBuild/MSBuild/Microsoft.Build.CommonTypes.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,7 @@ elementFormDefault="qualified">
<xs:element name="LinkIncremental" type="msb:boolean" substitutionGroup="msb:Property"/>
<xs:element name="ManifestCertificateThumbprint" type="msb:StringPropertyType" substitutionGroup="msb:Property"/>
<xs:element name="ManifestKeyFile" type="msb:StringPropertyType" substitutionGroup="msb:Property"/>
<xs:element name="EnableCustomCulture" type="msb:boolean" substitutionGroup="msb:EnableCustomCulture"/>
<xs:element name="MapFileExtensions" type="msb:boolean" substitutionGroup="msb:Property">
<xs:annotation>
<xs:documentation><!-- _locID_text="MapFileExtensions" _locComment="" -->boolean</xs:documentation>
Expand Down
43 changes: 4 additions & 39 deletions src/Tasks.UnitTests/AssemblyDependency/Miscellaneous.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3269,41 +3269,6 @@ public void ParentAssemblyResolvedFromAForGac()
Assert.Equal(reference2.ResolvedSearchPath, parentReferenceFolders[0].Directory);
}

/// <summary>
/// Generate a fake reference which has been resolved from the gac. We will use it to verify the creation of the exclusion list.
/// </summary>
/// <returns></returns>
private ReferenceTable GenerateTableWithAssemblyFromTheGlobalLocation(string location)
{
ReferenceTable referenceTable = new ReferenceTable(null, false, false, false, false, Array.Empty<string>(), null, null, null, null, null, null, SystemProcessorArchitecture.None, fileExists, null, null, null, null,
#if FEATURE_WIN32_REGISTRY
null, null, null,
#endif
null, null, new Version("4.0"), null, null, null, true, false, null, null, false, null, WarnOrErrorOnTargetArchitectureMismatchBehavior.None, false, false, null);

AssemblyNameExtension assemblyNameExtension = new AssemblyNameExtension(new AssemblyName("Microsoft.VisualStudio.Interopt, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"));
TaskItem taskItem = new TaskItem("Microsoft.VisualStudio.Interopt, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

Reference reference = new Reference(isWinMDFile, fileExists, getRuntimeVersion);
reference.MakePrimaryAssemblyReference(taskItem, false, ".dll");
// "Resolve the assembly from the gac"
reference.FullPath = "c:\\Microsoft.VisualStudio.Interopt.dll";
reference.ResolvedSearchPath = location;
referenceTable.AddReference(assemblyNameExtension, reference);

assemblyNameExtension = new AssemblyNameExtension(new AssemblyName("Team.System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"));
taskItem = new TaskItem("Team, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

reference = new Reference(isWinMDFile, fileExists, getRuntimeVersion);
reference.MakePrimaryAssemblyReference(taskItem, false, ".dll");

// "Resolve the assembly from the gac"
reference.FullPath = "c:\\Team.System.dll";
reference.ResolvedSearchPath = location;
referenceTable.AddReference(assemblyNameExtension, reference);
return referenceTable;
}

/// <summary>
/// Given a reference that resolves to a bad image, we should get a warning and
/// no reference. We don't want an exception.
Expand Down Expand Up @@ -6735,11 +6700,11 @@ public void ReferenceTableDependentItemsInDenyList3()
[Fact]
public void ReferenceTableDependentItemsInDenyList4()
{
ReferenceTable referenceTable = new ReferenceTable(null, false, false, false, false, Array.Empty<string>(), null, null, null, null, null, null, SystemProcessorArchitecture.None, fileExists, null, null, null,
ReferenceTable referenceTable = new ReferenceTable(null, false, false, false, false, false, Array.Empty<string>(), null, null, null, null, null, null, SystemProcessorArchitecture.None, fileExists, null, null, null,
#if FEATURE_WIN32_REGISTRY
null, null, null,
#endif
null, null, null, new Version("4.0"), null, null, null, true, false, null, null, false, null, WarnOrErrorOnTargetArchitectureMismatchBehavior.None, false, false, null);
null, null, null, new Version("4.0"), null, null, null, true, false, null, null, false, null, WarnOrErrorOnTargetArchitectureMismatchBehavior.None, false, false, null, Array.Empty<string>());
MockEngine mockEngine;
ResolveAssemblyReference rar;
Dictionary<string, string> denyList;
Expand Down Expand Up @@ -6913,11 +6878,11 @@ public void ReferenceTableDependentItemsInDenyListPrimaryWithSpecificVersion()

private static ReferenceTable MakeEmptyReferenceTable(TaskLoggingHelper log)
{
ReferenceTable referenceTable = new ReferenceTable(null, false, false, false, false, Array.Empty<string>(), null, null, null, null, null, null, SystemProcessorArchitecture.None, fileExists, null, null, null, null,
ReferenceTable referenceTable = new ReferenceTable(null, false, false, false, false, false, Array.Empty<string>(), null, null, null, null, null, null, SystemProcessorArchitecture.None, fileExists, null, null, null, null,
#if FEATURE_WIN32_REGISTRY
null, null, null,
#endif
null, null, new Version("4.0"), null, log, null, true, false, null, null, false, null, WarnOrErrorOnTargetArchitectureMismatchBehavior.None, false, false, null);
null, null, new Version("4.0"), null, log, null, true, false, null, null, false, null, WarnOrErrorOnTargetArchitectureMismatchBehavior.None, false, false, null, Array.Empty<string>());
return referenceTable;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Tasks.UnitTests/Microsoft.Build.Tasks.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
<None Include="..\Shared\UnitTests\xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="TestResources\CustomCulture\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="TestResources\Projects\Custom_COM\Custom_COM\Class1.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -155,6 +158,9 @@
<None Update="TestResources\Manifests\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<EmbeddedResource Update="TestResources\CustomCulture\*.resx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<None Update="TestResources\mycert.pfx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
89 changes: 89 additions & 0 deletions src/Tasks.UnitTests/ResolveAssemblyReference_CustomCultureTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;
using Microsoft.Build.UnitTests;
using Microsoft.Build.UnitTests.Shared;
using Shouldly;
using Xunit;

namespace Microsoft.Build.Tasks.UnitTests
{
/// <summary>
/// Unit tests for the ResolveAssemblyReference task.
/// </summary>
public class ResolveAssemblyReference_CustomCultureTests
{
private static string TestAssetsRootPath { get; } = Path.Combine(
Path.GetDirectoryName(typeof(AddToWin32Manifest_Tests).Assembly.Location) ?? AppContext.BaseDirectory,
"TestResources",
"CustomCulture");

[WindowsOnlyTheory]
[InlineData(true, "", true, true)]
[InlineData(false)]
[InlineData(true, "yue", false, true)]
[InlineData(false, "yue", false, true)]
[InlineData(true, "euy", true)]
[InlineData(true, "yue;euy")]
[InlineData(true, "euy;yue")]
public void E2EScenarioTests(bool enableCustomCulture, string customCultureExclusions = "", bool isYueCultureExpected = false, bool isEuyCultureExpected = false)
{
using (TestEnvironment env = TestEnvironment.Create())
{
// Set up project paths
var testAssetsPath = TestAssetsRootPath;
var solutionFolder = env.CreateFolder();
var solutionPath = solutionFolder.Path;

// Create and configure ProjectB
var projectBName = "ProjectB.csproj";
var projBOutputPath = env.CreateFolder().Path;
var projectBFolder = Path.Combine(solutionPath, projectBName);
Directory.CreateDirectory(projectBFolder);
var projBContent = File.ReadAllText(Path.Combine(testAssetsPath, projectBName))
.Replace("OutputPathPlaceholder", projBOutputPath)
.Replace("NonCultureResourceDirectoriesPlaceholder", customCultureExclusions)
.Replace("EnableCustomCulturePlaceholder", enableCustomCulture.ToString());
env.CreateFile(Path.Combine(projectBFolder, projectBName), projBContent);

// Copy ProjectA files to test solution folder
CopyTestAsset(testAssetsPath, "ProjectA.csproj", solutionPath);
CopyTestAsset(testAssetsPath, "Test.resx", solutionPath);
CopyTestAsset(testAssetsPath, "Test.yue.resx", solutionPath);
CopyTestAsset(testAssetsPath, "Test.euy.resx", solutionPath);

env.SetCurrentDirectory(projectBFolder);
var output = RunnerUtilities.ExecBootstrapedMSBuild("-restore", out bool buildSucceeded);

buildSucceeded.ShouldBeTrue($"MSBuild should complete successfully. Build output: {output}");

var yueCultureResourceDll = Path.Combine(projBOutputPath, "yue", "ProjectA.resources.dll");
AssertCustomCulture(isYueCultureExpected, "yue", yueCultureResourceDll);

var euyCultureResourceDll = Path.Combine(projBOutputPath, "euy", "ProjectA.resources.dll");
AssertCustomCulture(isEuyCultureExpected, "euy", euyCultureResourceDll);
}

void AssertCustomCulture(bool isCultureExpectedToExist, string customCultureName, string cultureResourcePath)
{
if (enableCustomCulture && isCultureExpectedToExist)
{
File.Exists(cultureResourcePath).ShouldBeTrue($"Expected '{customCultureName}' resource DLL not found at: {cultureResourcePath}");
}
else
{
File.Exists(cultureResourcePath).ShouldBeFalse($"Unexpected '{customCultureName}' culture DLL was found at: {cultureResourcePath}");
}
}
}

private void CopyTestAsset(string sourceFolder, string fileName, string destinationFolder)
{
var sourcePath = Path.Combine(sourceFolder, fileName);

File.Copy(sourcePath, Path.Combine(destinationFolder, fileName));
}
}
}
Loading