Skip to content

Commit

Permalink
Merge pull request #6273 from jeffkl/jeffkl/16.9-cherry-pick
Browse files Browse the repository at this point in the history
Handle unsupported paths in ProjectInSolution.AbsolutePath (#6238)
  • Loading branch information
marcpopMSFT committed Mar 17, 2021
2 parents 57a23d2 + 0db197e commit 5e4b48a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
24 changes: 24 additions & 0 deletions src/Build.UnitTests/Construction/SolutionProjectGenerator_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2477,6 +2477,30 @@ public void DirectorySolutionPropsTest(string projectName, bool enable)
}
}

/// <summary>
/// Regression test for https://github.com/dotnet/msbuild/issues/6236
/// </summary>
[Theory]
[InlineData("http://localhost:8080")]
[InlineData("a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-a-really-long-string-")]
public void AbsolutePathWorksForUnsupportedPaths(string relativePath)
{
string solutionFileContents =
$@"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31025.194
MinimumVisualStudioVersion = 10.0.40219.1
Project(""{{E24C65DC-7377-472B-9ABA-BC803B73C61A}}"") = ""WebSite1"", ""{relativePath}"", ""{{{{96E0707C-2E9C-4704-946F-FA583147737F}}}}""
EndProject";

SolutionFile solution = SolutionFile_Tests.ParseSolutionHelper(solutionFileContents);

ProjectInSolution projectInSolution = solution.ProjectsInOrder.ShouldHaveSingleItem();

projectInSolution.AbsolutePath.ShouldBe(Path.Combine(solution.SolutionFileDirectory, projectInSolution.RelativePath));
}

#region Helper Functions

/// <summary>
Expand Down
29 changes: 22 additions & 7 deletions src/Build/Construction/Solution/ProjectInSolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,26 @@ public string AbsolutePath
{
if (_absolutePath == null)
{
_absolutePath = Path.Combine(ParentSolution.SolutionFileDirectory, _relativePath);

// For web site projects, Visual Studio stores the URL of the site as the relative path so it cannot be normalized.
// Legacy behavior dictates that we must just return the result of Path.Combine()
if (!Uri.TryCreate(_relativePath, UriKind.Absolute, out Uri _))
{
try
{
#if NETFRAMEWORK && !MONO
_absolutePath = Path.GetFullPath(Path.Combine(ParentSolution.SolutionFileDirectory, _relativePath));
_absolutePath = Path.GetFullPath(_absolutePath);
#else
_absolutePath = FileUtilities.NormalizePath(Path.Combine(ParentSolution.SolutionFileDirectory, _relativePath));
_absolutePath = FileUtilities.NormalizePath(_absolutePath);
#endif
}
catch (Exception)
{
// The call to GetFullPath() can throw if the relative path is some unsupported value or the paths are too long for the current file system
// This falls back to previous behavior of returning a path that may not be correct but at least returns some value
}
}
}

return _absolutePath;
Expand Down Expand Up @@ -229,9 +244,9 @@ public string AbsolutePath

internal string TargetFrameworkMoniker { get; set; }

#endregion
#endregion

#region Methods
#region Methods

private bool _checkedIfCanBeMSBuildProjectFile;
private bool _canBeMSBuildProjectFile;
Expand Down Expand Up @@ -529,13 +544,13 @@ private static bool ElementContainsInvalidNamespaceDefitions(XmlElement mainProj
return false;
}

#endregion
#endregion

#region Constants
#region Constants

internal const int DependencyLevelUnknown = -1;
internal const int DependencyLevelBeingDetermined = -2;

#endregion
#endregion
}
}

0 comments on commit 5e4b48a

Please sign in to comment.