Skip to content

codegen: use command -v instead of fragile which + \$? pattern#620

Merged
chrispsommers merged 1 commit into
p4lang:mainfrom
Devansh-567:refactor/compile-protos-robustness
Jul 10, 2026
Merged

codegen: use command -v instead of fragile which + \$? pattern#620
chrispsommers merged 1 commit into
p4lang:mainfrom
Devansh-567:refactor/compile-protos-robustness

Conversation

@Devansh-567

Copy link
Copy Markdown
Contributor

Description

Refactored codegen/compile_protos.sh to use command -v instead of the fragile which + $? 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 the if block would silently break the error handling. Additionally, command -v is the POSIX-compliant standard for binary verification, offering better portability than the external which command.

Changes

  • Replaced which lookups with direct short-circuit evaluation (command -v ... || { ... }).
  • Removed fragile $? status checks following variable assignments.
  • Reduced overall line count while preserving identical functional behavior.

Signed-off-by: Devansh-567 <devansh.jay.singh@gmail.com>
@fruffy
fruffy requested a review from a team July 7, 2026 01:25

@chreekat chreekat left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Devansh-567

Copy link
Copy Markdown
Contributor Author

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. 😊

@chreekat

chreekat commented Jul 9, 2026

Copy link
Copy Markdown

I'd like to keep the if + $? check, but command -v instead of which would be fine :)

@chrispsommers chrispsommers left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chrispsommers
chrispsommers merged commit bbf9b97 into p4lang:main Jul 10, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants