@@ -22,6 +22,7 @@ internal class DependencyContextBuilder
22
22
private Dictionary < string , List < ReferenceInfo > > _compileReferences ;
23
23
private Dictionary < string , List < ResolvedFile > > _resolvedNuGetFiles ;
24
24
private Dictionary < string , SingleProjectInfo > _referenceProjectInfos ;
25
+ private IEnumerable < string > _excludeFromPublishPackageIds ;
25
26
private Dictionary < string , List < RuntimePackAssetInfo > > _runtimePackAssets ;
26
27
private CompilationOptions _compilationOptions ;
27
28
private string _referenceAssembliesPath ;
@@ -204,6 +205,12 @@ public DependencyContextBuilder WithReferenceProjectInfos(Dictionary<string, Sin
204
205
return this ;
205
206
}
206
207
208
+ public DependencyContextBuilder WithExcludeFromPublishAssets ( IEnumerable < string > excludeFromPublishPackageIds )
209
+ {
210
+ _excludeFromPublishPackageIds = excludeFromPublishPackageIds ;
211
+ return this ;
212
+ }
213
+
207
214
public DependencyContextBuilder WithMainProjectInDepsFile ( bool includeMainProjectInDepsFile )
208
215
{
209
216
_includeMainProjectInDepsFile = includeMainProjectInDepsFile ;
@@ -814,6 +821,38 @@ private void CalculateExcludedLibraries()
814
821
{
815
822
_dependencyLibraries [ packageToExcludeFromRuntime ] . ExcludeFromRuntime = true ;
816
823
}
824
+
825
+ // Include transitive dependencies of all top-level dependencies
826
+ Dictionary < string , DependencyLibrary > includedDependencies = new ( StringComparer . OrdinalIgnoreCase ) ;
827
+ Stack < string > dependencyListToWalk = new ( _mainProjectDependencies ) ;
828
+
829
+ while ( dependencyListToWalk . Count != 0 )
830
+ {
831
+ var dependencyName = dependencyListToWalk . Pop ( ) ;
832
+ if ( ! includedDependencies . ContainsKey ( dependencyName ) && _excludeFromPublishPackageIds ? . Contains ( dependencyName ) != true )
833
+ {
834
+ // There may not be a library in the assets file if a referenced project has
835
+ // PrivateAssets="all" for a package reference, and there is a package in the graph
836
+ // that depends on the same package.
837
+ if ( _dependencyLibraries . TryGetValue ( dependencyName , out var dependencyLibrary ) )
838
+ {
839
+ includedDependencies . Add ( dependencyName , dependencyLibrary ) ;
840
+ foreach ( var newDependency in _libraryDependencies [ dependencyName ] )
841
+ {
842
+ dependencyListToWalk . Push ( newDependency . Name ) ;
843
+ }
844
+ }
845
+ }
846
+ }
847
+
848
+ foreach ( var dependencyLibrary in _dependencyLibraries . Values )
849
+ {
850
+ if ( ! includedDependencies . ContainsKey ( dependencyLibrary . Name ) )
851
+ {
852
+ dependencyLibrary . ExcludeFromCompilation = true ;
853
+ dependencyLibrary . ExcludeFromRuntime = true ;
854
+ }
855
+ }
817
856
}
818
857
819
858
private string GetReferenceLibraryName ( ReferenceInfo reference )
0 commit comments