mkdir dirname
nim js -o:dirname main.nim writes nothing, and no error shown
it should either write to dirname/main.js, or error with : cannot write to dirname (dirname is a directory)
Some hints for the next hacktoberfest{er,ee} (is that even a word?)
C backend because gcc is smart enough to warn you that the chosen output path is not a fileprocessSwitchcompiler/pathutils module contains some interesting methods/a/b/c -> path: /a/b, filename: c)The deeper problem is inside the jsgen.nim file since it uses writeRopeIfNotEqual to write out the file and it returns false if the file contents are the same _and_ if the file cannot be opened. I believe it's been introduced (~10 years ago, time flies!) in order to provide some kind of very late caching that's probably not needed anymore, but let's ask @Araq about this first.
Trivia fact: The js backend at some point in time was also a PHP backend and, before that, a luaJIT one!
@manterolat @Araq @LemonBoy could we reopen?
mkdir /tmp/d06/ && /tmp/d06/
nim js -o:/tmp/d06/bar2/ main.nim
creates bar2/ directory but no js file is created, and no error
with s/js/c/ an error is shown ld: can't open output file for writing
maybe we could throw in case dirname contains trailing DirSep ? Note: this is not contrived, and can happen in scripts, eg:
nim js -o:$dir/$name foo.nim where env var name is empty for example
-o doesn't take a directory.
sure, but then it should error instead of silently doing I-don't-know-what (no error is shown, and I've no idea whether a file was produced or not, nor where, if it is produced); like I've said, in more complex cases (eg in scripts), user could issue a command nim js -o:$dir/$name foo.nim where $name ends up empty, in which case it should error, but instead something mysterious happens
I could add a small check in commands.nim (or in cgen.nim and jsgen.nim) that raises an error if the argument is already a directory:
if dirExists(AbsoluteDir outfile):
raise newException(IOError, "cannot create file (" & outfile.string & " is a directory")
according to principle of catching input errors as early as possible, I think in commands.nim (ie before it reaches cgen.nim and jsgen.nim IIRC) is where the test should be
Indeed, and if the folder does not exist it should also be created there and not in every damn backend.
Moreover the use of writeRopeIfNotEqual hides any error during the output writing so maybe that should be changed (removed?)
@Araq @krux02 @manterolat could we reopen?
this silently does nothing (no error, no /tmp/d16/dochack.js file generated)
ls /tmp/d16
cannot access '/tmp/d16': No such file or directory
nim js -o:/tmp/d16/dochack.js tools/dochack/dochack.nim
ls /tmp/d16
cannot access '/tmp/d16': No such file or directory
(likewise with case mentioned in https://github.com/nim-lang/Nim/issues/9154#issuecomment-428714193 with nim js -o:/tmp/d06/bar2/ main.nim which silently does nothing instead of giving an error )
Is this issue still relevant? I can't reproduce any mentioned bugs there.
indeed, something must've fixed it in meantime
Most helpful comment
Some hints for the next hacktoberfest{er,ee} (is that even a word?)
Cbackend becausegccis smart enough to warn you that the chosen output path is not a fileprocessSwitchcompiler/pathutilsmodule contains some interesting methods/a/b/c-> path:/a/b, filename:c)The deeper problem is inside the
jsgen.nimfile since it useswriteRopeIfNotEqualto write out the file and it returnsfalseif the file contents are the same _and_ if the file cannot be opened. I believe it's been introduced (~10 years ago, time flies!) in order to provide some kind of very late caching that's probably not needed anymore, but let's ask @Araq about this first.Trivia fact: The js backend at some point in time was also a PHP backend and, before that, a luaJIT one!