In the following directory structure:
.
├── a
│ ├── 1
│ ├── 2
│ ├── 3
│ └── 4
├── b
│ ├── 5
│ ├── 6
│ ├── 7
│ └── 8
├── c
├── d
└── test.hy
The following code prints the wrong path:
(eval-and-compile (import os) (import pathlib [Path]))
(defmacro with-cwd [dir #* body]
`(let [ cwd (.cwd Path) ]
(try (.chdir os ~dir)
~@body
(finally (.chdir os cwd)))))
(defmacro let-cwd [dir vars #* body] `(let ~vars (with-cwd ~dir ~@body)))
(setv a (/ (. (Path __file__) parent) "a"))
(let-cwd a [ b (/ a.parent "b") ] (print f"Hello from {(.cwd Path)}!\n") (print (.resolve b)))
It is supposed to print the following:
Hello from /home/shadowrylander/with-cwd-test/a!
/home/shadowrylander/with-cwd-test/b
While it is instead printing:
Hello from /home/shadowrylander/with-cwd-test/a!
/home/shadowrylander/with-cwd-test/a/b
Why is b
not properly assigned when doing (quasiquote (let ~vars (with-cwd ~dir ~@body)))
?
This looks like a bug (https://github.com/hylang/hy/issues/2318), although it has to do with __file__
and is unrelated to let
or macros. Remember, when you run into trouble, simplify your problematic code as much as you can, so you can figure out what's going on.