Skip to content

minify truncates its output file when the template render fails #178

Description

@fentas

argsh minify -t <template> -o <out> renders the template with:

```sh
envsubst '$data,$commit_sha,$version' <"${template}" >"${out}"
```

(libraries/main.sh:1563)

Two problems compound:

  1. The redirect truncates ${out} before envsubst runs. If the render fails, the previously-good artifact is already destroyed — replaced by a 0-byte file. set -euo pipefail aborts after the damage.

  2. envsubst may not be GNU envsubst. renvsubst is a common drop-in (lok8s installs it as envsubst), and it rejects the GNU SHELL-FORMAT positional argument:

    ```
    ERROR: Unknown flag: $data,$commit_sha,$version
    ```

    So a project whose own toolchain is on PATH gets a 0-byte bundle from a build that looks like it merely printed a warning.

This bit us on a published installer: install/build in lok8s writes docs/public/lo-up, which is served directly to curl -fsSL https://get.lok8s.io | sh. A build run with the project's env active silently replaced it with an empty file.

Suggested fix

Render to a temp file and mv on success, so a failed render cannot damage an existing output:

```sh
local tmp; tmp="$(mktemp)"
if ! envsubst '$data,$commit_sha,$version' <"${template}" >"${tmp}"; then
rm -f "${tmp}"
return 1
fi
[[ -s "${tmp}" ]] || { rm -f "${tmp}"; return 1; }
mv "${tmp}" "${out}"
```

Optionally also detect the flavor up front — envsubst --version | grep -q 'GNU gettext' — and fail with a clear message naming the culprit, since "Unknown flag" gives no hint that the wrong envsubst is on PATH.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions