eg, this code:
import os
for p in walkDir("/tmpXXXXX"):
echo p
echo "Hello"
If the directory /tmpXXXXX does not exist, then control flow just passes on to after the loop, rather than raising an exception about "directory not found".
In the nim code os.nim, for 0.19.2, around line 866, there's a call to opendir, and then logic proceeds if the returned pointer is not nil (ie, there wasn't an error.
But there's no branch in that code that handles the nil condition, gets the C error, raises, etc.
It's possible to eg, make a PR which adds the C error check, and then raises an exception. But that would break existing systems that depend on walkDir's current permissiveness. Also, with Nim 1.0 being just around the corner.
I'm picking on walkDir here, but I have seen this in some other places.
Would it be more correct to eg, update documentation for walkDir to state that by design it's not intended to? Similar to how eg, removeDir is documented as not raising an exception if the directory never existed originally?
This pattern (of e.g. checking dirExists before doing things in the directory) is known as LBYL (Look Before You Leap), and often leads to security problems known as TOCTOU (Time-of-check/Time-of-use) if the answer changes between the test time and the use time - depending, of course, on program logic; exploiting these is often not deterministic but possible if you can afford a few thousand attempts.
I to prefer no exceptions of any sort in these cases, but I do like to have some signal if interested; C's "errno" is a useful, if error prone, compromise - but it doesn't quite fit with Nim; and unfortunately, I have no other solution to offer.