Logo

dev-resources.site

for different kinds of informations.

Linting Proto Files With Buf

Published at
5/28/2023
Categories
protobuf
linting
buf
tooling
Author
themayurkumbhar
Categories
4 categories in total
protobuf
open
linting
open
buf
open
tooling
open
Author
15 person written this
themayurkumbhar
open
Linting Proto Files With Buf

Recently, I had the chance to work on the proto files for one of my projects to define the contract for a service. Since this was a brand-new service, I had to set it up from scratch and learned how to write and format the proto files using best practices.

What is proto? read more about protocol bufferes

There are different tools available for linting the proto files some of them are

  1. protoc-gen-lint
  2. prototool
  3. buf - we have used the buf as a proto linter. Follow this for installation of buf.

What is buf?

The buf CLI is a tool for working with Protocol Buffers. It has a variety of supported functions such as lint, breaking changes detection, formatter, etc.

Format your protos

To improve readability and have standard formatting enabled across teams, buf provides the auto-formatting on proto files. This enables the files to be formatted correctly and with following the standards.

Use the following command to format your files.

buf format
Enter fullscreen mode Exit fullscreen mode

This will produce any violations in formatting to the console.

To auto-format the files in-place with the difference in file changes, use the below command.

buf format -w --diff
Enter fullscreen mode Exit fullscreen mode

Detect code style checks

*Linter *- A static code analysis used to flag programming errors, bugs, stylistic errors, and suspicious constructs.

For proto files, itโ€™s no different, to verify the new changes adhere to proto standards use the buf linter.

buf lint /path/to/proto/files/
Enter fullscreen mode Exit fullscreen mode

This will produce any errors, warning to console if there are any violations in writing proto files. You can analyze and update the protofiles according to standard rules.

Automatically lint with GitLab CI pipeline

Whenever there is a change in your repository, your CI pipeline will automatically run buf lint to check for any violations.

To check add the below code snippet to the .gitlab-ci.yml

stages:
  - lint

lint:
  stage: lint
  image: 
    name: bufbuild/buf:1.7.0
    entrypoint: [""]
  script:
    - buf lint

Enter fullscreen mode Exit fullscreen mode

buf configuration rules

buf supports three categories of rule configurations.

  1. MINIMAL
  2. BASIC
  3. DEFAULT By default, buf has DEFAULT linting configuration which has below details.
version: v1
lint:
  use:
    - DEFAULT
breaking:
  use:
    - FILE

Enter fullscreen mode Exit fullscreen mode

Use buf mod init to generate DEFAUTL configuration rules for your project.

You can update this file with different rules as per the requirements.

 version: v1
 lint:
   use:
     - DEFAULT
   except:
     - PACKAGE_VERSION_SUFFIX
     - FIELD_LOWER_SNAKE_CASE
     - SERVICE_SUFFIX
   ignore:
     - google/type/datetime.proto
 breaking:
   use:
     - FILE

Enter fullscreen mode Exit fullscreen mode

Hope this article gave understanding of how to use buf tool for formatting, linting your proto files.
Happy learning!

linting Article's
30 articles in total
Favicon
How to Configure VSCode for Auto Formatting and Linting in Python
Favicon
Level Up Your OpenAPI Specs using Linting
Favicon
Most elemental code formatting for Typescript/Javascript: .editorconfig vs prettier
Favicon
standardrb, but more
Favicon
Introducing Japr - The Project Linter
Favicon
Level Up Your TypeScript Projects: Discover the Power of ESLint and Prettier
Favicon
Interview with Greg Molnar - Rails developer and penetration tester
Favicon
Setting WordPress Coding Standards per project using Composer
Favicon
Linting Proto Files With Buf
Favicon
ESLint: The Hows, Whys, and Who Behind It
Favicon
Puppet-lint: Soft dependency conflicts after updating
Favicon
Eslint rule to restrict imports
Favicon
Laravel Tooling: 4 tools for static analysis
Favicon
Terraform: Variable validation with samples
Favicon
An Incremental Approach to Linting to Your Projects
Favicon
Add EsLint to existing Angular Project and Configure WebStorm
Favicon
Quick ESLint guide + VsCode ESLint on save
Favicon
Chickity-check yo self before you wreck yo self!
Favicon
How to configure Eslint in Gatsby projects
Favicon
Automatically sorting your Tailwind CSS class names
Favicon
Setting up ESLint + Prettier
Favicon
Incrementally adding Stylelint rules with Betterer
Favicon
Conventional Git Commit Messages and Linting
Favicon
ยฟQuรฉ es Linting y ESLint?
Favicon
My 2020 Python linting setup: How I Learned to Stop Worrying and Love the automated code formatting
Favicon
ESLint not working in VSCode? Help build a troubleshooting checklist!
Favicon
SERIES: React Native (Step by Step) - Working with Typescript and Linting
Favicon
Adding Markdown Linting to my Blogโ€™s Build Process with GitHub Actions and markdownlint
Favicon
TIL - ๐Ÿงน unimport linter formatter
Favicon
Adding New Lint Rules Without the Fuss

Featured ones: