Skip to content

Commit 3c8b58b

Browse files
authored
When a feature name is invalid, report the error as part of the feature name rather than as part of the overall features block. (#1675)
Before and after demonstration: ``` PS C:\Dev\vcpkg-tool\out\build\Win-x64-Debug-WithArtifacts> type C:\Dev\vcpkg-tool\azure-pipelines\e2e-ports\vcpkg-requires-feature\vcpkg.json { "name": "vcpkg-requires-feature", "version": "0", "features": { "a": { "description": "Feature A" }, "b": { "description": "Feature B" }, "b_required": { "description": "This feature needs to be turned on for the port to build" }, "c": { "description": "Feature C" } } } PS C:\Dev\vcpkg-tool\out\build\Win-x64-Debug-WithArtifacts> C:\Dev\vcpkg\vcpkg.exe format-manifest C:\Dev\vcpkg-tool\azure-pipelines\e2e-ports\vcpkg-requires-feature\vcpkg.json C:\Dev\vcpkg-tool\azure-pipelines\e2e-ports\vcpkg-requires-feature\vcpkg.json: error: $.features (a set of features): features must be lowercase alphanumeric+hyphens, and not one of the reserved names PS C:\Dev\vcpkg-tool\out\build\Win-x64-Debug-WithArtifacts> .\vcpkg.exe format-manifest C:\Dev\vcpkg-tool\azure-pipelines\e2e-ports\vcpkg-requires-feature\vcpkg.json C:\Dev\vcpkg-tool\azure-pipelines\e2e-ports\vcpkg-requires-feature\vcpkg.json: error: $.features.b_required (a feature): features must be lowercase alphanumeric+hyphens, and not one of the reserved names PS C:\Dev\vcpkg-tool\out\build\Win-x64-Debug-WithArtifacts> ```
1 parent fc89362 commit 3c8b58b

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "bad-feature-name",
3+
"version": "0",
4+
"features": {
5+
"a": {
6+
"description": "Feature A"
7+
},
8+
"b_required": {
9+
"description": "This feature has an invalid name"
10+
},
11+
"c": {
12+
"description": "Feature C"
13+
}
14+
}
15+
}

azure-pipelines/end-to-end-tests-dir/format-manifest.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ $testProjects | % {
1818
}
1919
}
2020

21+
$output = Run-VcpkgAndCaptureOutput @commonArgs format-manifest "$PSScriptRoot/../e2e-assets/format-manifest-malformed/bad-feature-name.json"
22+
Throw-IfNotFailed
23+
$expected = "bad-feature-name.json: error: $.features.b_required (a feature): features must be lowercase alphanumeric+hyphens, and not one of the reserved names"
24+
Throw-IfNonContains -Expected $expected -Actual $output
25+
2126
Write-Trace "test re-serializing every manifest"
2227
$manifestDir = "$TestingRoot/manifest-dir"
2328
Copy-Item -Path "$env:VCPKG_ROOT/ports" -Destination $manifestDir -recurse -Force -Filter vcpkg.json

include/vcpkg/base/jsonreader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace vcpkg::Json
4646
void add_expected_type_error(const LocalizedString& expected_type);
4747
void add_extra_field_error(const LocalizedString& type, StringView fields, StringView suggestion = {});
4848
void add_generic_error(const LocalizedString& type, StringView message);
49+
void add_field_name_error(const LocalizedString& type, StringView field, StringView message);
4950

5051
void add_warning(LocalizedString type, StringView msg);
5152

src/vcpkg/base/json.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,12 @@ namespace vcpkg::Json
14921492
.append_raw(message));
14931493
}
14941494

1495+
void Reader::add_field_name_error(const LocalizedString& type, StringView field, StringView message)
1496+
{
1497+
PathGuard guard{m_path, field};
1498+
add_generic_error(type, message);
1499+
}
1500+
14951501
void Reader::check_for_unexpected_fields(const Object& obj,
14961502
View<StringLiteral> valid_fields,
14971503
const LocalizedString& type_name)

src/vcpkg/sourceparagraph.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,8 @@ namespace vcpkg
11081108
}
11091109
if (!Json::IdentifierDeserializer::is_ident(pr.first))
11101110
{
1111-
r.add_generic_error(type_name(), msg::format(msgInvalidFeature));
1111+
r.add_field_name_error(
1112+
FeatureDeserializer::instance.type_name(), pr.first, msg::format(msgInvalidFeature));
11121113
continue;
11131114
}
11141115
std::unique_ptr<FeatureParagraph> v;

0 commit comments

Comments
 (0)