Skip to content

Include only PackageReferences included at runtime in deps.json #46218

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ public static IEnumerable<object[]> ProjectData
new object[] { "dotnet.new", "1.0.0", null, "dotnet.new.resources", null, dotnetNewSatelliteAssemblies, null, null, null },
new object[] { "simple.dependencies", "1.0.0", null, "simple.dependencies", null, null, null, null, resolvedNuGetFiles },
};



}
}

Expand Down Expand Up @@ -190,11 +187,16 @@ public void ItDoesntCreateReferenceAssembliesWhenNoCompilationOptions()
.RuntimeLibraries
.SelectMany(l => l.Dependencies)
.Should()
.NotBeEmpty()
.And
.NotContain(d => d.Name == "System.NotConflicting")
.And
.NotContain(d => d.Name == "System.Collections.NonGeneric.Reference");
.BeEmpty();
}

[Fact]
public void ItDoesntCreateKeepUnneededRuntimeReferences()
{
DependencyContext dependencyContext = BuildDependencyContextWithReferenceAssemblies(useCompilationOptions: false);

dependencyContext.RuntimeLibraries.Count.Should().Be(1);
dependencyContext.RuntimeLibraries[0].Name.Should().Be("simple.dependencies"); // This is the entrypoint
}

[Fact]
Expand All @@ -213,6 +215,188 @@ public void ItHandlesReferenceAndPackageReferenceNameCollisions()
.Contain(c => c.Name == "System.Collections.NonGeneric.Reference.Reference" && c.Type == "referenceassembly");
}

// If an assembly is in withResources, it has to be a key in dependencies, even with an empty list.
private static DependencyContext BuildDependencyContextFromDependenciesWithResources(Dictionary<string, List<string>> dependencies, List<string> withResources, List<string> references, bool dllReference)
{
string mainProjectName = "simpleApp";
LockFile lockFile = TestLockFiles.GetLockFile(mainProjectName);

SingleProjectInfo mainProject = SingleProjectInfo.Create(
"/usr/Path",
mainProjectName,
".dll",
"1.0.0",
[]);
string mainProjectDirectory = Path.GetDirectoryName(mainProject.ProjectPath);


ITaskItem[] referencePaths = dllReference ? references.Select(reference =>
new MockTaskItem($"/usr/Path/{reference}.dll", new Dictionary<string, string> {
{ "CopyLocal", "false" },
{ "FusionName", $"{reference}, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" },
{ "Version", "" },
})).ToArray() : [];

ProjectContext projectContext = lockFile.CreateProjectContext(
FrameworkConstants.CommonFrameworks.Net10_0.GetShortFolderName(),
runtime: null,
platformLibraryName: Constants.DefaultPlatformLibrary,
runtimeFrameworks: null,
isSelfContained: false);

if (!dllReference)
{
projectContext.LockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(string.Empty, references));
}

Dictionary<string, SingleProjectInfo> referenceProjectInfos = new();

foreach (KeyValuePair<string, List<string>> kvp in dependencies)
{
projectContext.LockFileTarget.Libraries = projectContext.LockFileTarget.Libraries.Concat([
new LockFileTargetLibrary()
{
Name = kvp.Key,
Version = new NuGetVersion(4, 0, 0),
Type = withResources.Contains(kvp.Key) ? "project" : "unrealType",
Dependencies = kvp.Value.Select(n => new PackageDependency(n)).ToList()
}]).ToList();

if (withResources.Contains(kvp.Key))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having resource assets is not the most common case, so instead we should probably model whether the libraries have runtime or compilation assets (and then resource assets if we have additional scenarios for that).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All three are handled in the same way in this part of the code, but they're added in different ways...I don't disagree with you, exactly, but I suspect it would take me a couple days to get that to work, and it wouldn't actually test anything we aren't already properly testing using resource assets.

{
var fullPath = Path.GetFullPath(Path.Combine(mainProjectDirectory, kvp.Key));
lockFile.Libraries = lockFile.Libraries.Concat([new LockFileLibrary()
{
Name = kvp.Key,
Version = new NuGetVersion(4, 0, 0),
Type = "project",
MSBuildProject = fullPath
}]).ToList();

referenceProjectInfos.Add(fullPath, SingleProjectInfo.Create(kvp.Key, kvp.Key, ".dll", "4.0.0",
[new MockTaskItem($"{kvp.Key}.resource", new Dictionary<string, string>() {
{ "Culture", "en-us" },
{ "TargetPath", $"{kvp.Key}.resource" }
})]));
}
}

CompilationOptions compilationOptions = CreateCompilationOptions();

