Skip to content

Commit

Permalink
Improve diagnostics when an invalid triplet name is supplied.
Browse files Browse the repository at this point in the history
This fixes a regression introduced in #1474 where an error message is added but the corresponding text being parsed is not. This was reported as microsoft/vcpkg#41143
  • Loading branch information
BillyONeal committed Sep 25, 2024
1 parent c16c026 commit a7b45df
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 22 deletions.
17 changes: 16 additions & 1 deletion azure-pipelines/end-to-end-tests-dir/cli.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ $out = Run-VcpkgAndCaptureOutput -TestArgs @('install', 'this-is-super-not-a-#po
Throw-IfNotFailed

[string]$expected = @"
error: expected the end of input parsing a package spec; this usually means the indicated character is not allowed to be in a package spec. Port, triplet, and feature names are all lowercase alphanumeric+hypens.
error: expected the end of input parsing a package spec; this usually means the indicated character is not allowed to be in a package spec. Port, triplet, and feature names are all lowercase alphanumeric+hyphens.
on expression: this-is-super-not-a-#port
^
Expand All @@ -76,6 +76,21 @@ if (-Not ($out.Replace("`r`n", "`n").EndsWith($expected)))
throw 'Bad malformed --binarysource output; it was: ' + $out
}

$out = Run-VcpkgAndCaptureOutput -TestArgs @('install', 'zlib', '--triplet', 'ARM-windows')
Throw-IfNotFailed

$expected = @"
error: Invalid triplet name. Triplet names are all lowercase alphanumeric+hyphens.
on expression: ARM-windows
^
Built-in Triplets:
"@

if (-Not ($out.Replace("`r`n", "`n").StartsWith($expected)))
{
throw 'Bad malformed triplet output. It was: ' + $out
}

$out = Run-VcpkgAndCaptureStdErr -TestArgs @('x-package-info', 'zlib#notaport', '--x-json', '--x-installed')
Throw-IfNotFailed
$expected = @"
Expand Down
14 changes: 9 additions & 5 deletions include/vcpkg/base/message-data.inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2207,25 +2207,25 @@ DECLARE_MESSAGE(
(msg::package_name, msg::url),
"",
"\"{package_name}\" is not a valid feature name. "
"Feature names must be lowercase alphanumeric+hypens and not reserved (see {url} for more information).")
"Feature names must be lowercase alphanumeric+hyphens and not reserved (see {url} for more information).")
DECLARE_MESSAGE(ParseIdentifierError,
(msg::value, msg::url),
"{value} is a lowercase identifier like 'boost'",
"\"{value}\" is not a valid identifier. "
"Identifiers must be lowercase alphanumeric+hypens and not reserved (see {url} for more information).")
"Identifiers must be lowercase alphanumeric+hyphens and not reserved (see {url} for more information).")
DECLARE_MESSAGE(
ParsePackageNameNotEof,
(msg::url),
"",
"expected the end of input parsing a package name; this usually means the indicated character is not allowed to be "
"in a port name. Port names are all lowercase alphanumeric+hypens and not reserved (see {url} for more "
"in a port name. Port names are all lowercase alphanumeric+hyphens and not reserved (see {url} for more "
"information).")
DECLARE_MESSAGE(
ParsePackageNameError,
(msg::package_name, msg::url),
"",
"\"{package_name}\" is not a valid package name. "
"Package names must be lowercase alphanumeric+hypens and not reserved (see {url} for more information).")
"Package names must be lowercase alphanumeric+hyphens and not reserved (see {url} for more information).")
DECLARE_MESSAGE(ParsePackagePatternError,
(msg::package_name, msg::url),
"",
Expand All @@ -2237,11 +2237,15 @@ DECLARE_MESSAGE(
(),
"",
"expected the end of input parsing a package spec; this usually means the indicated character is not allowed to be "
"in a package spec. Port, triplet, and feature names are all lowercase alphanumeric+hypens.")
"in a package spec. Port, triplet, and feature names are all lowercase alphanumeric+hyphens.")
DECLARE_MESSAGE(ParseQualifiedSpecifierNotEofSquareBracket,
(msg::version_spec),
"",
"expected the end of input parsing a package spec; did you mean {version_spec} instead?")
DECLARE_MESSAGE(ParseTripletNotEof,
(),
"",
"Invalid triplet name. Triplet names are all lowercase alphanumeric+hyphens.")
DECLARE_MESSAGE(PathMustBeAbsolute,
(msg::path),
"",
Expand Down
4 changes: 4 additions & 0 deletions include/vcpkg/base/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ namespace vcpkg
int column;
};

void append_caret_line(LocalizedString& res,
const Unicode::Utf8Decoder& it,
const Unicode::Utf8Decoder& start_of_line);

