directory-same-package

Since 0.9.0 · In the rulesets: Recommended at error, Strict at error, All at error

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.

The rule reads the other .proto files in the directory of the linted file.

Turn the rule off if your build tools and code generators do not map directories to packages, and you deliberately keep several packages in one directory.

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.

Turn it off or change its severity

In apigenie.yaml, off turns the rule off and warning or error sets its severity; a rule with options takes the severity and the options as a pair, [warning, options]. lint.protobuf has the details.

commands:
  lint:
    protobuf:
      rules:
        directory-same-package: off

Related rules

Was this helpful?