koalaman / shellcheck

ShellCheck, a static analysis tool for shell scripts
https://www.shellcheck.net
GNU General Public License v3.0
36.45k stars 1.78k forks source link

Feature request: per-source `check-sourced` directive #2956

Open ShamrockLee opened 7 months ago

ShamrockLee commented 7 months ago

For bugs

For new checks and feature suggestions

Here's a snippet or screenshot that shows the problem:

#!/usr/bin/env bash

# I don't want to check this one.
# shellcheck check-sourced=false
source messy.sh 

# I would like to have this checked.
# shellcheck check-sourced=true
source clean.sh

# The rest of the script goes here
echo "Hello, world!"

Here's what shellcheck currently says:

ShellCheck currently doesn't provide such directive.


In temp.sh line 4:
# shellcheck check-sourced=false
             ^-- SC1107 (warning): This directive is unknown. It will be ignored

In temp.sh line 8:
# shellcheck check-sourced=true
             ^-- SC1107 (warning): This directive is unknown. It will be ignored

Here's what I wanted or expected to see:

ShellCheck includes warning from clean.sh but not from messy.sh.

Motivation:

  1. Developers sometimes want to include warnings from some sourced files but not from others. This is especially valid if messy.sh cannot be changed from the project scope.
  2. This feature provide a workaround for #356, by allowing developers to define a meta-script that sources the pre-sourced files before sourcing the target script to check. The directive proposed here helps exclude warnings from pre-sourced files, while include warnings from the target script.
ConnorBaker commented 5 months ago

While not satisfactory, I have found this at least useful while working on Nixpkgs:

# shellcheck source=<relative path to pkgs/stdenv/generic/setup.sh>
source /dev/null
ShamrockLee commented 5 months ago

While not satisfactory, I have found this at least useful while working on Nixpkgs:

# shellcheck source=<relative path to pkgs/stdenv/generic/setup.sh>
source /dev/null

What an excellent workaround for #356!

Per-source check-sourced would still be helpful when refactoring a codebase filled with un-lint scripts or excluding the warnings from an external shell library.