diff --git a/src/Build/Utilities/EngineFileUtilities.cs b/src/Build/Utilities/EngineFileUtilities.cs index 4dbd13ede6f..5e522888bb8 100644 --- a/src/Build/Utilities/EngineFileUtilities.cs +++ b/src/Build/Utilities/EngineFileUtilities.cs @@ -12,8 +12,6 @@ using Microsoft.Build.Framework; using Microsoft.Build.Shared; -#nullable disable - namespace Microsoft.Build.Internal { internal static class EngineFileUtilities @@ -22,7 +20,7 @@ internal static class EngineFileUtilities // Regexes for wildcard filespecs that should not get expanded // By default all wildcards are expanded. - private static List s_lazyWildCardExpansionRegexes; + private static List? s_lazyWildCardExpansionRegexes; static EngineFileUtilities() { @@ -59,8 +57,8 @@ internal static void CaptureLazyWildcardRegexes() internal static string[] GetFileListUnescaped( string directoryEscaped, string filespecEscaped, - object loggingMechanism = null, - IElementLocation excludeLocation = null) + object? loggingMechanism = null, + IElementLocation? excludeLocation = null) { return GetFileList( directoryEscaped, @@ -100,17 +98,17 @@ internal static string[] GetFileListUnescaped( /// for the Exclude attribute after detecting a drive enumerating wildcard. /// Array of file paths, escaped. internal static string[] GetFileListEscaped( - string directoryEscaped, + string? directoryEscaped, string filespecEscaped, - IEnumerable excludeSpecsEscaped = null, + IEnumerable? excludeSpecsEscaped = null, bool forceEvaluate = false, - FileMatcher fileMatcher = null, - object loggingMechanism = null, - IElementLocation includeLocation = null, - IElementLocation excludeLocation = null, - IElementLocation importLocation = null, - BuildEventContext buildEventContext = null, - string buildEventFileInfoFullPath = null, + FileMatcher? fileMatcher = null, + object? loggingMechanism = null, + IElementLocation? includeLocation = null, + IElementLocation? excludeLocation = null, + IElementLocation? importLocation = null, + BuildEventContext? buildEventContext = null, + string? buildEventFileInfoFullPath = null, bool disableExcludeDriveEnumerationWarning = false) { return GetFileList( @@ -171,18 +169,18 @@ internal static bool FilespecHasWildcards(string filespecEscaped) /// for the Exclude attribute after detecting a drive enumerating wildcard. /// Array of file paths. private static string[] GetFileList( - string directoryEscaped, - string filespecEscaped, + string? directoryEscaped, + string? filespecEscaped, bool returnEscaped, bool forceEvaluateWildCards, - IEnumerable excludeSpecsEscaped, + IEnumerable? excludeSpecsEscaped, FileMatcher fileMatcher, - object loggingMechanism = null, - IElementLocation includeLocation = null, - IElementLocation excludeLocation = null, - IElementLocation importLocation = null, - BuildEventContext buildEventContext = null, - string buildEventFileInfoFullPath = null, + object? loggingMechanism = null, + IElementLocation? includeLocation = null, + IElementLocation? excludeLocation = null, + IElementLocation? importLocation = null, + BuildEventContext? buildEventContext = null, + string? buildEventFileInfoFullPath = null, bool disableExcludeDriveEnumerationWarning = false) { ErrorUtilities.VerifyThrowInternalLength(filespecEscaped, nameof(filespecEscaped)); @@ -286,7 +284,7 @@ private static string[] GetFileList( default: throw new InternalErrorException(ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword( "UnknownLoggingType", - loggingMechanism.GetType(), + loggingMechanism?.GetType(), nameof(GetFileList))); } } @@ -327,7 +325,7 @@ private static string[] GetFileList( default: throw new InternalErrorException(ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword( "UnknownLoggingType", - loggingMechanism.GetType(), + loggingMechanism?.GetType(), nameof(GetFileList))); } } @@ -338,10 +336,10 @@ private static string[] GetFileList( // as a relative path, we will get back a bunch of relative paths. // If the filespec started out as an absolute path, we will get // back a bunch of absolute paths - (fileList, _, _, string globFailure) = fileMatcher.GetFiles(directoryUnescaped, filespecUnescaped, excludeSpecsUnescaped); + (fileList, _, _, string? globFailure) = fileMatcher.GetFiles(directoryUnescaped, filespecUnescaped, excludeSpecsUnescaped); - // log globing failure with the present logging mechanism - if (globFailure != null) + // log globing failure with the present logging mechanism, skip if there is no logging mechanism + if (globFailure != null && loggingMechanism != null) { switch (loggingMechanism) { @@ -388,7 +386,7 @@ private static string[] GetFileList( return fileList; } - private static void LogDriveEnumerationWarningWithTargetLoggingContext(TargetLoggingContext targetLoggingContext, IElementLocation includeLocation, IElementLocation excludeLocation, bool excludeFileSpecIsEmpty, bool disableExcludeDriveEnumerationWarning, string fileSpec) + private static void LogDriveEnumerationWarningWithTargetLoggingContext(TargetLoggingContext targetLoggingContext, IElementLocation? includeLocation, IElementLocation? excludeLocation, bool excludeFileSpecIsEmpty, bool disableExcludeDriveEnumerationWarning, string fileSpec) { // Both condition lines are necessary to skip for the first GetFileListEscaped call // and reach for the GetFileListUnescaped call when the wildcarded Exclude attribute results @@ -404,7 +402,7 @@ private static void LogDriveEnumerationWarningWithTargetLoggingContext(TargetLog fileSpec, XMakeAttributes.exclude, XMakeElements.itemGroup, - excludeLocation.LocationString); + excludeLocation?.LocationString ?? ""); } // Both conditions are necessary to reach for both GetFileListEscaped calls @@ -421,7 +419,7 @@ private static void LogDriveEnumerationWarningWithTargetLoggingContext(TargetLog } } - private static void LogDriveEnumerationWarningWithLoggingService(ILoggingService loggingService, IElementLocation includeLocation, BuildEventContext buildEventContext, string buildEventFileInfoFullPath, string filespecUnescaped) + private static void LogDriveEnumerationWarningWithLoggingService(ILoggingService loggingService, IElementLocation? includeLocation, BuildEventContext? buildEventContext, string? buildEventFileInfoFullPath, string filespecUnescaped) { if (buildEventContext != null && includeLocation != null) { @@ -437,7 +435,7 @@ private static void LogDriveEnumerationWarningWithLoggingService(ILoggingService } } - private static void LogDriveEnumerationWarningWithEvaluationLoggingContext(EvaluationLoggingContext evaluationLoggingContext, IElementLocation importLocation, IElementLocation includeLocation, IElementLocation excludeLocation, bool excludeFileSpecIsEmpty, string filespecUnescaped, string fileSpec) + private static void LogDriveEnumerationWarningWithEvaluationLoggingContext(EvaluationLoggingContext evaluationLoggingContext, IElementLocation? importLocation, IElementLocation? includeLocation, IElementLocation? excludeLocation, bool excludeFileSpecIsEmpty, string filespecUnescaped, string fileSpec) { if (importLocation != null) { @@ -468,7 +466,7 @@ private static void LogDriveEnumerationWarningWithEvaluationLoggingContext(Evalu } } - private static void ThrowDriveEnumerationExceptionWithTargetLoggingContext(IElementLocation includeLocation, IElementLocation excludeLocation, bool excludeFileSpecIsEmpty, string filespecUnescaped, string fileSpec) + private static void ThrowDriveEnumerationExceptionWithTargetLoggingContext(IElementLocation? includeLocation, IElementLocation? excludeLocation, bool excludeFileSpecIsEmpty, string filespecUnescaped, string fileSpec) { // The first condition is necessary to reach for both GetFileListEscaped calls // whenever the wildcarded Include attribute results in drive enumeration, and @@ -501,7 +499,7 @@ private static void ThrowDriveEnumerationExceptionWithTargetLoggingContext(IElem } } - private static void ThrowDriveEnumerationExceptionWithLoggingService(IElementLocation includeLocation, string filespecUnescaped) + private static void ThrowDriveEnumerationExceptionWithLoggingService(IElementLocation? includeLocation, string filespecUnescaped) { ProjectErrorUtilities.ThrowInvalidProject( includeLocation, @@ -509,10 +507,10 @@ private static void ThrowDriveEnumerationExceptionWithLoggingService(IElementLoc filespecUnescaped, XMakeAttributes.include, XMakeElements.itemGroup, - includeLocation.LocationString); + includeLocation?.LocationString ?? ""); } - private static void ThrowDriveEnumerationExceptionWithEvaluationLoggingContext(IElementLocation importLocation, IElementLocation includeLocation, IElementLocation excludeLocation, string filespecUnescaped, string fileSpec, bool excludeFileSpecIsEmpty) + private static void ThrowDriveEnumerationExceptionWithEvaluationLoggingContext(IElementLocation? importLocation, IElementLocation? includeLocation, IElementLocation? excludeLocation, string filespecUnescaped, string fileSpec, bool excludeFileSpecIsEmpty) { if (importLocation != null) { @@ -565,7 +563,7 @@ private static bool IsValidExclude(string exclude) private static List PopulateRegexFromEnvironment() { - string wildCards = Environment.GetEnvironmentVariable("MsBuildSkipEagerWildCardEvaluationRegexes"); + string? wildCards = Environment.GetEnvironmentVariable("MsBuildSkipEagerWildCardEvaluationRegexes"); if (string.IsNullOrEmpty(wildCards)) { return new List(0); @@ -590,7 +588,7 @@ private static List PopulateRegexFromEnvironment() private static bool MatchesLazyWildcard(string fileSpec) { - return _regexMatchCache.Value.GetOrAdd(fileSpec, file => s_lazyWildCardExpansionRegexes.Any(regex => regex.IsMatch(fileSpec))); + return _regexMatchCache.Value.GetOrAdd(fileSpec, file => s_lazyWildCardExpansionRegexes!.Any(regex => regex.IsMatch(fileSpec))); } /// @@ -601,7 +599,7 @@ private static bool MatchesLazyWildcard(string fileSpec) /// /// /// A Func that will return true IFF its argument matches any of the specified filespecs. - internal static Func GetFileSpecMatchTester(IList filespecsEscaped, string currentDirectory) + internal static Func GetFileSpecMatchTester(IList filespecsEscaped, string? currentDirectory) { var matchers = filespecsEscaped .Select(fs => new Lazy(() => FileSpecMatcherTester.Parse(currentDirectory, fs)))