Linter rulesProtobufdirectory-same-package

directory-same-package

Since 0.9.0

This rule checks whether all the .proto files of a directory declare the same package.

Rule Details

Many code generators, for example the Go one, turn a directory into a single package of the generated code. Files of different Protobuf packages in one directory then produce code that does not compile, and they make the layout of the API harder to follow.

The directory-same-package rule compares the package of the linted file with the packages of the other .proto files in the same directory (subdirectories are not included). If any of them declares another package, or no package while the linted file has one (or the other way round), the rule reports:

Other files of the directory declare other packages: <files>

where <files> lists each such file as '<package>' (<file name>), or as no package (<file name>) when the file has no package. The issue is reported at the package declaration, or at the syntax statement of a file without a package.

Files of the directory that cannot be read, for example because of syntax errors, or because they are proto2 or editions files, are ignored.

Configuration

This rule has no options.

Examples

Good. The directory acme/pets/v1 contains pet.proto:

syntax = "proto3";

package acme.pets.v1;

message Pet {
  string name = 1;
}

and owner.proto:

syntax = "proto3";

package acme.pets.v1;

message Owner {
  string name = 1;
}

Bad. The directory acme/pets/v1 contains the same pet.proto as above, and this owner.proto:

syntax = "proto3";

package acme.owners.v1;

message Owner {
  string name = 1;
}

The linter reports Other files of the directory declare other packages: 'acme.owners.v1' (owner.proto) in pet.proto, and Other files of the directory declare other packages: 'acme.pets.v1' (pet.proto) in owner.proto.

When Not to Use It

You may disable this rule if your build tools and code generators do not map directories to packages, and you deliberately keep several packages in one directory.

Compatibility

This rule is compatible with proto3 files. proto2 and editions files are rejected by the reader and are not linted. The rule reads the other .proto files in the directory of the linted file.

Related rules