提交 6780df8e 编写于 作者: D Dustin Campbell

Don't crash in MSBuildWorkspace on malformed paths

上级 72a6857b
......@@ -408,10 +408,16 @@ private IEnumerable<AnalyzerReference> ResolveAnalyzerReferences(CommandLineArgu
foreach (var path in commandLineArgs.AnalyzerReferences.Select(r => r.FilePath))
{
var fullPath = Path.GetFullPath(path);
if (File.Exists(fullPath))
string fullPath;
if (PathUtilities.IsAbsolute(path))
{
analyzerLoader.AddDependencyLocation(fullPath);
fullPath = FileUtilities.TryNormalizeAbsolutePath(path);
if (fullPath != null && File.Exists(fullPath))
{
analyzerLoader.AddDependencyLocation(fullPath);
}
}
}
......
......@@ -65,7 +65,7 @@ public bool TryGetAbsoluteProjectPath(string path, string baseDirectory, Diagnos
return true;
}
private string GetAbsolutePath(string path, string baseDirectory)
=> Path.GetFullPath(FileUtilities.ResolveRelativePath(path, baseDirectory) ?? path);
private static string GetAbsolutePath(string path, string baseDirectory)
=> FileUtilities.NormalizeAbsolutePath(FileUtilities.ResolveRelativePath(path, baseDirectory) ?? path);
}
}
......@@ -118,8 +118,9 @@ protected string GetDocumentFilePath(MSB.Framework.ITaskItem documentItem)
protected string GetAbsolutePath(string path)
{
var directoryPath = PathUtilities.GetDirectoryName(Project.FullPath);
return Path.GetFullPath(FileUtilities.ResolveRelativePath(path, directoryPath) ?? path);
var baseDirectory = PathUtilities.GetDirectoryName(Project.FullPath);
var absolutePath = FileUtilities.ResolveRelativePath(path, baseDirectory) ?? path;
return FileUtilities.TryNormalizeAbsolutePath(absolutePath) ?? absolutePath;
}
protected void ReadAdditionalFiles()
......
......@@ -199,7 +199,7 @@ private string GetAbsolutePathRelativeToProject(string path)
{
// TODO (tomat): should we report an error when drive-relative path (e.g. "C:goo.cs") is encountered?
var absolutePath = FileUtilities.ResolveRelativePath(path, _projectDirectory) ?? path;
return Path.GetFullPath(absolutePath);
return FileUtilities.TryNormalizeAbsolutePath(absolutePath) ?? absolutePath;
}
private string GetDocumentFilePath(MSB.Framework.ITaskItem documentItem)
......@@ -234,27 +234,31 @@ protected static string GetDocumentLogicalPath(MSB.Framework.ITaskItem documentI
}
else
{
var result = documentItem.ItemSpec;
if (Path.IsPathRooted(result))
var filePath = documentItem.ItemSpec;
if (!PathUtilities.IsAbsolute(filePath))
{
// If we have an absolute path, there are two possibilities:
result = Path.GetFullPath(result);
// If the document is within the current project directory (or subdirectory), then the logical path is the relative path
// from the project's directory.
if (result.StartsWith(projectDirectory, StringComparison.OrdinalIgnoreCase))
{
result = result.Substring(projectDirectory.Length);
}
else
{
// if the document lies outside the project's directory (or subdirectory) then place it logically at the root of the project.
// if more than one document ends up with the same logical name then so be it (the workspace will survive.)
return Path.GetFileName(result);
}
return filePath;
}
return result;
var normalizedPath = FileUtilities.TryNormalizeAbsolutePath(filePath);
if (normalizedPath == null)
{
return filePath;
}
// If the document is within the current project directory (or subdirectory), then the logical path is the relative path
// from the project's directory.
if (normalizedPath.StartsWith(projectDirectory, StringComparison.OrdinalIgnoreCase))
{
return normalizedPath.Substring(projectDirectory.Length);
}
else
{
// if the document lies outside the project's directory (or subdirectory) then place it logically at the root of the project.
// if more than one document ends up with the same logical name then so be it (the workspace will survive.)
return PathUtilities.GetFileName(normalizedPath);
}
}
}
......
......@@ -3597,6 +3597,8 @@ public async Task TestOpenProjectAsync_MalformedAdditionalFilePath()
// Project should open without an exception being thrown.
Assert.NotNull(project);
Assert.Contains(project.AdditionalDocuments, doc => doc.Name == "TEST::");
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册