Skip to content

Commit

Permalink
Deal with corrupted files or I/O errors when reading project files
Browse files Browse the repository at this point in the history
The rest of the MSBuild reading code can deal with this, but we need
to deal with it directly in this path too. Rather than force callers
to have to deal with "we can't give a host because the project file is
unreadable, so good luck", we'll just give back a host that we know
exists so the rest of the code can deal with the existing error logging
paths.
  • Loading branch information
jasonmalinowski committed Nov 2, 2023
1 parent 83ee2dc commit ea08f53
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/Workspaces/Core/MSBuild/MSBuild/BuildHostProcessManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,26 @@ private static BuildHostProcessKind GetKindForProject(string projectFilePath)
// we'll load the XML of the project directly, and inspect for certain elements.
XDocument document;

// Read the XML, prohibiting DTD processing due the the usual concerns there.
using (var fileStream = new FileStream(projectFilePath, FileMode.Open, FileAccess.Read))
using (var xmlReader = XmlReader.Create(fileStream, s_xmlSettings))
document = XDocument.Load(xmlReader);
var frameworkHostType = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? BuildHostProcessKind.NetFramework : BuildHostProcessKind.Mono;

try
{
// Read the XML, prohibiting DTD processing due the the usual security concerns there.
using (var fileStream = FileUtilities.OpenRead(projectFilePath))
using (var xmlReader = XmlReader.Create(fileStream, s_xmlSettings))
document = XDocument.Load(xmlReader);
}
catch (Exception e) when (e is IOException or XmlException)
{
// We were unable to read the file; rather than having callers of the build process manager have to deal with this special case
// we'll instead just give them a host that corresponds to what they are running as; we know that host unquestionably exists
// and the rest of the code can deal with this cleanly.
#if NET
return BuildHostProcessKind.NetCore;
#else
return frameworkHostType;
#endif
}

// If we don't have a root, doesn't really matter which. This project is just malformed.
if (document.Root == null)
Expand All @@ -276,7 +292,7 @@ private static BuildHostProcessKind GetKindForProject(string projectFilePath)
return BuildHostProcessKind.NetCore;

// Nothing that indicates it's an SDK-style project, so use our .NET framework host
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? BuildHostProcessKind.NetFramework : BuildHostProcessKind.Mono;
return frameworkHostType;
}

public enum BuildHostProcessKind
Expand Down

0 comments on commit ea08f53

Please sign in to comment.