yoheimuta / protolint

A pluggable linter and fixer to enforce Protocol Buffer style and conventions.
MIT License
577 stars 52 forks source link

Protolint doesn't work with -plugin in windows #144

Open rost5000 opened 3 years ago

rost5000 commented 3 years ago

I have a following proto file:

syntax = "proto3";
// A broken example of the official reference
// See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#proto_file
package examplePb;

option java_package = "com.example.foo";

import "other.proto";
import public "new.proto";

import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";

import "myproject/other_protos.proto";
import "myproject/main_protos.proto";

enum enumAllowingAlias {
  option allow_alias = true;
  UNKNOWN = 0;
  STARTED = 1;
  RUNNING = 2 [(custom_option) = "hello world"];
}
message outer {
  option (my_option).a = true;
  // inner is an inner message.
  message innerAAA {   // Level 2
    int64 ival = 1;
  }
  repeated inner inner_message = 2;
  EnumAllowingAlias enum_field =3;
  map<int32, string> my_map = 4;
  string reason_for_error = 5;
  string  end_of_support_version= 6;
  message AccountForAdmin {}
  message SpecialEndOfSupport {}
  required inner inner_message = 7;
  group Result = 8 {
    string url = 9;
  }
  repeated group Result = 10 {
  }
  repeated inner paper = 11;
  repeated group Regular = 12 {
  }
}
service SearchApi {
  rpc search (SearchRequest) returns (SearchResponse) {};
};

So I create a custom linter rule custumrule/enumrule.go:

package custumrule

import (
    "github.com/yoheimuta/go-protoparser/v4/parser"
    "github.com/yoheimuta/protolint/linter/report"
    "github.com/yoheimuta/protolint/linter/strs"
    "github.com/yoheimuta/protolint/linter/visitor"
)

// EnumNamesLowerSnakeCaseRule verifies that all enum names are LowerSnakeCase.
type EnumNamesLowerSnakeCaseRule struct{}

// NewEnumNamesLowerSnakeCaseRule creates a new EnumNamesLowerSnakeCaseRule.
func NewEnumNamesLowerSnakeCaseRule() EnumNamesLowerSnakeCaseRule {
    return EnumNamesLowerSnakeCaseRule{}
}

// ID returns the ID of this rule.
func (r EnumNamesLowerSnakeCaseRule) ID() string {
    return "ENUM_NAMES_LOWER_SNAKE_CASE"
}

// Purpose returns the purpose of this rule.
func (r EnumNamesLowerSnakeCaseRule) Purpose() string {
    return "Verifies that all enum names are LowerSnakeCase."
}

// IsOfficial decides whether or not this rule belongs to the official guide.
func (r EnumNamesLowerSnakeCaseRule) IsOfficial() bool {
    return true
}

// Apply applies the rule to the proto.
func (r EnumNamesLowerSnakeCaseRule) Apply(proto *parser.Proto) ([]report.Failure, error) {
    v := &enumNamesLowerSnakeCaseVisitor{
        BaseAddVisitor: visitor.NewBaseAddVisitor(r.ID()),
    }
    return visitor.RunVisitor(v, proto, r.ID())
}

type enumNamesLowerSnakeCaseVisitor struct {
    *visitor.BaseAddVisitor
}

// VisitEnum checks the enum field.
func (v *enumNamesLowerSnakeCaseVisitor) VisitEnum(e *parser.Enum) bool {
    if !strs.IsLowerSnakeCase(e.EnumName) {
        v.AddFailuref(e.Meta.Pos, "Enum name %q must be underscore_separated_names", e.EnumName)
    }
    return false
}

and main.go:

package main

import (
    "example.com/m/custumrule"
    "github.com/yoheimuta/protolint/plugin"
)

func main() {
    plugin.RegisterCustomRules(
        custumrule.NewEnumNamesLowerSnakeCaseRule(),
    )
}

I build project on windows and Linux subsystem by go buil main.go and try to run the command: protolint -plugin ./main.exe .. However, I receive failed client.Client(), err=exec: "sh": executable file not found in %PATH%

yoheimuta commented 3 years ago

@rost5000 Thank you for your information. It happens because the plugin depends on sh.

https://github.com/yoheimuta/protolint/blob/4a1b88feba6c0e9e2bf826f79cf1eda4b2a7effa/internal/cmd/subcmds/pluginFlag.go#L43

I'm going to remove this dependency.

yoheimuta commented 3 years ago

@rost5000 Can you try adding sh in your PATH? Because simply giving up sh -c seems to be not a solution. See https://github.com/yoheimuta/protolint/pull/145#discussion_r559080215.

Ref: