codegen: use command -v instead of fragile which + \$? pattern#620
Conversation
Signed-off-by: Devansh-567 <devansh.jay.singh@gmail.com>
There was a problem hiding this comment.
I think someone could just as easily argue for the reverse patch (e.g. "an if statement is a better indication of intent and more robust to accidentally adding more binary operators").
There is no bug being fixed.
So, I don't think this change passes the high bar that should be put in front of any change. After all, every commit may introduce bugs, may accidentally change behavior, and complicates history.
Fair, there's no active bug here, and I could have framed this better. The concrete motivation is that which isn't POSIX and its behavior/availability isn't guaranteed across environments the way command -v is; that's the main thing I'd want to fix vs. a style preference. If that's not enough of a bar for this repo, happy to close this, no strong objection either way. 😊 |
|
I'd like to keep the if + $? check, but |
Description
Refactored
codegen/compile_protos.shto usecommand -vinstead of the fragilewhich+$?pattern.The previous implementation relied on checking
$?immediately after a variable assignment (e.g.,PROTOC="$(which protoc)"). While functional, this pattern is highly fragile as any commands accidentally inserted between the assignment and theifblock would silently break the error handling. Additionally,command -vis the POSIX-compliant standard for binary verification, offering better portability than the externalwhichcommand.Changes
whichlookups with direct short-circuit evaluation (command -v ... || { ... }).$?status checks following variable assignments.