@karmaniverous/get-dotenv
    Preparing search index...

    Authoring Plugins: Diagnostics & Errors

    This page summarizes common diagnostics and error‑handling patterns for plugin authors. The goal is predictable behavior in CI and a consistent experience with the shipped plugins.

    Throw on subprocess failures by default. Only continue on error when the option is explicit (e.g., batch’s --ignore-errors), and document the behavior clearly.

    Honor the global capture contract:

    • If process.env.GETDOTENV_STDIO === 'pipe' or the merged options bag has capture: true, run your subprocess with stdio: 'pipe'.
    • Otherwise, inherit stdio for live interaction.
    import { buildSpawnEnv } from '@karmaniverous/get-dotenv';
    import {
    readMergedOptions,
    runCommand,
    shouldCapture,
    } from '@karmaniverous/get-dotenv/cliHost';

    const bag = readMergedOptions(thisCommand);
    const capture = shouldCapture(bag.capture);

    const ctx = cli.getCtx();
    const env = buildSpawnEnv(process.env, ctx.dotenv);

    await runCommand([file, ...args], false, {
    env,
    stdio: capture ? 'pipe' : 'inherit',
    });

    Note: with stdio: 'pipe', runCommand() re-emits the child's buffered stdout to stdout and its stderr to stderr after the child exits. Use runCommandResult() when you need the buffers themselves.

    When your plugin launches a subprocess, you can emit a short diagnostic prelude similar to the cmd plugin’s --trace:

    • For each key in { ...process.env, ...ctx.dotenv } (or a selected subset), print the origin (dotenv | parent | unset).
    • Apply presentation‑time redaction for secret‑like keys (user‑enabled): redactDisplay/redactObject/redactTriple with the merged redact and redactPatterns (custom patterns add to the built-in ones; a RegExp is used without its g and y flags, so it matches every key).
    • Optionally warn once per key about high‑entropy values (presentation‑only): maybeWarnEntropy with the merged warnEntropy, entropyThreshold, entropyMinLength and entropyWhitelist. Pass the merged bag's whitelist, which already holds the built-in patterns plus the user's; maybeWarnEntropy itself uses exactly the whitelist it is given (a RegExp without its g and y flags).

    This format is a best practice, not a requirement. Reuse when parity with the cmd plugin is desirable.

    Always use buildSpawnEnv(process.env, ctx.dotenv) to compose and normalize the child environment before passing it to your subprocess. This avoids platform‑specific surprises:

    • On Windows, dedupe keys case‑insensitively and ensure HOME/TMP/TEMP are coherent.
    • On POSIX, populate TMPDIR if a temp key is present.
    • runCommand/runCommandResult pass the built env as the child's complete environment (process.env is not merged back in), so on Windows a dotenv key wins even when an ambient variable differs from it only in case. An explicit env: {} is an empty environment, not "inherit": the child gets only the platform minimum. On Windows that is what libuv copies from the parent when the env lacks it (HOMEDRIVE, HOMEPATH, LOGONSERVER, PATH, SYSTEMDRIVE, SYSTEMROOT, TEMP, USERDOMAIN, USERNAME, USERPROFILE, WINDIR); on POSIX it is nothing. Leave env out to inherit process.env.
    import { buildSpawnEnv } from '@karmaniverous/get-dotenv';
    const env = buildSpawnEnv(process.env, ctx.dotenv);

    Prefer concise, single‑line status headers per run. Avoid verbose per‑line prefixes unless you introduce a dedicated --verbose or --debug.

    See also: