Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path resolution docs (current state only) v2 #11442

Merged
merged 1 commit into from
Jun 9, 2021

Conversation

cameel
Copy link
Member

@cameel cameel commented May 26, 2021

Replaces #11263 and #11105.
Related to #11138, #11036 and #9353.

This PR contains the parts of #11263 that are suitable for inclusion in the docs. I removed most of the examples and focused on laying out the rules. Some parts were also rewritten to make sense without the examples - for example the rules for relative imports are now stated more explicitly.

@cameel cameel self-assigned this May 26, 2021

Remix may add other source code providers in the future.
For a complete description of the virtual filesystem and the path resolution logic used by the
compiler see :ref:`Path Resolution <path-resolution>`.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should put the docs back here now that they are shorter?

They're still long enough to warrant a separate page so I left them as a separate page for now but I can easily change it.

@cameel cameel force-pushed the path-resolution-docs-current-state-v2 branch from 97d065f to 0ae7463 Compare May 28, 2021 15:25
@cameel
Copy link
Member Author

cameel commented May 28, 2021

I have just pushed an update. Most of the intro to the section about remappings has been rewritten. I think that addresses all the review commments.

@cameel
Copy link
Member Author

cameel commented May 28, 2021

For reference, I found the first version of the remapping docs: #348
This sheds some light on how they were originally intended to be used.

The Host Filesystem Loader will attempt to use it as a path and look up the file on disk.
At this point the platform-specific normalization rules kick in and ``/project/lib/math.sol`` and
``/project/lib/../lib///math.sol`` may actually result in the same file being loaded.
Note, however, that the compiler will still see them as separate source units that just happen to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could put this also in a .. note:: section?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to change it but left it as is after all. This sentence is strongly connected to this paragraph and I think that putting in a note (or even just in a separate paragraph) makes it read weird.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's rather confusing because the two import paths somehow appear out of nowhere and it's not clear what they refer to.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, now I get what the problem is. I have reworded that paragraph and changed how the examples are introduced so that now it makes sense as an actual note.

@cameel
Copy link
Member Author

cameel commented May 28, 2021

Updated. All new changes are in a single fixup commit on top.

Imports
=======

The import statement specifies an *import path*, which, after being lightly processed, becomes a
Copy link
Contributor

@chriseth chriseth Jun 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The import statement specifies an *import path*, which, after being lightly processed, becomes a
The import statement specifies an *import path*. This path, when relative (it starts with `./` or `../`), is combined with the path of the importing source unit to form the imported source unit name. If it is not relative, it is taken as the imported source unit name directly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, this ignores remappings. But still, I think we should replace "lightly processed" by a short summary of the processing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the paragraph about two kinds of import above the example and mentioned ./ and ../. Is that ok?

Comment on lines 230 to 233
Note the difference between a *relative import* (e.g. ``import "./util.sol"``) and a
*direct import with a relative path* (e.g. ``import "util.sol"``).
It is a relative import only if it starts with ``./`` or ``../``.
Otherwise it is a direct import.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried that the term "direct import with a relative path" could be confusing. Maybe we could say that unless the import starts with ./ or ../ it is a direct import and is thus absolute and imports do not have to start with / to be absolute or something...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this?

.. note::

    ``import "./util.sol"`` is a relative import but ``import "util.sol"`` is a direct one.
    Relative imports **must** start with ``./`` or ``../``.
    VFS does not have a root directory and absolute paths do not have to start with a slash.
    While both paths would be considered relative in the host filesystem, ``util.sol`` is actually
    absolute in the VFS.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

("must start" sound like it is an error otherwise)

.. note::

    Imports that start with `./` or `../` are relative imports, while all others are direct (absolute) imports.
    Direct imports do to need to start with `/`. This, ``import "./util.sol"`` is a relative import but ``import "util.sol"`` is a direct one.
    While both paths would be considered relative in the host filesystem, ``util.sol`` is actually
    absolute in the VFS.

"The VFS does not have a root directory" - of course it has, that's where all the topmost files are. You just don't need to use / to reference the root directory.

But yeah, this is probably not too important.

Copy link
Member Author

@cameel cameel Jun 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really, really do not want to use the term absolute import :)

Point taken about must. Replaced with always, which I hope does not have this connotation.

"The VFS does not have a root directory" - of course it has, that's where all the topmost files are. You just don't need to use / to reference the root directory.

I think one could also argue that all the topmost items are just floating there without any directory containing them but that would be splitting hairs and a root with an empty name makes sense too so I just removed that part for simplicity:)

So here's another attempt, combining my and your version:

.. note::

    Relative imports **always** start with ``./`` or ``../`` so ``import "util.sol"``, unlike
    ``import "./util.sol"``, is a direct import.
    While both paths would be considered relative in the host filesystem, ``util.sol`` is actually
    absolute in the VFS.

1. First a prefix is computed

- Prefix is initialized with the source unit name of the importing source unit.
- The last segment is removed from the prefix.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- The last segment is removed from the prefix.
- The last segment (the file name) is removed from the prefix.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also "segment" is introduced here without explanation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was sure it was a common term but apparently it's not. The Wikipedia page for filename does not even mention it.

While segment was not defined, "removal of the last segment" was.

Anyway, added a definition.


- Prefix is initialized with the source unit name of the importing source unit.
- The last segment is removed from the prefix.
- Then, for every leading ``../`` segment in the normalized import path the last segment is repeatedly removed from the prefix.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first mention of "normalized" - in which way is it normalized?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's explained below. I think it flows better that way because first you see the outline and only then the details are filled in. I could add "see below" but there would be a lot of that so I thought it was better as it is now.


For security reasons the compiler has restrictions on what directories it can access.
Paths of source files (and their subdirectories) specified on the command line and paths defined by
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Paths of source files (and their subdirectories) specified on the command line and paths defined by
Directories of source files (and their subdirectories) specified on the command line and paths defined by

(Is it directories or paths?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's directories. Changed to:

Parent directories of source files specified on the command line and target paths of remappings ...

I removed the part about subdirectories because I think it should be pretty intuitive. I'd expect this detail to be mentioned only if it was the opposite.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if you write 'Parent directories', I would interpret this as all (super-)directories leading up to the file, i.e. if you call solc /a/b/c/d.sol, then everything inside /a is accessible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Why do all the terms have to be so ambiguous!

Anyway, changed parent directories to directories now.

@cameel cameel force-pushed the path-resolution-docs-current-state-v2 branch 2 times, most recently from 296dda0 to 23a3781 Compare June 8, 2021 16:20
@cameel
Copy link
Member Author

cameel commented Jun 8, 2021

Pushed an updated version. Ready for another round. I think I have addressed all the comments.

@cameel cameel requested review from chriseth and aarlt June 8, 2021 16:24
aarlt
aarlt previously approved these changes Jun 8, 2021
Copy link
Member

@aarlt aarlt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for me!

@cameel cameel force-pushed the path-resolution-docs-current-state-v2 branch from dd89f53 to 5e37888 Compare June 8, 2021 19:42
@cameel
Copy link
Member Author

cameel commented Jun 8, 2021

Added fixes for two of @chriseth's comments.

I also squashed the fixups to make it ready for merge.

@chriseth chriseth merged commit 8de575f into develop Jun 9, 2021
@chriseth chriseth deleted the path-resolution-docs-current-state-v2 branch June 9, 2021 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants