Skip to content

Commit c7586b4

Browse files
authored
Merge pull request #32 from nexB/prepare-098beta1
Prepare release 0.9.8-beta1
2 parents 522e921 + 532d094 commit c7586b4

File tree

11 files changed

+143
-61
lines changed

11 files changed

+143
-61
lines changed

CHANGELOG.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@ Changelog
22
=========
33

44

5+
v0.9.8
6+
-------
7+
8+
This is a major feature update release with these updates and API breaking changes:
9+
10+
* Add new "--with-nuget-org" command line option to optionally include the
11+
nuget.org API feeds to the set of nuget.config-provided endpoints. Note that
12+
this is always included if there is no provided or available nuget.config
13+
14+
* Add extra debug tracing with the --debug option
15+
16+
* Do not create API or download URLs using a default. Only use API-provided values.
17+
18+
* Ensure we do not fail with a Package reference with no version.
19+
520

621
v0.9.7
722
-------

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ dotnet publish \
1717
--runtime linux-x64 \
1818
--self-contained true \
1919
--configuration Release \
20-
-p:Version=0.9.7 \
20+
-p:Version=0.9.8-beta1 \
2121
--output build \
2222
src/nuget-inspector/nuget-inspector.csproj

release.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
rm -rf release/
1717
mkdir release
1818

19-
VERSION=0.9.7
19+
VERSION=0.9.8-beta1
2020

2121
TARGET_BASE=nuget-inspector-$(git describe)
2222

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[bumpversion]
22
commit = False
33
tag = False
4-
current_version = 0.9.7
4+
current_version = 0.9.8-beta1
55
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+))?
66
serialize =
77
{major}.{minor}.{patch}-{release}

src/nuget-inspector/Config.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public static class Config
88
public static bool TRACE_NET = false;
99
public static bool TRACE_DEEP = false;
1010
public static bool TRACE_META = false;
11-
public const string NUGET_INSPECTOR_VERSION = "0.9.7";
11+
public static bool TRACE_OUTPUT = false;
12+
public const string NUGET_INSPECTOR_VERSION = "0.9.8-beta1";
1213
#pragma warning restore CA2211
1314
}

src/nuget-inspector/Models.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,7 @@ public void UpdateAttributes(
442442
if (
443443
string.IsNullOrWhiteSpace(api_data_url)
444444
&& download_url.StartsWith("https://api.nuget.org/")
445-
&& !string.IsNullOrWhiteSpace(synthetic_api_data_url)
446-
)
445+
&& !string.IsNullOrWhiteSpace(synthetic_api_data_url))
447446
{
448447
api_data_url = synthetic_api_data_url;
449448
}

src/nuget-inspector/NugetApi.cs

Lines changed: 79 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class NugetApi
3333
private readonly Dictionary<string, JObject> catalog_entry_by_catalog_url = new();
3434
private readonly Dictionary<string, List<PackageSearchMetadataRegistration>> psmrs_by_package_name = new();
3535
private readonly Dictionary<PackageIdentity, PackageSearchMetadataRegistration?> psmr_by_identity = new();
36-
private readonly Dictionary<(PackageIdentity, NuGetFramework), PackageDownload> download_by_identity = new();
36+
private readonly Dictionary<(PackageIdentity, NuGetFramework), PackageDownload?> download_by_identity = new();
3737
private readonly Dictionary<(PackageIdentity, NuGetFramework), SourcePackageDependencyInfo> spdi_by_identity = new();
3838

3939
private readonly List<SourceRepository> source_repositories = new();
@@ -44,7 +44,11 @@ public class NugetApi
4444

4545
private readonly NuGetFramework project_framework;
4646

47-
public NugetApi(string nuget_config_path, string project_root_path, NuGetFramework project_framework)
47+
public NugetApi(
48+
string nuget_config_path,
49+
string project_root_path,
50+
NuGetFramework project_framework,
51+
bool with_nuget_org)
4852
{
4953
this.project_framework = project_framework;
5054
List<Lazy<INuGetResourceProvider>> providers = new();
@@ -56,7 +60,8 @@ public NugetApi(string nuget_config_path, string project_root_path, NuGetFramewo
5660

5761
PopulateResources(
5862
providers: providers,
59-
settings: settings);
63+
settings: settings,
64+
with_nuget_org: with_nuget_org);
6065
}
6166

