Skip to content

ci(lint): lint shell, Python and PowerShell scripts in CI - #129

Draft
siloteemu wants to merge 2 commits into
ROCm:mainfrom
siloteemu:ci/lint-all-scripts
Draft

siloteemu wants to merge 2 commits into
ROCm:mainfrom
siloteemu:ci/lint-all-scripts

Conversation

@siloteemu

Copy link
Copy Markdown

Motivation

  • CI lints Rust only (cargo fmt, and clippy with warnings as errors). The repository also holds 27 shell scripts, 15 Python files and 10 PowerShell scripts outside third_party, and a mistake in any of them can merge unchecked. The only existing Python check is bandit in security.yml, which scans a hard-coded list of 6 files.
  • Findings that were already in the tree show the gap: unused imports and variables, a PowerShell function with a non-approved verb, and Python exceptions raised inside an except block without saying whether the cause is kept.
  • This is a proposal for the AXIS team to say whether the check is wanted and in what shape. It edits .github/workflows/ci.yml, AGENTS.md and DEVELOPMENT.md, and AGENTS.md lists editing .github/workflows/ under "Ask first", so it is offered for the maintainers to accept or reject. No maintainer has agreed to it yet.

Technical Details

Three steps are appended to the existing Format job in .github/workflows/ci.yml: Lint shell scripts, Lint Python and Lint PowerShell. There is no new job, and the job name, runner, timeout and permissions are unchanged. The job therefore stays in the CI publication gate, and no change to scripts/verify_workflow_structure.py or scripts/release_tools.py was needed. No release, nightly or GUI release job changed, so no pinned digest changed.

How the tools are installed:

  • ShellCheck (the shellcheck-py wheel) and ruff come from the new hash-locked .github/lint-requirements.in and .github/lint-requirements.txt, compiled with uv pip compile in the same style as security-requirements.txt. They are installed with --only-binary=:all: --require-hashes under actions/setup-python at the commit SHA that security.yml and the workflow verifier already pin.
  • PSScriptAnalyzer 1.25.0 is downloaded from the PowerShell Gallery with curl (https only, TLS 1.2 or later, retries, a maximum time) and checked against a pinned SHA-256 before it is extracted and imported.
  • Dependabot: the existing pip entry for /.github covers the new requirements file, in its security-tools group, so dependabot.yml is unchanged.

Rule selection is explicit, because default rule sets change between releases:

  • ruff.toml selects E4, E7, E9, F and B.
  • PSScriptAnalyzerSettings.psd1 fails on Error, Warning and ParseError findings. ParseError is named because the analyzer drops syntax errors when the severity filter does not list them. It excludes three rules for every script, each with its reason written beside it: PSAvoidUsingWriteHost (console scripts and installers), PSUseShouldProcessForStateChangingFunctions and PSUseSingularNouns (both protect exported cmdlets, and the flagged functions are private helpers).

Findings that were already in the tree are fixed, or suppressed on the line with a reason:

  • Shell: three assigned-but-unused variables removed in e2e/linux/test_e2e_daemon.sh. SC2016 suppressed where a literal $auto is matched, SC2043 on a one-item loop that is meant to grow, and SC2329 added to the existing SC2317 suppression on a function called only through an EXIT trap.
  • Python: unused imports removed, from None added to two ArchiveError raises that deliberately drop the caught FileExistsError, and B023 suppressed on a lambda that runs inside its own loop iteration.
  • PowerShell: Quote-Arg renamed to ConvertTo-QuotedArg (approved verb), and non-ASCII characters in comments replaced with ASCII, which the analyzer's encoding rule is expected to flag.

DEVELOPMENT.md gains the local lint commands, including how to install ruff and ShellCheck on Linux arm64, where shellcheck-py has no wheel and the combined install fails, and how to change the pinned versions. AGENTS.md gains the rule for a finding (fix it, or suppress that one finding with a stated reason, and never exclude a file) and an "Ask first" line for adding or widening a rule exclusion in the lint config. Both files are covered by the same request to the maintainers: they are part of what is offered for acceptance.

Not in this PR, as possible follow-ups:

  • clippy for the Windows and macOS targets (that code, about 3.6k lines, is never linted by clippy today) and for gui/linux.
  • actionlint for the inline run: shell in the workflows.
  • A guard for extensionless shebang scripts, which git ls-files '*.sh' does not match.
  • Widening bandit to all Python files.
  • ruff format and PowerShell 5.1 compatibility checks.
  • Linting the Rust helpers in e2e/windows/helpers.
  • Optionally running the lint steps even when cargo fmt fails (if: always()).
  • Optionally pinning the new steps in the workflow verifier, project-wide, together with fmt, clippy and test.
  • Whether to keep downloading PSScriptAnalyzer from the PowerShell Gallery inside a required job or use the runner image's copy, and whether to pin ShellCheck as a release binary instead of the shellcheck-py wheel.

Overlap: PRs #52, #105 and #114 touch nearby files (ci.yml, scan configs, new scripts), and a local merge with each head was clean. If #114 lands first, the new scripts it adds will be linted by this check and may need fixes.

Security impact: CI-only change, and nothing shipped to users changes. It adds no secrets, no new permissions and no new action digests. It does add new third-party download paths into a required job: PyPI wheels, checked by the hash lock, and the PSScriptAnalyzer package from the PowerShell Gallery, checked by SHA-256 before extraction. It cannot weaken isolation or turn an enforcement failure into permissive behavior.

Test Plan

