Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Don't publish PrivateAssets Fixes #39400 #45259

Merged
merged 5 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ internal class DependencyContextBuilder
private Dictionary<string, List<ReferenceInfo>> _compileReferences;
private Dictionary<string, List<ResolvedFile>> _resolvedNuGetFiles;
private Dictionary<string, SingleProjectInfo> _referenceProjectInfos;
private IEnumerable<string> _excludeFromPublishPackageIds;
private Dictionary<string, List<RuntimePackAssetInfo>> _runtimePackAssets;
private CompilationOptions _compilationOptions;
private string _referenceAssembliesPath;
Expand Down Expand Up @@ -204,6 +205,12 @@ public DependencyContextBuilder WithReferenceProjectInfos(Dictionary<string, Sin
return this;
}

public DependencyContextBuilder WithExcludeFromPublishAssets(IEnumerable<string> excludeFromPublishPackageIds)
{
_excludeFromPublishPackageIds = excludeFromPublishPackageIds;
return this;
}

public DependencyContextBuilder WithMainProjectInDepsFile(bool includeMainProjectInDepsFile)
{
_includeMainProjectInDepsFile = includeMainProjectInDepsFile;
Expand Down Expand Up @@ -814,6 +821,37 @@ private void CalculateExcludedLibraries()
{
_dependencyLibraries[packageToExcludeFromRuntime].ExcludeFromRuntime = true;
}

// Include transitive dependencies of all top-level dependencies
Dictionary<string, DependencyLibrary> includedDependencies = new(StringComparer.OrdinalIgnoreCase);
Stack<string> dependencyListToWalk = new(_mainProjectDependencies);

while (dependencyListToWalk.Count != 0)
{
var dependencyName = dependencyListToWalk.Pop();
// There may not be a library in the assets file if a referenced project has
// PrivateAssets="all" for a package reference, and there is a package in the graph
// that depends on the same package.
if (!includedDependencies.ContainsKey(dependencyName) &&
_excludeFromPublishPackageIds?.Contains(dependencyName) != true &&
_dependencyLibraries.TryGetValue(dependencyName, out var dependencyLibrary))
{
includedDependencies.Add(dependencyName, dependencyLibrary);
foreach (var newDependency in _libraryDependencies[dependencyName])
{
dependencyListToWalk.Push(newDependency.Name);
}
}
}

foreach (var dependencyLibrary in _dependencyLibraries.Values)
{
if (!includedDependencies.ContainsKey(dependencyLibrary.Name))
{
dependencyLibrary.ExcludeFromCompilation = true;
dependencyLibrary.ExcludeFromRuntime = true;
}
}
}

private string GetReferenceLibraryName(ReferenceInfo reference)
Expand Down
3 changes: 3 additions & 0 deletions src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public class GenerateDepsFile : TaskBase

public ITaskItem CompilerOptions { get; set; }

public ITaskItem[] ExcludeFromPublishPackageReferences { get; set; } = Array.Empty<ITaskItem>();

public ITaskItem[] RuntimeStorePackages { get; set; }

// NuGet compilation assets
Expand Down Expand Up @@ -232,6 +234,7 @@ bool ShouldIncludeRuntimeAsset(ITaskItem item)
.WithDirectReferences(directReferences)
.WithDependencyReferences(dependencyReferences)
.WithReferenceProjectInfos(referenceProjects)
.WithExcludeFromPublishAssets(PackageReferenceConverter.GetPackageIds(ExcludeFromPublishPackageReferences))
.WithRuntimePackAssets(runtimePackAssets)
.WithCompilationOptions(compilationOptions)
.WithReferenceAssembliesPath(FrameworkReferenceResolver.GetDefaultReferenceAssembliesPath())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,7 @@ Copyright (c) .NET Foundation. All rights reserved.
PlatformLibraryName="$(MicrosoftNETPlatformLibrary)"
RuntimeFrameworks="@(RuntimeFramework)"
CompilerOptions="@(DependencyFileCompilerOptions)"
ExcludeFromPublishPackageReferences="@(_ExcludeFromPublishPackageReference)"
RuntimeStorePackages="@(RuntimeStorePackages)"
CompileReferences="@(ResolvedCompileFileDefinitions)"
ResolvedNuGetFiles="@(_ResolvedNuGetFilesForPublish)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ Copyright (c) .NET Foundation. All rights reserved.
PlatformLibraryName="$(MicrosoftNETPlatformLibrary)"
RuntimeFrameworks="@(RuntimeFramework)"
CompilerOptions="@(DependencyFileCompilerOptions)"
ExcludeFromPublishPackageReferences="@(_ExcludeFromPublishPackageReference)"
CompileReferences="@(ResolvedCompileFileDefinitions)"
ResolvedNuGetFiles="@(NativeCopyLocalItems);@(ResourceCopyLocalItems);@(RuntimeCopyLocalItems)"
UserRuntimeAssemblies="@(UserRuntimeAssembly)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,18 @@ public void Build_ProducesDepsFileWithCompilationContext_ButNoReferences()
depsFile.Should().Exist();
var dependencyContext = ReadDependencyContext(depsFile.FullName);

// Ensure some compile references exist
var packageReference = dependencyContext.CompileLibraries.First(l => l.Name == "System.Runtime.CompilerServices.Unsafe");
packageReference.Assemblies.Should().NotBeEmpty();
if (TargetFramework.Equals("netcoreapp2.2"))
{
// Ensure compile references from a PrivateAssets="all" PackageReference don't exist
var packageReference = dependencyContext.CompileLibraries.FirstOrDefault(l => l.Name == "System.Runtime.CompilerServices.Unsafe", defaultValue: null);
packageReference.Should().BeNull();
}
else
{
// Ensure some compile references exist
var packageReference = dependencyContext.CompileLibraries.First(l => l.Name == "System.Runtime.CompilerServices.Unsafe");
packageReference.Assemblies.Should().NotBeEmpty();
}

var projectReference = dependencyContext.CompileLibraries.First(l => l.Name == TestProjectName);
projectReference.Assemblies.Should().NotBeEmpty();
Expand Down
Loading