67.17. shfmt-3.14.1
Shell parser, formatter and linter
| Version | 3.14.1, release 1 |
| Shelf | devtools — Debuggers, tracers, profilers, analysers, linters, language servers and developer utilities |
| Phase | 40_lang (Languages), 25 of 186 in build order |
| Licence | BSD-3-Clause |
| Homepage | https://github.com/mvdan/sh |
| Build time | 0.3 SBU |
| Installed size | 3.8 MiB in 3 files |
| Recipe | ports/core/devtools/shfmt/kpkgbuild |
About
shfmt parses POSIX shell, bash, mksh, zsh and Bats scripts and prints them back in one consistent format; shfmt -d shows the difference instead, and a script it cannot parse is reported with the line and column. It comes from the mvdan/sh module, a Go shell parser, and is built with cgo off from its vendor bundle. testing/preflight.sh checks every build.sh in the tree with bash -n, which finds syntax errors and nothing else; shfmt parses each script into a syntax tree, so it also reports what a syntax check passes, such as inconsistent formatting.
Why it is in KDOS
The comment above it in script/phases/40_lang/packages.d/devtools.txt:
preflight runs
bash -nover ~470 build.sh files, which catches a syntax error and nothing else. shfmt PARSES them.
Sources
shfmt-3.14.1.tar.gz(289.0 KiB)
sha256ec4bdb88ab6c95686be3a4eeb4ad77d2b49d33d2ed7b0a65035cd52d2d87c443
upstream: https://github.com/mvdan/sh/archive/refs/tags/v3.14.1.tar.gz
archive copy: sources-1/devtools--shfmt-3.14.1.tar.gzshfmt-vendor-3.14.1.tar.xz(831.6 KiB)
sha256a14e8b7fcdb4145f9dad6446363fdbce08d28e7d1f89b89e7a8c6e4c538c9dc8
a vendor bundle: the dependencies upstream’s own tool would download, packed once
archive copy: sources-1/devtools--shfmt-vendor-3.14.1.tar.xz
Dependencies
Required by: no port names it.
Installation
From the repository root, inside the build container, this is the line kdosbuild runs for the port:
script/chroot/exec.sh bash -c 'source script/phases/40_lang/phase.env && kpkg install shfmt'
What kpkg does
kpkg runs these steps, in this order, as root inside the chroot. The port directory is /ports/core/devtools/shfmt.
1. Verify every declared source. Each file a sha256 = line names, found in the port directory or in /var/cache/kpkg/sources, must hash to its line; a mismatch stops the build before anything is unpacked.
cd /ports/core/devtools/shfmt
echo 'ec4bdb88ab6c95686be3a4eeb4ad77d2b49d33d2ed7b0a65035cd52d2d87c443 shfmt-3.14.1.tar.gz' | sha256sum -c -
echo 'a14e8b7fcdb4145f9dad6446363fdbce08d28e7d1f89b89e7a8c6e4c538c9dc8 shfmt-vendor-3.14.1.tar.xz' | sha256sum -c -
2. Unpack. The work directory is emptied and the first source is unpacked into $SRC with its top directory stripped; any later tarball lands unstripped beside it in $SRC_ROOT, and a file that is not a tarball is copied into $SRC.
SRC_ROOT=/var/cache/kpkg/work/shfmt
SRC=$SRC_ROOT/shfmt-3.14.1
PKG=$SRC_ROOT/pkg
rm -rf "$SRC_ROOT"
mkdir -p "$SRC" "$PKG"
tar --no-same-permissions --warning=no-unknown-keyword -xf shfmt-3.14.1.tar.gz -C "$SRC" --strip-components=1
Every config.guess and config.sub older than automake’s is then replaced with automake’s, so a configure script recognises the musl target.
3. Build. build.sh is sourced in a subshell under set -e, from $SRC, with name, version, release and the five paths PKGNAME, PORT_SRC, SRC_ROOT, SRC and PKG set as shell variables. Its body, verbatim:
tar xf $PORT_SRC/${name}-vendor-${version}.tar.xz
export CGO_ENABLED=0
go build -mod=vendor -ldflags "-s -w -X main.version=v$version" -o shfmt ./cmd/shfmt
install -Dm755 shfmt $PKG/usr/bin/shfmt
install -d $PKG/usr/share/man/man1
scdoc < cmd/shfmt/shfmt.1.scd > $PKG/usr/share/man/man1/shfmt.1
The switches, and why each is set:
-X main.version=v$version— Meant to stamp the version into the program, butcmd/shfmthas nomain.versionvariable, and the Go linker ignores an-Xfor a symbol that does not exist.shfmt --versionprints the main module’s version from the build information instead, which for a build from a source tree is(devel).
The common switches of each build system are explained in the Flag glossary.
4. Package. kpkg removes libtool archives, the info directory index, mimeinfo.cache and every fonts.dir from $PKG, copies the source’s licence files into /usr/share/licenses/shfmt, prunes what the image does not ship, lints the tree and rolls it reproducibly:
tar --sort=name --format=gnu --numeric-owner --owner=0 --group=0 \
--mtime=@$SOURCE_DATE_EPOCH \
--use-compress-program='xz -9 -T0 --block-size=32MiB --no-adjust' \
-cf shfmt-3.14.1-1.tar.xz -C "$PKG" .
5. Adopt. kpkgadd installs the package’s files over the root, refusing a path another package owns, records the file list in /var/lib/kpkg/db/shfmt, and runs the triggers its paths call for.
Contents
Installed programs: shfmt.
Installed directories: /usr/share/licenses/shfmt.
Short descriptions
shfmt |
Format shell programs |
Notes
The manual page is written in scdoc, as cmd/shfmt/shfmt.1.scd, and scdoc, in depends, renders it to shfmt.1 at install time.