warpnet / salt-lint

A command-line utility that checks for best practices in SaltStack.
https://salt-lint.readthedocs.io/en/latest/
MIT License
152 stars 39 forks source link

Feature Request: Identify unused imports #51

Open myii opened 4 years ago

myii commented 4 years ago

Taking this example:

{% from "zabbix/macros.jinja" import files_switch with context -%}

I've actually come across many of these examples throughout SaltStack Formulas. Would be great if salt-lint could catch these.

myii commented 4 years ago

Note, this may not be that simple due to uses of import as.

kubernetes-formula/kubernetes/kubectl/config/environ/install.sls:

{%- from tplroot ~ "/map.jinja" import kubernetes as k8s with context %}

prometheus-formula/prometheus/archive/install.sls:

{%- from tplroot ~ "/map.jinja" import prometheus as p with context %}
myii commented 4 years ago

A similar issue is unused Jinja variables.

lighttpd-formula/lighttpd/config.sls@4fca60f

{% set file = lighttpd.get('config', ['lighttpd.conf']) %}

file is no longer being used anywhere after being set. It used to be, in an earlier version of the file:

lighttpd-formula/lighttpd/config.sls@e69c9a0

    - source: {{ files_switch('lighttpd', ['/etc/lighttpd/' ~ file,
                                           '/etc/lighttpd/' ~ file ~ '.jinja']) }}

It would be great if salt-lint could catch these as well.

roaldnefs commented 4 years ago

Thanks @myii for the feature request!

This requires salt-lint on (partially) rendering the state, or some logic that can tokenize the Jinja part. The last might actually be easier, but requires writing a tokenizer. Because this only related to the Jinja part, I'm first going to try to fix #19 and #48.

myii commented 4 years ago

... I'm first going to try to fix #19 and #48.

@roaldnefs Great, thanks for all of your hard work.