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 #28892

Closed
wants to merge 11 commits into from
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>9a8d9cf9-379b-45c1-a95a-482ecb76a56c</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>$(MSBuildProjectName)</RootNamespace>
<AssemblyName>$(MSBuildProjectName)</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System"/>
<Reference Include="System.Core"/>
<Reference Include="System.Xml.Linq"/>
<Reference Include="System.Data.DataSetExtensions"/>
<Reference Include="Microsoft.CSharp"/>
<Reference Include="System.Data"/>
<Reference Include="System.Net.Http"/>
<Reference Include="System.Xml"/>
</ItemGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace AuxLibrary
{
public static class Helper
{
public static void WriteMessage()
{
Console.WriteLine("This string came from AuxLibrary!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace MainLibrary
{
public static class Helper
{
public static void WriteMessage()
{
Console.WriteLine("This string came from MainLibrary!");
AuxLibrary.Helper.WriteMessage();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>a60a2336-1e2d-4c7a-85cb-a9e890e2558b</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>$(MSBuildProjectName)</RootNamespace>
<AssemblyName>$(MSBuildProjectName)</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System"/>
<Reference Include="System.Core"/>
<Reference Include="System.Xml.Linq"/>
<Reference Include="System.Data.DataSetExtensions"/>
<Reference Include="Microsoft.CSharp"/>
<Reference Include="System.Data"/>
<Reference Include="System.Net.Http"/>
<Reference Include="System.Xml"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AuxLibrary\AuxLibrary.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace TestApp
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("TestApp --depends on--> MainLibrary --depends on--> AuxLibrary");
MainLibrary.Helper.WriteMessage();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>$(CurrentTargetFramework)</TargetFramework>
<ProduceReferenceAssembly>false</ProduceReferenceAssembly>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\MainLibrary\MainLibrary.csproj" />
</ItemGroup>
</Project>
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,7 +53,7 @@ public void ItBuildsDependencyContextsFromProjectLockFiles(
ReferenceInfo.CreateDirectReferenceInfos(
referencePaths ?? new ITaskItem[] { },
referenceSatellitePaths ?? new ITaskItem[] { },
projectContextHasProjectReferences: false,
lockFileLookup: lockFileLookup,
i => true);

ProjectContext projectContext = lockFile.CreateProjectContext(
Expand All @@ -67,7 +68,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 +265,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 +326,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
34 changes: 17 additions & 17 deletions src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,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 +155,14 @@ 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 have most 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);

IEnumerable<ReferenceInfo> dependencyReferences =
ReferenceInfo.CreateDependencyReferenceInfos(ReferenceDependencyPaths, ReferenceSatellitePaths, isUserRuntimeAssembly);
Expand Down Expand Up @@ -210,7 +210,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
28 changes: 22 additions & 6 deletions src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,33 @@ public static IEnumerable<ReferenceInfo> CreateReferenceInfos(IEnumerable<ITaskI
public static IEnumerable<ReferenceInfo> CreateDirectReferenceInfos(
IEnumerable<ITaskItem> referencePaths,
IEnumerable<ITaskItem> referenceSatellitePaths,
bool projectContextHasProjectReferences,
LockFileLookup lockFileLookup,
Func<ITaskItem, bool> isRuntimeAssembly)
{

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

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

string outputName = Path.GetFileName(referencePath.ItemSpec);
string projectName = Path.GetFileNameWithoutExtension(outputName);
wrall marked this conversation as resolved.
Show resolved Hide resolved
if (string.IsNullOrEmpty(projectName))
{
return true;
}

return lockFileLookup.GetProject(projectName) != null;
wrall marked this conversation as resolved.
Show resolved Hide resolved
}

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 +163,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
Loading