Skip to content

Add Cmd.check_available! - #466

Open
dmisiuk wants to merge 1 commit into
roc-lang:mainfrom
dmisiuk:issue-223-check-command-available
Open

Add Cmd.check_available!#466
dmisiuk wants to merge 1 commit into
roc-lang:mainfrom
dmisiuk:issue-223-check-command-available

Conversation

@dmisiuk

@dmisiuk dmisiuk commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Adds Cmd.check_available!, closes #223 — check whether a command can be found on the system before trying to run it.

Discussed on Zulip. @lukewilliamboswell approved a pure-Roc implementation on the existing primitives, so there are no host or glue changes.

Implementation

check_available! : Str => Bool
  • Reads PATH via Env.var! and splits it byte-preservingly through OsStr.to_raw, so non-UTF-8 entries survive.
  • Branches on Env.platform! for the :/; separator and, on Windows, the candidate extensions from %PATHEXT% (a name is tried as-is and with each extension appended).
  • A name containing a path separator is checked directly instead of being looked up.

Directories are excluded. Path.is_executable! (Unix) and Path.exists! (Windows) both return true for a directory, so a candidate is additionally required to not be a directory. Because is_dir! does not follow symlinks, a symlink to an executable still resolves while a real directory named like the command is rejected. The only residual is a symlink pointing at a directory, which is documented. (The triage note asked to avoid the directory failure mode; this does, in pure Roc.)

Testing

Six expect blocks cover the pure core — PATH splitting for all three OsStr representations, byte preservation of a non-UTF-8 entry (verified to fail when the bytes are altered), empty-entry dropping, and separator detection.

Verified end-to-end against a local build with a directory, a symlink, and a real executable sharing a name:

realtool (executable file) -> available
faketool (directory)       -> missing
linktool (symlink to exe)  -> available

examples/check-command.roc demonstrates the API and its test_spec.json case asserts a real command is found and a bogus one is not. Full ./scripts/test.py passes — host build plus all examples. The example's run case is Unix-only (like command.roc), since it checks a Unix command by name.

@dmisiuk dmisiuk closed this Aug 2, 2026
@dmisiuk dmisiuk reopened this Aug 2, 2026
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.

Add check_command_available functionality for basic-cli

1 participant