6267
/// <summary>
@@ -114,13 +119,13 @@ private List<PackageSearchMetadataRegistration> FindPackageVersionsThroughCache(
114119
/// <returns>PackageSearchMetadataRegistration or null</returns>
115120
public PackageSearchMetadataRegistration? FindPackageVersion(PackageIdentity pid)
116121
{
117-
if (Config.TRACE_NET)
122+
if (Config.TRACE)
118123
Console.WriteLine($"FindPackageVersion: {pid}");
119124

120125
if (psmr_by_identity.TryGetValue(key: pid, out PackageSearchMetadataRegistration? psmr))
121126
{
122-
if (Config.TRACE_NET)
123-
Console.WriteLine($"Metadata Cache hit for '{pid}'");
127+
if (Config.TRACE)
128+
Console.WriteLine($" Metadata Cache hit for '{pid}'");
124129
return psmr;
125130
}
126131

@@ -139,30 +144,32 @@ private List<PackageSearchMetadataRegistration> FindPackageVersionsThroughCache(
139144

140145
if (psmr != null)
141146
{
142-
if (Config.TRACE_NET)
147+
if (Config.TRACE)
143148
Console.WriteLine($" Found metadata for '{pid}' from: {metadata_resources}");
144149
psmr_by_identity[pid] = psmr;
145150
return psmr;
146151
}
147152
}
148153
catch (Exception ex)
149154
{
150-
if (Config.TRACE_NET)
151-
Console.WriteLine($"FAILED to Fetch metadata for '{pid}' with: {ex.StackTrace}");
155+
if (Config.TRACE)
156+
Console.WriteLine($" FAILED to Fetch metadata for '{pid}' with: {ex.StackTrace}");
152157

153158
exceptions.Add(item: ex);
154159
}
155160
}
156161

157-
if (Config.TRACE_NET && exceptions.Any())
158-
{
159-
Console.WriteLine($"ERROR: No package found for {pid}.");
160-
foreach (var ex in exceptions)
161-
Console.WriteLine($" {ex}");
162-
}
162+
var error_message = $"No package metadata found for {pid}.";
163+
foreach (var ex in exceptions)
164+
error_message += $"\n {ex}";
165+
166+
if (Config.TRACE)
167+
Console.WriteLine(error_message);
168+
163169
// cache this null too
164170
psmr_by_identity[pid] = null;
165-
return null;
171+
172+
throw new Exception(error_message);
166173
}
167174

