/// <summary> /// Constructor /// </summary> internal AssemblyFoldersFromConfigCache(AssemblyFoldersFromConfig assemblyFoldersFromConfig, FileExists fileExists) { AssemblyFoldersFromConfig = assemblyFoldersFromConfig; _fileExists = fileExists; if (Environment.GetEnvironmentVariable("MSBUILDDISABLEASSEMBLYFOLDERSEXCACHE") != null) { _useOriginalFileExists = true; } else { _filesInDirectories = assemblyFoldersFromConfig.AsParallel() .Where(assemblyFolder => FileUtilities.DirectoryExistsNoThrow(assemblyFolder.DirectoryPath)) .SelectMany( assemblyFolder => Directory.GetFiles(assemblyFolder.DirectoryPath, "*.*", SearchOption.TopDirectoryOnly)) .ToImmutableHashSet(StringComparer.OrdinalIgnoreCase); } }
/// <summary> /// Initialize this class if it hasn't been initialized yet. /// </summary> private void LazyInitialize() { if (_isInitialized) { return; } _isInitialized = true; // Crack the search path just one time. Match match = s_crackAssemblyFoldersFromConfigSentinel.Value.Match(this.searchPathElement); _wasMatch = false; if (match.Success) { _targetRuntimeVersion = match.Groups["TARGETRUNTIMEVERSION"].Value.Trim(); _assemblyFolderConfigFile = match.Groups["ASSEMBLYFOLDERCONFIGFILE"].Value.Trim(); if (_targetRuntimeVersion.Length != 0) { // Tolerate version keys that don't begin with "v" as these could come from user input if (!_targetRuntimeVersion.StartsWith("v", StringComparison.OrdinalIgnoreCase)) { _targetRuntimeVersion = _targetRuntimeVersion.Insert(0, "v"); } _wasMatch = true; bool useCache = Environment.GetEnvironmentVariable("MSBUILDDISABLEASSEMBLYFOLDERSEXCACHE") == null; string key = "6f7de854-47fe-4ae2-9cfe-9b33682abd91" + searchPathElement; if (useCache && _buildEngine != null) { _assemblyFoldersCache = _buildEngine.GetRegisteredTaskObject(key, RegisteredTaskObjectLifetime.Build) as AssemblyFoldersFromConfigCache; } if (_assemblyFoldersCache == null) { // This should never happen. Microsoft.Common.CurrentVersion.targets will not specify a AssemblyFoldersFromConfig search path // if the specified (or default) file is not found. ErrorUtilities.VerifyThrow(File.Exists(_assemblyFolderConfigFile), $"The AssemblyFolders config file specified does not exist: {_assemblyFolderConfigFile}"); try { AssemblyFoldersFromConfig assemblyFolders = new AssemblyFoldersFromConfig(_assemblyFolderConfigFile, _targetRuntimeVersion, targetProcessorArchitecture); _assemblyFoldersCache = new AssemblyFoldersFromConfigCache(assemblyFolders, fileExists); if (useCache) { _buildEngine?.RegisterTaskObject(key, _assemblyFoldersCache, RegisteredTaskObjectLifetime.Build, true /* dispose early ok*/); } } catch (SerializationException e) { _taskLogger.LogError(ResourceUtilities.GetResourceString("ResolveAssemblyReference.AssemblyFoldersConfigFileMalformed"), _assemblyFolderConfigFile, e.Message); return; } } fileExists = _assemblyFoldersCache.FileExists; } } }