Clarify remapping

Update layout-of-source-files.rst
This commit is contained in:
Chris Ward 2018-08-17 12:51:32 +02:00
parent 6c0261e6ab
commit 448118caec

View File

@ -96,43 +96,44 @@ filesystem, it can also map to resources discovered via e.g. ipfs, http or git.
Use in Actual Compilers Use in Actual Compilers
----------------------- -----------------------
When the compiler is invoked, it is not only possible to specify how to When invoking the compiler, you can specify how to discover the first element
discover the first element of a path, but it is possible to specify path prefix of a path, and also path prefix remappings. For
remappings so that e.g. ``github.com/ethereum/dapp-bin/library`` is remapped to example you can setup a remapping so that everything imported from the virtual
``/usr/local/dapp-bin/library`` and the compiler will read the files from there. directory ``github.com/ethereum/dapp-bin/library`` would actually be read from
If multiple remappings can be applied, the one with the longest key is tried first. your local directory ``/usr/local/dapp-bin/library``.
An empty prefix is not allowed. Furthermore, these remappings can depend on the context, If multiple remappings apply, the one with the longest key is tried first.
which allows you to configure packages to import e.g. different versions of a library An empty prefix is not allowed. The remappings can depend on a context,
of the same name. which allows you to configure packages to import e.g., different versions of a
library of the same name.
**solc**: **solc**:
For solc (the commandline compiler), these remappings are provided as For solc (the commandline compiler), you provide these path remappings as
``context:prefix=target`` arguments, where both the ``context:`` and the ``context:prefix=target`` arguments, where both the ``context:`` and the
``=target`` parts are optional (where target defaults to prefix in that ``=target`` parts are optional (``target`` defaults to ``prefix`` in this
case). All remapping values that are regular files are compiled (including case). All remapping values that are regular files are compiled (including
their dependencies). This mechanism is completely backwards-compatible (as long their dependencies).
as no filename contains = or :) and thus not a breaking change. All imports
in files in or below the directory ``context`` that import a file that
starts with ``prefix`` are redirected by replacing ``prefix`` by ``target``.
So as an example, if you clone This mechanism is backwards-compatible (as long
``github.com/ethereum/dapp-bin/`` locally to ``/usr/local/dapp-bin``, you can use as no filename contains ``=`` or ``:``) and thus not a breaking change. All
the following in your source file: files in or below the ``context`` directory that import a file that starts with
``prefix`` are redirected by replacing ``prefix`` by ``target``.
For example, if you clone ``github.com/ethereum/dapp-bin/`` locally to
``/usr/local/dapp-bin``, you can use the following in your source file:
:: ::
import "github.com/ethereum/dapp-bin/library/iterable_mapping.sol" as it_mapping; import "github.com/ethereum/dapp-bin/library/iterable_mapping.sol" as it_mapping;
and then run the compiler as Then run the compiler:
.. code-block:: bash .. code-block:: bash
solc github.com/ethereum/dapp-bin/=/usr/local/dapp-bin/ source.sol solc github.com/ethereum/dapp-bin/=/usr/local/dapp-bin/ source.sol
As a more complex example, suppose you rely on some module that uses a As a more complex example, suppose you rely on a module that uses an old
very old version of dapp-bin. That old version of dapp-bin is checked version of dapp-bin that you checked out to ``/usr/local/dapp-bin_old``, then you can run:
out at ``/usr/local/dapp-bin_old``, then you can use
.. code-block:: bash .. code-block:: bash
@ -140,28 +141,29 @@ out at ``/usr/local/dapp-bin_old``, then you can use
module2:github.com/ethereum/dapp-bin/=/usr/local/dapp-bin_old/ \ module2:github.com/ethereum/dapp-bin/=/usr/local/dapp-bin_old/ \
source.sol source.sol
so that all imports in ``module2`` point to the old version but imports This means that all imports in ``module2`` point to the old version but imports
in ``module1`` get the new version. in ``module1`` point to the new version.
Note that solc only allows you to include files from certain directories: .. note::
They have to be in the directory (or subdirectory) of one of the explicitly
specified source files or in the directory (or subdirectory) of a remapping ``solc`` only allows you to include files from certain directories. They have
target. If you want to allow direct absolute includes, just add the to be in the directory (or subdirectory) of one of the explicitly specified
remapping ``/=/``. source files or in the directory (or subdirectory) of a remapping target. If
you want to allow direct absolute includes, add the remapping ``/=/``.
If there are multiple remappings that lead to a valid file, the remapping If there are multiple remappings that lead to a valid file, the remapping
with the longest common prefix is chosen. with the longest common prefix is chosen.
**Remix**: **Remix**:
`Remix <https://remix.ethereum.org/>`_ `Remix <https://remix.ethereum.org/>`_ provides an automatic remapping for
provides an automatic remapping for github and will also automatically retrieve GitHub and automatically retrieves the file over the network. You can import
the file over the network: the iterable mapping as above, e.g.
You can import the iterable mapping by e.g.
``import "github.com/ethereum/dapp-bin/library/iterable_mapping.sol" as it_mapping;``.
Other source code providers may be added in the future. ::
import "github.com/ethereum/dapp-bin/library/iterable_mapping.sol" as it_mapping;
Remix may add other source code providers in the future.
.. index:: ! comment, natspec .. index:: ! comment, natspec