diff --git a/src/SourceBuild/content/eng/tools/tasks/Microsoft.DotNet.UnifiedBuild.Tasks/UpdateNuGetConfigPackageSourcesMappings.cs b/src/SourceBuild/content/eng/tools/tasks/Microsoft.DotNet.UnifiedBuild.Tasks/UpdateNuGetConfigPackageSourcesMappings.cs index 9b7050ef46d4..74c181cc7fa1 100644 --- a/src/SourceBuild/content/eng/tools/tasks/Microsoft.DotNet.UnifiedBuild.Tasks/UpdateNuGetConfigPackageSourcesMappings.cs +++ b/src/SourceBuild/content/eng/tools/tasks/Microsoft.DotNet.UnifiedBuild.Tasks/UpdateNuGetConfigPackageSourcesMappings.cs @@ -62,6 +62,9 @@ public class UpdateNuGetConfigPackageSourcesMappings : Task private Dictionary> allSourcesPackages = []; private Dictionary> oldSourceMappingPatterns = []; + // allOldSourceMappingPatterns is a union of all patterns from oldSourceMappingPatterns + List allOldSourceMappingPatterns = []; + // All other dictionaries are: 'package id', 'list of package versions' private Dictionary> currentPackages = []; private Dictionary> referencePackages = []; @@ -136,14 +139,17 @@ public override bool Execute() } } - // Union all package sources to get the distinct list. These will get added to + // Union all package sources to get the distinct list. Remove all original patterns + // from online feeds that were unique to those feeds. + // + // These will get added to // all custom sources and all online sources based on the following logic: // If there were existing mappings for online feeds, add cummulative mappings // from all feeds to these two. // If there were no existing mappings, add default mappings for all online feeds. List packagePatterns = pkgSrcMappingElement.Descendants() .Where(e => e.Name == "packageSource") - .SelectMany(e => e.Descendants().Where(e => e.Name == "package")) + .SelectMany(e => e.Descendants().Where(e => e.Name == "package" && !allOldSourceMappingPatterns.Contains(e.Attribute("pattern").Value))) .Select(e => e.Attribute("pattern").Value) .Distinct() .ToList(); @@ -154,11 +160,7 @@ public override bool Execute() } AddMappingsForCustomSources(pkgSrcMappingElement, pkgSourcesElement, packagePatterns); - - if (oldSourceMappingPatterns.Count == 0) - { - AddMappingsForOnlineSources(pkgSrcMappingElement, pkgSourcesElement, packagePatterns); - } + AddMappingsForOnlineSources(pkgSrcMappingElement, pkgSourcesElement, packagePatterns); } using (var writer = XmlWriter.Create(NuGetConfigFile, new XmlWriterSettings { NewLineChars = newLineChars, Indent = true })) @@ -180,28 +182,41 @@ private void AddMappingsForCustomSources(XElement pkgSrcMappingElement, XElement { if (null != GetElement(pkgSourcesElement, "add", sourceName)) { - ReplaceSourceMappings(pkgSrcMappingElement, sourceName, packagePatterns); + AddSourceMappings(pkgSrcMappingElement, sourceName, packagePatterns); + + // Add all old source mapping patterns for custom sources. + // Unlike local sources, custom sources cannot be enumerated. + XElement pkgSrcElement = GetElement(pkgSrcMappingElement, "packageSource", sourceName); + if (pkgSrcElement != null) + { + foreach (string pattern in allOldSourceMappingPatterns) + { + pkgSrcElement.Add(new XElement("package", new XAttribute("pattern", pattern))); + } + } } } } - private void ReplaceSourceMappings(XElement pkgSrcMappingElement, string sourceName, List packagePatterns) + private void AddSourceMappings(XElement pkgSrcMappingElement, string sourceName, List packagePatterns) { - XElement pkgSrc = new XElement("packageSource", new XAttribute("key", sourceName)); - foreach (string packagePattern in packagePatterns) - { - pkgSrc.Add(new XElement("package", new XAttribute("pattern", packagePattern))); - } + XElement pkgSrc; XElement existingPkgSrcElement = GetElement(pkgSrcMappingElement, "packageSource", sourceName); if (existingPkgSrcElement != null) { - existingPkgSrcElement.ReplaceWith(pkgSrc); + pkgSrc = existingPkgSrcElement; } else { + pkgSrc = new XElement("packageSource", new XAttribute("key", sourceName)); pkgSrcMappingElement.Add(pkgSrc); } + + foreach (string packagePattern in packagePatterns) + { + pkgSrc.Add(new XElement("package", new XAttribute("pattern", packagePattern))); + } } private void AddMappingsForOnlineSources(XElement pkgSrcMappingElement, XElement pkgSourcesElement, List packagePatterns) @@ -215,7 +230,7 @@ private void AddMappingsForOnlineSources(XElement pkgSrcMappingElement, XElement .Select(e => e.Attribute("key").Value) .Distinct()) { - ReplaceSourceMappings(pkgSrcMappingElement, sourceName, packagePatterns); + AddSourceMappings(pkgSrcMappingElement, sourceName, packagePatterns); } } @@ -377,6 +392,10 @@ private void GetExistingFilteredSourceMappings(XElement pkgSrcMappingElement) !prebuiltPackages.ContainsKey(pattern)) { filteredPatterns.Add(pattern); + if (!allOldSourceMappingPatterns.Contains(pattern)) + { + allOldSourceMappingPatterns.Add(pattern); + } } }