168175
/// <summary>
@@ -292,26 +299,34 @@ public static ISettings LoadNugetConfigSettings(
292299
/// from a feed URL and a nuget.config file path.
293300
/// These are MetadataResourceList and DependencyInfoResourceList attributes.
294301
/// </summary>
295-
private void PopulateResources(List<Lazy<INuGetResourceProvider>> providers, ISettings settings)
302+
private void PopulateResources(
303+
List<Lazy<INuGetResourceProvider>> providers,
304+
ISettings settings,
305+
bool with_nuget_org = false)
296306
{
297307
PackageSourceProvider package_source_provider = new(settings: settings);
298308
List<PackageSource> package_sources = package_source_provider.LoadPackageSources().ToList();
299309
if (Config.TRACE)
300310
Console.WriteLine($"\nPopulateResources: Loaded {package_sources.Count} package sources from nuget.config");
301311

302-
// always nuget.org as last resort
303-
var nuget_source = new PackageSource(source: "https://api.nuget.org/v3/index.json", name : "nuget.org");
304-
package_sources.Add(nuget_source);
312+
if (with_nuget_org || !package_sources.Any())
313+
{
314+
// Use nuget.org as last resort
315+
var nuget_source = new PackageSource(
316+
source: "https://api.nuget.org/v3/index.json",
317+
name : "nuget.org");
318+
package_sources.Add(nuget_source);
319+
}
305320

306321
HashSet<string> seen = new();
307322
foreach (PackageSource package_source in package_sources)
308323
{
309-
var uri = package_source.SourceUri.ToString();
310-
if (seen.Contains(uri))
324+
var source_url = package_source.SourceUri.ToString();
325+
if (seen.Contains(source_url))
311326
continue;
312327
SourceRepository source_repository = new(source: package_source, providers: providers);
313328
AddSourceRepo(source_repo: source_repository);
314-
seen.Add(uri);
329+
seen.Add(source_url);
315330
}
316331
}
317332

@@ -451,24 +466,18 @@ public IEnumerable<PackageDependency> GetPackageDependenciesForPackage(PackageId
451466
if (spdi != null)
452467
{
453468
download = PackageDownload.FromSpdi(spdi);
469+
download_by_identity[(identity, project_framework)] = download;
454470
}
455471
else
456472
{
457-
if (Config.TRACE_NET)
458-
Console.WriteLine($" Crafting plain download for package '{identity}'");
473+
if (Config.TRACE)
474+
Console.WriteLine($" No download info available for package '{identity}'");
459475

460-
// Last resort we craft a synthetic download URL
461-
string name_lower = identity.Id.ToLower();
462-
string version_lower = identity.Version.ToString().ToLower();
463-
download = new()
464-
{
465-
download_url = $"https://api.nuget.org/v3-flatcontainer/{name_lower}/{version_lower}/{name_lower}.{version_lower}.nupkg"
466-
};
476+
download_by_identity[(identity, project_framework)] = download;
467477
}
468-
download_by_identity[(identity, project_framework)] = download;
469478
}
470479

471-
if (!with_details || (with_details && download.IsEnhanced()))
480+
if (!with_details || (with_details && download?.IsEnhanced() == true))
472481
return download;
473482

474483
// We need to fetch the SHA512 (and the size)
@@ -511,10 +520,12 @@ public IEnumerable<PackageDependency> GetPackageDependenciesForPackage(PackageId
511520
}
512521

513522
string hash = catalog_entry["packageHash"]!.ToString();
514-
download.hash = Convert.ToHexString(Convert.FromBase64String(hash));
515-
download.hash_algorithm = catalog_entry["packageHashAlgorithm"]!.ToString();
516-
download.size = (int)catalog_entry["packageSize"]!;
517-
523+
if (download != null)
524+
{
525+
download.hash = Convert.ToHexString(Convert.FromBase64String(hash));
526+
download.hash_algorithm = catalog_entry["packageHashAlgorithm"]!.ToString();
527+
download.size = (int)catalog_entry["packageSize"]!;
528+
}
518529
if (Config.TRACE_NET)
519530
Console.WriteLine($" download: {download}");
520531
download_by_identity[(identity, project_framework)] = download;
@@ -656,7 +667,17 @@ public HashSet<SourcePackageDependencyInfo> ResolveDependenciesForPackageReferen
656667

657668
var packages = new List<PackageId>();
658669
foreach (var targetref in target_references)
659-
packages.Add(PackageId.FromReference(targetref));
670+
{
671+
try
672+
{
673+
packages.Add(PackageId.FromReference(targetref));
674+
}
675+
catch (Exception ex)
676+
{
677+
Console.WriteLine($" FAILED: targetref: {targetref}");
678+
throw new Exception(targetref.ToString(), ex);
679+
}
680+
}
660681

661682
walk_context.ProjectLibraryProviders.Add(new ProjectLibraryProvider(packages));
662683

@@ -913,10 +934,26 @@ public override string ToString()
913934

914935
public static PackageId FromReference(PackageReference reference)
915936
{
916-
bool allow_prerel = reference.HasAllowedVersions && reference.AllowedVersions.MinVersion.IsPrerelease;
937+
if (reference == null)
938+
throw new ArgumentNullException(nameof(reference));
939+
var av = reference.AllowedVersions;
940+
bool allow_prerel = false;
941+
string? version = null;
942+
if (av != null)
943+
{
944+
var mv = reference.AllowedVersions.MinVersion;
945+
if (mv != null)
946+
allow_prerel = mv.IsPrerelease;
947+
version = reference.AllowedVersions.ToNormalizedString();
948+
}
949+
if (version == null && reference.PackageIdentity.Version != null)
950+
{
951+
version = reference.PackageIdentity.Version.ToString();
952+
}
953+
917954
return new PackageId(
918-
id:reference.PackageIdentity.Id,
919-
version: reference.AllowedVersions.ToNormalizedString(),
955+
id: reference.PackageIdentity.Id,
956+
version: version ?? "",
920957
allow_prerelease_versions: allow_prerel
921958
);
922959
}

src/nuget-inspector/Options.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ public class Options
2626
description: "Path to a nuget.config file to use, ignoring all other nuget.config.")]
2727
public string NugetConfigPath = "";
2828

