Linter rulesProtobufimport-resolved

import-resolved

Since 0.9.0

This rule checks whether every file imported by a Protobuf file can be found.

Rule Details

An import that cannot be found breaks the compilation of the file. It also keeps the linter from checking the rules that need the imported declarations: import-used, for example, stays silent for a file whose names it cannot resolve.

The import-resolved rule looks up every import the way the linter does for all rules:

  1. in the import paths, in order: first those given with --proto-path, then those of the import_paths key of the configuration (relative to the directory of the configuration file);
  2. then in the directory of the importing file and in each directory above it, nearest first.

The well-known files of the Protobuf distribution (google/protobuf/timestamp.proto, google/protobuf/empty.proto and the other files of google/protobuf) are known built in, so they are always found, even when they are not on the import paths.

For every import that is not found, the rule reports:

Import '<import>' is not found in the import paths or the directories above the file

A file that is found but cannot be read, for example because of syntax errors, or because it is a proto2 or editions file, counts as found.

To add an import path in the configuration:

version: "1.0"
commands:
  lint:
    protobuf:
      import_paths: [proto, third_party/googleapis]

Configuration

This rule has no options.

Examples

Good. The file acme/pets/v1/pet.proto, next to acme/pets/v1/owner.proto:

syntax = "proto3";

package acme.pets.v1;

import "acme/pets/v1/owner.proto";
import "google/protobuf/timestamp.proto";

message Pet {
  string name = 1;
  Owner owner = 2;
  google.protobuf.Timestamp born_at = 3;
}

Bad. No acme/pets/v1/kind.proto exists in the import paths or in the directories above the file. The linter reports:

Import 'acme/pets/v1/kind.proto' is not found in the import paths or the directories above the file

syntax = "proto3";

package acme.pets.v1;

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

message Pet {
  string name = 1;
  Kind kind = 2;
}

When Not to Use It

You may disable this rule when the imported files are generated or fetched during the build, and are not available where the linter runs. Consider adding their location to import_paths instead.

Compatibility

This rule is compatible with proto3 files. proto2 and editions files are rejected by the reader and are not linted. The rule looks for the imported files in the import paths and in the directories above the importing file.

Related rules