Skip to content

require with relative path crashes the compiler #1897

@daehwannam

Description

@daehwannam

I wrote the following scripts where file_b.hy tries to get macro do-twice in file_a.hy via "require" with relative paths.

relative_test.zip

  • directories & files
    relative_test
    ├── dir1
    │ ├── dir2
    │ │ └── file_a.hy
    │ └── dir3
    │ └── file_b.hy
    └── main.hy

  • main.hy
    (import dir1.dir3.file_b)

  • file_a.hy
    (defmacro do-twice [x] `(do ~x ~x))
    (defn call-twice [f] (f) (f))

  • file_b.hy
    (import [..dir2.file_a [call-twice]])
    (call-twice (fn [] (print "hello")))
    ;; the code below raise error
    (require [..dir2.file_a [do-twice]]) ; it works when "..dir2.file_a" is replaced with "dir1.dir2.file_a"
    (do-twice (print "hello"))

When I run main.hy, the import statement in file_b.hy works to get function call-twice (if require statement is commented), but the require statement doesn't work. I think require statement works with absolute path but it doesn't work with relative path. Is it bug or unintended use of require statement?

Here is the execution result of the scripts:
----------------------------------------------shell-begin----------------------------------------------
user@ubuntu:/home/data/user/exercise/hy/relative_test$ hy main.hy
Traceback (most recent call last):
File "/home/user/bin/anaconda3/envs/test/bin/hy", line 10, in
sys.exit(hy_main())
File "/home/user/bin/anaconda3/envs/test/lib/python3.6/runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "/home/user/bin/anaconda3/envs/test/lib/python3.6/runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "/home/user/bin/anaconda3/envs/test/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/data/user/exercise/hy/relative_test/main.hy", line 2, in
(import dir1.dir3.file_b)
File "", line 971, in _find_and_load
File "", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked
File "", line 674, in exec_module
File "", line 781, in get_code
File "/home/user/bin/anaconda3/envs/test/lib/python3.6/importlib/init.py", line 121, in import_module
raise TypeError(msg.format(name))
hy.errors.HyCompileError: Internal Compiler Bug 😱
⤷ Traceback (most recent call last):
File "/home/user/bin/anaconda3/envs/test/lib/python3.6/site-packages/hy/compiler.py", line 433, in compile
ret = self.compile_atom(tree)
File "/home/user/bin/anaconda3/envs/test/lib/python3.6/site-packages/hy/compiler.py", line 427, in compile_atom
return Result() + _model_compilers[type(atom)](self, atom)
File "/home/user/bin/anaconda3/envs/test/lib/python3.6/site-packages/hy/compiler.py", line 1777, in compile_expression
self, expr, unmangle(sroot), *parse_tree)
File "/home/user/bin/anaconda3/envs/test/lib/python3.6/site-packages/hy/compiler.py", line 1196, in compile_import_or_require
prefix=prefix):
File "/home/user/bin/anaconda3/envs/test/lib/python3.6/site-packages/hy/macros.py", line 175, in require
source_module = importlib.import_module(source_module)
File "/home/user/bin/anaconda3/envs/test/lib/python3.6/importlib/init.py", line 121, in import_module
raise TypeError(msg.format(name))
TypeError: the 'package' argument is required to perform a relative import for '..dir2.file_a'

user@ubuntu:/home/data/user/exercise/hy/relative_test$
----------------------------------------------shell-end----------------------------------------------

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions