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

fix[lang]: disallow absolute relative imports #4268

Merged
merged 36 commits into from
Dec 17, 2024

Conversation

sandbubbles
Copy link
Collaborator

@sandbubbles sandbubbles commented Oct 1, 2024

What I did

How I did it

  • Removed implicit relative imports in submodules, eliminating ambiguity in import paths.
  • Removed the lines that added the current directory to the top of the search paths. As a result, the poke_search_path function may no longer be necessary.

How to verify it

  • added tests

Commit message

this commit changes the behavior of search paths within
submodules. previously the current module's parent was appended to
the search path in the import recursion. this means that given the
following directory structure, this code is legal:

```
# directory structure:
# subdir/foo.vy
# subdir/bar.vy
```

```
# subdir/foo.vy
import bar
```

which has the same semantics as

```
# subdir/foo.vy
from . import bar
```

this represented a divergence wrt python's import system. in python,
only the second example is allowed.

this commit changes the semantics to match python, so that relative
imports use the current directory as search path, but absolute imports
can only use the top-level search path. (the directory containing the
top-level compilation target is still included in the search path, but
this only happens at the top of the recursion, same as in python -
https://docs.python.org/3/library/sys_path_init.html).

misc/refactor:
- remove dead `add_search_path()` method
- remove now-dead `poke_search_path()`

Description for the changelog

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

@sandbubbles sandbubbles changed the title fix[lang]: disallow absolute relative paths fix[lang]: disallow absolute relative imports Oct 1, 2024
Copy link
Member

@charles-cooper charles-cooper left a comment

Choose a reason for hiding this comment

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

yes, i think poke_search_paths is probably dead now. can you check? if so, let's remove it!

@@ -789,11 +789,7 @@ def _add_import(
# load an InterfaceT or ModuleInfo from an import.
# raises FileNotFoundError
def _load_import(self, node: vy_ast.VyperNode, level: int, module_str: str, alias: str) -> Any:
Copy link
Member

Choose a reason for hiding this comment

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

i guess we can fuse load_import_helper right?

Copy link

codecov bot commented Oct 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.46%. Comparing base (ebe26a6) to head (be01034).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4268      +/-   ##
==========================================
- Coverage   91.12%   88.46%   -2.67%     
==========================================
  Files         115      115              
  Lines       16235    16236       +1     
  Branches     2732     2733       +1     
==========================================
- Hits        14794    14363     -431     
- Misses       1004     1352     +348     
- Partials      437      521      +84     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cyberthirst
Copy link
Collaborator

cyberthirst commented Oct 17, 2024

@charles-cooper, have you considered the concept of packages? because we don't have it, we allow relative imports that can point even outside the project's directory

@cyberthirst
Copy link
Collaborator

cyberthirst commented Oct 29, 2024

yeah i know, those will raise a compiler panic if you try to create an input bundle.

i have main.vy in ./tests/custom and run vyper from the same dir as in which tests are

from .... import lib1

exports: lib1.__interface__

so the import targets a file which is in the parent of the project's dir (from which vyper's run) .. and it compiles fine

i tried to create an output bundle (vyper -f archive) with this setup and it led to compiler panic

Error compiling: tests/custom/test4.vy
vyper.exceptions.CompilerPanic: Invalid path: /Users/blabla/lib1.vy

This is an unhandled internal compiler error. Please create an issue on Github to notify the developers!
https://github.com/vyperlang/vyper/issues/new?template=bug.md

@charles-cooper
Copy link
Member

@sandbubbles please resolve merge conflicts

res = self.input_bundle.load_file(path)

if level != 0:
self.input_bundle.search_paths += [current_search_path]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self.input_bundle.search_paths += [current_search_path]
self.input_bundle.search_paths.append(current_search_path)

Copy link
Member

Choose a reason for hiding this comment

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

this is kind of weird actually because on line 221, we already set search_paths to [current_search_path]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think the issue is we need to set it temporarily to only one path, but then we need to add it to search paths in case there is an absolute import that uses it

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

i pushed a clean up in a140226

Copy link
Collaborator Author

@sandbubbles sandbubbles Dec 14, 2024

Choose a reason for hiding this comment

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

I must have though that example such as the following should work (but its what we were fixing in the first place).

#a.vy
from .subdir import b

and

#b.vy
from subdir.subsubdir import c

@cyberthirst cyberthirst added the release - must release blocker label Dec 11, 2024
@sandbubbles
Copy link
Collaborator Author

This should be ready for rereview

@charles-cooper charles-cooper added this to the v0.4.1 milestone Dec 12, 2024
Copy link
Member

@charles-cooper charles-cooper left a comment

Choose a reason for hiding this comment

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

lgtm, thank you

@charles-cooper charles-cooper enabled auto-merge (squash) December 17, 2024 18:48
@charles-cooper charles-cooper merged commit b0ea1b3 into vyperlang:master Dec 17, 2024
159 of 160 checks passed
pcaversaccio added a commit to pcaversaccio/snekmate that referenced this pull request Dec 18, 2024
### 🕓 Changelog

This commit updates the version `pragma`s in all Vyper source files to
target the latest `master` version `v0.4.1b4`, aligning with the release
of Vyper's newest beta version,
[`v0.4.1b3`](https://github.com/vyperlang/vyper/releases/tag/v0.4.1b3).
Additionally, this PR addresses a disallowed pattern in the
`IERC1155MetadataURI.vyi` interface definition by fixing the use of
absolute relative imports, which are prohibited in Vyper since PR
[#4268](vyperlang/vyper#4268).

---------

Signed-off-by: Pascal Marco Caversaccio <pascal.caversaccio@hotmail.ch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release - must release blocker
Projects
None yet
Development

Successfully merging this pull request may close these issues.

import system allows absolute relative imports
3 participants