PMunch / nimcr

84 stars 10 forks source link

Does not work with nim 1.6.2 #16

Closed vitaliel closed 2 years ago

vitaliel commented 2 years ago

Hi, I tried nimcr, but I got errors. I can use .nims extension for scripting, but wanted a script without it.

Script file script-sample:

#!/usr/bin/env nimcr

echo "Hello"
chmod +x script-sample
./script-sample
(nimcr) Error on build running command: nim c -d:release --colors:on --nimcache:/tmp/nimcache-0000000044ABFB1F --out:"/home/lz/snippets/nim/.script-sample" /home/lz/snippets/nim/script-sampleHint: used config file '/home/lz/.choosenim/toolchains/nim-1.6.2/config/nim.cfg' [Conf]
Hint: used config file '/home/lz/.choosenim/toolchains/nim-1.6.2/config/config.nims' [Conf]

........................................................
Error: invalid module name: script-sample

I renamed file to script_sample

./script_sample
(nimcr) Error on build running command: nim c -d:release --colors:on --nimcache:/tmp/nimcache-00000000D9AAFB98 --out:"/h
ome/lz/snippets/nim/.script_sample" /home/lz/snippets/nim/script_sampleHint: used config file '/home/lz/.choosenim/toolc
hains/nim-1.6.2/config/nim.cfg' [Conf]
Hint: used config file '/home/lz/.choosenim/toolchains/nim-1.6.2/config/config.nims' [Conf]

.........................................................
Error: cannot open '/home/lz/snippets/nim/script_sample.nim'
vitaliel commented 2 years ago

I found similar tool that works in nim 1.6.2 https://github.com/lee-b/nimrun

PMunch commented 2 years ago

You seem to have trouble naming your file correctly. If you name it script_sample.nim you should be able to have

#!/usr/bin/env nimcr
echo "Hello"

in that file and then run it with: ./script_sample.nim.

The first error you showed is just a Nim error because you can't have dashes in the module/filename.

EDIT: Also note that nimrun and nimcr differs quite significantly in how they work. nimcr essentially calls nim c -r on your file, it allows extra arguments both to the compiler and your program, and caches the resulting build so subsequent runs are as quick as the compiled binary. nimrun creates a nimble package behind the scenes, this has the side effect that you can't run external packages from your script. And it doesn't cache the result, meaning it will rebuild your script every time you run it.