Run locally on Linux aarch64 only. Not tested on Windows or macOS.

  • ruff check . and ShellCheck 0.11.0 over all 27 tracked .sh files, before and after the fixes.
  • python3 scripts/verify_workflow_structure.py .
  • actionlint and zizmor 1.26.1 on the workflows.
  • A deliberately defective shell script and a deliberately defective Python file, to confirm the checks fail with file, line and rule.
  • pip download --require-hashes for the CI platform (x86_64, CPython 3.12, manylinux) to confirm the hash lock is accepted there, and a fresh download of the PSScriptAnalyzer package to confirm its SHA-256.
  • python3 -m unittest discover -s scripts -p 'test_*.py', scripts/check_linux_mxc_packaging.sh and scripts/test_security_tier0.sh, each also run on an untouched copy of origin/main for comparison.

Test Result

Passed locally:

  • ruff check . reports 0 findings (6 before the fixes).
  • ShellCheck reports 0 findings over the 27 .sh files (6 before the fixes).
  • python3 scripts/verify_workflow_structure.py . reports OK.
  • actionlint and zizmor 1.26.1 report no new findings on the workflows.
  • The defective shell script fails with its file, line and rule (SC2086), and the defective Python file fails with its file, line and rule (F821).
  • The hash lock is accepted for x86_64 CPython 3.12 manylinux by pip download --require-hashes.
  • The PSScriptAnalyzer package SHA-256 matches the pinned value on a fresh download.

Failing or skipped locally, for reasons of this machine and identical on untouched origin/main:

  • The Python unit tests ran 185 tests with 41 failures, 18 errors and 3 skipped. An untouched origin/main copy gives the same result, with the same 59 failing test names. The cause is the machine, not this change: it has no trusted bwrap, and it is linux-aarch64, so the installer tests report no release artifact for that platform.
  • scripts/check_linux_mxc_packaging.sh and scripts/test_security_tier0.sh fail the same way on untouched origin/main, so they are evidence for neither outcome.
  • No Rust checks were run: no Rust changed, and rustfmt is not available on this host. A skipped test is not a passing test, and none of the above is claimed as a pass.

Not verified:

  • PowerShell and PSScriptAnalyzer were not run, because pwsh could not be run in the environment used. The PowerShell findings are therefore not measured locally, and the PowerShell half of the defect-detection check is unproven. The Format job is the first place the PowerShell step runs, and it may report findings that need another change.
  • The exact pip install command was not run on the CI platform (only the hash lock was checked there), and the CI run itself has not been measured.
  • Nothing was tested on Windows or macOS.

CI linted only Rust, so mistakes in the repository's shell, Python and
PowerShell scripts could merge unchecked: unused imports and variables,
a PowerShell function with a non-approved verb, and Python exceptions
raised inside an except block without saying whether the cause is kept.

Run ShellCheck, ruff and PSScriptAnalyzer in the existing Format job.
That job is already part of the CI publication gate, so this adds no
new required check and changes neither the gate nor the workflow
verifier. A PR that adds a finding now fails Format.

The tools are pinned:
- ruff and ShellCheck come from hash-locked pip requirements
  (.github/lint-requirements.txt, compiled from the .in file next to
  it) and are installed with --require-hashes and --only-binary.
- setup-python is pinned to a commit SHA.
- PSScriptAnalyzer is downloaded as a fixed version and checked against
  a SHA-256 before it is imported.

Rule selection is explicit. ruff uses E4, E7, E9, F and B instead of
its default set, which changes between releases. PSScriptAnalyzer
fails on Error, Warning and ParseError findings; ParseError is named
because the analyzer drops syntax errors when the severity filter does
not list them. PSScriptAnalyzerSettings.psd1 excludes three rules for
every script, each with its reason written beside it:
- PSAvoidUsingWriteHost, because these are console scripts.
- PSUseShouldProcessForStateChangingFunctions and PSUseSingularNouns,
  because both protect exported cmdlets and these helpers are private.

The findings in the existing scripts are fixed or suppressed with the
reason on the line:
- Remove unused imports and three assigned-but-unused variables.
- Suppress ShellCheck SC2016 where a literal $auto is matched, SC2043
  on a one-item loop that is meant to grow, and SC2329 on a function
  called only through an EXIT trap.
- Suppress ruff B023 on a lambda that runs inside its loop iteration,
  and add "from None" to two ArchiveError raises that deliberately drop
  the caught FileExistsError.
- Rename the PowerShell helper Quote-Arg to ConvertTo-QuotedArg to use
  an approved verb.
- Replace non-ASCII characters in PowerShell comments with ASCII,
  which the analyzer's encoding rule is expected to flag.

The ShellCheck and ruff findings were fixed against local runs. The
PowerShell findings were not measured locally, so the first CI run is
the first measurement and may show that the PowerShell step needs more
changes.

Signed-off-by: Teemu Kuusisto <[email protected]>
The Format job now runs ShellCheck, ruff and PSScriptAnalyzer, but
neither DEVELOPMENT.md nor AGENTS.md said so. A contributor or agent
would learn about the checks only from a failing PR.

DEVELOPMENT.md gains a local check for shell, Python and PowerShell
files, using the same hash-locked requirements as CI and the same
PSScriptAnalyzer settings file. It covers Linux arm64, where
shellcheck-py has no wheel and the combined install fails, so ruff and
ShellCheck are installed separately. It also says how to change the
pinned tool versions and that Install-Module does not check the package
hash CI checks. The CI summary now lists the new checks under Lint &
format.

AGENTS.md gains the rule for a finding: fix it, or suppress that one
finding with a stated reason where the code is right. Where a rule is
wrong for a whole class of files, the only other route is a rule
exclusion in the lint config; excluding a file, or turning a rule off
only to make a finding go away, is not allowed. Adding or widening such
an exclusion goes under "Ask first", with the reason written beside it
and stated in the PR.

Signed-off-by: Teemu Kuusisto <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant