1

If I have this task in a nimble file:

task readme, "generate README.md":
  exec "nim c -r readme.nim > README.md"

with this readme.nim:

echo "# Hello\nworld"

executing task with nimble (nimble readme) does not redirect the output of readme.nim to file. As expected running nim c -r readme.nim > README.md from terminal correctly creates/updates README.md.

Is this intended behaviour of nimble? is there a workaround?

note: the above was tested on windows.

xbello
  • 7,223
  • 3
  • 28
  • 41
pietroppeter
  • 1,433
  • 13
  • 30
  • note that nimble tag, besides being very little used (29 questions including this one currently: https://stackoverflow.com/questions/tagged/nimble) has been used with a lot different meanings (grails plugin, R package, a CRM, ...). Not sure what should be done there (edit tag info? ask for removal (how)?) – pietroppeter Oct 21 '20 at 08:51
  • Regarding the tag: users with more than 1.5K points can create new tags. What would you suggest here? nimble-manager? nimble-package? – xbello Oct 21 '20 at 09:03
  • probably nimble-nim? – pietroppeter Oct 21 '20 at 09:08

2 Answers2

2

thanks to answer by @xbello and ensuing discussion, I found a good workaround for my use case:

task readme, "generate README.md":
  exec "nim c readme.nim"
  "README.md".writeFile(staticExec("readme"))

the explanation to why the simple exec has to do with the fact that nimble uses nimscript.exec which internally uses rawExec which is a builtin that (judging from different behaviours reported here for windows and linux) is not entirely cross-platform when it regards output pipeline.

pietroppeter
  • 1,433
  • 13
  • 30
1

I end up with the expected README.md:

$ cat README.md
# Hello
world

But sometimes (the readme.nim has to be compiled or recompiled) I end up with something like this:

CC: readme.nim
# Hello
world

That is, the full stdout (not the stderr) of the nim c -r readme.nim command, as expected. As a workaround you could encapsulate what you want to do in the readme.nim:

import os

let f: File = open(commandLineParams()[0], fmWrite)
f.write "# Hello\nworld"
f.close()

And in your nimble file:

task readme, "generate README.md":         
  exec "nim c -r readme.nim README.md"

Another workaround could be to suppress the output of nim c:

task readme, "generate README.md":
  exec "nim c --verbosity:0 -r readme.nim > README.md"
xbello
  • 7,223
  • 3
  • 28
  • 41
  • I should have mentioned that the above was tested on windows, I will add that. I guess this is on linux, right? – pietroppeter Oct 21 '20 at 09:09
  • Yes, it is on Linux. – xbello Oct 21 '20 at 09:12
  • ok, thanks. the above is useful to know. the workaround, while a sensible way of doing that, would not work for my use case where I do not want to change the readme.nim (in particular the workaround requires to change all echo appearing in the file). I was hoping for a workaround in the nimble file. – pietroppeter Oct 21 '20 at 09:15
  • also the fact that sometimes you have a different output tells me that something is happening with exec command in nimble that I do not understand and I would like to (besides not being cross platform). mmh, maybe I should open an issue in nimble... – pietroppeter Oct 21 '20 at 09:20
  • 1
    Nimble uses nimscript.exec, that just executes a command without capturing any output. It just knows if all went OK (exit 0) or something failed. Again: when you capture the output of "nim c -r script.nim", that captures the output of both executions "nim c" and "script". So I guess another workaroud would be "nim -c --verbosity:0 -r readme.nim > README.md" – xbello Oct 21 '20 at 09:24
  • ok, now looking at nimscript.exec I was able to reach system.staticExec and actually find a workaround that also works on windows: `exec "nim c readme.nim"; "README.md".writeFile(staticExec("readme"))`. Do you want to add it to your answer so that I can accept it? – pietroppeter Oct 21 '20 at 09:44
  • 1
    In this case, it's better that you write your own answer and accept it. It's encouraged by StackOverflow rules (it has its own medal and all: https://stackoverflow.com/help/badges/14/self-learner) – xbello Oct 21 '20 at 09:47