# package-same-directory

Source: https://api-genie.dev/docs/linter/protobuf/rules/package-same-directory

A rule of the Protobuf linter, which reads proto3 files; the reader rejects proto2 and editions files. Since 0.9.0. In the rulesets: Recommended at warning, Strict at error, All at error.

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.

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.

Turn the rule off if your build tools and code generators do not map directories to packages, and
you deliberately split large packages into several directories.

## Examples

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

```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`:

```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:

```proto
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)`
