Skip to content

Commit e09f0c8

Browse files
committed
Allow opt-out of deps.json library trimming
1 parent 801788c commit e09f0c8

File tree

6 files changed

+58
-27
lines changed

6 files changed

+58
-27
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ internal class DependencyContextBuilder
2929
private string _referenceAssembliesPath;
3030
private Dictionary<PackageIdentity, string> _filteredPackages;
3131
private bool _includeMainProjectInDepsFile = true;
32+
private bool _trimLibrariesWithoutAssets = true;
3233
private readonly Dictionary<string, DependencyLibrary> _dependencyLibraries;
3334
private readonly Dictionary<string, List<LibraryDependency>> _libraryDependencies;
3435
private readonly List<string> _mainProjectDependencies;
@@ -212,6 +213,12 @@ public DependencyContextBuilder WithMainProjectInDepsFile(bool includeMainProjec
212213
return this;
213214
}
214215

216+
public DependencyContextBuilder WithTrimLibrariesWithoutAssets(bool trimLibrariesWithoutAssets)
217+
{
218+
_trimLibrariesWithoutAssets = trimLibrariesWithoutAssets;
219+
return this;
220+
}
221+
215222
public DependencyContextBuilder WithRuntimePackAssets(IEnumerable<RuntimePackAssetInfo> runtimePackAssets)
216223
{
217224
_runtimePackAssets = new Dictionary<string, List<RuntimePackAssetInfo>>();
@@ -356,39 +363,42 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
356363
}
357364
}
358365

