Skip to content

Commit 428195c

Browse files
authored
null check logging mechanism before logging glob failure (#11537)
* null check logging mechanism before logging glob failure * turn on nullable annotation and resolve issues
1 parent d0cb70a commit 428195c

File tree

1 file changed

+37
-39
lines changed

1 file changed

+37
-39
lines changed

src/Build/Utilities/EngineFileUtilities.cs

+37-39
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
using Microsoft.Build.Framework;
1313
using Microsoft.Build.Shared;
1414

15-
#nullable disable
16-
1715
namespace Microsoft.Build.Internal
1816
{
1917
internal static class EngineFileUtilities
@@ -22,7 +20,7 @@ internal static class EngineFileUtilities
2220

2321
// Regexes for wildcard filespecs that should not get expanded
2422
// By default all wildcards are expanded.
25-
private static List<Regex> s_lazyWildCardExpansionRegexes;
23+
private static List<Regex>? s_lazyWildCardExpansionRegexes;
2624

2725
static EngineFileUtilities()
2826
{
@@ -59,8 +57,8 @@ internal static void CaptureLazyWildcardRegexes()
5957
internal static string[] GetFileListUnescaped(
6058
string directoryEscaped,
6159
string filespecEscaped,
62-
object loggingMechanism = null,
63-
IElementLocation excludeLocation = null)
60+
object? loggingMechanism = null,
61+
IElementLocation? excludeLocation = null)
6462
{
6563
return GetFileList(
6664
directoryEscaped,
@@ -100,17 +98,17 @@ internal static string[] GetFileListUnescaped(
10098
/// for the Exclude attribute after detecting a drive enumerating wildcard.</param>
10199
/// <returns>Array of file paths, escaped.</returns>
102100
internal static string[] GetFileListEscaped(
103-
string directoryEscaped,
101+
string? directoryEscaped,
104102
string filespecEscaped,
105-
IEnumerable<string> excludeSpecsEscaped = null,
103+
IEnumerable<string>? excludeSpecsEscaped = null,
106104
bool forceEvaluate = false,
107-
FileMatcher fileMatcher = null,
108-
object loggingMechanism = null,
109-
IElementLocation includeLocation = null,
110-
IElementLocation excludeLocation = null,
111-
IElementLocation importLocation = null,
112-
BuildEventContext buildEventContext = null,
113-
string buildEventFileInfoFullPath = null,
105+
FileMatcher? fileMatcher = null,
106+
object? loggingMechanism = null,
107+
IElementLocation? includeLocation = null,
108+
IElementLocation? excludeLocation = null,
109+
IElementLocation? importLocation = null,
110+
BuildEventContext? buildEventContext = null,
111+
string? buildEventFileInfoFullPath = null,
114112
bool disableExcludeDriveEnumerationWarning = false)
115113
{
116114
return GetFileList(
@@ -171,18 +169,18 @@ internal static bool FilespecHasWildcards(string filespecEscaped)
171169
/// for the Exclude attribute after detecting a drive enumerating wildcard.</param>
172170
/// <returns>Array of file paths.</returns>
173171
private static string[] GetFileList(
174-
string directoryEscaped,
175-
string filespecEscaped,
172+
string? directoryEscaped,
173+
string? filespecEscaped,
176174
bool returnEscaped,
177175
bool forceEvaluateWildCards,
178-
IEnumerable<string> excludeSpecsEscaped,
176+
IEnumerable<string>? excludeSpecsEscaped,
179177
FileMatcher fileMatcher,
180-
object loggingMechanism = null,
181-
IElementLocation includeLocation = null,
182-
IElementLocation excludeLocation = null,
183-
IElementLocation importLocation = null,
184-
BuildEventContext buildEventContext = null,
185-
string buildEventFileInfoFullPath = null,
178+
object? loggingMechanism = null,
179+
IElementLocation? includeLocation = null,
180+
IElementLocation? excludeLocation = null,
181+
IElementLocation? importLocation = null,
182+
BuildEventContext? buildEventContext = null,
183+
string? buildEventFileInfoFullPath = null,
186184
bool disableExcludeDriveEnumerationWarning = false)
187185
{
188186
ErrorUtilities.VerifyThrowInternalLength(filespecEscaped, nameof(filespecEscaped));
@@ -286,7 +284,7 @@ private static string[] GetFileList(
286284
default:
287285
throw new InternalErrorException(ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword(
288286
"UnknownLoggingType",
289-
loggingMechanism.GetType(),
287+
loggingMechanism?.GetType(),
290288
nameof(GetFileList)));
291289
}
292290
}
@@ -327,7 +325,7 @@ private static string[] GetFileList(
327325
default:
328326
throw new InternalErrorException(ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword(
329327
"UnknownLoggingType",
330-
loggingMechanism.GetType(),
328+
loggingMechanism?.GetType(),
331329
nameof(GetFileList)));
332330
}
333331
}
@@ -338,10 +336,10 @@ private static string[] GetFileList(
338336
// as a relative path, we will get back a bunch of relative paths.
339337
// If the filespec started out as an absolute path, we will get
340338
// back a bunch of absolute paths
341-
(fileList, _, _, string globFailure) = fileMatcher.GetFiles(directoryUnescaped, filespecUnescaped, excludeSpecsUnescaped);
339+
(fileList, _, _, string? globFailure) = fileMatcher.GetFiles(directoryUnescaped, filespecUnescaped, excludeSpecsUnescaped);
342340

343-
// log globing failure with the present logging mechanism
344-
if (globFailure != null)
341+
// log globing failure with the present logging mechanism, skip if there is no logging mechanism
342+
if (globFailure != null && loggingMechanism != null)
345343
{
346344
switch (loggingMechanism)
347345
{
@@ -388,7 +386,7 @@ private static string[] GetFileList(
388386
return fileList;
389387
}
390388

391-
private static void LogDriveEnumerationWarningWithTargetLoggingContext(TargetLoggingContext targetLoggingContext, IElementLocation includeLocation, IElementLocation excludeLocation, bool excludeFileSpecIsEmpty, bool disableExcludeDriveEnumerationWarning, string fileSpec)
389+
private static void LogDriveEnumerationWarningWithTargetLoggingContext(TargetLoggingContext targetLoggingContext, IElementLocation? includeLocation, IElementLocation? excludeLocation, bool excludeFileSpecIsEmpty, bool disableExcludeDriveEnumerationWarning, string fileSpec)
392390
{
393391
// Both condition lines are necessary to skip for the first GetFileListEscaped call
394392
// and reach for the GetFileListUnescaped call when the wildcarded Exclude attribute results
@@ -404,7 +402,7 @@ private static void LogDriveEnumerationWarningWithTargetLoggingContext(TargetLog
404402
fileSpec,
405403
XMakeAttributes.exclude,
406404
XMakeElements.itemGroup,
407-
excludeLocation.LocationString);
405+
excludeLocation?.LocationString ?? "");
408406
}
409407

410408
// Both conditions are necessary to reach for both GetFileListEscaped calls
@@ -421,7 +419,7 @@ private static void LogDriveEnumerationWarningWithTargetLoggingContext(TargetLog
421419
}
422420
}
423421

424-
private static void LogDriveEnumerationWarningWithLoggingService(ILoggingService loggingService, IElementLocation includeLocation, BuildEventContext buildEventContext, string buildEventFileInfoFullPath, string filespecUnescaped)
422+
private static void LogDriveEnumerationWarningWithLoggingService(ILoggingService loggingService, IElementLocation? includeLocation, BuildEventContext? buildEventContext, string? buildEventFileInfoFullPath, string filespecUnescaped)
425423
{
426424
if (buildEventContext != null && includeLocation != null)
427425
{
@@ -437,7 +435,7 @@ private static void LogDriveEnumerationWarningWithLoggingService(ILoggingService
437435
}
438436
}
439437

440-
private static void LogDriveEnumerationWarningWithEvaluationLoggingContext(EvaluationLoggingContext evaluationLoggingContext, IElementLocation importLocation, IElementLocation includeLocation, IElementLocation excludeLocation, bool excludeFileSpecIsEmpty, string filespecUnescaped, string fileSpec)
438+
private static void LogDriveEnumerationWarningWithEvaluationLoggingContext(EvaluationLoggingContext evaluationLoggingContext, IElementLocation? importLocation, IElementLocation? includeLocation, IElementLocation? excludeLocation, bool excludeFileSpecIsEmpty, string filespecUnescaped, string fileSpec)
441439
{
442440
if (importLocation != null)
443441
{
@@ -468,7 +466,7 @@ private static void LogDriveEnumerationWarningWithEvaluationLoggingContext(Evalu
468466
}
469467
}
470468

471-
private static void ThrowDriveEnumerationExceptionWithTargetLoggingContext(IElementLocation includeLocation, IElementLocation excludeLocation, bool excludeFileSpecIsEmpty, string filespecUnescaped, string fileSpec)
469+
private static void ThrowDriveEnumerationExceptionWithTargetLoggingContext(IElementLocation? includeLocation, IElementLocation? excludeLocation, bool excludeFileSpecIsEmpty, string filespecUnescaped, string fileSpec)
472470
{
473471
// The first condition is necessary to reach for both GetFileListEscaped calls
474472
// whenever the wildcarded Include attribute results in drive enumeration, and
@@ -501,18 +499,18 @@ private static void ThrowDriveEnumerationExceptionWithTargetLoggingContext(IElem
501499
}
502500
}
503501

504-
private static void ThrowDriveEnumerationExceptionWithLoggingService(IElementLocation includeLocation, string filespecUnescaped)
502+
private static void ThrowDriveEnumerationExceptionWithLoggingService(IElementLocation? includeLocation, string filespecUnescaped)
505503
{
506504
ProjectErrorUtilities.ThrowInvalidProject(
507505
includeLocation,
508506
DriveEnumeratingWildcardMessageResourceName,
509507
filespecUnescaped,
510508
XMakeAttributes.include,
511509
XMakeElements.itemGroup,
512-
includeLocation.LocationString);
510+
includeLocation?.LocationString ?? "");
513511
}
514512

515-
private static void ThrowDriveEnumerationExceptionWithEvaluationLoggingContext(IElementLocation importLocation, IElementLocation includeLocation, IElementLocation excludeLocation, string filespecUnescaped, string fileSpec, bool excludeFileSpecIsEmpty)
513+
private static void ThrowDriveEnumerationExceptionWithEvaluationLoggingContext(IElementLocation? importLocation, IElementLocation? includeLocation, IElementLocation? excludeLocation, string filespecUnescaped, string fileSpec, bool excludeFileSpecIsEmpty)
516514
{
517515
if (importLocation != null)
518516
{
@@ -565,7 +563,7 @@ private static bool IsValidExclude(string exclude)
565563

566564
private static List<Regex> PopulateRegexFromEnvironment()
567565
{
568-
string wildCards = Environment.GetEnvironmentVariable("MsBuildSkipEagerWildCardEvaluationRegexes");
566+
string? wildCards = Environment.GetEnvironmentVariable("MsBuildSkipEagerWildCardEvaluationRegexes");
569567
if (string.IsNullOrEmpty(wildCards))
570568
{
571569
return new List<Regex>(0);
@@ -590,7 +588,7 @@ private static List<Regex> PopulateRegexFromEnvironment()
590588

591589
private static bool MatchesLazyWildcard(string fileSpec)
592590
{
593-
return _regexMatchCache.Value.GetOrAdd(fileSpec, file => s_lazyWildCardExpansionRegexes.Any(regex => regex.IsMatch(fileSpec)));
591+
return _regexMatchCache.Value.GetOrAdd(fileSpec, file => s_lazyWildCardExpansionRegexes!.Any(regex => regex.IsMatch(fileSpec)));
594592
}
595593

596594
/// <summary>
@@ -601,7 +599,7 @@ private static bool MatchesLazyWildcard(string fileSpec)
601599
/// <param name="filespecsEscaped"></param>
602600
/// <param name="currentDirectory"></param>
603601
/// <returns>A Func that will return true IFF its argument matches any of the specified filespecs.</returns>
604-
internal static Func<string, bool> GetFileSpecMatchTester(IList<string> filespecsEscaped, string currentDirectory)
602+
internal static Func<string, bool> GetFileSpecMatchTester(IList<string> filespecsEscaped, string? currentDirectory)
605603
{
606604
var matchers = filespecsEscaped
607605
.Select(fs => new Lazy<FileSpecMatcherTester>(() => FileSpecMatcherTester.Parse(currentDirectory, fs)))

0 commit comments

Comments
 (0)