Fix env var validation conflicts#619
Conversation
Environment variables with matching names on two commands with the same parent were validated against the requirements of both commands. It is caused by `Reset()` and `Validate()` walking the entire tree rather than only the nodes on the selected path.
The fix hand-rolled a tree recursion and a *Node-keyed "selected" set to scope Reset to the traced path. Since Visit already surfaces every flag/positional/argument as a *Value, keep Reset on the existing Visit walk and key tolerance off a *Value set instead, dropping the bespoke recursion. Promote the nodeValues helper to a Node.Values() method alongside AllFlags/Leaves, reused by both Reset and Validate. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Thanks for tracking this down, @hindessm — the diagnosis is spot on and the regression tests are exactly what this needed. Env vars and defaults for commands that aren't on the selected path should never fail the parse. I pushed a small follow-up commit (23f3f14) that keeps the same behaviour but reuses the existing machinery rather than introducing a parallel one:
Net effect is a little less code and one traversal idiom instead of two. Tests and lint pass. Happy to squash or drop the commit if you'd rather keep authorship clean — whatever you prefer. |
|
You're very welcome. I tripped over the bug and, having used kong for several projects, it was interesting trying to figure out how it fix it. I'm happy with commit history as it is; thanks for cleaning it up and taking the time to provide the rationale. |
Environment variables with matching names on two commands with the same parent were validated against the requirements of both commands.
Caused by
Reset()andValidate()walking the entire tree rather than only the nodes on the selected path.Note that, this changes some error behaviour slightly. For example, if a default value was not a member of an enum set the validation would always catch this even if the node was not in the selected path.