Skip to content
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

Deps.json should include project references that aren't present in project.assets.json #28963

Merged
merged 7 commits into from
Nov 17, 2022
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 @@ -40,6 +40,7 @@ public void ItBuildsDependencyContextsFromProjectLockFiles(
object[] resolvedNuGetFiles)
{
LockFile lockFile = TestLockFiles.GetLockFile(mainProjectName);
LockFileLookup lockFileLookup = new LockFileLookup(lockFile);

SingleProjectInfo mainProject = SingleProjectInfo.Create(
"/usr/Path",
Expand All @@ -52,8 +53,9 @@ public void ItBuildsDependencyContextsFromProjectLockFiles(
ReferenceInfo.CreateDirectReferenceInfos(
referencePaths ?? new ITaskItem[] { },
referenceSatellitePaths ?? new ITaskItem[] { },
projectContextHasProjectReferences: false,
i => true);
lockFileLookup: lockFileLookup,
i => true,
true);

ProjectContext projectContext = lockFile.CreateProjectContext(
FrameworkConstants.CommonFrameworks.NetCoreApp10.GetShortFolderName(),
Expand All @@ -67,7 +69,7 @@ public void ItBuildsDependencyContextsFromProjectLockFiles(
resolvedNuGetFiles = Array.Empty<ResolvedFile>();
}

DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph: null, projectContext: projectContext)
DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph: null, projectContext: projectContext, libraryLookup: lockFileLookup)
.WithDirectReferences(directReferences)
.WithCompilationOptions(compilationOptions)
.WithResolvedNuGetFiles((ResolvedFile[]) resolvedNuGetFiles)
Expand Down Expand Up @@ -264,7 +266,7 @@ private DependencyContext BuildDependencyContextWithReferenceAssemblies(bool use
useCompilationOptions ? CreateCompilationOptions() :
null;

DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph: null, projectContext: projectContext)
DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph: null, projectContext: projectContext, libraryLookup: new LockFileLookup(lockFile))
.WithReferenceAssemblies(ReferenceInfo.CreateReferenceInfos(referencePaths))
.WithCompilationOptions(compilationOptions)
.Build();
Expand Down Expand Up @@ -325,7 +327,7 @@ public void ItCanGenerateTheRuntimeFallbackGraph()
void CheckRuntimeFallbacks(string runtimeIdentifier, int fallbackCount)
{
projectContext.LockFileTarget.RuntimeIdentifier = runtimeIdentifier;
var dependencyContextBuilder = new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph, projectContext);
var dependencyContextBuilder = new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph, projectContext, libraryLookup: new LockFileLookup(lockFile));
var runtimeFallbacks = dependencyContextBuilder.Build().RuntimeGraph;

runtimeFallbacks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@ internal class DependencyContextBuilder

private const string NetCorePlatformLibrary = "Microsoft.NETCore.App";

