From 0912cc96c0120da12e1975d2a96cbc8336e32dbf Mon Sep 17 00:00:00 2001 From: Ankit Mishra Date: Mon, 21 May 2018 14:39:49 -0700 Subject: Improving error message for RIDS in lock file --- .../ResolveNuGetPackageAssets.cs | 25 ++++++++++++++++++---- .../Strings.Designer.cs | 20 ++++++++++++++++- src/Microsoft.NuGet.Build.Tasks/Strings.resx | 8 +++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs b/src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs index 7de0f02..aa56fe5 100644 --- a/src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs +++ b/src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs @@ -36,6 +36,8 @@ namespace Microsoft.NuGet.Build.Tasks internal const string NuGetAssetTypeRuntime = "runtime"; internal const string NuGetAssetTypeResource = "resource"; + internal const string RuntimeIdentifiersProperty = "RuntimeIdentifiers"; + private readonly List _analyzers = new List(); private readonly List _copyLocalItems = new List(); private readonly List _references = new List(); @@ -715,7 +717,18 @@ namespace Microsoft.NuGet.Build.Tasks private void GiveErrorForMissingRuntimeIdentifier() { - string runtimePiece = '"' + RuntimeIdentifier + "\": { }"; + var runtimePiece = RuntimeIdentifier; + var runtimesSection = $"<{RuntimeIdentifiersProperty}>{RuntimeIdentifier}"; + var missingRuntimeInRuntimesErrorString = nameof(Strings.MissingSpecificRuntimeIdentifier); + var missingRuntimesErrorString = nameof(Strings.MissingRuntimeIdentifiers); + + if (IsLockFileProjectJsonBased(ProjectLockFile)) + { + runtimePiece = '"' + RuntimeIdentifier + "\": { }"; + runtimesSection = "\"runtimes\": { " + runtimePiece + " }"; + missingRuntimeInRuntimesErrorString = nameof(Strings.MissingRuntimeInRuntimesSection); + missingRuntimesErrorString = nameof(Strings.MissingRuntimesSection); + } bool hasRuntimesSection; try @@ -734,12 +747,11 @@ namespace Microsoft.NuGet.Build.Tasks if (hasRuntimesSection) { - ThrowExceptionIfNotAllowingFallback(nameof(Strings.MissingRuntimeInRuntimesSection), RuntimeIdentifier, runtimePiece); + ThrowExceptionIfNotAllowingFallback(missingRuntimeInRuntimesErrorString, RuntimeIdentifier, runtimePiece); } else { - var runtimesSection = "\"runtimes\": { " + runtimePiece + " }"; - ThrowExceptionIfNotAllowingFallback(nameof(Strings.MissingRuntimesSection), runtimesSection); + ThrowExceptionIfNotAllowingFallback(missingRuntimesErrorString, runtimesSection); } } @@ -1027,5 +1039,10 @@ namespace Microsoft.NuGet.Build.Tasks return String.Empty; } } + + private static bool IsLockFileProjectJsonBased(string lockFilePath) + { + return lockFilePath.EndsWith("lock.json", StringComparison.OrdinalIgnoreCase); + } } } diff --git a/src/Microsoft.NuGet.Build.Tasks/Strings.Designer.cs b/src/Microsoft.NuGet.Build.Tasks/Strings.Designer.cs index 9b3de63..429667d 100644 --- a/src/Microsoft.NuGet.Build.Tasks/Strings.Designer.cs +++ b/src/Microsoft.NuGet.Build.Tasks/Strings.Designer.cs @@ -19,7 +19,7 @@ namespace Microsoft.NuGet.Build.Tasks { // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class Strings { @@ -114,6 +114,15 @@ namespace Microsoft.NuGet.Build.Tasks { } } + /// + /// Looks up a localized string similar to Your project csproj file doesn't have the "RuntimeIdentifiers" property. You should add '{0}' to your project csproj file and then re-run NuGet restore.. + /// + internal static string MissingRuntimeIdentifiers { + get { + return ResourceManager.GetString("MissingRuntimeIdentifiers", resourceCulture); + } + } + /// /// Looks up a localized string similar to Your project.json doesn't list '{0}' as a targeted runtime. You should add '{1}' inside your "runtimes" section in your project.json, and then re-run NuGet restore.. /// @@ -132,6 +141,15 @@ namespace Microsoft.NuGet.Build.Tasks { } } + /// + /// Looks up a localized string similar to Your project csproj file doesn't list '{0}' as a targeted runtime. You should add '{1}' to the "RuntimeIdentifiers" property in your project csproj file and then re-run NuGet restore.. + /// + internal static string MissingSpecificRuntimeIdentifier { + get { + return ResourceManager.GetString("MissingSpecificRuntimeIdentifier", resourceCulture); + } + } + /// /// Looks up a localized string similar to No targets could be found in the lock file. Make sure you have a supports or runtimes section i your project.json file.. /// diff --git a/src/Microsoft.NuGet.Build.Tasks/Strings.resx b/src/Microsoft.NuGet.Build.Tasks/Strings.resx index 16d5d64..4490c3b 100644 --- a/src/Microsoft.NuGet.Build.Tasks/Strings.resx +++ b/src/Microsoft.NuGet.Build.Tasks/Strings.resx @@ -135,12 +135,20 @@ The project.json is referencing the project '{0}', but an output path was not specified on an item in the {1} property. + + Your project csproj file doesn't have the "RuntimeIdentifiers" property. You should add '{0}' to your project csproj file and then re-run NuGet restore. + 0 - RID + Your project.json doesn't list '{0}' as a targeted runtime. You should add '{1}' inside your "runtimes" section in your project.json, and then re-run NuGet restore. Your project.json doesn't have a runtimes section. You should add '{0}' to your project.json and then re-run NuGet restore. + + Your project csproj file doesn't list '{0}' as a targeted runtime. You should add '{1}' to the "RuntimeIdentifiers" property in your project csproj file and then re-run NuGet restore. + 0/1 - RID + No targets could be found in the lock file. Make sure you have a supports or runtimes section i your project.json file. -- cgit v1.2.3 From 67d7e5768d6ce34f26530d04d1ba40e5f2dab22f Mon Sep 17 00:00:00 2001 From: Ankit Mishra Date: Tue, 22 May 2018 10:16:09 -0700 Subject: Improving other messages that referenced project.json --- .../ReferenceResolutionTests.cs | 6 +-- .../ResolveNuGetPackageAssets.cs | 30 ++++++++++--- .../Strings.Designer.cs | 49 +++++++++++++--------- src/Microsoft.NuGet.Build.Tasks/Strings.resx | 47 ++++++++++++++------- 4 files changed, 88 insertions(+), 44 deletions(-) diff --git a/src/Microsoft.NuGet.Build.Tasks.Tests/ReferenceResolutionTests.cs b/src/Microsoft.NuGet.Build.Tasks.Tests/ReferenceResolutionTests.cs index 3b690ab..2a8e2d2 100644 --- a/src/Microsoft.NuGet.Build.Tasks.Tests/ReferenceResolutionTests.cs +++ b/src/Microsoft.NuGet.Build.Tasks.Tests/ReferenceResolutionTests.cs @@ -88,7 +88,7 @@ namespace Microsoft.NuGet.Build.Tasks.Tests runtimeIdentifier: "missing-runtime-identifier", allowFallbackOnTargetSelection: false)); - Assert.Equal(nameof(Strings.MissingRuntimeInRuntimesSection), exception.ResourceName); + Assert.Equal(nameof(Strings.MissingRuntimeInProjectJson), exception.ResourceName); Assert.Equal(new[] { "missing-runtime-identifier", "\"missing-runtime-identifier\": { }" }, exception.MessageArgs); } @@ -103,7 +103,7 @@ namespace Microsoft.NuGet.Build.Tasks.Tests allowFallbackOnTargetSelection: false, projectJsonFileContents: "{ }")); - Assert.Equal(nameof(Strings.MissingRuntimesSection), exception.ResourceName); + Assert.Equal(nameof(Strings.MissingRuntimesSectionInProjectJson), exception.ResourceName); Assert.Equal(new[] { "\"runtimes\": { \"missing-runtime-identifier\": { } }" }, exception.MessageArgs); } @@ -117,7 +117,7 @@ namespace Microsoft.NuGet.Build.Tasks.Tests runtimeIdentifier: "missing-runtime-identifier", allowFallbackOnTargetSelection: false)); - Assert.Equal(nameof(Strings.MissingFramework), exception.ResourceName); + Assert.Equal(nameof(Strings.MissingFrameworkInProjectJson), exception.ResourceName); Assert.Equal(new[] { "Missing,Version=1.0" }, exception.MessageArgs); } diff --git a/src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs b/src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs index aa56fe5..ac81c61 100644 --- a/src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs +++ b/src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs @@ -690,7 +690,7 @@ namespace Microsoft.NuGet.Build.Tasks } else { - ThrowExceptionIfNotAllowingFallback(nameof(Strings.MissingFramework), TargetMonikers.First().ItemSpec); + GiveErrorForMissingFramework(); } // If we're still here, that means we're allowing fallback, so let's try @@ -709,25 +709,34 @@ namespace Microsoft.NuGet.Build.Tasks var firstTarget = (JObject)enumerableTargets.FirstOrDefault().Value; if (firstTarget == null) { - throw new ExceptionFromResource(nameof(Strings.NoTargetsInLockFile)); + GiveErrorForNoTargets(); } return firstTarget; } + private void GiveErrorForNoTargets() + { + var noTargetsInLockFileErrorString = IsLockFileProjectJsonBased(ProjectLockFile) ? + nameof(Strings.NoTargetsInLockFileForProjectJson) : + nameof(Strings.NoTargetsInLockFileForCsproj); + + throw new ExceptionFromResource(noTargetsInLockFileErrorString); + } + private void GiveErrorForMissingRuntimeIdentifier() { var runtimePiece = RuntimeIdentifier; var runtimesSection = $"<{RuntimeIdentifiersProperty}>{RuntimeIdentifier}"; - var missingRuntimeInRuntimesErrorString = nameof(Strings.MissingSpecificRuntimeIdentifier); - var missingRuntimesErrorString = nameof(Strings.MissingRuntimeIdentifiers); + var missingRuntimeInRuntimesErrorString = nameof(Strings.MissingRuntimeIdentifierInCsproj); + var missingRuntimesErrorString = nameof(Strings.MissingRuntimeIdentifiersInCsproj); if (IsLockFileProjectJsonBased(ProjectLockFile)) { runtimePiece = '"' + RuntimeIdentifier + "\": { }"; runtimesSection = "\"runtimes\": { " + runtimePiece + " }"; - missingRuntimeInRuntimesErrorString = nameof(Strings.MissingRuntimeInRuntimesSection); - missingRuntimesErrorString = nameof(Strings.MissingRuntimesSection); + missingRuntimeInRuntimesErrorString = nameof(Strings.MissingRuntimeInProjectJson); + missingRuntimesErrorString = nameof(Strings.MissingRuntimesSectionInProjectJson); } bool hasRuntimesSection; @@ -755,6 +764,15 @@ namespace Microsoft.NuGet.Build.Tasks } } + private void GiveErrorForMissingFramework() + { + var missingFrameworkErrorString = IsLockFileProjectJsonBased(ProjectLockFile) ? + nameof(Strings.MissingFrameworkInProjectJson) : + nameof(Strings.MissingFrameworkInCsproj); + + ThrowExceptionIfNotAllowingFallback(missingFrameworkErrorString, TargetMonikers.First().ItemSpec); + } + private void ThrowExceptionIfNotAllowingFallback(string resourceName, params string[] messageArgs) { if (!AllowFallbackOnTargetSelection) diff --git a/src/Microsoft.NuGet.Build.Tasks/Strings.Designer.cs b/src/Microsoft.NuGet.Build.Tasks/Strings.Designer.cs index 429667d..6aebdb8 100644 --- a/src/Microsoft.NuGet.Build.Tasks/Strings.Designer.cs +++ b/src/Microsoft.NuGet.Build.Tasks/Strings.Designer.cs @@ -79,16 +79,25 @@ namespace Microsoft.NuGet.Build.Tasks { } /// - /// Looks up a localized string similar to Your project is not referencing the "{0}" framework. Add a reference to "{0}" in the "frameworks" section of your project.json, and then re-run NuGet restore.. + /// Looks up a localized string similar to Your project does not reference "{0}" framework. Add a reference to "{0}" in the "TargetFrameworks" property of your project csproj file and then re-run NuGet restore.. /// - internal static string MissingFramework { + internal static string MissingFrameworkInCsproj { get { - return ResourceManager.GetString("MissingFramework", resourceCulture); + return ResourceManager.GetString("MissingFrameworkInCsproj", resourceCulture); } } /// - /// Looks up a localized string similar to Your project is consuming assets from the project '{0}' but no MSBuild project is found in the project.lock.json. Check the project references in your project file, and re-run NuGet restore.. + /// Looks up a localized string similar to Your project does not reference "{0}" framework. Add a reference to "{0}" in the "frameworks" section of your project.json and then re-run NuGet restore.. + /// + internal static string MissingFrameworkInProjectJson { + get { + return ResourceManager.GetString("MissingFrameworkInProjectJson", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Your project is consuming assets from the project '{0}' but no MSBuild project is found in the project lock file. Check the project references in your project csproj file and re-run NuGet restore.. /// internal static string MissingMSBuildPathInProjectPackage { get { @@ -106,56 +115,56 @@ namespace Microsoft.NuGet.Build.Tasks { } /// - /// Looks up a localized string similar to The project.json is referencing the project '{0}', but an output path was not specified on an item in the {1} property.. + /// Looks up a localized string similar to Your project csproj file doesn't list '{0}' as a targeted runtime. You should add '{1}' to the "RuntimeIdentifiers" property in your project csproj file and then re-run NuGet restore.. /// - internal static string MissingProjectReference { + internal static string MissingRuntimeIdentifierInCsproj { get { - return ResourceManager.GetString("MissingProjectReference", resourceCulture); + return ResourceManager.GetString("MissingRuntimeIdentifierInCsproj", resourceCulture); } } /// /// Looks up a localized string similar to Your project csproj file doesn't have the "RuntimeIdentifiers" property. You should add '{0}' to your project csproj file and then re-run NuGet restore.. /// - internal static string MissingRuntimeIdentifiers { + internal static string MissingRuntimeIdentifiersInCsproj { get { - return ResourceManager.GetString("MissingRuntimeIdentifiers", resourceCulture); + return ResourceManager.GetString("MissingRuntimeIdentifiersInCsproj", resourceCulture); } } /// - /// Looks up a localized string similar to Your project.json doesn't list '{0}' as a targeted runtime. You should add '{1}' inside your "runtimes" section in your project.json, and then re-run NuGet restore.. + /// Looks up a localized string similar to Your project.json doesn't list '{0}' as a targeted runtime. You should add '{1}' inside your "runtimes" section in your project.json and then re-run NuGet restore.. /// - internal static string MissingRuntimeInRuntimesSection { + internal static string MissingRuntimeInProjectJson { get { - return ResourceManager.GetString("MissingRuntimeInRuntimesSection", resourceCulture); + return ResourceManager.GetString("MissingRuntimeInProjectJson", resourceCulture); } } /// /// Looks up a localized string similar to Your project.json doesn't have a runtimes section. You should add '{0}' to your project.json and then re-run NuGet restore.. /// - internal static string MissingRuntimesSection { + internal static string MissingRuntimesSectionInProjectJson { get { - return ResourceManager.GetString("MissingRuntimesSection", resourceCulture); + return ResourceManager.GetString("MissingRuntimesSectionInProjectJson", resourceCulture); } } /// - /// Looks up a localized string similar to Your project csproj file doesn't list '{0}' as a targeted runtime. You should add '{1}' to the "RuntimeIdentifiers" property in your project csproj file and then re-run NuGet restore.. + /// Looks up a localized string similar to No targets could be found in the lock file. Make sure you have "RuntimeIdentifiers" property in your project csproj file.. /// - internal static string MissingSpecificRuntimeIdentifier { + internal static string NoTargetsInLockFileForCsproj { get { - return ResourceManager.GetString("MissingSpecificRuntimeIdentifier", resourceCulture); + return ResourceManager.GetString("NoTargetsInLockFileForCsproj", resourceCulture); } } /// - /// Looks up a localized string similar to No targets could be found in the lock file. Make sure you have a supports or runtimes section i your project.json file.. + /// Looks up a localized string similar to No targets could be found in the lock file. Make sure you have a supports or runtimes section in your project.json file.. /// - internal static string NoTargetsInLockFile { + internal static string NoTargetsInLockFileForProjectJson { get { - return ResourceManager.GetString("NoTargetsInLockFile", resourceCulture); + return ResourceManager.GetString("NoTargetsInLockFileForProjectJson", resourceCulture); } } diff --git a/src/Microsoft.NuGet.Build.Tasks/Strings.resx b/src/Microsoft.NuGet.Build.Tasks/Strings.resx index 4490c3b..4e0906e 100644 --- a/src/Microsoft.NuGet.Build.Tasks/Strings.resx +++ b/src/Microsoft.NuGet.Build.Tasks/Strings.resx @@ -119,46 +119,63 @@ The preprocessor token '{0}' has been given more than one value. Choosing '{1}' as the value. + 0 token +1 - chosen value Lock file {0} couldn't be found. Run a NuGet package restore to generate this file. + 0 - lock file - - Your project is not referencing the "{0}" framework. Add a reference to "{0}" in the "frameworks" section of your project.json, and then re-run NuGet restore. + + Your project does not reference "{0}" framework. Add a reference to "{0}" in the "TargetFrameworks" property of your project csproj file and then re-run NuGet restore. + 0 - target framework + + + Your project does not reference "{0}" framework. Add a reference to "{0}" in the "frameworks" section of your project.json and then re-run NuGet restore. + 0 - target framework - Your project is consuming assets from the project '{0}' but no MSBuild project is found in the project.lock.json. Check the project references in your project file, and re-run NuGet restore. + Your project is consuming assets from the project '{0}' but no MSBuild project is found in the project lock file. Check the project references in your project csproj file and re-run NuGet restore. + 0 - project name The package '{0}' could not be found in the libraries section of the lock file. This may indicate your lock file is corrupted. + 0 package id - - The project.json is referencing the project '{0}', but an output path was not specified on an item in the {1} property. + + Your project csproj file doesn't list '{0}' as a targeted runtime. You should add '{1}' to the "RuntimeIdentifiers" property in your project csproj file and then re-run NuGet restore. + 0 &1 - Runtime Identifier - + Your project csproj file doesn't have the "RuntimeIdentifiers" property. You should add '{0}' to your project csproj file and then re-run NuGet restore. - 0 - RID + 0 - Runtime Identifier - - Your project.json doesn't list '{0}' as a targeted runtime. You should add '{1}' inside your "runtimes" section in your project.json, and then re-run NuGet restore. + + Your project.json doesn't list '{0}' as a targeted runtime. You should add '{1}' inside your "runtimes" section in your project.json and then re-run NuGet restore. + 0 - Runtime Identifier - + Your project.json doesn't have a runtimes section. You should add '{0}' to your project.json and then re-run NuGet restore. + 0 - Runtime Identifier - - Your project csproj file doesn't list '{0}' as a targeted runtime. You should add '{1}' to the "RuntimeIdentifiers" property in your project csproj file and then re-run NuGet restore. - 0/1 - RID + + No targets could be found in the lock file. Make sure you have "RuntimeIdentifiers" property in your project csproj file. - - No targets could be found in the lock file. Make sure you have a supports or runtimes section i your project.json file. + + No targets could be found in the lock file. Make sure you have a supports or runtimes section in your project.json file. The package {0} with version {1} could not be found in {2}. Run a NuGet package restore to download the package. + 0 - package id +1 - package version +2 - package folder path The {0} property must be set in order to consume preprocessed content. + 0 - directory path The token '{0}' is unrecognized. + 0 - token \ No newline at end of file -- cgit v1.2.3 From 5be6895a10d0a7e2193757dd3ec7426567f31274 Mon Sep 17 00:00:00 2001 From: Ankit Mishra Date: Tue, 22 May 2018 14:08:20 -0700 Subject: Adding tests --- .../Helpers/TempRoot.cs | 4 +- .../Microsoft.NuGet.Build.Tasks.Tests.csproj | 20 +- .../NuGetTestHelpers.cs | 5 +- .../ProjectReferences/Resources.Designer.cs | 2 +- .../ReferenceResolutionTests.cs | 211 ++++++++++++++++++++- src/Microsoft.NuGet.Build.Tasks.Tests/app.config | 4 +- src/Microsoft.NuGet.Build.Tasks.Tests/project.json | 8 - .../ResolveNuGetPackageAssets.cs | 24 ++- .../Strings.Designer.cs | 4 +- src/Microsoft.NuGet.Build.Tasks/Strings.resx | 2 +- 10 files changed, 242 insertions(+), 42 deletions(-) delete mode 100644 src/Microsoft.NuGet.Build.Tasks.Tests/project.json diff --git a/src/Microsoft.NuGet.Build.Tasks.Tests/Helpers/TempRoot.cs b/src/Microsoft.NuGet.Build.Tasks.Tests/Helpers/TempRoot.cs index 7cda580..004171b 100644 --- a/src/Microsoft.NuGet.Build.Tasks.Tests/Helpers/TempRoot.cs +++ b/src/Microsoft.NuGet.Build.Tasks.Tests/Helpers/TempRoot.cs @@ -54,9 +54,9 @@ namespace Microsoft.NuGet.Build.Tasks.Tests.Helpers return dir; } - public TempFile CreateFile(TempRoot root, string prefix = null, string extension = null, string directory = null, [CallerFilePath]string callerSourcePath = null, [CallerLineNumber]int callerLineNumber = 0) + public TempFile CreateFile(string prefix = null, string extension = null, string directory = null, [CallerFilePath]string callerSourcePath = null, [CallerLineNumber]int callerLineNumber = 0) { - return AddFile(new DisposableFile(root, prefix, extension, directory, callerSourcePath, callerLineNumber)); + return AddFile(new DisposableFile(this, prefix, extension, directory, callerSourcePath, callerLineNumber)); } public DisposableFile AddFile(DisposableFile file) diff --git a/src/Microsoft.NuGet.Build.Tasks.Tests/Microsoft.NuGet.Build.Tasks.Tests.csproj b/src/Microsoft.NuGet.Build.Tasks.Tests/Microsoft.NuGet.Build.Tasks.Tests.csproj index 342af76..270ee06 100644 --- a/src/Microsoft.NuGet.Build.Tasks.Tests/Microsoft.NuGet.Build.Tasks.Tests.csproj +++ b/src/Microsoft.NuGet.Build.Tasks.Tests/Microsoft.NuGet.Build.Tasks.Tests.csproj @@ -9,7 +9,7 @@ Properties Microsoft.NuGet.Build.Tasks.Tests Microsoft.NuGet.Build.Tasks.Tests - v4.5 + v4.6.2 512 true @@ -72,7 +72,9 @@ - + + Designer + @@ -83,7 +85,6 @@ - @@ -101,6 +102,19 @@ + + + 4.7.0 + + + 2.3.1 + + + 2.3.1 + runtime; build; native; contentfiles; analyzers + all + +