Skip to content

os: propagate POSIX error code from os.ls/1 and os.rmdir_all/1 on UNIX-like systems#25392

Merged
spytheman merged 2 commits into
vlang:masterfrom
gechandesu:posix_err
Sep 27, 2025
Merged

os: propagate POSIX error code from os.ls/1 and os.rmdir_all/1 on UNIX-like systems#25392
spytheman merged 2 commits into
vlang:masterfrom
gechandesu:posix_err

Conversation

@gechandesu

Copy link
Copy Markdown
Contributor

Return the error code from os.ls/1 and os.mkdir_all/1 to enable detailed error handling e.g. you can now differ ENOENT (no such file or directory) and other possible I/O errors:

>>> os.ls('/nonexistent') or { println(err.code()) }
2
>>> os.rmdir_all('/nonexistent') or { println(err.code()) }
2

@vlang vlang deleted a comment from huly-for-github Bot Sep 27, 2025
Comment thread vlib/os/os_nix.c.v
dir := unsafe { C.opendir(&char(path.str)) }
if isnil(dir) {
return error('ls() couldnt open dir "${path}"')
return error_posix(msg: 'ls() couldnt open dir "${path}"')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is not safe, since the C.errno could be changed during the string interpolation in the argument.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you can call e := last_error() separately, if you need to include details about the exact reason, and then access its e.code() and e.msg() to compose the final error

@spytheman spytheman merged commit 11edca1 into vlang:master Sep 27, 2025
78 of 79 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.

2 participants