struct ParseMessage
{
SourceLoc location = {};
Expand Down
11 changes: 6 additions & 5 deletions locales/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1252,19 +1252,20 @@
"ParagraphExpectedColonAfterField": "expected ':' after field name",
"ParagraphExpectedFieldName": "expected field name",
"ParagraphUnexpectedEndOfLine": "unexpected end of line, to span a blank line use \" .\"",
"ParseFeatureNameError": "\"{package_name}\" is not a valid feature name. Feature names must be lowercase alphanumeric+hypens and not reserved (see {url} for more information).",
"ParseFeatureNameError": "\"{package_name}\" is not a valid feature name. Feature names must be lowercase alphanumeric+hyphens and not reserved (see {url} for more information).",
"_ParseFeatureNameError.comment": "An example of {package_name} is zlib. An example of {url} is https://github.com/microsoft/vcpkg.",
"ParseIdentifierError": "\"{value}\" is not a valid identifier. Identifiers must be lowercase alphanumeric+hypens and not reserved (see {url} for more information).",
"ParseIdentifierError": "\"{value}\" is not a valid identifier. Identifiers must be lowercase alphanumeric+hyphens and not reserved (see {url} for more information).",
"_ParseIdentifierError.comment": "{value} is a lowercase identifier like 'boost' An example of {url} is https://github.com/microsoft/vcpkg.",
"ParsePackageNameError": "\"{package_name}\" is not a valid package name. Package names must be lowercase alphanumeric+hypens and not reserved (see {url} for more information).",
"ParsePackageNameError": "\"{package_name}\" is not a valid package name. Package names must be lowercase alphanumeric+hyphens and not reserved (see {url} for more information).",
"_ParsePackageNameError.comment": "An example of {package_name} is zlib. An example of {url} is https://github.com/microsoft/vcpkg.",
"ParsePackageNameNotEof": "expected the end of input parsing a package name; this usually means the indicated character is not allowed to be in a port name. Port names are all lowercase alphanumeric+hypens and not reserved (see {url} for more information).",
"ParsePackageNameNotEof": "expected the end of input parsing a package name; this usually means the indicated character is not allowed to be in a port name. Port names are all lowercase alphanumeric+hyphens and not reserved (see {url} for more information).",
"_ParsePackageNameNotEof.comment": "An example of {url} is https://github.com/microsoft/vcpkg.",
"ParsePackagePatternError": "\"{package_name}\" is not a valid package pattern. Package patterns must use only one wildcard character (*) and it must be the last character in the pattern (see {url} for more information).",
"_ParsePackagePatternError.comment": "An example of {package_name} is zlib. An example of {url} is https://github.com/microsoft/vcpkg.",
"ParseQualifiedSpecifierNotEof": "expected the end of input parsing a package spec; this usually means the indicated character is not allowed to be in a package spec. Port, triplet, and feature names are all lowercase alphanumeric+hypens.",
"ParseQualifiedSpecifierNotEof": "expected the end of input parsing a package spec; this usually means the indicated character is not allowed to be in a package spec. Port, triplet, and feature names are all lowercase alphanumeric+hyphens.",
"ParseQualifiedSpecifierNotEofSquareBracket": "expected the end of input parsing a package spec; did you mean {version_spec} instead?",
"_ParseQualifiedSpecifierNotEofSquareBracket.comment": "An example of {version_spec} is zlib:[email protected].",
"ParseTripletNotEof": "Invalid triplet name. Triplet names are all lowercase alphanumeric+hyphens.",
"PathMustBeAbsolute": "Value of environment variable X_VCPKG_REGISTRIES_CACHE is not absolute: {path}",
"_PathMustBeAbsolute.comment": "An example of {path} is /foo/bar.",
"PerformingPostBuildValidation": "Performing post-build validation",
Expand Down
15 changes: 11 additions & 4 deletions src/vcpkg/base/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ namespace vcpkg
}
}

static void append_caret_line(LocalizedString& res, const SourceLoc& loc)
void append_caret_line(LocalizedString& res,
const Unicode::Utf8Decoder& cursor,
const Unicode::Utf8Decoder& start_of_line)
{
auto line_end = Util::find_if(loc.it, ParserBase::is_lineend);
auto line_end = Util::find_if(cursor, ParserBase::is_lineend);
StringView line = StringView{
loc.start_of_line.pointer_to_current(),
start_of_line.pointer_to_current(),
line_end.pointer_to_current(),
};

Expand All @@ -42,7 +44,7 @@ namespace vcpkg
std::string caret_string;
caret_string.append(line_prefix_space, ' ');
// note *it is excluded because it is where the ^ goes
for (auto it = loc.start_of_line; it != loc.it; ++it)
for (auto it = start_of_line; it != cursor; ++it)
{
if (*it == '\t')
caret_string.push_back('\t');
Expand All @@ -57,6 +59,11 @@ namespace vcpkg
res.append_indent().append_raw(caret_string);
}

static void append_caret_line(LocalizedString& res, const SourceLoc& loc)
{
append_caret_line(res, loc.it, loc.start_of_line);
}

LocalizedString ParseMessage::format(StringView origin, MessageKind kind) const
{
LocalizedString res;
Expand Down
19 changes: 12 additions & 7 deletions src/vcpkg/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,22 @@ namespace vcpkg

[[nodiscard]] ExpectedL<Unit> check_triplet(StringView name, const TripletDatabase& database)
{
// Intentionally show the lowercased string
auto as_lower = Strings::ascii_to_lowercase(name);

if (std::find_if_not(name.begin(), name.end(), ParserBase::is_package_name_char) != name.end())
Unicode::Utf8Decoder start_of_line{name};
for (auto cursor = start_of_line; !cursor.is_eof(); ++cursor)
{
return msg::format_error(msgParseQualifiedSpecifierNotEof);
if (!ParserBase::is_package_name_char(*cursor))
{
auto result = msg::format_error(msgParseTripletNotEof).append_raw('\n');
append_caret_line(result, cursor, start_of_line);
result.append_raw('\n');
append_help_topic_valid_triplet(result, database);
return result;
}
}

if (!database.is_valid_triplet_canonical_name(as_lower))
if (!database.is_valid_triplet_canonical_name(name))
{
LocalizedString result = msg::format_error(msgInvalidTriplet, msg::triplet = as_lower);
LocalizedString result = msg::format_error(msgInvalidTriplet, msg::triplet = name);
result.append_raw('\n');
append_help_topic_valid_triplet(result, database);
return result;
Expand Down

0 comments on commit a7b45df

Please sign in to comment.