public DependencyContextBuilder(SingleProjectInfo mainProjectInfo, bool includeRuntimeFileVersions, RuntimeGraph runtimeGraph, ProjectContext projectContext)
public DependencyContextBuilder(SingleProjectInfo mainProjectInfo, bool includeRuntimeFileVersions, RuntimeGraph runtimeGraph, ProjectContext projectContext, LockFileLookup libraryLookup)
{
_mainProjectInfo = mainProjectInfo;
_includeRuntimeFileVersions = includeRuntimeFileVersions;
_runtimeGraph = runtimeGraph;

var libraryLookup = new LockFileLookup(projectContext.LockFile);

_dependencyLibraries = projectContext.LockFileTarget.Libraries
.Select(lockFileTargetLibrary =>
{
Expand Down
37 changes: 20 additions & 17 deletions src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public class GenerateDepsFile : TaskWithAssemblyResolveHooks

public bool IncludeRuntimeFileVersions { get; set; }

public bool IncludeProjectsNotInAssetsFile { get; set; }

[Required]
public string RuntimeGraphPath { get; set; }

Expand Down Expand Up @@ -125,20 +127,19 @@ private Dictionary<PackageIdentity, string> GetFilteredPackages()

private void WriteDepsFile(string depsFilePath)
{
ProjectContext projectContext;
if (AssetsFilePath == null)
{
projectContext = null;
}
else
ProjectContext projectContext = null;
LockFileLookup lockFileLookup = null;
if (AssetsFilePath != null)
{
LockFile lockFile = new LockFileCache(this).GetLockFile(AssetsFilePath);
projectContext = lockFile.CreateProjectContext(
TargetFramework,
RuntimeIdentifier,
PlatformLibraryName,
RuntimeFrameworks,
IsSelfContained);
TargetFramework,
RuntimeIdentifier,
PlatformLibraryName,
RuntimeFrameworks,
IsSelfContained);

lockFileLookup = new LockFileLookup(lockFile);
}

CompilationOptions compilationOptions = CompilationOptionsConverter.ConvertFrom(CompilerOptions);
Expand All @@ -156,13 +157,15 @@ private void WriteDepsFile(string depsFilePath)
IEnumerable<ReferenceInfo> referenceAssemblyInfos =
ReferenceInfo.CreateReferenceInfos(ReferenceAssemblies);

// If there is a generated asset file. The projectContext will have project reference.
// So remove it from directReferences to avoid duplication
var projectContextHasProjectReferences = projectContext != null;
// If there is a generated asset file, the projectContext will contain most of the project references.
// So remove any project reference contained within projectContext from directReferences to avoid duplication
IEnumerable<ReferenceInfo> directReferences =
ReferenceInfo.CreateDirectReferenceInfos(ReferencePaths,
ReferenceInfo.CreateDirectReferenceInfos(
ReferencePaths,
ReferenceSatellitePaths,
projectContextHasProjectReferences, isUserRuntimeAssembly);
lockFileLookup,
isUserRuntimeAssembly,
IncludeProjectsNotInAssetsFile);

IEnumerable<ReferenceInfo> dependencyReferences =
ReferenceInfo.CreateDependencyReferenceInfos(ReferenceDependencyPaths, ReferenceSatellitePaths, isUserRuntimeAssembly);
Expand Down Expand Up @@ -210,7 +213,7 @@ bool ShouldIncludeRuntimeAsset(ITaskItem item)
RuntimeGraph runtimeGraph =
IsSelfContained ? new RuntimeGraphCache(this).GetRuntimeGraph(RuntimeGraphPath) : null;

builder = new DependencyContextBuilder(mainProject, IncludeRuntimeFileVersions, runtimeGraph, projectContext);
builder = new DependencyContextBuilder(mainProject, IncludeRuntimeFileVersions, runtimeGraph, projectContext, lockFileLookup);
}
else
{
Expand Down
46 changes: 39 additions & 7 deletions src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,49 @@ public static IEnumerable<ReferenceInfo> CreateReferenceInfos(IEnumerable<ITaskI
public static IEnumerable<ReferenceInfo> CreateDirectReferenceInfos(
IEnumerable<ITaskItem> referencePaths,
IEnumerable<ITaskItem> referenceSatellitePaths,
bool projectContextHasProjectReferences,
Func<ITaskItem, bool> isRuntimeAssembly)
LockFileLookup lockFileLookup,
Func<ITaskItem, bool> isRuntimeAssembly,
bool includeProjectsNotInAssetsFile)
{

bool filterOutProjectReferenceIfInProjectContextAlready(ITaskItem referencePath)
bool lockFileContainsProject(ITaskItem referencePath)
{
return (projectContextHasProjectReferences ? !IsProjectReference(referencePath) : true);
if (lockFileLookup == null)
{
return false;
}

if (!IsProjectReference(referencePath))
{
return false;
}

if (!includeProjectsNotInAssetsFile)
{
return true;
}

string projectName;
string projectFilePath = referencePath.GetMetadata(MetadataKeys.MSBuildSourceProjectFile);
if (!string.IsNullOrEmpty(projectFilePath))
{
projectName = Path.GetFileNameWithoutExtension(projectFilePath);
}
else
{
// fall back to using the path to the output DLL
projectName = Path.GetFileNameWithoutExtension(referencePath.ItemSpec);
if (string.IsNullOrEmpty(projectName))
{
// unexpected - let's assume this project was already included in the assets file.
return true;
}
}

return lockFileLookup.GetProject(projectName) != null;
}

IEnumerable<ITaskItem> directReferencePaths = referencePaths
.Where(r => filterOutProjectReferenceIfInProjectContextAlready(r) && !IsNuGetReference(r) && isRuntimeAssembly(r));
.Where(r => !lockFileContainsProject(r) && !IsNuGetReference(r) && isRuntimeAssembly(r));

return CreateFilteredReferenceInfos(directReferencePaths, referenceSatellitePaths);
}
Expand Down Expand Up @@ -147,7 +179,7 @@ private static string GetVersion(ITaskItem referencePath)
if (!string.IsNullOrEmpty(fusionName))
{
AssemblyName assemblyName = new AssemblyName(fusionName);
version = assemblyName.Version.ToString();
version = assemblyName.Version?.ToString();
}

if (string.IsNullOrEmpty(version))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Copyright (c) .NET Foundation. All rights reserved.
ResolvedRuntimeTargetsFiles="@(RuntimeTargetsCopyLocalItems)"
TargetFramework="$(TargetFramework)"
RuntimeGraphPath="$(BundledRuntimeIdentifierGraphFile)"
IncludeProjectsNotInAssetsFile="$(IncludeProjectsNotInAssetsFileInDepsFile)"
/>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,8 @@ Copyright (c) .NET Foundation. All rights reserved.
IsSelfContained="$(SelfContained)"
IsSingleFile="$(_IsSingleFilePublish)"
IncludeRuntimeFileVersions="$(IncludeFileVersionsInDependencyFile)"
RuntimeGraphPath="$(BundledRuntimeIdentifierGraphFile)"/>
RuntimeGraphPath="$(BundledRuntimeIdentifierGraphFile)"
IncludeProjectsNotInAssetsFile="$(IncludeProjectsNotInAssetsFileInDepsFile)"/>

<ItemGroup>
<ResolvedFileToPublish Include="$(IntermediateDepsFilePath)">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ Copyright (c) .NET Foundation. All rights reserved.
<MetadataUpdaterSupport Condition="'$(MetadataUpdaterSupport)' == '' and '$(Configuration)' != 'Debug'">false</MetadataUpdaterSupport>
</PropertyGroup>

<PropertyGroup>
<!-- Turn on IncludeProjectsNotInAssetsFileInDepsFile by default. -->
<IncludeProjectsNotInAssetsFileInDepsFile Condition="'$(IncludeProjectsNotInAssetsFileInDepsFile)' == ''">true</IncludeProjectsNotInAssetsFileInDepsFile>
</PropertyGroup>

<PropertyGroup>
<CoreBuildDependsOn>
_CheckForBuildWithNoBuild;
Expand Down Expand Up @@ -213,7 +218,8 @@ Copyright (c) .NET Foundation. All rights reserved.
ResolvedRuntimeTargetsFiles="@(RuntimeTargetsCopyLocalItems)"
IsSelfContained="$(SelfContained)"
IncludeRuntimeFileVersions="$(IncludeFileVersionsInDependencyFile)"
RuntimeGraphPath="$(BundledRuntimeIdentifierGraphFile)"/>
RuntimeGraphPath="$(BundledRuntimeIdentifierGraphFile)"
IncludeProjectsNotInAssetsFile="$(IncludeProjectsNotInAssetsFileInDepsFile)"/>

<ItemGroup>
<!-- Do this in an ItemGroup instead of as an output parameter of the GenerateDepsFile task so that it still gets added to the item set
Expand Down
Loading