Skip to content
Snippets Groups Projects
user avatar
Ilya Konyukhov authored
**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.
4e27b0e6
History

Tarantool

Build Status Code Coverage Telegram Slack Gitter Google Groups

https://tarantool.io/en/

Tarantool is an in-memory database and application server.

Key features of the application server:

  • 100% compatible drop-in replacement for Lua 5.1, based on LuaJIT 2.1. Simply use #!/usr/bin/tarantool instead of #!/usr/bin/lua in your script.
  • full support for Lua modules and a rich set of own modules, including cooperative multitasking, non-blocking I/O, access to external databases, etc

Key features of the database:

  • ANSI SQL, including views, joins, referential and check constraints
  • MsgPack data format and MsgPack based client-server protocol
  • two data engines: 100% in-memory with optional persistence and an own implementation of LSM-tree, to use with large data sets
  • multiple index types: HASH, TREE, RTREE, BITSET
  • asynchronous master-master replication
  • authentication and access control
  • the database is just a C extension to the application server and can be turned off

Supported platforms are Linux/x86 and FreeBSD/x86, Mac OS X.

Tarantool is ideal for data-enriched components of scalable Web architecture: queue servers, caches, stateful Web applications.

To download and install Tarantool as a binary package for your OS, please visit https://tarantool.io/en/download/.

To build Tarantool from source, see detailed instructions in the Tarantool documentation at https://tarantool.io/en/doc/2.1/dev_guide/building_from_source/.

Please report bugs at https://github.com/tarantool/tarantool/issues We also warmly welcome your feedback in the discussion mailing list, tarantool@googlegroups.com.

Thank you for your interest in Tarantool!