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

intern fails to rebind a referred var if that var is present in the "user" namespace #637

Closed
sritchie opened this issue Nov 23, 2021 · 2 comments

Comments

@sritchie
Copy link
Contributor

version

0.2.7

platform

JVM:

openjdk version "17.0.1" 2021-10-19 LTS
OpenJDK Runtime Environment Zulu17.30+15-CA (build 17.0.1+12-LTS)
OpenJDK 64-Bit Server VM Zulu17.30+15-CA (build 17.0.1+12-LTS, mixed mode, sharing)

problem

ns-unmap and intern are not successful at rebinding a referred var if that var shares a name with an existing var supplied via the 'user namespace or the :bindings context options key.

repro

(require '[sci.core :as sci])
(require '[clojure.test :refer [is]])

(let [remote {'cake "cake"}
        opts {:namespaces {'remote remote}
              :bindings {'cake "new-cake"}}]
    ;; First confirm that `cake` evaluates to "new-cake":
    (is (= "new-cake"
           (sci/eval-form (sci/init opts) 'cake)))

    ;; I can overwrite bindings with `ns-unmap` and `intern`:
    (is (= "bar"
           (sci/eval-form
            (sci/init opts)
            '(do (ns-unmap *ns* 'cake)
                 (intern *ns* 'cake "bar")
                 cake))))

    ;; BUG: Rebinding a referred var using `ns-unmap` and `intern` fails when
    ;; some var of the same name was set using the `:bindings` key.
    (is (= "cake"
           (sci/eval-form
            (sci/init opts)
            '(do (require '[remote :refer [cake]])
                 (ns-unmap *ns* 'cake)
                 (intern *ns* 'cake "bar")
                 cake))))

    ;; This is also a problem if the initial bindings are supplied to the
    ;; `'user` namespace (I am pretty sure these are equivalent)
    (is (= "cake"
           (sci/eval-form
            (sci/init (-> opts
                          (dissoc :bindings)
                          (assoc-in [:namespaces 'user] (:bindings opts))))
            '(do (require '[remote :refer [cake]])
                 (ns-unmap *ns* 'cake)
                 (intern *ns* 'cake "bar")
                 cake))))

    ;; If `'cake` is not present in bindings, or if `:bindings` is gone, the bug
    ;; goes away and we can rebind the referred var:
    (is (= "bar"
           (sci/eval-form
            (sci/init (update opts :bindings  dissoc 'cake))
            '(do (require '[remote :refer [cake]])
                 (ns-unmap *ns* 'cake)
                 (intern *ns* 'cake "bar")
                 cake)))))
@borkdude
Copy link
Collaborator

Should be fixed on master.

@sritchie
Copy link
Contributor Author

Thank you!

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

No branches or pull requests

2 participants