Skip to content

Commit ce514e0

Browse files
Document new behavior for 'setsearchroot' (#3515)
1 parent a7ce4fd commit ce514e0

File tree

1 file changed

+97
-42
lines changed

1 file changed

+97
-42
lines changed

doc/reference/reference_lua/other.rst

Lines changed: 97 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ and variables which are outside all modules.
2020
| :ref:`dostring() | Parse and execute an arbitrary |
2121
| <other-dostring>` | chunk of Lua code |
2222
+--------------------------------------+---------------------------------+
23-
| :ref:`package.path | Where Tarantool looks for Lua |
24-
| <other-package_path>` | additions |
23+
| :ref:`package.path | Get file paths used to search |
24+
| <other-package_path>` | for Lua modules |
2525
+--------------------------------------+---------------------------------+
26-
| :ref:`package.cpath | Where Tarantool looks for C |
27-
| <other-package_cpath>` | additions |
26+
| :ref:`package.cpath | Get file paths used to search |
27+
| <other-package_cpath>` | for C modules |
2828
+--------------------------------------+---------------------------------+
29-
| :ref:`package.loaded | What Tarantool has already |
30-
| <other-package_loaded>` | looked for and found |
31-
+--------------------------------------+---------------------------------+
32-
| :ref:`package.setsearchroot | Set the root path for a |
33-
| <other-package_setsearchroot>` | directory search |
29+
| :ref:`package.loaded | Show Lua or C modules |
30+
| <other-package_loaded>` | loaded by Tarantool |
3431
+--------------------------------------+---------------------------------+
3532
| :ref:`package.searchroot | Get the root path for a |
3633
| <other-package_searchroot>` | directory search |
3734
+--------------------------------------+---------------------------------+
35+
| :ref:`package.setsearchroot | Set the root path for a |
36+
| <other-package_setsearchroot>` | directory search |
37+
+--------------------------------------+---------------------------------+
3838

3939

4040
.. _other-tonumber64:
@@ -140,59 +140,114 @@ and variables which are outside all modules.
140140

141141
.. data:: package.path
142142

143-
This is a string that Tarantool uses to search for Lua modules,
144-
especially important for ``require()``.
145-
See :ref:`Modules, rocks and applications <app_server-modules>`.
143+
Get file paths used to search for Lua :ref:`modules <app_server-modules>`.
144+
For example, these paths are used to find modules loaded using the ``require()`` directive.
145+
146+
See also: :ref:`package.searchroot() <other-package_searchroot>`
146147

147148
.. _other-package_cpath:
148149

149150
.. data:: package.cpath
150151

151-
This is a string that Tarantool uses to search for C modules,
152-
especially important for ``require()``.
153-
See :ref:`Modules, rocks and applications <app_server-modules>`.
152+
Get file paths used to search for C :ref:`modules <app_server-modules>`.
153+
For example, these paths are used to find modules loaded using the ``require()`` directive.
154+
155+
See also: :ref:`package.searchroot() <other-package_searchroot>`
154156

155157
.. _other-package_loaded:
156158

157159
.. data:: package.loaded
158160

159-
This is a string that shows what Lua or C modules Tarantool
160-
has loaded, so that their functions and members are available.
161-
Initially it has all the pre-loaded modules, which don't need
162-
``require()``.
161+
Show Lua or C modules loaded by Tarantool, so that their functions and members are available.
162+
``loaded`` shows both pre-loaded modules and modules added using the ``require()`` directive.
163+
164+
See also: :ref:`package.searchroot() <other-package_searchroot>`
165+
166+
.. _other-package_searchroot:
167+
168+
.. function:: package.searchroot()
169+
170+
Return the current search root, which defines the path to the root directory from which dependencies are loaded.
171+
By default, the search root is the current directory.
172+
173+
.. NOTE::
174+
175+
The current directory is obtained using :ref:`debug.sourcedir() <debug-sourcedir>`.
176+
177+
**Example**
178+
179+
Suppose the application has the following structure:
180+
181+
.. code-block:: none
182+
183+
/home/testuser/myapp
184+
├── .rocks/share/tarantool/
185+
│ └── foo.lua
186+
├── init.lua
187+
└── modules
188+
└── bar.lua
189+
190+
In this case, modules are placed in the same directory as the application initialization file.
191+
If you :ref:`run the application <app_server-launching_app_binary>` using the ``tarantool`` command from the ``myapp`` directory, ...
192+
193+
.. code-block:: console
194+
195+
/home/testuser/myapp$ tarantool init.lua
196+
197+
... the search root is ``/home/testuser/myapp`` and Tarantool finds all modules in this directory automatically.
198+
This means that to load the ``foo`` and ``modules.bar`` modules in ``init.lua``, you only need to add the corresponding ``require`` directives:
199+
200+
.. code-block:: lua
201+
202+
-- init.lua --
203+
require('foo')
204+
require('modules.bar')
205+
206+
Starting with :doc:`2.11.0 </release/2.11.0>`, you can also run the application using the ``tarantool`` command from the directory other than ``myapp``:
207+
208+
.. code-block:: console
209+
210+
/home/testuser$ tarantool myapp/init.lua
211+
212+
In this case, the path to the initialization file (``/home/testuser/myapp``) is added to search paths for modules.
213+
214+
To load modules placed outside of the path to the application directory, use :ref:`package.setsearchroot() <other-package_setsearchroot>`.
163215

164216
.. _other-package_setsearchroot:
165217

166218
.. function:: package.setsearchroot([search-root])
167219

168-
Set the search root. The search root is the root directory from
169-
which dependencies are loaded.
220+
Set the search root, which defines the path to the root directory from which dependencies are loaded.
221+
By default, the search root is the current directory (see :ref:`package.searchroot() <other-package_searchroot>`).
170222

171-
:param string search-root: the path. Default = current directory.
223+
:param string search-root: a relative or absolute path to the search root. If ``search-root`` is a relative path, it is expanded to an absolute path. You can omit this argument or set it to :ref:`box.NULL <box-null>` to reset the search root to the current directory.
172224

173-
The search-root string must contain a relative or absolute path.
174-
If it is a relative path, then it will be expanded to an
175-
absolute path.
176-
If search-root is omitted, or is box.NULL, then the search root
177-
is reset to the current directory, which is found with debug.sourcedir().
225+
**Example**
178226

179-
Example:
227+
Suppose external modules are stored outside the application directory, for example:
180228

181-
Suppose that a Lua file ``myapp/init.lua`` is the project root. |br|
182-
Suppose the current path is ``/home/tara``. |br|
183-
Add this as the first line of ``myapp/init.lua``: |br|
184-
:code:`package.setsearchroot()` |br|
185-
Start the project with |br|
186-
:code:`$ tarantool myapp/init.lua` |br|
187-
The search root will be the default, made absolute: ``/home/tara/myapp``.
188-
Within the Lua application all dependencies will be searched relative
189-
to ``/home/tara/myapp``.
229+
.. code-block:: none
190230
191-
.. _other-package_searchroot:
231+
/home/testuser/
232+
├── myapp
233+
│ └── init.lua
234+
└── mymodules
235+
├── .rocks/share/tarantool/
236+
│ └── foo.lua
237+
└── modules
238+
└── bar.lua
192239
193-
.. function:: package.searchroot()
240+
In this case, you can specify the ``/home/testuser/mymodules`` path as the search root for modules in the following way:
241+
242+
.. code-block:: lua
243+
244+
-- init.lua --
245+
package.setsearchroot('/home/testuser/mymodules')
246+
247+
Then, you can load the ``foo`` and ``bar`` modules using the ``require()`` directive:
194248

195-
Return a string with the current search root.
196-
After ``package.setsearchroot('/home')`` the returned
197-
string will be ``/home'``.
249+
.. code-block:: lua
198250
251+
-- init.lua --
252+
require('foo')
253+
require('modules.bar')

0 commit comments

Comments
 (0)