private void ResolveCurrentDirectoryInLookupPaths() { var indexesToRemove = new SortedDictionary <int, int>(); int removalListCount = 0; string fileSystemProviderName = _context.ProviderNames.FileSystem; SessionStateInternal sessionState = _context.EngineSessionState; // Only use the directory if it gets resolved by the FileSystemProvider bool isCurrentDriveValid = sessionState.CurrentDrive != null && sessionState.CurrentDrive.Provider.NameEquals(fileSystemProviderName) && sessionState.IsProviderLoaded(fileSystemProviderName); string environmentCurrentDirectory = Directory.GetCurrentDirectory(); LocationGlobber pathResolver = _context.LocationGlobber; // Loop through the relative paths and resolve them foreach (int index in _lookupPaths.IndexOfRelativePath()) { string?resolvedDirectory = null; string?resolvedPath = null; CommandDiscovery.discoveryTracer.WriteLine( "Lookup directory \"{0}\" appears to be a relative path. Attempting resolution...", _lookupPaths[index]); if (isCurrentDriveValid) { try { ProviderInfo provider; resolvedPath = pathResolver.GetProviderPath( _lookupPaths[index], out provider); } catch (ProviderInvocationException providerInvocationException) { CommandDiscovery.discoveryTracer.WriteLine( "The relative path '{0}', could not be resolved because the provider threw an exception: '{1}'", _lookupPaths[index], providerInvocationException.Message); } catch (InvalidOperationException) { CommandDiscovery.discoveryTracer.WriteLine( "The relative path '{0}', could not resolve a home directory for the provider", _lookupPaths[index]); } // Note, if the directory resolves to multiple paths, only the first is used. if (!string.IsNullOrEmpty(resolvedPath)) { CommandDiscovery.discoveryTracer.TraceError( "The relative path resolved to: {0}", resolvedPath); resolvedDirectory = resolvedPath; } else { CommandDiscovery.discoveryTracer.WriteLine( "The relative path was not a file system path. {0}", _lookupPaths[index]); } } else { CommandDiscovery.discoveryTracer.TraceWarning( "The current drive is not set, using the process current directory: {0}", environmentCurrentDirectory); resolvedDirectory = environmentCurrentDirectory; } // If we successfully resolved the path, make sure it is unique. Remove // any duplicates found after the first occurrence of the path. if (resolvedDirectory != null) { int existingIndex = _lookupPaths.IndexOf(resolvedDirectory); if (existingIndex != -1) { if (existingIndex > index) { // The relative path index is less than the explicit path, // so remove the explicit path. indexesToRemove.Add(removalListCount++, existingIndex); _lookupPaths[index] = resolvedDirectory; } else { // The explicit path index is less than the relative path // index, so remove the relative path. indexesToRemove.Add(removalListCount++, index); } } else { // Change the relative path to the resolved path. _lookupPaths[index] = resolvedDirectory; } } else { // The directory couldn't be resolved so remove it from the // lookup paths. indexesToRemove.Add(removalListCount++, index); } } // Now remove all the duplicates starting from the back of the collection. // As each element is removed, elements that follow are moved up to occupy // the emptied index. for (int removeIndex = indexesToRemove.Count; removeIndex > 0; --removeIndex) { int indexToRemove = indexesToRemove[removeIndex - 1]; _lookupPaths.RemoveAt(indexToRemove); } }