Skip to content

Config loading errors silently discarded in preAction hook #97

@MattDevy

Description

@MattDevy

Bug

The preAction hook in src/cli.ts silently discards all config loading errors. When --config-file points to a nonexistent file, contains invalid YAML, references a missing context, or fails schema validation, the error is swallowed and the command runs with whatever config was loaded at startup (or no config at all).

Steps to reproduce

# Nonexistent config file: silently ignored, uses startup config
elastic es --config-file /nonexistent info
# => succeeds using default config, exit 0

# Invalid YAML: silently ignored
echo "invalid: yaml: [[[" > /tmp/bad.yml
elastic es --config-file /tmp/bad.yml info
# => succeeds using default config, exit 0

# Missing context: silently ignored
echo -e 'current_context: fake\ncontexts: {}' > /tmp/bad-ctx.yml
elastic es --config-file /tmp/bad-ctx.yml info
# => succeeds using default config, exit 0

# --use-context with nonexistent context: silently ignored
elastic es --use-context nonexistent info
# => succeeds using default config, exit 0

In every case the user has no indication their config override was ignored.

Root cause

src/cli.ts:28-37:

program.hook('preAction', async (thisCommand) => {
  const result = await loadConfig({ ... })
  if (result.ok) {
    setResolvedConfig(result.value)
  }
  // No else clause: errors are discarded
})

The loadConfig() function returns well-structured error messages (including "Context X not found. Available contexts: ..." and Zod validation errors), but the preAction hook never surfaces them.

The code comment on line 26-27 says "On error, print a structured message and exit" but the implementation does neither.

Expected behavior

Config loading failures should:

  1. Print the error message to stderr
  2. Exit with a non-zero code
  3. Never fall through to run the command with stale/wrong config

Suggested fix

program.hook('preAction', async (thisCommand) => {
  const result = await loadConfig({ ... })
  if (result.ok) {
    setResolvedConfig(result.value)
  } else {
    process.stderr.write(`Error: ${result.error.message}\n`)
    process.exit(1)
  }
})

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions