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

"Unable to resolve symbol: deftest" when using :refer :all #52

Closed
jumarko opened this issue Mar 19, 2018 · 12 comments
Closed

"Unable to resolve symbol: deftest" when using :refer :all #52

jumarko opened this issue Mar 19, 2018 · 12 comments

Comments

@jumarko
Copy link

jumarko commented Mar 19, 2018

I'm using flycheck-joker in spacemacs and I'm very happy with what it provides.
However, one thing that bothers me quite a lot is that joker reports deftest and is as unresolved symbols.
Here's the minimal repro for me:

(ns myapp.util-test
  (:require [myapp.util :as u]
                 [clojure.test :refer :all]))

(deftest namespaced-map
  (testing "keys are namespaced"
    (let [unqualified-keys {:name "Juraj" :age 32}
          qualified-keys #:user{:name "Juraj" :age 32}]
      (is (= qualified-keys unqualified-keys)))))

Here's the screenshot:
image

I realized that this might be because deftest and is are macros so I tried to add following to ~/.joker (inspired by https://github.com/candid82/joker#reducing-false-positives):

{:known-macros [clojure.test/deftest clojure.test/testing clojure.test/is clojure.test/are]}

No success, however.

Any ideas why that happens?
Is it something that should work out of the box?

@candid82
Copy link
Owner

I need to document this in readme :). No, if you use :refer :all Joker won't be able to properly resolve symbols because it doesn't know what vars are declared in the required namespace (clojure.test in this case). There are generally two options here:

  1. Refer specific symbols: [clojure.test :refer [deftest testing is are]]. There are not too many symbols in clojure.test namespace that are normally used, so this is not too tedious, and you only need to do it once per file.
  2. Use alias and qualified symbols:
(:require [clojure.test :as t])
(t/deftest ...)

I generally prefer first option for clojure.test namespace.

There is now a third option (not released yet): #50. It allows you to make Joker aware of any additional declarations (like deftest and is) by providing them in .jokerd/linter.clj[s|c] files. Note that such declarations become valid even if you don't require the namespace they come from, so this feature should be used sparingly (see discussion at #50 for more details).

@jumarko
Copy link
Author

jumarko commented Mar 19, 2018

Thanks for the clarification, very much appreciated.
I don't usually like to adjust my code just because of linter's business, however - and it's quite difficult when working on a large shared code base. Therefore, I'm looking forward to #50 being released :).

@trevor-brandt
Copy link

I appreciate all the discussion here. For the most part, this works as expected. I put this in my .jokerd/linter.cljd:

(defn deftest [name & body])
(defn is ([form]) ([form msg]))

And I put this in my .joker:

{:ignored-unused-namespaces [clojure.test]}

The one caveat I'm trying to get around is the name of every test is now being shown as invalid, because joker is linting deftest as a function, which would expect the first argument (the test name in this case) to be a valid symbol. But joker can't find it because it's a new symbol. Any thoughts?

Great work btw... new to the linter, but using it in vscode and loving it.

@candid82
Copy link
Owner

Try this:

(defmacro deftest [name & body] `(fn [] ~@body))

Pretty hacky, but should work. The linter will basically replace all your deftest forms with (fn [] <body>) at macro-expansion step and then lint the body as usual.

@trevor-brandt
Copy link

Ha, awesome. That did work, and it makes sense. Thanks for the help!

@candid82
Copy link
Owner

Closing this issue for now as there is nothing actionable here at the moment. I have some ideas on how to make Joker aware of commonly used namespaces (like clojure.test), but nothing concrete yet. I'll put a note about :refer :all in readme for now.

@agzam
Copy link

agzam commented Dec 3, 2018

@tjbrandt why not simply (declare deftest) in linter.clj ? I think that worked for me

@agzam
Copy link

agzam commented Dec 3, 2018

@candid82 Can you suggest anything for things like
(jdbc/with-db-transaction [t-conn *db*]
where it complains that unable to resolve symbol t-con?

@candid82
Copy link
Owner

candid82 commented Dec 4, 2018

@agzam it should not complain if you have clojure.java.jdbc/with-db-transaction in :known-macros in .joker file.

@candid82
Copy link
Owner

candid82 commented Dec 4, 2018

@agzam Thanks for (declare deftest) suggestion. It also works and simpler than the solution I proposed above.

@didibus
Copy link

didibus commented Jun 22, 2019

What I had to do was:

In .joker add:

{:ignored-unused-namespaces [clojure.test]
 :known-macros [deftest]}

In .jokerd/linter.clj add:

(in-ns 'joker.core)
(declare deftest)

@trevor-brandt
Copy link

trevor-brandt commented Jun 24, 2019

@didibus Thanks for dropping in on this discussion even though it's been around for a while. The defmacro solution was working for several months, but for some reason this seemed to regress a few months ago and started showing linter errors again for deftest, testing, etc. (I'm using Spacemacs and was having issues even with the most recent v0.12.4.) I just lived with it until... your in-ns form fixed it!

If I do exactly what you suggest in linter.clj, for some reason it still gives me an error on the name of the deftest. (For example, (deftest my-fn-test ...) gives an error on my-fn-test, because without knowing deftest is a macro, I believe the linter looks for my-fn-test to be a valid symbol. This might be pretty specific to my setup though, who knows.)

But it now works for me after adding your in-ns to my linter.clj. Here are my clojure.test declarations:

(in-ns 'joker.core)
(defmacro deftest [name & body] `(fn [] ~@body))
(defmacro testing [string & body] `(fn [] ~string ~@body))
(defn is ([form]) ([form msg]))
(defmacro are [argv expr & args] `(fn [] ~argv ~@expr ~@args))
(defn use-fixtures [fixture-type & args] fixture-type)

There might be a simpler way for some of these, but if I just use declare for deftest it was still giving me those errors. (Generally I'm needing to use defmacro when there's something in the form that doesn't resolve to a real symbol.)

Hope this helps someone! (@MicahElliott I know you were looking to a solution for this in the past too.)

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

5 participants