diff --git a/src/Shared/FileMatcher.cs b/src/Shared/FileMatcher.cs index f444acd8c3f..6aed15166a4 100644 --- a/src/Shared/FileMatcher.cs +++ b/src/Shared/FileMatcher.cs @@ -130,13 +130,13 @@ internal FileMatcher(IFileSystem fileSystem, GetFileSystemEntries getFileSystemE path, "*", directory, - false).ToArray()); + false)); IEnumerable filteredEntriesForPath = (pattern != null && pattern != "*") ? allEntriesForPath.Where(o => IsMatch(Path.GetFileName(o), pattern)) : allEntriesForPath; return stripProjectDirectory - ? RemoveProjectDirectory(filteredEntriesForPath, directory) - : filteredEntriesForPath; + ? RemoveProjectDirectory(filteredEntriesForPath, directory).ToArray() + : filteredEntriesForPath.ToArray(); } else { @@ -178,7 +178,7 @@ internal enum FileSystemEntity /// /// /// An enumerable of filesystem entries. - internal delegate IEnumerable GetFileSystemEntries(FileSystemEntity entityType, string path, string pattern, string projectDirectory, bool stripProjectDirectory); + internal delegate IReadOnlyList GetFileSystemEntries(FileSystemEntity entityType, string path, string pattern, string projectDirectory, bool stripProjectDirectory); internal static void ClearFileEnumerationsCache() { @@ -237,7 +237,7 @@ internal static bool HasPropertyOrItemReferences(string filespec) /// If true the project directory should be stripped /// The file system abstraction to use that implements file system operations /// - private static IEnumerable GetAccessibleFileSystemEntries(IFileSystem fileSystem, FileSystemEntity entityType, string path, string pattern, string projectDirectory, bool stripProjectDirectory) + private static IReadOnlyList GetAccessibleFileSystemEntries(IFileSystem fileSystem, FileSystemEntity entityType, string path, string pattern, string projectDirectory, bool stripProjectDirectory) { path = FileUtilities.FixFilePath(path); switch (entityType) @@ -260,7 +260,7 @@ private static IEnumerable GetAccessibleFileSystemEntries(IFileSystem fi /// /// The file system abstraction to use that implements file system operations /// An enumerable of matching file system entries (can be empty). - private static IEnumerable GetAccessibleFilesAndDirectories(IFileSystem fileSystem, string path, string pattern) + private static IReadOnlyList GetAccessibleFilesAndDirectories(IFileSystem fileSystem, string path, string pattern) { if (fileSystem.DirectoryExists(path)) { @@ -270,7 +270,7 @@ private static IEnumerable GetAccessibleFilesAndDirectories(IFileSystem ? fileSystem.EnumerateFileSystemEntries(path, pattern) .Where(o => IsMatch(Path.GetFileName(o), pattern)) : fileSystem.EnumerateFileSystemEntries(path, pattern) - ); + ).ToArray(); } // for OS security catch (UnauthorizedAccessException) @@ -326,7 +326,7 @@ private static bool ShouldEnforceMatching(string searchPattern) /// /// The file system abstraction to use that implements file system operations /// Files that can be accessed. - private static IEnumerable GetAccessibleFiles + private static IReadOnlyList GetAccessibleFiles ( IFileSystem fileSystem, string path, @@ -370,7 +370,7 @@ bool stripProjectDirectory files = RemoveInitialDotSlash(files); } - return files; + return files.ToArray(); } catch (System.Security.SecurityException) { @@ -394,7 +394,7 @@ bool stripProjectDirectory /// Pattern to match /// The file system abstraction to use that implements file system operations /// Accessible directories. - private static IEnumerable GetAccessibleDirectories + private static IReadOnlyList GetAccessibleDirectories ( IFileSystem fileSystem, string path, @@ -428,7 +428,7 @@ string pattern directories = RemoveInitialDotSlash(directories); } - return directories; + return directories.ToArray(); } catch (System.Security.SecurityException) { @@ -528,11 +528,10 @@ GetFileSystemEntries getFileSystemEntries } else { - // getFileSystemEntries(...) returns an empty array if longPath doesn't exist. - IEnumerable entries = getFileSystemEntries(FileSystemEntity.FilesAndDirectories, longPath, parts[i], null, false); + // getFileSystemEntries(...) returns an empty enumerable if longPath doesn't exist. + IReadOnlyList entries = getFileSystemEntries(FileSystemEntity.FilesAndDirectories, longPath, parts[i], null, false); - int entriesCount = entries.Count(); - if (0 == entriesCount) + if (0 == entries.Count) { // The next part doesn't exist. Therefore, no more of the path will exist. // Just return the rest. @@ -543,13 +542,13 @@ GetFileSystemEntries getFileSystemEntries break; } - // Since we know there are no wild cards, this should be length one. - ErrorUtilities.VerifyThrow(entriesCount == 1, + // Since we know there are no wild cards, this should be length one, i.e. MoveNext should return false. + ErrorUtilities.VerifyThrow(entries.Count == 1, "Unexpected number of entries ({3}) found when enumerating '{0}' under '{1}'. Original path was '{2}'", - parts[i], longPath, path, entriesCount); + parts[i], longPath, path, entries.Count); // Entries[0] contains the full path. - longPath = entries.First(); + longPath = entries[0]; // We just want the trailing node. longParts[i - startingElement] = Path.GetFileName(longPath);