# import-no-cycle

Source: https://apigenie.pl/docs/linter/protobuf/rules/import-no-cycle

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.

## Configuration

This rule has no options.

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

## When Not to Use It

We recommend to always enable this rule.

## 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 imported files, found in the import paths and in the
directories above the importing file.
