Skip to content

Commit 0f6abb6

Browse files
aristidispmeta-codesync[bot]
authored andcommitted
Add no_package extra-validation flag and rename missing_package to empty_or_no_package
Summary: Add a new `no_package` extra-validation flag (default: `none`) that fires when a .thrift file has no `package` directive at all — not even an explicitly empty `package;`. This is distinct from the existing check (renamed from `missing_package` to `empty_or_no_package`) which fires when the package has no URI content. Reviewed By: hchokshi Differential Revision: D95601653 fbshipit-source-id: b4cdca23537f05e0aab8a1316a0430805bbd6140
1 parent e8acbd0 commit 0f6abb6

5 files changed

Lines changed: 157 additions & 38 deletions

File tree

third-party/thrift/src/thrift/compiler/compiler.cc

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,22 @@ void printUsageTo(FILE* stream) {
160160
Action to take on `required` (struct and exception) fields.
161161
Default: warn
162162
163-
missing_package=none|warn|error
164-
Action to take on files that do not have a package.
163+
no_package=none|warn|error
164+
Action to take on files that do not have any `package`
165+
directive (not even an empty one).
165166
Default: warn
167+
NOTE: This will become an `error` soon, following the full
168+
deployment of the empty `package;` (with
169+
`@thrift.AllowLegacyMissingUris`).
170+
171+
empty_or_no_package=none|warn|error
172+
Action to take on files that either do not have a package
173+
(like no_package above) OR have an empty package (`package;`).
174+
Default: warn
175+
176+
NOTE: if both empty_or_no_package and no_package validations
177+
are enabled, the higher level of validation will be applied
178+
to the (overlapping) case when there is no `package` at all.
166179
167180
missing_uris=none|warn|error
168181
Action to take on types (struct, union, exception, enum)
@@ -970,8 +983,15 @@ std::string parse_args(
970983

971984
if (maybe_parse_validation_level_flag(
972985
/*flag=*/validator,
973-
/*prefix=*/"missing_package",
974-
&sparams.missing_package)) {
986+
/*prefix=*/"no_package",
987+
&sparams.no_package)) {
988+
continue;
989+
}
990+
991+
if (maybe_parse_validation_level_flag(
992+
/*flag=*/validator,
993+
/*prefix=*/"empty_or_no_package",
994+
&sparams.empty_or_no_package)) {
975995
continue;
976996
}
977997

@@ -1191,9 +1211,12 @@ void record_invocation_params(
11911211
fmt::format(
11921212
"required_field_qualifier={}",
11931213
fmt::underlying(sparams.required_field_qualifier)));
1214+
sema_params_metric.add(
1215+
fmt::format("no_package={}", fmt::underlying(sparams.no_package)));
11941216
sema_params_metric.add(
11951217
fmt::format(
1196-
"missing_package={}", fmt::underlying(sparams.missing_package)));
1218+
"empty_or_no_package={}",
1219+
fmt::underlying(sparams.empty_or_no_package)));
11971220
sema_params_metric.add(
11981221
fmt::format("missing_uris={}", fmt::underlying(sparams.missing_uris)));
11991222
sema_params_metric.add(

third-party/thrift/src/thrift/compiler/sema/sema_context.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,13 @@ struct sema_params {
157157
// Action to take on `required` (struct and exception) fields.
158158
validation_level required_field_qualifier = validation_level::warn;
159159

160-
// Action to take on files without a package.
161-
validation_level missing_package = validation_level::warn;
160+
// Action to take on files without any `package` directive (not even an empty
161+
// one).
162+
validation_level no_package = validation_level::warn;
163+
164+
// Action to take on files that either do not have a package (like no_package
165+
// above) or have an empty package (`package;`).
166+
validation_level empty_or_no_package = validation_level::warn;
162167

163168
// Action to take on types (struct, union, exception, enum) missing Thrift
164169
// URIs (without the annotation that explicitly allows it, i.e.

third-party/thrift/src/thrift/compiler/sema/standard_validator.cc

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,33 @@ void validate_python_namespaces(sema_context& ctx, const t_program& program) {
455455

456456
void validate_program_package(sema_context& ctx, const t_program& program) {
457457
const t_package& package = program.package();
458+
if (!package.is_explicit()) {
459+
// No package directive at all. Both no_package and empty_or_no_package
460+
// cover this case — report once at the higher level.
461+
const sema_params::validation_level level = std::max(
462+
ctx.sema_parameters().no_package,
463+
ctx.sema_parameters().empty_or_no_package);
464+
ctx.report(
465+
program,
466+
validation_to_diagnostic_level(level),
467+
"Thrift file is missing a `package` directive. This will soon become "
468+
"an error (see https://fburl.com/thrift-uri-add-package).");
469+
return;
470+
}
471+
472+
// There is a (potentially empty) `package` directive ...
458473
if (package.empty()) {
474+
// Explicitly empty package (package;). Only empty_or_no_package applies.
459475
ctx.report(
460476
program,
461-
validation_to_diagnostic_level(ctx.sema_parameters().missing_package),
462-
"Thrift file should have a (non-empty) package. Packages will soon be "
463-
"required, at which point missing packages will trigger a Thrift compiler error. "
464-
"For more details, see https://fburl.com/thrift-uri-add-package");
477+
validation_to_diagnostic_level(
478+
ctx.sema_parameters().empty_or_no_package),
479+
"Thrift file should have a (non-empty) package. This will soon become "
480+
"an error (see https://fburl.com/thrift-uri-add-package).");
465481
return;
466482
}
483+
484+
// There is a non-empty `package ...` directive ...
467485
try {
468486
thrift::detail::check_univeral_name_domain(package.domain());
469487
} catch (const std::exception& e) {

third-party/thrift/src/thrift/compiler/test/compiler_test.cc

Lines changed: 99 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ TEST(CompilerTest, missing_uris_annotated_types) {
16411641
include "thrift/annotation/thrift.thrift"
16421642
16431643
package;
1644-
# expected-warning@-1: Thrift file should have a (non-empty) package. Packages will soon be required, at which point missing packages will trigger a Thrift compiler error. For more details, see https://fburl.com/thrift-uri-add-package
1644+
# expected-warning@-1: Thrift file should have a (non-empty) package. This will soon become an error (see https://fburl.com/thrift-uri-add-package).
16451645
16461646
// Valid: struct with URI
16471647
@thrift.Uri{value = "facebook.com/thrift/test/ValidStruct"}
@@ -1702,7 +1702,7 @@ TEST(CompilerTest, missing_uris_annotated_package) {
17021702
17031703
@thrift.AllowLegacyMissingUris
17041704
package;
1705-
# expected-warning@-2: Thrift file should have a (non-empty) package. Packages will soon be required, at which point missing packages will trigger a Thrift compiler error. For more details, see https://fburl.com/thrift-uri-add-package
1705+
# expected-warning@-2: Thrift file should have a (non-empty) package. This will soon become an error (see https://fburl.com/thrift-uri-add-package).
17061706
17071707
// Valid: struct with explicit URI
17081708
@thrift.Uri{value = "facebook.com/thrift/test/ValidStruct"}
@@ -1747,7 +1747,7 @@ TEST(CompilerTest, thrift_missing_uris_unnecessary_package_annotation) {
17471747
@thrift.AllowLegacyMissingUris
17481748
package;
17491749
# expected-error@-2: Unnecessary use of @thrift.AllowLegacyMissingUris at the package level: there are no types who are missing URIs in the file.
1750-
# expected-warning@-3: Thrift file should have a (non-empty) package. Packages will soon be required, at which point missing packages will trigger a Thrift compiler error. For more details, see https://fburl.com/thrift-uri-add-package
1750+
# expected-warning@-3: Thrift file should have a (non-empty) package. This will soon become an error (see https://fburl.com/thrift-uri-add-package).
17511751
17521752
// Struct has a URI, making the package annotation redundant.
17531753
@thrift.Uri{value = "facebook.com/thrift/test/ValidStruct"}
@@ -1766,7 +1766,7 @@ TEST(CompilerTest, thrift_missing_uris_unnecessary_package_annotation_none) {
17661766
17671767
@thrift.AllowLegacyMissingUris
17681768
package;
1769-
# expected-warning@-2: Thrift file should have a (non-empty) package. Packages will soon be required, at which point missing packages will trigger a Thrift compiler error. For more details, see https://fburl.com/thrift-uri-add-package
1769+
# expected-warning@-2: Thrift file should have a (non-empty) package. This will soon become an error (see https://fburl.com/thrift-uri-add-package).
17701770
17711771
// Struct has a URI, making the package annotation redundant.
17721772
@thrift.Uri{value = "facebook.com/thrift/test/ValidStruct"}
@@ -1785,7 +1785,7 @@ TEST(CompilerTest, thrift_missing_uris_redundant_package_and_type_annotation) {
17851785
17861786
@thrift.AllowLegacyMissingUris
17871787
package;
1788-
# expected-warning@-2: Thrift file should have a (non-empty) package. Packages will soon be required, at which point missing packages will trigger a Thrift compiler error. For more details, see https://fburl.com/thrift-uri-add-package
1788+
# expected-warning@-2: Thrift file should have a (non-empty) package. This will soon become an error (see https://fburl.com/thrift-uri-add-package).
17891789
17901790
// Annotation is redundant: package already has the annotation
17911791
@thrift.AllowLegacyMissingUris
@@ -1807,7 +1807,7 @@ TEST(
18071807
18081808
@thrift.AllowLegacyMissingUris
18091809
package;
1810-
# expected-warning@-2: Thrift file should have a (non-empty) package. Packages will soon be required, at which point missing packages will trigger a Thrift compiler error. For more details, see https://fburl.com/thrift-uri-add-package
1810+
# expected-warning@-2: Thrift file should have a (non-empty) package. This will soon become an error (see https://fburl.com/thrift-uri-add-package).
18111811
18121812
// Annotation is redundant: package already has the annotation
18131813
@thrift.AllowLegacyMissingUris
@@ -3045,7 +3045,7 @@ TEST(CompilerTest, base_service_defined_after_use) {
30453045
TEST(CompilerTest, cpp_orderable) {
30463046
check_compile(
30473047
R"(
3048-
# expected-warning@-1: Thrift file should have a (non-empty) package. Packages will soon be required, at which point missing packages will trigger a Thrift compiler error. For more details, see https://fburl.com/thrift-uri-add-package
3048+
# expected-warning@-1: Thrift file is missing a `package` directive. This will soon become an error (see https://fburl.com/thrift-uri-add-package).
30493049
include "thrift/annotation/cpp.thrift"
30503050
include "thrift/annotation/thrift.thrift"
30513051
@@ -3512,26 +3512,99 @@ TEST(CompilerTest, required_field_qualifier) {
35123512
{"--extra-validation", "required_field_qualifier=warn"});
35133513
}
35143514

3515-
TEST(CompilerTest, missing_package) {
3516-
static const char* kMissingPackage =
3517-
"Thrift file should have a (non-empty) package. Packages will soon be "
3518-
"required, at which point missing packages will trigger a Thrift compiler error. "
3519-
"For more details, see https://fburl.com/thrift-uri-add-package";
3515+
/**
3516+
* Tests the various validation behavior for missing package directives,
3517+
* depending on the `no_package` and `empty_or_no_package` validation flags.
3518+
*/
3519+
TEST(CompilerTest, no_package) {
3520+
// Missing directive, but no validation
3521+
check_compile(
3522+
R"(
3523+
struct TestStruct { }
3524+
)",
3525+
{"--extra-validation",
3526+
"empty_or_no_package=none,no_package=none,missing_uris=none"});
3527+
3528+
// Now with some validation. Note that the higher of `empty_or_no_package` and
3529+
// `no_package` takes effect.
35203530
check_compile(
3521-
"struct TestStruct { }",
3522-
{"--extra-validation", "missing_package=none,missing_uris=none"});
3531+
R"(
3532+
# expected-warning@-1: Thrift file is missing a `package` directive. This will soon become an error (see https://fburl.com/thrift-uri-add-package).
3533+
struct TestStruct { }
3534+
)",
3535+
{"--extra-validation",
3536+
"empty_or_no_package=warn,no_package=none,missing_uris=none"});
3537+
3538+
check_compile(
3539+
R"(
3540+
# expected-error@-1: Thrift file is missing a `package` directive. This will soon become an error (see https://fburl.com/thrift-uri-add-package).
3541+
struct TestStruct { }
3542+
)",
3543+
{"--extra-validation",
3544+
"empty_or_no_package=warn,no_package=error,missing_uris=none"});
3545+
}
3546+
3547+
/**
3548+
* Tests the various validation behavior for explicitly empty package
3549+
* directives, depending on the `no_package` and `empty_or_no_package`
3550+
* validation flags.
3551+
*/
3552+
TEST(CompilerTest, empty_package) {
3553+
// Empty package directive, but no validation
35233554
check_compile(
3524-
fmt::format(
3525-
"# expected-warning@1: {}\n"
3526-
"struct TestStruct {{ }}",
3527-
kMissingPackage),
3528-
{"--extra-validation", "missing_package=warn,missing_uris=none"});
3555+
R"(
3556+
package;
3557+
3558+
struct TestStruct { }
3559+
)",
3560+
{"--extra-validation",
3561+
"empty_or_no_package=none,no_package=none,missing_uris=none"});
3562+
3563+
// The no_package validation does not apply:
35293564
check_compile(
3530-
fmt::format(
3531-
"# expected-error@1: {}\n"
3532-
"struct TestStruct {{ }}",
3533-
kMissingPackage),
3534-
{"--extra-validation", "missing_package=error,missing_uris=none"});
3565+
R"(
3566+
package;
3567+
3568+
struct TestStruct { }
3569+
)",
3570+
{"--extra-validation",
3571+
"empty_or_no_package=none,no_package=error,missing_uris=none"});
3572+
3573+
// The empty_or_no_package validation DOES apply
3574+
check_compile(
3575+
R"(
3576+
package;
3577+
# expected-warning@-1: Thrift file should have a (non-empty) package. This will soon become an error (see https://fburl.com/thrift-uri-add-package).
3578+
struct TestStruct { }
3579+
)",
3580+
{"--extra-validation",
3581+
"empty_or_no_package=warn,no_package=error,missing_uris=none"});
3582+
}
3583+
3584+
/**
3585+
* Tests the various validation behavior for non-empty package directives,
3586+
* depending on the `no_package` and `empty_or_no_package` validation flags.
3587+
*/
3588+
TEST(CompilerTest, non_empty_package) {
3589+
// Non-empty package directive, but no validation
3590+
check_compile(
3591+
R"(
3592+
package "facebook.com/test"
3593+
3594+
struct TestStruct { }
3595+
)",
3596+
{"--extra-validation",
3597+
"empty_or_no_package=none,no_package=none,missing_uris=none"});
3598+
3599+
// Even with validation, no warnings/errors:
3600+
check_compile(
3601+
R"(
3602+
package "facebook.com/test"
3603+
3604+
struct TestStruct { }
3605+
)",
3606+
{"--extra-validation",
3607+
"empty_or_no_package=error,no_package=error,missing_uris=none"});
35353608
}
35363609

35373610
TEST(CompilerTest, bidirectional_streaming) {
@@ -4118,7 +4191,7 @@ TEST(CompilerTest, ignore_missing_uri_opt_out_basic) {
41184191
include "thrift/annotation/thrift.thrift"
41194192
41204193
package;
4121-
# expected-warning@-1: Thrift file should have a (non-empty) package. Packages will soon be required, at which point missing packages will trigger a Thrift compiler error. For more details, see https://fburl.com/thrift-uri-add-package
4194+
# expected-warning@-1: Thrift file should have a (non-empty) package. This will soon become an error (see https://fburl.com/thrift-uri-add-package).
41224195
41234196
// Valid: struct with URI
41244197
@thrift.Uri{value = "facebook.com/thrift/test/ValidStruct"}
@@ -4151,7 +4224,7 @@ TEST(CompilerTest, ignore_missing_uri_opt_out_with_allow_legacy) {
41514224
41524225
@thrift.AllowLegacyMissingUris
41534226
package;
4154-
# expected-warning@-2: Thrift file should have a (non-empty) package. Packages will soon be required, at which point missing packages will trigger a Thrift compiler error. For more details, see https://fburl.com/thrift-uri-add-package
4227+
# expected-warning@-2: Thrift file should have a (non-empty) package. This will soon become an error (see https://fburl.com/thrift-uri-add-package).
41554228
41564229
// Error: package-level AllowLegacyMissingUris does not suppress
41574230
struct PkgAnnotatedStruct { }

third-party/thrift/src/thrift/compiler/test/standard_validator_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ TEST(StandardValidatorTest, WarnProgramMissingPackage) {
272272
)");
273273

274274
check_compile(R"(
275-
# expected-warning@-1: Thrift file should have a (non-empty) package. Packages will soon be required, at which point missing packages will trigger a Thrift compiler error. For more details, see https://fburl.com/thrift-uri-add-package
275+
# expected-warning@-1: Thrift file is missing a `package` directive. This will soon become an error (see https://fburl.com/thrift-uri-add-package).
276276
)");
277277
}
278278

0 commit comments

Comments
 (0)