Linter rulesProtobufpackage-same-directory

package-same-directory

Since 0.9.0

This rule checks whether all the files of a package are in the same directory.

Rule Details

A package spread over several directories is hard to find, and code generators that map directories to packages of the generated code, like the Go one, split it into several packages that cannot see each other.

The package-same-directory rule looks for other files that declare the same package as the linted file but are in another directory. If there are some, it reports:

Package '<package>' is also declared in other directories: <directories>

where <directories> lists each other directory once, as '<directory>' (<file name>) with the absolute path of the directory and the name of one of its files. The issue is reported at the package declaration.

The rule can only compare the files the linter knows about: the files being linted, the other .proto files in their directories, and the files these import, directly or indirectly. A file of the package that none of them reaches is not seen. Files that cannot be read, for example because of syntax errors, or because they are proto2 or editions files, are ignored.

Files without a package are not checked.

Configuration

This rule has no options.

Examples

Good. The files of acme.pets.v1 are all in acme/pets/v1. acme/pets/v1/pet.proto:

syntax = "proto3";

package acme.pets.v1;

import "acme/pets/v1/owner.proto";

message Pet {
  string name = 1;
  Owner owner = 2;
}

and acme/pets/v1/owner.proto:

syntax = "proto3";

package acme.pets.v1;

message Owner {
  string name = 1;
}

Bad. The same pet.proto imports acme/pets/v1/owners/owner.proto, which declares the same package in a subdirectory:

syntax = "proto3";

package acme.pets.v1;

import "acme/pets/v1/owners/owner.proto";

message Pet {
  string name = 1;
  Owner owner = 2;
}

With the project in /work, the linter reports, in pet.proto:

Package 'acme.pets.v1' is also declared in other directories: '/work/acme/pets/v1/owners' (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 split large packages into several directories.

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 files of the package: the other files in the directories of the linted files, and the imported files, found in the import paths and in the directories above the importing file.

Related rules