return new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph: null, projectContext: projectContext, libraryLookup: new LockFileLookup(lockFile))
.WithReferenceAssemblies(ReferenceInfo.CreateReferenceInfos(referencePaths))
.WithCompilationOptions(compilationOptions)
.WithReferenceProjectInfos(referenceProjectInfos)
.Build();
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void DirectReferenceToPackageWithNoAssets(bool dllReference)
{
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources([], [], ["System.A"], dllReference);
Save(dependencyContext);
dependencyContext.RuntimeLibraries.Count.Should().Be(1);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void IndirectReferenceToPackageWithNoAssets(bool dllReference)
{
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>() {
{ "System.A", ["System.B"] }
}, ["System.A"], ["System.A"], dllReference);
Save(dependencyContext);
dependencyContext.RuntimeLibraries.Count.Should().Be(2);
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.A"));
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void PackageWithNoAssetsReferencesPackageWithNoAssets(bool dllReference)
{
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>() {
{ "System.A", ["System.B"] },
{ "System.B", [] }
}, [], ["System.A"], dllReference);
Save(dependencyContext);
dependencyContext.RuntimeLibraries.Count.Should().Be(1);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void PackageWithNoAssetsReferencesPackageWithAssets(bool dllReference)
{
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>() {
{ "System.A", ["System.B"] },
{ "System.B", [] }
}, ["System.B"], ["System.A"], dllReference);
Save(dependencyContext);
dependencyContext.RuntimeLibraries.Count.Should().Be(3);
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.A"));
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.B"));
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void PackageWithNoAssetsReferencesPackageReferencesByOtherPackage(bool dllReference)
{
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>()
{
{ "System.A", ["System.B"] },
{ "System.B", [] },
}, ["System.B"], ["System.A", "System.B"], dllReference);
Save(dependencyContext);
dependencyContext.RuntimeLibraries.Count.Should().Be(2);
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.B"));
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void PackageWithNoAssetsReferencesPackageWithAssetsWithOtherReferencer(bool dllReference)
{
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>()
{
{ "System.A", ["System.B"] },
{ "System.B", [] },
{ "System.C", ["System.B"] }
}, ["System.B", "System.C"], ["System.A", "System.C"], dllReference);
Save(dependencyContext);
dependencyContext.RuntimeLibraries.Count.Should().Be(3);
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.C"));
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.B"));
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void TwoPackagesWithNoAssetsReferencePackageWithAssets(bool dllReference)
{
DependencyContext dependencyContext = BuildDependencyContextFromDependenciesWithResources(new Dictionary<string, List<string>>()
{
{ "System.A", ["System.B"] },
{ "System.C", ["System.B"] },
{ "System.B", [] }
}, ["System.B"], ["System.A", "System.C"], dllReference);
Save(dependencyContext);
dependencyContext.RuntimeLibraries.Count.Should().Be(3);
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.B"));
if (dependencyContext.RuntimeLibraries.Any(x => x.Name.Equals("System.A")))
{
dependencyContext.RuntimeLibraries.Should().NotContain(x => x.Name.Equals("System.C"));
}
else
{
dependencyContext.RuntimeLibraries.Should().Contain(x => x.Name.Equals("System.C"));
}
}

private DependencyContext BuildDependencyContextWithReferenceAssemblies(bool useCompilationOptions)
{
string mainProjectName = "simple.dependencies";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"version": 3,
"targets": {
"net10.0": {}
},
"libraries": {},
"projectFileDependencyGroups": {
"net10.0": []
},
"packageFolders": {
"C:\\Users\\nmytelka\\.nuget\\packages\\": {},
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages": {}
},
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "C:\\git\\repro\\consoletest\\consoletest.csproj",
"projectName": "consoletest",
"projectPath": "C:\\git\\repro\\consoletest\\consoletest.csproj",
"packagesPath": "C:\\Users\\username\\.nuget\\packages\\",
"outputPath": "C:\\git\\repro\\consoletest\\obj\\",
"projectStyle": "PackageReference",
"fallbackFolders": [],
"configFilePaths": [
"C:\\Users\\username\\AppData\\Roaming\\NuGet\\NuGet.Config"
],
"originalTargetFrameworks": [
"net10.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net10.0": {
"targetAlias": "net10.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
},
"restoreAuditProperties": {
"enableAudit": "true",
"auditLevel": "low",
"auditMode": "all"
},
"SdkAnalysisLevel": "10.0.100"
},
"frameworks": {
"net10.0": {
"targetAlias": "net10.0",
"imports": [
"net472",
"net481"
],
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\git\\sdk\\artifacts\\bin\\redist\\Debug\\dotnet\\sdk\\10.0.100-dev\\RuntimeIdentifierGraph.json"
}
}
}
}
Loading
Loading