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

Load before/after.{solutionName}.sln.targets for .slnx #11535

Merged
merged 3 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ internal static SolutionFile ParseSolutionHelper(string solutionFileContents, bo
}
}

private static string ConvertToSlnx(string slnPath)
internal static string ConvertToSlnx(string slnPath)
{
string slnxPath = slnPath + "x";
ISolutionSerializer serializer = SolutionSerializers.GetSerializerByMoniker(slnPath).ShouldNotBeNull();
Expand Down
58 changes: 47 additions & 11 deletions src/Build.UnitTests/Construction/SolutionProjectGenerator_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,64 @@ public void Dispose()
/// Test that if a before.{sln}>.targets or after.{sln}.targets file has one of the default targets (Build, Clean, etc.) that it includes only the user-defined target.
/// </summary>
[Theory]
[InlineData("before.MySln.sln.targets")]
[InlineData("after.MySln.sln.targets")]
[InlineData("name.that.does.Not.Affect.The.Build.targets")]
public void SolutionProjectIgnoresDuplicateDefaultTargets(string name)
[InlineData("before.MySln.sln.targets", false)]
[InlineData("before.MySln.sln.targets", true)]
[InlineData("after.MySln.sln.targets", false)]
[InlineData("after.MySln.sln.targets", true)]
[InlineData("name.that.does.Not.Affect.The.Build.targets", false)]
[InlineData("name.that.does.Not.Affect.The.Build.targets", true)]
public void SolutionProjectIgnoresDuplicateDefaultTargets(string name, bool convertToSlnx)
{
using (TestEnvironment testEnvironment = TestEnvironment.Create())
{
TransientTestFolder folder = testEnvironment.CreateFolder(createFolder: true);
TransientTestFile sln = testEnvironment.CreateFile(folder, "MySln.sln", @"Microsoft Visual Studio Solution File, Format Version 16.00");
TransientTestFile targetsFile = testEnvironment.CreateFile(folder, name,
@"<Project>
<Target Name=""Build"" AfterTargets=""NonsenseTarget"">
</Target>
</Project>");
ProjectInstance[] instances = SolutionProjectGenerator.Generate(SolutionFile.Parse(sln.Path), null, null, _buildEventContext, CreateMockLoggingService());
string solutionFileContents = "Microsoft Visual Studio Solution File, Format Version 12.00";
TransientTestFile sln = testEnvironment.CreateFile(folder, "MySln.sln", solutionFileContents);
string solutionPath = convertToSlnx ? SolutionFile_NewParser_Tests.ConvertToSlnx(sln.Path) : sln.Path;
testEnvironment.CreateFile(folder, name,
"""
<Project>
<Target Name="TestTarget" />
</Project>
""");
ProjectInstance[] instances = SolutionProjectGenerator.Generate(SolutionFile.Parse(solutionPath), null, null, _buildEventContext, CreateMockLoggingService());
instances.ShouldHaveSingleItem();
instances[0].Targets["Build"].AfterTargets.ShouldBe(string.Empty);
MockLogger logger = new MockLogger(output);
instances[0].Build(targets: null, new List<ILogger> { logger }).ShouldBeTrue();
}
}

/// <summary>
/// Test that targets in before.{sln}.targets and after.{sln}.targets files are included in the project.
/// </summary>
[Theory]
[InlineData("before.MySln.sln.targets", false)]
[InlineData("before.MySln.sln.targets", true)]
[InlineData("after.MySln.sln.targets", false)]
[InlineData("after.MySln.sln.targets", true)]
public void SolutionProjectIncludesBeforeAndAfterTargets(string name, bool convertToSlnx)
{
using (TestEnvironment testEnvironment = TestEnvironment.Create())
{
TransientTestFolder folder = testEnvironment.CreateFolder(createFolder: true);
string solutionFileContents = "Microsoft Visual Studio Solution File, Format Version 12.00";
TransientTestFile sln = testEnvironment.CreateFile(folder, "MySln.sln", solutionFileContents);
string solutionPath = convertToSlnx ? SolutionFile_NewParser_Tests.ConvertToSlnx(sln.Path) : sln.Path;
testEnvironment.CreateFile(folder, name,
"""
<Project>
<Target Name="TestTarget" />
</Project>
""");
ProjectInstance[] instances = SolutionProjectGenerator.Generate(SolutionFile.Parse(solutionPath), null, null, _buildEventContext, CreateMockLoggingService());
instances.ShouldHaveSingleItem();
instances[0].Targets.ShouldContainKey("TestTarget");
MockLogger logger = new MockLogger(output);
instances[0].Build(targets: null, new List<ILogger> { logger }).ShouldBeTrue();
}
}

[Fact]
public void BuildProjectAsTarget()
{
Expand Down
31 changes: 22 additions & 9 deletions src/Build/Construction/Solution/SolutionProjectGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -948,15 +948,7 @@ private ProjectInstance CreateTraversalInstance(string wrapperProjectToolsVersio
// Add our local extensibility points to the project representing the solution
// Imported at the top: before.mysolution.sln.targets
// Imported at the bottom: after.mysolution.sln.targets
string escapedSolutionFile = EscapingUtilities.Escape(Path.GetFileName(_solutionFile.FullPath));
string escapedSolutionDirectory = EscapingUtilities.Escape(_solutionFile.SolutionFileDirectory);
string localFile = Path.Combine(escapedSolutionDirectory, "before." + escapedSolutionFile + ".targets");
ProjectImportElement importBeforeLocal = traversalProject.CreateImportElement(localFile);
importBeforeLocal.Condition = @"exists('" + localFile + "')";

localFile = Path.Combine(escapedSolutionDirectory, "after." + escapedSolutionFile + ".targets");
ProjectImportElement importAfterLocal = traversalProject.CreateImportElement(localFile);
importAfterLocal.Condition = @"exists('" + localFile + "')";
(ProjectImportElement importBeforeLocal, ProjectImportElement importAfterLocal) = CreateBeforeAndAfterSolutionImports(traversalProject);

// Put locals second so they can override globals if they want
traversalProject.PrependChild(importBeforeLocal);
Expand Down Expand Up @@ -1025,6 +1017,27 @@ private ProjectInstance CreateTraversalInstance(string wrapperProjectToolsVersio
return traversalInstance;
}

private (ProjectImportElement ImportBeforeSln, ProjectImportElement ImportAfterSln) CreateBeforeAndAfterSolutionImports(ProjectRootElement traversalProject)
{
string escapedSolutionFileName = EscapingUtilities.Escape(Path.GetFileName(_solutionFile.FullPath));
if (escapedSolutionFileName.EndsWith(".slnx"))
{
// We want to load only after.{solutionFileName}.sln.targets for solution files with .slnx extension
escapedSolutionFileName = escapedSolutionFileName.Substring(0, escapedSolutionFileName.Length - 1);
}

string escapedSolutionDirectory = EscapingUtilities.Escape(_solutionFile.SolutionFileDirectory);
string localFile = Path.Combine(escapedSolutionDirectory, $"before.{escapedSolutionFileName}.targets");
ProjectImportElement importBeforeLocal = traversalProject.CreateImportElement(localFile);
importBeforeLocal.Condition = $"exists('{localFile}')";

localFile = Path.Combine(escapedSolutionDirectory, $"after.{escapedSolutionFileName}.targets");
ProjectImportElement importAfterLocal = traversalProject.CreateImportElement(localFile);
importAfterLocal.Condition = $"exists('{localFile}')";

return (importBeforeLocal, importAfterLocal);
}

private void EmitMetaproject(ProjectRootElement metaproject, string path)
{
if (Traits.Instance.EmitSolutionMetaproj)
Expand Down