LUA_PATH

LUA_PATH my new friend

Have you heard about LUA_PATH? No? Then let me tell you what that is: In essence it’s an environment variable used by any Lua interpreter used to determine where to find additional modules execute code tries to load using require.

Now rather than the usual well-known PATH environment variables (which are used to set the search paths for binaries executed on a Unix-ish system, in case you didn’t know) – of course – Lua had to do it slightly differently:

  1. It uses ; as separator instead of : (the former means one has to take extra care to correctly quote the value when setting the variable because ; is used for command separation in shells)
  2. It allows for wildcards in paths which Lua will substitute the module name into. Why? I’ve no idea but they’re using the ? as name substitution wildcard which pretty much everywhere else means: Substitute exactly one character.
  3. It allows for filenames in paths, too. WTF?!? Actually it’s even documented in the Lua book, however what they gloriously fail to mention: It does not work like everyone would expect; In a nutshell: If you do a something = require ("myfoo") and myfoo cannot be found anywhere in the search path it will automatically load the file specified in the LUA_PATH and bind it to the variable something yielding a great WTF?!? moment (or a day or two if you’re working on something complex or just don’t have the experience) since this will pretty much never the result you’d expect plus it may vary greatly from environment to environment the code is run in. Great job Lua team!

So here’re my tip of the day: If you need to modify the Lua path, completely forget about the possibility of adding a file directly, just always use a path with a wildcard. And if there’s a chance that someone might want to use a compiled version of Lua code, don’t forget to add a seperate wildcard for that, too.

That’s it for today, see you next time around.