Nim: `nim js -o:dirname main.nim` writes nothing, and no error shown

Created on 2 Oct 2018  路  10Comments  路  Source: nim-lang/Nim

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)

Easy Hacktoberfest

Most helpful comment

Some hints for the next hacktoberfest{er,ee} (is that even a word?)

  • This problem affects the frontend, namely the part that parses the option strings.
  • You don't see this problem with the C backend because gcc is smart enough to warn you that the chosen output path is not a file
  • The parsing happens in processSwitch
  • The compiler/pathutils module contains some interesting methods
  • What you want to do is to check that:

    • The given argument is a path + filename (eg. /a/b/c -> path: /a/b, filename: c)

    • The path exists

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!

All 10 comments

Some hints for the next hacktoberfest{er,ee} (is that even a word?)

  • This problem affects the frontend, namely the part that parses the option strings.
  • You don't see this problem with the C backend because gcc is smart enough to warn you that the chosen output path is not a file
  • The parsing happens in processSwitch
  • The compiler/pathutils module contains some interesting methods
  • What you want to do is to check that:

    • The given argument is a path + filename (eg. /a/b/c -> path: /a/b, filename: c)

    • The path exists

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hlaaftana picture hlaaftana  路  3Comments

teroz picture teroz  路  3Comments

juancarlospaco picture juancarlospaco  路  3Comments

koki-koba picture koki-koba  路  3Comments

liquid600pgm picture liquid600pgm  路  3Comments