Skip to content

Commit 5e4b48a

Browse files
authored
Merge pull request #6273 from jeffkl/jeffkl/16.9-cherry-pick
Handle unsupported paths in ProjectInSolution.AbsolutePath (#6238)
2 parents 57a23d2 + 0db197e commit 5e4b48a

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/Build.UnitTests/Construction/SolutionProjectGenerator_Tests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,6 +2477,30 @@ public void DirectorySolutionPropsTest(string projectName, bool enable)
24772477
}
24782478
}
24792479

2480+
/// <summary>
2481+
/// Regression test for https://github.com/dotnet/msbuild/issues/6236
2482+
/// </summary>
2483+
[Theory]
2484+
[InlineData("http://localhost:8080")]
2485+
[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-")]
2486+
public void AbsolutePathWorksForUnsupportedPaths(string relativePath)
2487+
{
2488+
string solutionFileContents =
2489+
$@"
2490+
Microsoft Visual Studio Solution File, Format Version 12.00
2491+
# Visual Studio Version 16
2492+
VisualStudioVersion = 16.0.31025.194
2493+
MinimumVisualStudioVersion = 10.0.40219.1
2494+
Project(""{{E24C65DC-7377-472B-9ABA-BC803B73C61A}}"") = ""WebSite1"", ""{relativePath}"", ""{{{{96E0707C-2E9C-4704-946F-FA583147737F}}}}""
2495+
EndProject";
2496+
2497+
SolutionFile solution = SolutionFile_Tests.ParseSolutionHelper(solutionFileContents);
2498+
2499+
ProjectInSolution projectInSolution = solution.ProjectsInOrder.ShouldHaveSingleItem();
2500+
2501+
projectInSolution.AbsolutePath.ShouldBe(Path.Combine(solution.SolutionFileDirectory, projectInSolution.RelativePath));
2502+
}
2503+
24802504
#region Helper Functions
24812505

24822506
/// <summary>

src/Build/Construction/Solution/ProjectInSolution.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,26 @@ public string AbsolutePath
168168
{
169169
if (_absolutePath == null)
170170
{
171+
_absolutePath = Path.Combine(ParentSolution.SolutionFileDirectory, _relativePath);
172+
173+
// For web site projects, Visual Studio stores the URL of the site as the relative path so it cannot be normalized.
174+
// Legacy behavior dictates that we must just return the result of Path.Combine()
175+
if (!Uri.TryCreate(_relativePath, UriKind.Absolute, out Uri _))
176+
{
177+
try
178+
{
171179
#if NETFRAMEWORK && !MONO
172-
_absolutePath = Path.GetFullPath(Path.Combine(ParentSolution.SolutionFileDirectory, _relativePath));
180+
_absolutePath = Path.GetFullPath(_absolutePath);
173181
#else
174-
_absolutePath = FileUtilities.NormalizePath(Path.Combine(ParentSolution.SolutionFileDirectory, _relativePath));
182+
_absolutePath = FileUtilities.NormalizePath(_absolutePath);
175183
#endif
184+
}
185+
catch (Exception)
186+
{
187+
// 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
188+
// This falls back to previous behavior of returning a path that may not be correct but at least returns some value
189+
}
190+
}
176191
}
177192

178193
return _absolutePath;
@@ -229,9 +244,9 @@ public IReadOnlyDictionary<string, ProjectConfigurationInSolution> ProjectConfig
229244

230245
internal string TargetFrameworkMoniker { get; set; }
231246

232-
#endregion
247+
#endregion
233248

234-
#region Methods
249+
#region Methods
235250

236251
private bool _checkedIfCanBeMSBuildProjectFile;
237252
private bool _canBeMSBuildProjectFile;
@@ -529,13 +544,13 @@ private static bool ElementContainsInvalidNamespaceDefitions(XmlElement mainProj
529544
return false;
530545
}
531546

532-
#endregion
547+
#endregion
533548

534-
#region Constants
549+
#region Constants
535550

536551
internal const int DependencyLevelUnknown = -1;
537552
internal const int DependencyLevelBeingDetermined = -2;
538553

539-
#endregion
554+
#endregion
540555
}
541556
}

0 commit comments

Comments
 (0)