edubart / nelua-lang

Minimal, efficient, statically-typed and meta-programmable systems programming language heavily inspired by Lua, which compiles to C and native code.
https://nelua.io
MIT License
2.06k stars 67 forks source link

Obscure behavior when dealing with name clashes #134

Closed stefanos82 closed 3 years ago

stefanos82 commented 3 years ago

While I was testing content for #133, I have discovered what in my humble opinion can be considered a very VERY dangerous behavior in generated code.

I have a tmp.nelua located in /home/stefanos/code/nelua/ and decided to create another tmp.nelua, but this new file located in /home/stefanos/tmp/.

The contents of /home/stefanos/code/nelua/tmp.nelua were obviously replaced by contents of /home/stefanos/tmp/tmp.nelua, so was my generated executable.

What would happen if I had original tmp executable running during the compilation of the newly created tmp?

Should not it better to generate name that consists of the entire path, plus the file name instead?

E.g.: /home/stefanos/code/nelua/tmp.nelua should become in /home/stefanos/.cache/ something like home_stefanos_code_nelua_tmp.c; this could reduce the chances to have a conflict.

edubart commented 3 years ago

What would happen if I had original tmp executable running during the compilation of the newly created tmp?

On Linux this would work fine, as Linux allows you to replace binaries that are being run, on Windows this would give a compile error, because you cannot replace running files.

On Linux this is really issue only if you try to compiler/run both files at the same time, in that situation you will may have compile errors, or run different application that you expected, this have happened with me before

I don't think it's that dangerous, this is a detail how the compiler works. If you want to avoid that you can simple use the old behavior of using nelua_cache directory instead of ~/.cache, by changing the cache_dir variable in your neluacfg.lua, you could also configure .neluacfg.lua per project basis, so you use different cache for different projects. I will state this is the official workaround.

The behavior will not change, renaming files to full path have other issues that I won't to avoid, like the file name being too long, on Windows a file path is limited to 260 characters. Also would make my life harder to locate generated files when developing Nelua (and would also for others).