# import-no-cycle

Source: https://api-genie.dev/docs/linter/protobuf/rules/import-no-cycle

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 error, Strict at error, All at error.

This rule checks whether Protobuf files import each other in a cycle.

## Rule details

`protoc` rejects files that import themselves through a chain of imports, so such files cannot be
compiled at all.

For every import of the linted file, the `import-no-cycle` rule follows the imports of the
imported file, and of the files it imports, looking for a path back to the linted file. If there
is one, it reports:

`Import '<import>' creates an import cycle: <file name> -> <import> -> ... -> <import>`

A file that imports itself is reported with `Import '<import>' creates an import cycle: the file
imports itself`.

The cycle starts with the name of the linted file, followed by the imports that lead back to it,
as they are written in the import statements. Every file of the cycle that is linted reports its
own import.

Imports that cannot be found are ignored (see `import-resolved`). Files linted without a path are
not checked.

The rule reads the imported files, found in the import paths and in the directories above the
importing file.

## Examples

**Good**. `acme/pets/v1/pet.proto` imports `acme/pets/v1/owner.proto`, which imports nothing
from the package.

```proto
syntax = "proto3";

package acme.pets.v1;

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

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

**Bad**. The same `acme/pets/v1/pet.proto`, while `acme/pets/v1/owner.proto` imports it back:

```proto
syntax = "proto3";

package acme.pets.v1;

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

message Owner {
  string name = 1;
  repeated Pet pets = 2;
}
```

The linter reports, in `pet.proto`:

`Import 'acme/pets/v1/owner.proto' creates an import cycle: pet.proto -> acme/pets/v1/owner.proto -> acme/pets/v1/pet.proto`

and in `owner.proto`:

`Import 'acme/pets/v1/pet.proto' creates an import cycle: owner.proto -> acme/pets/v1/pet.proto -> acme/pets/v1/owner.proto`
