Skip to content

Commit

Permalink
Detect and ignore dwproj in sln
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyGerlicher committed Jan 4, 2018
1 parent 35269ac commit 4fd6871
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/Build.UnitTests/Construction/SolutionFile_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ public void CanBeMSBuildFileRejectsMSBuildLikeFiles()
ProjectInSolution project2 = solution.ProjectsByGuid["{DEA89696-F42B-4B58-B7EE-017FF40817D1}"];

project1.CanBeMSBuildProjectFile(out error).ShouldBe(false);
// TODO: https://github.com/Microsoft/msbuild/issues/2064
//project2.CanBeMSBuildProjectFile(out error).ShouldBe(false);
project2.CanBeMSBuildProjectFile(out error).ShouldBe(false);
}
}

Expand Down
31 changes: 29 additions & 2 deletions src/Build/Construction/Solution/ProjectInSolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using BuildEventFileInfo = Microsoft.Build.Shared.BuildEventFileInfo;
using ErrorUtilities = Microsoft.Build.Shared.ErrorUtilities;
using System.Collections.ObjectModel;
using System.Linq;

namespace Microsoft.Build.Construction
{
Expand Down Expand Up @@ -329,13 +330,20 @@ internal bool CanBeMSBuildProjectFile(out string errorMessage)
bool defaultNamespace = String.Compare(mainProjectElement.NamespaceURI,
XMakeAttributes.defaultXmlNamespace,
StringComparison.OrdinalIgnoreCase) == 0;
bool projectElementInvalid = ElementContainsInvalidNamespaceDefitions(mainProjectElement);

// If the MSBuild namespace is declared, it is very likely an MSBuild project that should be built.
if (defaultNamespace)
{
_canBeMSBuildProjectFile = true;
return _canBeMSBuildProjectFile;
}

// This is a bit of a special case, but an rptproj file will contain a Project with no schema that is
// not an MSBuild file. It will however have ToolsVersion="2.0" which is not supported with an empty
// schema. This is not a great solution, but it should cover the customer reported issue. See:
// https://github.com/Microsoft/msbuild/issues/2064
if (defaultNamespace ||
emptyNamespace && mainProjectElement.GetAttribute("ToolsVersion") != "2.0")
if (emptyNamespace && !projectElementInvalid && mainProjectElement.GetAttribute("ToolsVersion") != "2.0")
{
_canBeMSBuildProjectFile = true;
return _canBeMSBuildProjectFile;
Expand Down Expand Up @@ -478,6 +486,25 @@ static internal string DisambiguateProjectTargetName(string uniqueProjectName)
return uniqueProjectName;
}

/// <summary>
/// Check a Project element for known invalid namespace definitions.
/// </summary>
/// <param name="mainProjectElement">Project XML Element</param>
/// <returns>True if the element contains known invalid namespace definitions</returns>
private static bool ElementContainsInvalidNamespaceDefitions(XmlElement mainProjectElement)
{
if (mainProjectElement.HasAttributes)
{
// Data warehouse projects (.dwproj) will contain a Project element but are invalid MSBuild. Check attributes
// on Project for signs that this is a .dwproj file. If there are, it's not a valid MSBuild file.
return mainProjectElement.Attributes.OfType<XmlAttribute>().Any(a =>
a.Name.Equals("xmlns:dwd", StringComparison.OrdinalIgnoreCase) ||
a.Name.StartsWith("xmlns:dd", StringComparison.OrdinalIgnoreCase));
}

return false;
}

#endregion

#region Constants
Expand Down

0 comments on commit 4fd6871

Please sign in to comment.