Skip to content

Commit

Permalink
Issue a warning on restore about obsolete DotNetCliToolReference
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate McMaster committed Mar 22, 2018
1 parent 4d1111e commit cabf60c
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
***********************************************************************************************
Microsoft.NET.ObsoleteReferences.targets
WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
created a backup copy. Incorrect changes to this file will make it
impossible to load or build your projects from the command-line or the IDE.
Copyright (c) .NET Foundation. All rights reserved.
***********************************************************************************************
-->
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
</PropertyGroup>

<Target Name="_WarnAboutObsoleteDotNetCliToolReferences" BeforeTargets="CollectPackageReferences">
<ItemGroup>
<_ReferenceToObsoleteDotNetCliTool Include="@(DotNetCliToolReference)" />
<DotNetCliToolReference Remove="@(BundledDotNetCliToolReference)" />
<_ReferenceToObsoleteDotNetCliTool Remove="@(DotNetCliToolReference)" />
</ItemGroup>

<Warning
Code="DOTNET1018"
Text="Using DotNetCliToolReference to reference '%(_ReferenceToObsoleteDotNetCliTool.Identity)' is obsolete and can be removed from this project. This tool is bundled by default in the .NET Core SDK."
Condition=" '%(_ReferenceToObsoleteDotNetCliTool.Identity)' != '' " />
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and
created a backup copy. Incorrect changes to this file will make it
impossible to load or build your projects from the command-line or the IDE.
Copyright (c) .NET Foundation. All rights reserved.
Copyright (c) .NET Foundation. All rights reserved.
***********************************************************************************************
-->
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
Expand All @@ -20,15 +20,15 @@ Copyright (c) .NET Foundation. All rights reserved.
<GenerateResourceMSBuildArchitecture Condition=" '$(GenerateResourceMSBuildArchitecture)' == '' ">CurrentArchitecture</GenerateResourceMSBuildArchitecture>
<GenerateResourceMSBuildRuntime Condition=" '$(GenerateResourceMSBuildRuntime)' == '' ">CurrentRuntime</GenerateResourceMSBuildRuntime>
</PropertyGroup>

<Import Project="Microsoft.NET.Sdk.Common.targets" />

<ImportGroup>
<Import Project="$(MSBuildThisFileDirectory)Microsoft.PackageDependencyResolution.targets" Condition="Exists('$(MSBuildThisFileDirectory)Microsoft.PackageDependencyResolution.targets')" />
</ImportGroup>

<Import Project="Microsoft.NET.Sdk.DefaultItems.targets" />

<UsingTask TaskName="GenerateDepsFile" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
<UsingTask TaskName="GenerateRuntimeConfigurationFiles" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
<UsingTask TaskName="GetAssemblyVersion" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
Expand Down Expand Up @@ -121,7 +121,7 @@ Copyright (c) .NET Foundation. All rights reserved.
Inputs="$(ProjectAssetsFile)"
Outputs="$(ProjectDepsFilePath)">

<!--
<!--
Explicitly not passing any ExcludeFromPublishPackageReferences information during 'Build', since these dependencies
should be included during 'Build'. They are only excluded on 'Publish'.
-->
Expand Down Expand Up @@ -177,9 +177,9 @@ Copyright (c) .NET Foundation. All rights reserved.
HostConfigurationOptions="@(RuntimeHostConfigurationOption)"
AdditionalProbingPaths="@(AdditionalProbingPath)"
IsSelfContained="$(SelfContained)">

</GenerateRuntimeConfigurationFiles>

<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
during incremental builds when the task is skipped -->
Expand Down Expand Up @@ -236,19 +236,19 @@ Copyright (c) .NET Foundation. All rights reserved.
<RuntimeHostConfigurationOption Include="System.GC.Concurrent"
Condition="'$(ConcurrentGarbageCollection)' != ''"
Value="$(ConcurrentGarbageCollection)" />

<RuntimeHostConfigurationOption Include="System.GC.Server"
Condition="'$(ServerGarbageCollection)' != ''"
Value="$(ServerGarbageCollection)" />

<RuntimeHostConfigurationOption Include="System.GC.RetainVM"
Condition="'$(RetainVMGarbageCollection)' != ''"
Value="$(RetainVMGarbageCollection)" />

<RuntimeHostConfigurationOption Include="System.Threading.ThreadPool.MinThreads"
Condition="'$(ThreadPoolMinThreads)' != ''"
Value="$(ThreadPoolMinThreads)" />

<RuntimeHostConfigurationOption Include="System.Threading.ThreadPool.MaxThreads"
Condition="'$(ThreadPoolMaxThreads)' != ''"
Value="$(ThreadPoolMaxThreads)" />
Expand Down Expand Up @@ -303,15 +303,15 @@ Copyright (c) .NET Foundation. All rights reserved.
<NETSdkError Condition="'@(NativeRestoredAppHostNETCore->Count())' &gt; 1"
ResourceName="MultipleFilesResolved"
FormatArguments="$(_DotNetAppHostExecutableName)" />

