MegaLinter: Reduce your technical debt and secure your code base for free !

Nicolas Vuillamy
6 min readMar 20, 2022

Using linters on your projects will provide the following benefits:

  • Reduce the number of bugs , security flaws and performance issues
  • Improve coding skills of every member of your team
  • Ensure a unique code style for your sources, so it is more easily understandable
  • Reduce the technical debt, so ensure lower maintenance charges for your applications in the future

There are linters existing for practically every language, format or configuration files, and most of them are open-source.

See full article at https://promyze.com/open-source-linters-2021/

You can learn more about linters on the following web sites and articles:

Strategies

* Pricing for private and enterprise usage

There are different strategies to use linters on your projects:

I chose on all my projects to use IDE linter plugins + MegaLinter, and this tutorial will describe how to setup such strategy on a Github repository using Github Actions as CI (the instructions are applicable to other git and CI providers)

Note: such strategy is opiniotated, there are pros and cons for all strategies, that can depend of the team experience, the available budget, the project size…

What is MegaLinter ?

MegaLinter is an 100% Open-Source tool for CI/CD workflows that analyzes consistency and quality of 49 languages, 22 formats, 21 tooling formats , excessive copy-pastes and spelling mistakes in your repository sources, generates various reports, and can even apply formatting and auto-fixes, to ensure all your projects sources are clean, whatever IDE/toolbox are used by their developers.

Ready to use out of the box as a GitHub Action or any CI system, highly configurable and free for all uses

MegaLinter is now used all around the world (source: Google analytics for documentation pages)

MegaLinter Setup

MegaLinter provides a command-line installer that will allow you to automatically create configuration and CI job files allowing to run a code analysis at every pull request, or even everytime a new commit is pushed in a repository.

Pre-requisite: you need to have node.js installed on your computer.

  • Clone your repository
  • Create a new git branch that will be used for definition
  • Run command npx mega-linter-runner --install and follow the instructions.
  • If you decide to automatically apply fixes, add the following item in your .gitignore file: report/
  • Commit the updated files, push your branch then create a Pull Request

Configure MegaLinter

The first MegaLinter job should fail and display errors in a post on the Pull Request page.

Don’t panic, you don’t have to fix all the issues: for each of them, you have the choice between:

  • Solve the linter error
  • Configure the linter to ignore some errors
  • Exclude files from linting
  • Disable the linter
  • Define all errors from this linter as non-blocking
  • Define a maximum number of errors allowed for a linter

Note: you’ll need linter keys that can be found in documentation in column Configuration key

Solve a linter error

You can see the linter errors details by viewing CI job artifacts.

Just update your code to solve the error, commit and push to trigger a new MegaLinter analysis job :)

Configure a linter to ignore some errors

Each linter has a page in MegaLinter documentation , and contains info about:

  • How to configure the linter using its configuration file
  • MegaLinter variable names that you can use to override the default linter call built and run by MegaLinter

Some linters like cspell provide a fixed configuration file in artifacts, that you can copy-paste in your local files.

Get .cspell.json from MegaLinter artifact

Ignore files

Files can be ignored using RegExp patterns defined in .mega-linter.ymlvariables:

  • FILTER_REGEX_EXCLUDE
  • <linter-key>_FILTER_REGEX_EXCLUDE

You can easily build regular expressions using https://regex101.com/

Disable a linter

You can disable a linter by adding its linter key in property DISABLE_LINTERSin .mega-linter.yml

Define a linter as non-blocking

Each linter can be defined as non-blocking in .mega-linter.yml`configuration file using variable

<linter-key>_DISABLE_ERRORS: true

Define a maximum number of errors allowed for a linter

If your existing code base is big, you may want to avoid to solve the existing linter errors, but prevent from adding new errors.

In that case, you can define a maximum number of allowed errors for a linter using <linter-key>_DISABLE_ERRORS_IF_LESS_THAN: <number>.

Use a MegaLinter flavor

If a flavor is suggested, to improve performances you can update file .github/workflows/mega-linter.ymlto use theMegaLinter Flavor Github Action given in the documentation.

Bonus: Automatically apply fixes

When there are no blocking errors, if you chose to automatically apply fixes, you can see that MegaLinter will add a new commit with fixes.

Bonus: IDE Configuration

You can update your repository VsCode recommended extensions to benefit from linter checks with the same configuration than MegaLinter, while editing your source code.

Download MegaLinter artifact, and just copy-paste IDE-config folder content in your repository: it contains recommended VsCode extensions file and linter default configuration files

You’re all set !

Now your repository sources are clean and secure, you can merge your Pull Request !

Advanced usage

MegaLinter is highly configurable, you can also check the following chapters in the documentation

What’s next ?

MegaLinter v6 is currently being implemented, and will have a big focus on Security linters, and a lot of other surprises, stay tuned !

--

--

Open-Source addict and CTO at Cloudity. Creator of MegaLinter, npm-groovy-lint & sfdx-hardis