app: let user define search root for packages
**Intro**: By default, Lua uses package.path/cpath to load dependencies when user calls `require("something")`. It also allows to add custom loaders to extend its functionality. Tarantool transforms default behavior by injecting some custom loaders into `package.loaders`. Firstly, it allows to load modules from CWD. Secondly, it looks for rocks modules starting from CWD and traversing up to root "/" directory. This "start from CWD" behaviour is hardcoded and not customizable. **Problem**: Let say we have a lua app with all dependencies under myapp directory. If you start your application like this, you won't be able to load any of them unless you specify myapp in every require call: ```bash tarantool myapp/init.lua ``` To allow user scripts require dependencies relatively, user has to first determine appropriate directory path relatively to scripts file and then patch package.path/cpath with both modules and rocks paths. **Solution**: Introduce the notion of a search root which is used as base for rocks and module loaders. Let user easily set this path to control how application dependencies are loaded. There is 2 new functions introduced in this patch: - package.setsearchroot(path) is used to set new search root to look dependency from - package.searchroot() returns search root currently setup. If no search root was setup, `fio.cwd()` is returned. @TarantoolBot document Title: Document new package.setsearchroot and package.searchroot functions What this patch does: `package.setsearchroot(path)` function sets search root which is used as root directory to load dependencies from: - path must be a string (relative or absolute). It will be expanded to absolute path and set as a search root; - if no path specified, it sets current file directory as a search root (using debug.sourcedir); - if path is box.NULL, that searchroot will be reset, so default behaviour will apply (CWD). With that said everything a user now has to do is to just put this line of code: ```lua package.setsearchroot() ``` at the top of project init file "myapp/init.lua" which is placed in the project root and start an app by simply calling: ```bash $ tarantool myapp/init.lua ``` This will set search root to the absolute path of `myapp` directory, so all dependencies will be looked relative to that directory.
Loading
Please register or sign in to comment