From 2ce3ec10f53503b60fa31c6cf9a5fee7265e8104 Mon Sep 17 00:00:00 2001 From: heng-liu <45407901+heng-liu@users.noreply.github.com> Date: Tue, 19 May 2020 17:08:32 -0700 Subject: [PATCH] fix all the rest warnings raised by code analysis (#3401) --- src/NuGet.Core/NuGet.Commands/MSBuildProjectFactory.cs | 8 ++++++++ .../RestoreCommand/Diagnostics/UnresolvedMessages.cs | 4 ++++ .../RestoreCommand/Utility/BuildAssetsUtils.cs | 4 ++++ .../RestoreCommand/Utility/LockFileUtils.cs | 4 ++++ .../Providers/RepositorySignatureResourceProvider.cs | 4 ++++ .../Resources/PackageDetailsUriResourceV3.cs | 5 +++++ .../NuGet.Protocol/Resources/PackageUpdateResource.cs | 4 ++++ .../NuGet.Protocol/Resources/ReportAbuseResourceV3.cs | 7 ++++++- src/NuGet.Core/NuGet.Protocol/Utility/CachingUtility.cs | 9 ++++++++- .../NuGet.Protocol/Utility/LocalFolderUtility.cs | 4 ++++ .../NuGet.Protocol/Utility/OfflineFeedUtility.cs | 6 +++++- 11 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/NuGet.Core/NuGet.Commands/MSBuildProjectFactory.cs b/src/NuGet.Core/NuGet.Commands/MSBuildProjectFactory.cs index eeacbd8664e..07eb4ee8392 100644 --- a/src/NuGet.Core/NuGet.Commands/MSBuildProjectFactory.cs +++ b/src/NuGet.Core/NuGet.Commands/MSBuildProjectFactory.cs @@ -259,12 +259,20 @@ public static string GetTargetPathForSourceFile(string sourcePath, string projec } var projectName = Path.GetFileName(projectDirectory); var targetPath = Path.Combine(SourcesFolder, projectName); +#if NETCOREAPP + if (sourcePath.Contains(projectDirectory, StringComparison.Ordinal)) +#else if (sourcePath.Contains(projectDirectory)) +#endif { // This is needed because Path.GetDirectoryName returns a path with Path.DirectorySepartorChar var projectDirectoryWithSeparatorChar = PathUtility.GetPathWithDirectorySeparator(projectDirectory); +#if NETCOREAPP + var relativePath = Path.GetDirectoryName(sourcePath).Replace(projectDirectoryWithSeparatorChar, string.Empty, StringComparison.Ordinal); +#else var relativePath = Path.GetDirectoryName(sourcePath).Replace(projectDirectoryWithSeparatorChar, string.Empty); +#endif if (!string.IsNullOrEmpty(relativePath) && PathUtility.IsDirectorySeparatorChar(relativePath[0])) { relativePath = relativePath.Substring(1, relativePath.Length - 1); diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Diagnostics/UnresolvedMessages.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Diagnostics/UnresolvedMessages.cs index ea2e0de8930..b2e5c495d88 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Diagnostics/UnresolvedMessages.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Diagnostics/UnresolvedMessages.cs @@ -74,7 +74,11 @@ public static async Task GetMessageAsync(string targetGraphNa { // Project // Check if the name is a path and if it exists. All project paths should have been normalized and converted to full paths before this. +#if NETCOREAPP + if (unresolved.Name.IndexOf(Path.DirectorySeparatorChar, StringComparison.Ordinal) > -1 && File.Exists(unresolved.Name)) +#else if (unresolved.Name.IndexOf(Path.DirectorySeparatorChar) > -1 && File.Exists(unresolved.Name)) +#endif { // File exists but the dg spec did not contain the spec code = NuGetLogCode.NU1105; diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/BuildAssetsUtils.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/BuildAssetsUtils.cs index 286c5072162..811a940056e 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/BuildAssetsUtils.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/BuildAssetsUtils.cs @@ -758,7 +758,11 @@ private static HashSet ConvertToPackageDependencyInfo( private static XElement GeneratePackagePathProperty(LocalPackageInfo localPackageInfo) { +#if NETCOREAPP + return GenerateProperty($"Pkg{localPackageInfo.Id.Replace(".", "_", StringComparison.Ordinal)}", localPackageInfo.ExpandedPath); +#else return GenerateProperty($"Pkg{localPackageInfo.Id.Replace(".", "_")}", localPackageInfo.ExpandedPath); +#endif } } } diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/LockFileUtils.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/LockFileUtils.cs index bd551458132..55a7cb937e9 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/LockFileUtils.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/LockFileUtils.cs @@ -826,7 +826,11 @@ private static void ClearIfExists(IList group) where T : LockFileItem var fileName = Path.GetFileName(firstItem.Path); Debug.Assert(!string.IsNullOrEmpty(fileName)); +#if NETCOREAPP + Debug.Assert(firstItem.Path.IndexOf('/', StringComparison.Ordinal) > 0); +#else Debug.Assert(firstItem.Path.IndexOf('/') > 0); +#endif var emptyDir = firstItem.Path.Substring(0, firstItem.Path.Length - fileName.Length) + PackagingCoreConstants.EmptyFolder; diff --git a/src/NuGet.Core/NuGet.Protocol/Providers/RepositorySignatureResourceProvider.cs b/src/NuGet.Core/NuGet.Protocol/Providers/RepositorySignatureResourceProvider.cs index 6f5ffa9f191..356c474fb8e 100644 --- a/src/NuGet.Core/NuGet.Protocol/Providers/RepositorySignatureResourceProvider.cs +++ b/src/NuGet.Core/NuGet.Protocol/Providers/RepositorySignatureResourceProvider.cs @@ -104,7 +104,11 @@ private async Task GetRepositorySignatureResourceAs private static string GenerateCacheKey(ServiceIndexEntry serviceEntry) { +#if NETCOREAPP + var index = serviceEntry.Type.IndexOf('/', StringComparison.Ordinal); +#else var index = serviceEntry.Type.IndexOf('/'); +#endif var version = serviceEntry.Type.Substring(index + 1).Trim(); return $"repository_signatures_{version}"; diff --git a/src/NuGet.Core/NuGet.Protocol/Resources/PackageDetailsUriResourceV3.cs b/src/NuGet.Core/NuGet.Protocol/Resources/PackageDetailsUriResourceV3.cs index d145c22c350..25a3b940d70 100644 --- a/src/NuGet.Core/NuGet.Protocol/Resources/PackageDetailsUriResourceV3.cs +++ b/src/NuGet.Core/NuGet.Protocol/Resources/PackageDetailsUriResourceV3.cs @@ -50,8 +50,13 @@ private static bool IsValidUriTemplate(string uriTemplate) public Uri GetUri(string id, NuGetVersion version) { var uriString = _template +#if NETCOREAPP + .Replace("{id}", id, StringComparison.OrdinalIgnoreCase) + .Replace("{version}", version.ToNormalizedString(), StringComparison.OrdinalIgnoreCase); +#else .Replace("{id}", id) .Replace("{version}", version.ToNormalizedString()); +#endif return new Uri(uriString); } diff --git a/src/NuGet.Core/NuGet.Protocol/Resources/PackageUpdateResource.cs b/src/NuGet.Core/NuGet.Protocol/Resources/PackageUpdateResource.cs index 101f84daeb2..a9f2c1e7398 100644 --- a/src/NuGet.Core/NuGet.Protocol/Resources/PackageUpdateResource.cs +++ b/src/NuGet.Core/NuGet.Protocol/Resources/PackageUpdateResource.cs @@ -827,7 +827,11 @@ private async Task GetSecureApiKey( } catch(HttpRequestException ex) { +#if NETCOREAPP + if (ex.Message.Contains("Response status code does not indicate success: 403 (Forbidden).", StringComparison.OrdinalIgnoreCase)) +#else if (ex.Message.Contains("Response status code does not indicate success: 403 (Forbidden).")) +#endif { return InvalidApiKey; } diff --git a/src/NuGet.Core/NuGet.Protocol/Resources/ReportAbuseResourceV3.cs b/src/NuGet.Core/NuGet.Protocol/Resources/ReportAbuseResourceV3.cs index 8f12ce6b297..d995aee1f59 100644 --- a/src/NuGet.Core/NuGet.Protocol/Resources/ReportAbuseResourceV3.cs +++ b/src/NuGet.Core/NuGet.Protocol/Resources/ReportAbuseResourceV3.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; @@ -33,8 +33,13 @@ public ReportAbuseResourceV3(string uriTemplate) public Uri GetReportAbuseUrl(string id, NuGetVersion version) { var uriString = _uriTemplate +#if NETCOREAPP + .Replace("{id}", id, StringComparison.OrdinalIgnoreCase) + .Replace("{version}", version.ToNormalizedString(), StringComparison.OrdinalIgnoreCase); +#else .Replace("{id}", id) .Replace("{version}", version.ToNormalizedString()); +#endif return new Uri(uriString); } diff --git a/src/NuGet.Core/NuGet.Protocol/Utility/CachingUtility.cs b/src/NuGet.Core/NuGet.Protocol/Utility/CachingUtility.cs index 2aaba071894..85870fd3a93 100644 --- a/src/NuGet.Core/NuGet.Protocol/Utility/CachingUtility.cs +++ b/src/NuGet.Core/NuGet.Protocol/Utility/CachingUtility.cs @@ -86,10 +86,17 @@ public static string RemoveInvalidFileNameChars(string value) { var invalid = Path.GetInvalidFileNameChars(); return new string( +#if NETCOREAPP + value.Select(ch => invalid.Contains(ch) ? '_' : ch).ToArray() + ) + .Replace("__", "_", StringComparison.Ordinal) + .Replace("__", "_", StringComparison.Ordinal); +#else value.Select(ch => invalid.Contains(ch) ? '_' : ch).ToArray() ) .Replace("__", "_") .Replace("__", "_"); +#endif } } -} \ No newline at end of file +} diff --git a/src/NuGet.Core/NuGet.Protocol/Utility/LocalFolderUtility.cs b/src/NuGet.Core/NuGet.Protocol/Utility/LocalFolderUtility.cs index 1124f9d789f..7c0834c468f 100644 --- a/src/NuGet.Core/NuGet.Protocol/Utility/LocalFolderUtility.cs +++ b/src/NuGet.Core/NuGet.Protocol/Utility/LocalFolderUtility.cs @@ -964,7 +964,11 @@ public static IEnumerable ResolvePackageFromPath(string packagePath, boo private static string EnsurePackageExtension(string packagePath, bool isSnupkg) { +#if NETCOREAPP + if (packagePath.IndexOf('*', StringComparison.Ordinal) == -1) +#else if (packagePath.IndexOf('*') == -1) +#endif { // If there's no wildcard in the path to begin with, assume that it's an absolute path. return packagePath; diff --git a/src/NuGet.Core/NuGet.Protocol/Utility/OfflineFeedUtility.cs b/src/NuGet.Core/NuGet.Protocol/Utility/OfflineFeedUtility.cs index f2083bda026..f468b791f38 100644 --- a/src/NuGet.Core/NuGet.Protocol/Utility/OfflineFeedUtility.cs +++ b/src/NuGet.Core/NuGet.Protocol/Utility/OfflineFeedUtility.cs @@ -87,7 +87,11 @@ public static void ThrowIfInvalid(string path) } var invalidPathChars = Path.GetInvalidPathChars(); +#if NETCOREAPP + if (invalidPathChars.Any(p => path.Contains(p, StringComparison.Ordinal))) +#else if (invalidPathChars.Any(p => path.Contains(p))) +#endif { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Path_Invalid, @@ -257,4 +261,4 @@ private static string GetHash(string nupkgFilePath) return packageHash; } } -} \ No newline at end of file +}