Messing around with compile-time file reading using staticRead(). Seems to work fine with paths relative to the application root, but not with an absolute path. I get Error: cannot open '/home/user/Desktop/file.txt'.
No issues when using absolute paths with open() at runtime.
Anyone know why staticRead() won't work that way?
I see there's an issue with staticRead() from files in nested directories too.
https://github.com/Araq/Nim/issues/765
Maybe it's related or maybe staticRead() is simply just going to work relative to the app root. I can work around it easily enough. Just curious.
...actually, I thought I'd be able to use the full path that I get from currentSourcePath() and then use getAppDir() to strip away the first part of the path, but getAppDir() apparently can't be called at compile time.
I don't see any way in the system module to get the path to the root of the application. I could hardcode it, but that would be a drag. Any thoughts on that?
That's great! I'll keep my eye on that PR.
Thanks def!
You should be able to work around it for now using staticExec + cat, e.g.
const data = staticExec "cat /path/to/file"
While this is not ideal and somewhat inefficient, it should work on all POSIX systems.
One of the first programs I wrote (when I started learning Nim) wanted to staticRead from /usr/share/dict/words.
There are certain files/paths that are assumed to exist on (modern) Linux systems.
"Do we need to fix this? Absolute paths are horrible, how can you build on another machine with that?"
That's a fair point. The reason I was using an absolute path in the first place was that I wasn't getting relative paths in nested modules to work (as described in the linked bug report)... so it's somewhat of an XY Problem.
However, given that the absolute path can found dynamically, it doesn't necessarily make it non-portable, though for me unnecessary when the other bug is fixed.