protolint: indentRule causing runtime error when the file structure needs to be re-ordered

When using protolint to lint some files, I realized that when the file needs to be re-ordered, runtime error will occur. For example, if the “import” statement comes before “package” statement as below:

syntax = "proto3";
import "template.proto"
package helloworld;
option java_package = "com.helloworld.foo"

It will show the following error, when you run protolint.

panic: runtime error: index out of range [54] with length 51

goroutine 1 [running]:
github.com/yoheimuta/protolint/internal/addon/rules.indentVisitor.validateIndent({0xc000526000, {0xc74c28, 0x2}, 0x0, 0x1, 0x0, 0xc000520060}, {{0xc0002ae28b, 0xc}, 0x543, ...}, ...)
        /home/runner/work/protolint/protolint/internal/addon/rules/indentRule.go:304 +0x505
github.com/yoheimuta/protolint/internal/addon/rules.indentVisitor.validateIndentLeading(...)
        /home/runner/work/protolint/protolint/internal/addon/rules/indentRule.go:291
github.com/yoheimuta/protolint/internal/addon/rules.indentVisitor.VisitService({0xc000526000, {0xc74c28, 0x2}, 0x0, 0x1, 0x0, 0xc000520060}, 0xc00029f5e0)
        /home/runner/work/protolint/protolint/internal/addon/rules/indentRule.go:267 +0x13c
github.com/yoheimuta/protolint/linter/visitor.extendedDisableRuleVisitor.VisitService({{0xd51128, 0xc000520090}, 0xc000528018}, 0xc00029f5e0)
        /home/runner/work/protolint/protolint/linter/visitor/extendedDisableRuleVisitor.go:150 +0xa8
github.com/yoheimuta/go-protoparser/v4/parser.(*Service).Accept(0xc00029f5e0, {0xd4f6d0, 0xc000528030})
        /home/runner/go/pkg/mod/github.com/yoheimuta/go-protoparser/v4@v4.6.0/parser/service.go:83 +0x39
github.com/yoheimuta/go-protoparser/v4/parser.(*Proto).Accept(0xc000464a50, {0xd4f6d0, 0xc000528030})
        /home/runner/go/pkg/mod/github.com/yoheimuta/go-protoparser/v4@v4.6.0/parser/proto.go:26 +0x99
github.com/yoheimuta/protolint/linter/visitor.RunVisitor({0xd51128, 0xc000520090}, 0xc000284558, {0xc76db4, 0x6})
        /home/runner/work/protolint/protolint/linter/visitor/hasExtendedVisitor.go:34 +0xca
github.com/yoheimuta/protolint/internal/addon/rules.IndentRule.Apply({{0xc74c28, 0xb2884f}, 0xc, 0x0}, 0xc00031e400)
        /home/runner/work/protolint/protolint/internal/addon/rules/indentRule.go:75 +0x117
github.com/yoheimuta/protolint/internal/linter.(*Linter).Run(0x0, 0xc000515970, {0xc000323800, 0x12, 0x0})
        /home/runner/work/protolint/protolint/internal/linter/linter.go:33 +0xf3
github.com/yoheimuta/protolint/internal/cmd/subcmds/lint.(*CmdLint).runOneFile(0xc0002a2f00, {{0xc0002ae280, 0x17}, {0xc0002ae28b, 0xc}})
        /home/runner/work/protolint/protolint/internal/cmd/subcmds/lint/cmdLint.go:138 +0x16e
github.com/yoheimuta/protolint/internal/cmd/subcmds/lint.(*CmdLint).run(0xc0002a2f00)
        /home/runner/work/protolint/protolint/internal/cmd/subcmds/lint/cmdLint.go:107 +0xb8
github.com/yoheimuta/protolint/internal/cmd/subcmds/lint.(*CmdLint).Run(0xc0002a2f00)
        /home/runner/work/protolint/protolint/internal/cmd/subcmds/lint/cmdLint.go:84 +0x4d
github.com/yoheimuta/protolint/internal/cmd.doLint({0xc0000380a0, 0x0, 0x11514a0}, {0xd32260, 0xc000006018}, {0xd32260, 0xc000006020})
        /home/runner/work/protolint/protolint/internal/cmd/cmd.go:110 +0x2df
github.com/yoheimuta/protolint/internal/cmd.doSub({0xc000038090, 0x21ce9909878, 0x0}, {0xd32260, 0xc000006018}, {0xd32260, 0xc000006020})
        /home/runner/work/protolint/protolint/internal/cmd/cmd.go:64 +0xb2
github.com/yoheimuta/protolint/internal/cmd.Do({0xc000038090, 0x60, 0x0}, {0xd32260, 0xc000006018}, {0xd32260, 0xc000006020})
        /home/runner/work/protolint/protolint/internal/cmd/cmd.go:49 +0x86
main.main()
        /home/runner/work/protolint/protolint/cmd/protolint/main.go:11 +0x6c

I have tested with the example code from https://github.com/protocolbuffers/protobuf/blob/main/examples/addressbook.proto (with switching around the order of “import” statement and “package” statement), I still get the similar runtime error. It seems to be the INDENT rule problem because if I disable the INDENT rule, the protolint will be able to run as expected. However, in that case, I won’t be able to get any reports about the violations on indentation. Is it possible to fix it? Thank you!

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (9 by maintainers)

Most upvoted comments

Hi @yoheimuta , it works well! Really appreciate it! Thank you very much! 😄