29+
// If True, return extra metadata details when available such as SHA512
2930
public bool WithDetails;
3031
public bool WithFallback;
32+
public bool WithNuGetOrg;
3133
public bool ShowHelp;
3234
public bool Verbose;
35+
public bool Debug;
3336
public bool ShowVersion;
3437
public bool ShowAbout;
3538

@@ -63,12 +66,18 @@ public List<string> AsCliList()
6366
if (Verbose)
6467
options.Add("--verbose");
6568

69+
if (Debug)
70+
options.Add("--debug");
71+
6672
if (WithDetails)
6773
options.Add("--with-details");
6874

6975
if (WithFallback)
7076
options.Add("--with-fallback");
7177

78+
if (WithNuGetOrg)
79+
options.Add("--with-nuget-org");
80+
7281
return options;
7382
}
7483

@@ -96,10 +105,15 @@ public List<string> AsCliList()
96105
command_options.Add(prototype: "with-fallback", description: "Optionally use a plain XML project file parser as fallback from failures.",
97106
action: value => options.WithDetails = value != null);
98107

108+
command_options.Add(prototype: "with-nuget-org", description: "Optionally use the officila, public nuget.org API as a fallback in addition to nuget.config-configured API sources.",
109+
action: value => options.WithNuGetOrg = value != null);
110+
99111
command_options.Add(prototype: "h|help", description: "Show this message and exit.",
100112
action: value => options.ShowHelp = value != null);
101113
command_options.Add(prototype: "v|verbose", description: "Display more verbose output.",
102114
action: value => options.Verbose = value != null);
115+
command_options.Add(prototype: "debug", description: "Display very verbose debug output.",
116+
action: value => options.Debug = value != null);
103117
command_options.Add(prototype: "version", description: "Display nuget-inspector version and exit.",
104118
action: value => options.ShowVersion = value != null);
105119
command_options.Add(prototype: "about", description: "Display information about nuget-inspector and exit.",

src/nuget-inspector/Program.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Diagnostics;
2-
using System.Xml;
32
using Microsoft.Build.Locator;
3+
using Newtonsoft.Json;
44
using NuGet.Frameworks;
55

66
namespace NugetInspector;
@@ -99,7 +99,8 @@ private static ExecutionResult ExecuteInspector(Options options)
9999
var nuget_api_service = new NugetApi(
100100
nuget_config_path: options.NugetConfigPath,
101101
project_root_path: project_options.ProjectDirectory,
102-
project_framework: project_framework);
102+
project_framework: project_framework,
103+
with_nuget_org: options.WithNuGetOrg);
103104

104105
var scanner = new ProjectScanner(
105106
options: project_options,
@@ -134,6 +135,17 @@ private static ExecutionResult ExecuteInspector(Options options)
134135
var output_formatter = new OutputFormatJson(scan_result: scan_result);
135136
output_formatter.Write();
136137

138+
if (Config.TRACE_OUTPUT)
139+
{
140+
Console.WriteLine("\n=============JSON OUTPUT================");
141+
string output = JsonConvert.SerializeObject(
142+
value: output_formatter.scan_output,
143+
formatting: Formatting.Indented);
144+
Console.WriteLine(output);
145+
146+
Console.WriteLine("=======================================\n");
147+
}
148+
137149
BasePackage project_package = scan_result.project_package;
138150

139151
bool success = scan_result.Status == ScanResult.ResultStatus.Success;
@@ -246,6 +258,14 @@ private static ParsedOptions ParseCliArgs(string[] args)
246258
{
247259
Config.TRACE = true;
248260
}
261+
if (options.Debug)
262+
{
263+
Config.TRACE = true;
264+
Config.TRACE_DEEP = true;
265+
Config.TRACE_META = true;
266+
Config.TRACE_NET = true;
267+
Config.TRACE_OUTPUT = true;
268+
}
249269

250270
if (Config.TRACE_ARGS)
251271
Console.WriteLine($"argument: with-details: {options.WithDetails}");

0 commit comments

Comments
 (0)