Skip to content

Add Framework Assemblies from project assets file to compiler references #489

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 4 commits into from
Dec 20, 2016
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
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net451</TargetFramework>
<RuntimeIdentifier>win7-x86</RuntimeIdentifier>
</PropertyGroup>

<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.0.0" />
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this have a reference to Microsoft.NETCore.App until #450 is merged?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this for the case where the SDK is not present? I would have expected Project Sdk="Microsoft.NET.Sdk" to be sufficient here, and it appears to work on all my test machines.

Copy link
Contributor

Choose a reason for hiding this comment

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

NerCore.App is needed for netcore projects (even if the SDK is present). This is however a Desktop project - so not needed here.

</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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;
using System.ComponentModel.DataAnnotations;

class Program
{
static void Main(string[] args)
{
var b = new VerifyCodeViewModel
{
Provider = "Required Test Provider"
};
Console.WriteLine(b.Provider);
}

public class VerifyCodeViewModel
{
[Required]
public string Provider { get; set; }
}
}
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.Diagnostics;

namespace StopwatchLib
{
public static class Helper
{
public static Stopwatch StartWatch()
{
return Stopwatch.StartNew();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net45</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NETStandard.Library" Version="1.6" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ private void GetFileDependencies(LockFileTargetLibrary package, string targetNam
item.SetMetadata(MetadataKeys.ParentTarget, targetName); // Foreign Key
item.SetMetadata(MetadataKeys.ParentPackage, packageId); // Foreign Key

if (fileGroup == FileGroup.FrameworkAssembly)
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 it would be helpful to have a comment here explaining how the FrameworkAssembly reference is expressed in the lockfile. From the code I gather that it has a path which instead of referring to a file in the package is just the name of the framework reference to add.

{
// NOTE: the path provided for framework assemblies is the name of the framework reference
item.SetMetadata("FrameworkAssembly", filePath);
}

foreach (var property in properties)
{
item.SetMetadata(property.Key, property.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,34 @@ Copyright (c) .NET Foundation. All rights reserved.
</CreateItem>
</Target>

<!--
============================================================
HELPER: Get File Dependencies matching active TFM and RID
============================================================
-->

<Target Name="_ComputeActiveTFMFileDependencies"
DependsOnTargets="RunResolvePackageDependencies"
Returns="_ActiveTFMFileDependencies">
<ItemGroup>
<_ActiveTFMFileDependencies Include="@(FileDependencies->WithMetadataValue('ParentTarget', '$(_NugetTargetMonikerAndRID)'))" />
</ItemGroup>
</Target>

<!--
============================================================
Reference Targets: For populating References based on lock file
- _ComputeLockFileReferences
- _ComputeLockFileFrameworks
- ResolveLockFileReferences
============================================================
-->

<Target Name="_ComputeLockFileReferences"
DependsOnTargets="RunResolvePackageDependencies"
DependsOnTargets="_ComputeActiveTFMFileDependencies"
Returns="ResolvedCompileFileDefinitions">
<ItemGroup>
<TFMFileItems Include="@(FileDependencies->WithMetadataValue('ParentTarget', '$(_NugetTargetMonikerAndRID)'))" />
<_CompileFileItems Include="@(TFMFileItems->WithMetadataValue('FileGroup', 'CompileTimeAssembly'))" />
<_CompileFileItems Include="@(_ActiveTFMFileDependencies->WithMetadataValue('FileGroup', 'CompileTimeAssembly'))" />

<!-- Get corresponding file definitions -->
<__CompileFileDefinitions Include="@(FileDefinitions)" Exclude="@(_CompileFileItems)" />
Expand All @@ -251,11 +265,28 @@ Copyright (c) .NET Foundation. All rights reserved.
</ItemGroup>
</Target>

<Target Name="_ComputeLockFileFrameworks"
Condition="'$(DisableLockFileFrameworks)' != 'true'"
DependsOnTargets="_ComputeActiveTFMFileDependencies"
Returns="ResolvedFrameworkAssemblies">
<ItemGroup>
<_FrameworkAssemblies Include="@(_ActiveTFMFileDependencies->WithMetadataValue('FileGroup', 'FrameworkAssembly'))" />

<ResolvedFrameworkAssemblies Include="%(_FrameworkAssemblies.FrameworkAssembly)">
<Private>false</Private>
<NuGetIsFrameworkReference>true</NuGetIsFrameworkReference>
<NuGetSourceType>Package</NuGetSourceType>
</ResolvedFrameworkAssemblies>

</ItemGroup>
</Target>

<Target Name="ResolveLockFileReferences"
DependsOnTargets="_ComputeLockFileReferences">
DependsOnTargets="_ComputeLockFileReferences;_ComputeLockFileFrameworks">
<ItemGroup>
<!-- Add the references we computed -->
<Reference Include="@(ResolvedCompileFileDefinitions)" />
<Reference Include="@(ResolvedFrameworkAssemblies)" />
</ItemGroup>
</Target>

Expand Down Expand Up @@ -295,25 +326,23 @@ Copyright (c) .NET Foundation. All rights reserved.
-->

<Target Name="_ComputeLockFileCopyLocal"
DependsOnTargets="RunResolvePackageDependencies;RunProduceContentAssets"
DependsOnTargets="_ComputeActiveTFMFileDependencies;RunProduceContentAssets"
Returns="NativeCopyLocalItems;RuntimeCopyLocalItems;ResourceCopyLocalItems;AllCopyLocalItems">
<ItemGroup>
<TFMFileItems Include="@(FileDependencies->WithMetadataValue('ParentTarget', '$(_NugetTargetMonikerAndRID)'))" />

<!--NativeLibrary-->
<_NativeFileItems Include="@(TFMFileItems->WithMetadataValue('FileGroup', 'NativeLibrary'))" />
<_NativeFileItems Include="@(_ActiveTFMFileDependencies->WithMetadataValue('FileGroup', 'NativeLibrary'))" />
<__NativeCopyLocalItems Include="@(FileDefinitions)" Exclude="@(_NativeFileItems)" />
<_NativeCopyLocalItems Include="@(FileDefinitions)" Exclude="@(__NativeCopyLocalItems)" />
<NativeCopyLocalItems Include="%(_NativeCopyLocalItems.ResolvedPath)" />

<!--RuntimeAssembly-->
<_RuntimeFileItems Include="@(TFMFileItems->WithMetadataValue('FileGroup', 'RuntimeAssembly'))" />
<_RuntimeFileItems Include="@(_ActiveTFMFileDependencies->WithMetadataValue('FileGroup', 'RuntimeAssembly'))" />
<__RuntimeCopyLocalItems Include="@(FileDefinitions)" Exclude="@(_RuntimeFileItems)" />
<_RuntimeCopyLocalItems Include="@(FileDefinitions)" Exclude="@(__RuntimeCopyLocalItems)" />
<RuntimeCopyLocalItems Include="%(_RuntimeCopyLocalItems.ResolvedPath)" />

<!--ResourceAssembly-->
<_ResourceFileItems Include="@(TFMFileItems->WithMetadataValue('FileGroup', 'ResourceAssembly'))" />
<_ResourceFileItems Include="@(_ActiveTFMFileDependencies->WithMetadataValue('FileGroup', 'ResourceAssembly'))" />
<__ResourceCopyLocalItems Include="@(FileDefinitions)" Exclude="@(_ResourceFileItems)" />
<_ResourceCopyLocalItems Include="@(FileDefinitions)" Exclude="@(__ResourceCopyLocalItems)" />
<ResourceCopyLocalItems Include="%(_ResourceCopyLocalItems.ResolvedPath)" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// 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 FluentAssertions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Xml.Linq;
using Xunit;
using static Microsoft.NET.TestFramework.Commands.MSBuildTest;

namespace Microsoft.NET.Build.Tests
{
public class GivenThatWeWantToBuildAppsWithFrameworkRefs
{
private TestAssetsManager _testAssetsManager = TestAssetsManager.TestProjectsAssetsManager;

[Fact]
public void It_builds_the_projects_successfully()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return;
}

var testAsset = _testAssetsManager
.CopyTestAsset("AppsWithFrameworkReferences")
.WithSource();

testAsset.Restore("EntityFrameworkApp");
testAsset.Restore("StopwatchLib");

VerifyProjectsBuild(testAsset);
}

[Fact]
public void It_builds_with_disable_implicit_frameworkRefs()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return;
}

var testAsset = _testAssetsManager
.CopyTestAsset("AppsWithFrameworkReferences")
.WithSource();

testAsset.Restore("EntityFrameworkApp");
testAsset.Restore("StopwatchLib");

VerifyProjectsBuild(testAsset, "/p:DisableImplicitFrameworkReferences=true");
}

void VerifyProjectsBuild(TestAsset testAsset, params string[] buildArgs)
{
VerifyBuild(testAsset, "StopwatchLib", "net45", buildArgs,
"StopwatchLib.dll",
"StopwatchLib.pdb");

VerifyBuild(testAsset, "EntityFrameworkApp", "net451", buildArgs,
"EntityFrameworkApp.exe",
"EntityFrameworkApp.pdb",
"EntityFrameworkApp.runtimeconfig.dev.json",
"EntityFrameworkApp.runtimeconfig.json");

// Try running EntityFrameworkApp.exe
var appProjectDirectory = Path.Combine(testAsset.TestRoot, "EntityFrameworkApp");
var buildCommand = new BuildCommand(Stage0MSBuild, appProjectDirectory);
var outputDirectory = buildCommand.GetOutputDirectory("net451");

Command.Create(Path.Combine(outputDirectory.FullName, "EntityFrameworkApp.exe"), Enumerable.Empty<string>())
.CaptureStdOut()
.Execute()
.Should()
.Pass()
.And
.HaveStdOutContaining("Required Test Provider");
}

private void VerifyBuild(TestAsset testAsset, string project, string targetFramework,
string [] buildArgs,
params string [] expectedFiles)
{
var appProjectDirectory = Path.Combine(testAsset.TestRoot, project);

var buildCommand = new BuildCommand(Stage0MSBuild, appProjectDirectory);
var outputDirectory = buildCommand.GetOutputDirectory(targetFramework);

buildCommand
.Execute(buildArgs)
.Should()
.Pass();

outputDirectory.Should().HaveFiles(expectedFiles);
}

[Fact]
public void The_clean_target_removes_all_files_from_the_output_folder()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return;
}

var testAsset = _testAssetsManager
.CopyTestAsset("AppsWithFrameworkReferences")
.WithSource();

testAsset.Restore("EntityFrameworkApp");
testAsset.Restore("StopwatchLib");

VerifyClean(testAsset, "StopwatchLib", "net45",
"StopwatchLib.dll",
"StopwatchLib.pdb");

VerifyClean(testAsset, "EntityFrameworkApp", "net451",
"EntityFrameworkApp.exe",
"EntityFrameworkApp.pdb",
"EntityFrameworkApp.runtimeconfig.dev.json",
"EntityFrameworkApp.runtimeconfig.json");
}

private void VerifyClean(TestAsset testAsset, string project, string targetFramework,
params string[] expectedFiles)
{
var appProjectDirectory = Path.Combine(testAsset.TestRoot, project);

var buildCommand = new BuildCommand(Stage0MSBuild, appProjectDirectory);
var outputDirectory = buildCommand.GetOutputDirectory(targetFramework);

buildCommand
.Execute()
.Should()
.Pass();

outputDirectory.Should().HaveFiles(expectedFiles);

var cleanCommand = Stage0MSBuild.CreateCommandForTarget("Clean", buildCommand.FullPathProjectFile);

cleanCommand
.Execute()
.Should()
.Pass();

outputDirectory.Should().OnlyHaveFiles(Array.Empty<string>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,7 @@ public void It_publishes_the_project_with_a_refs_folder_and_correct_deps_file()
{
var testAsset = _testAssetsManager
.CopyTestAsset("CompilationContext", "PreserveCompilationContext")
.WithSource()
.WithProjectChanges(project =>
{
// Workaround for https://github.com/dotnet/sdk/issues/367

var ns = project.Root.Name.Namespace;
var propertyGroup = project.Root.Elements(ns + "PropertyGroup").FirstOrDefault();
propertyGroup.Should().NotBeNull();

propertyGroup.Add(new XElement(ns + "DisableImplicitFrameworkReferences", "true"));
});
.WithSource();

testAsset.Restore("TestApp");
testAsset.Restore("TestLibrary");
Expand All @@ -52,7 +42,12 @@ public void It_publishes_the_project_with_a_refs_folder_and_correct_deps_file()
}

publishCommand
.Execute($"/p:TargetFramework={targetFramework}")
.Execute(
$"/p:TargetFramework={targetFramework}",

// Additional properties to workaround https://github.com/Microsoft/msbuild/issues/1345
"/p:DisableImplicitFrameworkReferences=true",
"/p:DisableLockFileFrameworks=true")
.Should()
.Pass();

Expand Down