import-resolved

Since 0.9.0 · In the rulesets: Recommended at warning, Strict at error, All at error

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]

The rule looks for the imported files in the import paths and in the directories above the importing file.

Turn the rule off 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.

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;
}

Turn it off or change its severity

In apigenie.yaml, off turns the rule off and warning or error sets its severity; a rule with options takes the severity and the options as a pair, [warning, options]. lint.protobuf has the details.

commands:
  lint:
    protobuf:
      rules:
        import-resolved: off

Related rules

Was this helpful?