<EmbedAppNameInHost AppHostSourcePath="@(NativeRestoredAppHostNETCore)"
AppHostDestinationDirectoryPath="$(AppHostDestinationDirectoryPath)"
AppBinaryName="$(AssemblyName)$(TargetExt)"
Condition="'@(NativeRestoredAppHostNETCore)' != '' ">

<Output TaskParameter="ModifiedAppHostPath" ItemName="NativeAppHostNETCore" />
</EmbedAppNameInHost>

<ItemGroup Condition="'@(NativeAppHostNETCore)' == '' ">
<NativeAppHostNETCore Include="@(NativeCopyLocalItems)"
Condition="'%(NativeCopyLocalItems.FileName)%(NativeCopyLocalItems.Extension)' == '$(_DotNetHostExecutableName)'" />
Expand All @@ -320,7 +320,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<NETSdkError Condition="'@(NativeAppHostNETCore->Count())' &gt; 1"
ResourceName="MultipleFilesResolved"
FormatArguments="@(NativeAppHostNETCore)" />

<ItemGroup Condition="'@(NativeAppHostNETCore)' != '' ">
<NativeNETCoreCopyLocalItems Include="@(NativeAppHostNETCore)">
<!-- Rename the host executable to the app's name -->
Expand Down Expand Up @@ -355,7 +355,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<_FrameworkReferenceAssemblies Include="@(ReferencePath)"
Condition="%(ReferencePath.FrameworkFile) == 'true' or
%(ReferencePath.ResolvedFrom) == 'ImplicitlyExpandDesignTimeFacades'" />

<!--
"ReferenceOnly" assemblies are assemblies that are only used at compile-time, and they can't be resolved
by the normal compile-assembly resolvers at runtime:
Expand All @@ -366,7 +366,7 @@ Copyright (c) .NET Foundation. All rights reserved.
-->
<_ReferenceOnlyAssemblies Include="@(ReferencePath)"
Exclude="@(_FrameworkReferenceAssemblies)"
Condition="%(ReferencePath.CopyLocal) != 'true' and
Condition="%(ReferencePath.CopyLocal) != 'true' and
%(ReferencePath.NuGetSourceType) == ''" />

<_ReferenceAssemblies Include="@(_FrameworkReferenceAssemblies)" />
Expand Down Expand Up @@ -394,7 +394,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<RunArguments Condition="'$(RunArguments)' == ''">$(StartArguments)</RunArguments>
</PropertyGroup>
</When>

<When Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and '$(_IsExecutable)' == 'true'">
<PropertyGroup Condition="'$(SelfContained)' != 'true'">
<!-- TODO: https://github.com/dotnet/sdk/issues/20 Need to get the DotNetHost path from MSBuild -->
Expand All @@ -410,7 +410,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<RunArguments Condition="'$(RunArguments)' == ''">$(StartArguments)</RunArguments>
</PropertyGroup>
</When>

<When Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework' and '$(_IsExecutable)' == 'true'">
<PropertyGroup>
<RunCommand Condition="'$(RunCommand)' == ''">$(TargetPath)</RunCommand>
Expand Down Expand Up @@ -511,31 +511,31 @@ Copyright (c) .NET Foundation. All rights reserved.
============================================================
-->
<Target Name="_DefaultMicrosoftNETPlatformLibrary">

<PropertyGroup Condition="'$(MicrosoftNETPlatformLibrary)' == ''">
<MicrosoftNETPlatformLibrary Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">Microsoft.NETCore.App</MicrosoftNETPlatformLibrary>
</PropertyGroup>

</Target>

<!--
============================================================
GetAllRuntimeIdentifiers
============================================================
-->
<Target Name="GetAllRuntimeIdentifiers"
<Target Name="GetAllRuntimeIdentifiers"
Returns="$(RuntimeIdentifiers);$(RuntimeIdentifier)" />

<!--
============================================================
InjectTargetPathMetadata
Update TargetPathWithTargetPlatformMoniker with target framework
identifier and version metadata. This is so that the
ImplicitlyExpandNETStandardFacades target can determine if a
referenced project needs the .NET Standard facades even if
the project hasn't been compiled to disk yet.
See https://github.com/dotnet/sdk/issues/1403 for more context
============================================================
-->
Expand All @@ -546,7 +546,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<TargetFrameworkVersion>$(_TargetFrameworkVersionWithoutV)</TargetFrameworkVersion>
</TargetPathWithTargetPlatformMoniker>
</ItemDefinitionGroup>

