@@ -88,6 +88,11 @@ private static (string[] csFiles, string[] references) ReadProjectFileAsXml(File
8888 var projDir = fileName . Directory ;
8989 var root = projFile . DocumentElement ;
9090
91+ if ( root is null )
92+ {
93+ throw new NotSupportedException ( "Project file without root is not supported." ) ;
94+ }
95+
9196 // Figure out if it's dotnet core
9297
9398 var netCoreProjectFile = root . GetAttribute ( "Sdk" ) == "Microsoft.NET.Sdk" ;
@@ -96,34 +101,52 @@ private static (string[] csFiles, string[] references) ReadProjectFileAsXml(File
96101 {
97102 var explicitCsFiles = root
98103 . SelectNodes ( "/Project/ItemGroup/Compile/@Include" , mgr )
99- . NodeList ( )
104+ ? . NodeList ( )
100105 . Select ( node => node . Value )
101- . Select ( cs => Path . DirectorySeparatorChar == '/' ? cs . Replace ( "\\ " , "/" ) : cs )
102- . Select ( f => Path . GetFullPath ( Path . Combine ( projDir . FullName , f ) ) ) ;
106+ . Select ( cs => GetFullPath ( cs , projDir ) )
107+ . Where ( s => s is not null )
108+ ?? Enumerable . Empty < string > ( ) ;
103109
104110 var additionalCsFiles = System . IO . Directory . GetFiles ( directoryName , "*.cs" , SearchOption . AllDirectories ) ;
105111
112+ #nullable disable warnings
106113 return ( explicitCsFiles . Concat ( additionalCsFiles ) . ToArray ( ) , Array . Empty < string > ( ) ) ;
114+ #nullable restore warnings
107115 }
108116
109117 var references = root
110118 . SelectNodes ( "/msbuild:Project/msbuild:ItemGroup/msbuild:Reference/@Include" , mgr )
111- . NodeList ( )
119+ ? . NodeList ( )
112120 . Select ( node => node . Value )
113- . ToArray ( ) ;
121+ . Where ( s => s is not null )
122+ . ToArray ( )
123+ ?? Array . Empty < string > ( ) ;
114124
115125 var relativeCsIncludes = root
116126 . SelectNodes ( "/msbuild:Project/msbuild:ItemGroup/msbuild:Compile/@Include" , mgr )
117- . NodeList ( )
127+ ? . NodeList ( )
118128 . Select ( node => node . Value )
119- . ToArray ( ) ;
129+ . ToArray ( )
130+ ?? Array . Empty < string > ( ) ;
120131
121132 var csFiles = relativeCsIncludes
122- . Select ( cs => Path . DirectorySeparatorChar == '/' ? cs . Replace ( " \\ " , "/" ) : cs )
123- . Select ( f => Path . GetFullPath ( Path . Combine ( projDir . FullName , f ) ) )
133+ . Select ( cs => GetFullPath ( cs , projDir ) )
134+ . Where ( s => s is not null )
124135 . ToArray ( ) ;
125136
137+ #nullable disable warnings
126138 return ( csFiles , references ) ;
139+ #nullable restore warnings
140+ }
141+
142+ private static string ? GetFullPath ( string ? file , DirectoryInfo ? projDir )
143+ {
144+ if ( file is null )
145+ {
146+ return null ;
147+ }
148+
149+ return Path . GetFullPath ( Path . Combine ( projDir ? . FullName ?? string . Empty , Path . DirectorySeparatorChar == '/' ? file . Replace ( "\\ " , "/" ) : file ) ) ;
127150 }
128151
129152 private readonly string [ ] references ;
0 commit comments