Skip to content

Commit

Permalink
error when TPV is missing from dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 28, 2020
1 parent 156230a commit 0954642
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 30 deletions.
21 changes: 21 additions & 0 deletions src/NuGet.Core/NuGet.Commands/RestoreCommand/RestoreCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ public async Task<RestoreResult> ExecuteAsync(CancellationToken token)
_success = false;
}

if (!CheckPlatformVersions())
{
// the errors will be added to the assets file
_success = false;
}

// evaluate packages.lock.json file
var packagesLockFilePath = PackagesLockFileUtilities.GetNuGetLockFilePath(_request.Project);
var isLockFileValid = false;
Expand Down Expand Up @@ -404,6 +410,21 @@ public async Task<RestoreResult> ExecuteAsync(CancellationToken token)
}
}

private bool CheckPlatformVersions()
{
IEnumerable<NuGetFramework> badPlatforms = _request.Project.TargetFrameworks.Select(tfm => tfm.FrameworkName).Where(fw => !string.IsNullOrEmpty(fw.Platform) && (fw.PlatformVersion == FrameworkConstants.EmptyVersion));
if (badPlatforms.Any())
{
NuGetFramework fw = badPlatforms.First();
_logger.Log(RestoreLogMessage.CreateError(NuGetLogCode.NU1012, string.Format(CultureInfo.CurrentCulture, Strings.Error_PlatformVersionNotPresent, fw.Framework, fw.Platform)));
return false;
}
else
{
return true;
}
}

private async Task<bool> AreCentralVersionRequirementsSatisfiedAsync()
{
// The dependencies should not have versions explicitelly defined if cpvm is enabled.
Expand Down
9 changes: 9 additions & 0 deletions src/NuGet.Core/NuGet.Commands/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 32 additions & 28 deletions src/NuGet.Core/NuGet.Commands/Strings.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -1111,4 +1111,8 @@ For more information, visit https://docs.nuget.org/docs/reference/command-line-r
<data name="SpecValidation_OriginalTargetFrameworksMustMatchAliases" xml:space="preserve">
<value>The original target frameworks value must match the aliases. Original target frameworks: {0}, aliases: {1}.</value>
</data>
</root>
<data name="Error_PlatformVersionNotPresent" xml:space="preserve">
<value>TargetFramework '{0}' has a platform '{1}', but a platform version could not be inferred. Specify the platform version explicitly.</value>
<comment>{0} should be the TargetFramework. {1} should be the TargetPlatformIdentifier</comment>
</data>
</root>
5 changes: 5 additions & 0 deletions src/NuGet.Core/NuGet.Common/Errors/NuGetLogCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public enum NuGetLogCode
/// </summary>
NU1011 = 1011,

/// <summary>
/// Target Framework has a platform component, but the platform version could not be inferred.
/// </summary>
NU1012 = 1012,

/// <summary>
/// Unable to resolve package, generic message for unknown type constraints.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
NuGet.Common.NuGetLogCode.NU1010 = 1010 -> NuGet.Common.NuGetLogCode
NuGet.Common.NuGetLogCode.NU1011 = 1011 -> NuGet.Common.NuGetLogCode
NuGet.Common.NuGetLogCode.NU1012 = 1012 -> NuGet.Common.NuGetLogCode
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
NuGet.Common.NuGetLogCode.NU1010 = 1010 -> NuGet.Common.NuGetLogCode
NuGet.Common.NuGetLogCode.NU1011 = 1011 -> NuGet.Common.NuGetLogCode

NuGet.Common.NuGetLogCode.NU1012 = 1012 -> NuGet.Common.NuGetLogCode
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
NuGet.Common.NuGetLogCode.NU1010 = 1010 -> NuGet.Common.NuGetLogCode
NuGet.Common.NuGetLogCode.NU1011 = 1011 -> NuGet.Common.NuGetLogCode

NuGet.Common.NuGetLogCode.NU1012 = 1012 -> NuGet.Common.NuGetLogCode
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using FluentAssertions;
using Newtonsoft.Json.Linq;
using NuGet.Commands.Test;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Frameworks;
using NuGet.LibraryModel;
Expand Down Expand Up @@ -2989,6 +2990,71 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
}
}

[Fact]
public async Task RestoreCommand_WhenPlatformVersionIsEmpty_ThrowsError()
{
using (var pathContext = new SimpleTestPathContext())
using (var context = new SourceCacheContext())
{
var configJson = JObject.Parse(@"
{
""frameworks"": {
""net5.0-windows"": {
""dependencies"": {
""A"": {
""version"" : ""1.0.0"",
}
}
}
}
}");

// Arrange
var packageA = new SimpleTestPackageContext("a", "1.0.0");
packageA.Files.Clear();
packageA.AddFile("lib/net5.0-windows/a.dll");

await SimpleTestPackageUtility.CreateFolderFeedV3Async(
pathContext.PackageSource,
PackageSaveMode.Defaultv3,
packageA);

var sources = new List<PackageSource>
{
new PackageSource(pathContext.PackageSource)
};
var logger = new TestLogger();

var projectDirectory = Path.Combine(pathContext.SolutionRoot, "TestProject");
var cachingSourceProvider = new CachingSourceProvider(new PackageSourceProvider(NullSettings.Instance));

var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", Path.Combine(projectDirectory, "project.csproj")).WithTestRestoreMetadata();
var dgSpec = new DependencyGraphSpec();
dgSpec.AddProject(spec);
dgSpec.AddRestore(spec.Name);

var request = new TestRestoreRequest(spec, sources, pathContext.UserPackagesFolder, logger)
{
ProjectStyle = ProjectStyle.PackageReference,
DependencyGraphSpec = dgSpec,
AllowNoOp = true,
};
var command = new RestoreCommand(request);

// Act
var result = await command.ExecuteAsync();
await result.CommitAsync(logger, CancellationToken.None);

// Assert
Assert.False(result.Success);
Assert.Equal(1, logger.ErrorMessages.Count);
logger.ErrorMessages.TryDequeue(out var errorMessage);
Assert.True(errorMessage.Contains("platform version could not be inferred"));
var messagesForNU1012 = result.LockFile.LogMessages.Where(m => m.Code == NuGetLogCode.NU1012);
Assert.Equal(1, messagesForNU1012.Count());
}
}

private static byte[] GetTestUtilityResource(string name)
{
return ResourceTestUtility.GetResourceBytes(
Expand Down

0 comments on commit 0954642

Please sign in to comment.