<!--
============================================================
Project Capabilities
Expand All @@ -572,6 +572,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<Import Project="$(MSBuildThisFileDirectory)Microsoft.NET.GenerateAssemblyInfo.targets" />
<Import Project="$(MSBuildThisFileDirectory)Microsoft.NET.ComposeStore.targets" />
<Import Project="$(MSBuildThisFileDirectory)Microsoft.NET.CrossGen.targets" />
<Import Project="$(MSBuildThisFileDirectory)Microsoft.NET.ObsoleteReferences.targets" />
<Import Project="$(MSBuildThisFileDirectory)Microsoft.NET.Publish.targets" />
<Import Project="$(MSBuildThisFileDirectory)Microsoft.NET.PackTool.targets" />
<Import Project="$(MSBuildThisFileDirectory)Microsoft.NET.PreserveCompilationContext.targets" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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.IO;
using System.Linq;
using System.Xml.Linq;
using FluentAssertions;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.ProjectConstruction;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.NET.Restore.Tests
{
public class GivenThatWeWantToIgnoreObsoleteDotNetCliToolPackages : SdkTest
{
public GivenThatWeWantToIgnoreObsoleteDotNetCliToolPackages(ITestOutputHelper log) : base(log)
{
}

[Fact]
public void It_issues_warning_and_skips_restore_for_obsolete_DotNetCliToolReference()
{
const string obsoletePackageId = "Banana.CommandLineTool";

TestProject toolProject = new TestProject()
{
Name = "ObsoleteCliToolRefRestoreProject",
IsSdkProject = true,
TargetFrameworks = "netstandard2.0",
};

toolProject.DotNetCliToolReferences.Add(new TestPackageReference(obsoletePackageId, "99.99.99", null));

var toolProjectInstance = _testAssetsManager.CreateTestProject(toolProject, identifier: toolProject.Name)
.WithProjectChanges(project =>
{
var ns = project.Root.Name.Namespace;

var itemGroup = new XElement(ns + "ItemGroup");
project.Root.Add(itemGroup);

itemGroup.Add(new XElement(ns + "BundledDotNetCliToolReference",
new XAttribute("Include", obsoletePackageId)));
});

NuGetConfigWriter.Write(toolProjectInstance.TestRoot, NuGetConfigWriter.DotnetCoreMyGetFeed);

toolProjectInstance.Restore(Log, toolProject.Name, "/v:n");

var restoreCommand = toolProjectInstance.GetRestoreCommand(Log, toolProject.Name);
restoreCommand.Execute("/v:n").Should()
.Pass()
.And
.HaveStdOutContaining($"warning DOTNET1018: Using DotNetCliToolReference to reference '{obsoletePackageId}' is obsolete and can be removed from this project. This tool is bundled by default in the .NET Core SDK.");

string toolAssetsFilePath = Path.Combine(TestContext.Current.NuGetCachePath, ".tools", toolProject.Name.ToLowerInvariant(), "99.99.99", toolProject.TargetFrameworks, "project.assets.json");
Assert.False(File.Exists(toolAssetsFilePath), "Tool assets path should not have been generated");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class TestProject
public string RuntimeFrameworkVersion { get; set; }

public string RuntimeIdentifier { get; set; }

// TargetFrameworkVersion applies to non-SDK projects
public string TargetFrameworkVersion { get; set; }

Expand All @@ -33,6 +33,8 @@ public class TestProject

public List<TestPackageReference> PackageReferences { get; } = new List<TestPackageReference>();

public List<TestPackageReference> DotNetCliToolReferences { get; } = new List<TestPackageReference>();

public Dictionary<string, string> SourceFiles { get; } = new Dictionary<string, string>();

public Dictionary<string, string> EmbeddedResources { get; } = new Dictionary<string, string>();
Expand Down Expand Up @@ -129,11 +131,19 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder)
packageReferenceItemGroup = new XElement(ns + "ItemGroup");
projectXml.Root.Add(packageReferenceItemGroup);
}

foreach (TestPackageReference packageReference in PackageReferences)
{
packageReferenceItemGroup.Add(new XElement(ns + "PackageReference",
new XAttribute("Include", $"{packageReference.ID}"),
new XAttribute("Version", $"{packageReference.Version}")));
packageReferenceItemGroup.Add(new XElement(ns + "PackageReference",
new XAttribute("Include", $"{packageReference.ID}"),
new XAttribute("Version", $"{packageReference.Version}")));
}

foreach (TestPackageReference dotnetCliToolReference in DotNetCliToolReferences)
{
packageReferenceItemGroup.Add(new XElement(ns + "DotNetCliToolReference",
new XAttribute("Include", $"{dotnetCliToolReference.ID}"),
new XAttribute("Version", $"{dotnetCliToolReference.Version}")));
}

var targetFrameworks = IsSdkProject ? TargetFrameworks.Split(';') : new[] { "net" };
Expand Down Expand Up @@ -161,7 +171,7 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder)

// Update SDK reference to the version under test
targetTestAsset.SetSdkVersion(projectXml);

}
else
{
Expand Down

0 comments on commit cabf60c

Please sign in to comment.