359-
var unprocessedLibraries = runtimeLibraries.ToHashSet();
360-
while (unprocessedLibraries.Any())
366+
if (_trimLibrariesWithoutAssets)
361367
{
362-
var lib = unprocessedLibraries.First();
363-
unprocessedLibraries.Remove(lib);
364-
365-
if (lib.Library.Name.Equals("xunit.core", StringComparison.OrdinalIgnoreCase))
368+
var unprocessedLibraries = runtimeLibraries.ToHashSet();
369+
while (unprocessedLibraries.Any())
366370
{
367-
// Special case xunit.core, it should not be removed because the xUnit v2 runner looks for this library in the deps.json to
368-
// identify test projects.
369-
// See https://github.com/dotnet/sdk/issues/49248
370-
continue;
371-
}
371+
var lib = unprocessedLibraries.First();
372+
unprocessedLibraries.Remove(lib);
372373

373-
if (lib.Library.RuntimeAssemblyGroups.Count == 0 && lib.Library.NativeLibraryGroups.Count == 0 && lib.Library.ResourceAssemblies.Count == 0)
374-
{
375-
if (lib.Library.Dependencies.All(d => !libraries.TryGetValue(d.Name, out var dependency) || dependency.Dependents.Count > 1))
374+
if (lib.Library.Name.Equals("xunit.core", StringComparison.OrdinalIgnoreCase))
376375
{
377-
runtimeLibraries.Remove(lib);
378-
libraries.Remove(lib.Library.Name);
379-
foreach (var dependency in lib.Library.Dependencies)
376+
// Special case xunit.core, it should not be removed because the xUnit v2 runner looks for this library in the deps.json to
377+
// identify test projects.
378+
// See https://github.com/dotnet/sdk/issues/49248
379+
continue;
380+
}
381+
382+
if (lib.Library.RuntimeAssemblyGroups.Count == 0 && lib.Library.NativeLibraryGroups.Count == 0 && lib.Library.ResourceAssemblies.Count == 0)
383+
{
384+
if (lib.Library.Dependencies.All(d => !libraries.TryGetValue(d.Name, out var dependency) || dependency.Dependents.Count > 1))
380385
{
381-
if (libraries.TryGetValue(dependency.Name, out ModifiableRuntimeLibrary value))
386+
runtimeLibraries.Remove(lib);
387+
libraries.Remove(lib.Library.Name);
388+
foreach (var dependency in lib.Library.Dependencies)
382389
{
383-
value.Dependents.Remove(lib.Library.Name);
390+
if (libraries.TryGetValue(dependency.Name, out ModifiableRuntimeLibrary value))
391+
{
392+
value.Dependents.Remove(lib.Library.Name);
393+
}
384394
}
385-
}
386395

387-
foreach (var dependent in lib.Dependents)
388-
{
389-
if (libraries.TryGetValue(dependent, out var dep))
396+
foreach (var dependent in lib.Dependents)
390397
{
391-
unprocessedLibraries.Add(dep);
398+
if (libraries.TryGetValue(dependent, out var dep))
399+
{
400+
unprocessedLibraries.Add(dep);
401+
}
392402
}
393403
}
394404
}

src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public class GenerateDepsFile : TaskBase
4949
[Required]
5050
public bool IncludeMainProject { get; set; }
5151

52+
public bool TrimDepsJsonLibrariesWithoutAssets { get; set; }
53+
5254
// @(ReferencePath) that will be passed to
5355
public ITaskItem[] ReferencePaths { get; set; } = Array.Empty<ITaskItem>();
5456

@@ -230,6 +232,7 @@ bool ShouldIncludeRuntimeAsset(ITaskItem item)
230232

231233
builder = builder
232234
.WithMainProjectInDepsFile(IncludeMainProject)
235+
.WithTrimLibrariesWithoutAssets(TrimDepsJsonLibrariesWithoutAssets)
233236
.WithReferenceAssemblies(referenceAssemblyInfos)
234237
.WithDirectReferences(directReferences)
235238
.WithDependencyReferences(dependencyReferences)

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.DesignerSupport.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Copyright (c) .NET Foundation. All rights reserved.
5959
AssetsFilePath="$(ProjectAssetsFile)"
6060
DepsFilePath="$(_DesignerDepsFilePath)"
6161
IncludeMainProject="false"
62+
TrimDepsJsonLibrariesWithoutAssets="$(TrimDepsJsonLibrariesWithoutAssets)"
6263
IncludeRuntimeFileVersions="$(IncludeFileVersionsInDependencyFile)"
6364
IsSelfContained="false"
6465
PlatformLibraryName="$(MicrosoftNETPlatformLibrary)"

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,7 @@ Copyright (c) .NET Foundation. All rights reserved.
12481248
ReferenceAssemblies="@(_ReferenceAssemblies)"
12491249
RuntimePackAssets="@(RuntimePackAsset)"
12501250
IncludeMainProject="$(IncludeMainProjectInDepsFile)"
1251+
TrimDepsJsonLibrariesWithoutAssets="$(TrimDepsJsonLibrariesWithoutAssets)"
12511252
RuntimeIdentifier="$(RuntimeIdentifier)"
12521253
PlatformLibraryName="$(MicrosoftNETPlatformLibrary)"
12531254
RuntimeFrameworks="@(RuntimeFramework)"

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Copyright (c) .NET Foundation. All rights reserved.
8888
<ProjectRuntimeConfigFilePath Condition="'$(ProjectRuntimeConfigFilePath)' == ''">$(TargetDir)$(ProjectRuntimeConfigFileName)</ProjectRuntimeConfigFilePath>
8989
<ProjectRuntimeConfigDevFilePath Condition="'$(ProjectRuntimeConfigDevFilePath)' == '' and $(GenerateRuntimeConfigDevFile) == 'true'">$(TargetDir)$(AssemblyName).runtimeconfig.dev.json</ProjectRuntimeConfigDevFilePath>
9090
<IncludeMainProjectInDepsFile Condition=" '$(IncludeMainProjectInDepsFile)' == '' ">true</IncludeMainProjectInDepsFile>
91+
<TrimDepsJsonLibrariesWithoutAssets Condition=" '$(TrimDepsJsonLibrariesWithoutAssets)' == '' ">true</TrimDepsJsonLibrariesWithoutAssets>
9192
</PropertyGroup>
9293

9394
<Import Project="Microsoft.NET.Sdk.Shared.targets" />
@@ -316,6 +317,7 @@ Copyright (c) .NET Foundation. All rights reserved.
316317
ReferenceAssemblies="@(_ReferenceAssemblies)"
317318
RuntimePackAssets="@(RuntimePackAsset)"
318319
IncludeMainProject="$(IncludeMainProjectInDepsFile)"
320+
TrimDepsJsonLibrariesWithoutAssets="$(TrimDepsJsonLibrariesWithoutAssets)"
319321
RuntimeIdentifier="$(RuntimeIdentifier)"
320322
PlatformLibraryName="$(MicrosoftNETPlatformLibrary)"
321323
RuntimeFrameworks="@(RuntimeFramework)"

test/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildADesktopLibrary.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,22 @@ public void PackageReferences_with_private_assets_do_not_appear_in_deps_file(str
136136
}
137137
}
138138

139-
[Fact]
140-
public void PackageWithoutAssets_ShouldNotShowUpInDepsJson()
139+
[Theory]
140+
[InlineData(true)]
141+
[InlineData(false)]
142+
public void PackageWithoutAssets_ShouldNotShowUpInDepsJson(bool trimLibrariesWithoutAssets)
141143
{
142144
var testProject = new TestProject()
143145
{
144146
TargetFrameworks = ToolsetInfo.CurrentTargetFramework
145147
};
146148
testProject.PackageReferences.Add(new TestPackageReference("Nerdbank.GitVersioning", "3.6.146"));
147149

150+
if (!trimLibrariesWithoutAssets)
151+
{
152+
testProject.AdditionalProperties["TrimDepsJsonLibrariesWithoutAssets"] = "False";
153+
}
154+
148155
var testAsset = _testAssetsManager.CreateTestProject(testProject);
149156

150157
var buildCommand = new BuildCommand(testAsset);
@@ -153,7 +160,14 @@ public void PackageWithoutAssets_ShouldNotShowUpInDepsJson()
153160
using (var depsJsonFileStream = File.OpenRead(Path.Combine(buildCommand.GetOutputDirectory(ToolsetInfo.CurrentTargetFramework).FullName, $"{testProject.Name}.deps.json")))
154161
{
155162
var dependencyContext = new DependencyContextJsonReader().Read(depsJsonFileStream);
156-
dependencyContext.RuntimeLibraries.Any(l => l.Name.Equals("Nerdbank.GitVersioning")).Should().BeFalse();
163+
if (trimLibrariesWithoutAssets)
164+
{
165+
dependencyContext.RuntimeLibraries.Any(l => l.Name.Equals("Nerdbank.GitVersioning")).Should().BeFalse();
166+
}
167+
else
168+
{
169+
dependencyContext.RuntimeLibraries.Any(l => l.Name.Equals("Nerdbank.GitVersioning")).Should().BeTrue();
170+
}
157171
}
158172
}
159173

0 commit comments

Comments
 (0)