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

Dont remove ns requires without an alias #359

Closed
dpsutton opened this issue Jan 21, 2022 · 9 comments
Closed

Dont remove ns requires without an alias #359

dpsutton opened this issue Jan 21, 2022 · 9 comments

Comments

@dpsutton
Copy link

Clj-kondo and refactor-nrepl disagree about how to clean ns forms and it would be nice if we could unify their notions. The problem is around how to tell the linter to not remove a namespace require which has no visible usages in the buffer, but is required for side effects. Refactor nrepl will remove them unconditionally and there seems to be an agreed upon workaround to include a form

(ns (:require [foo.bar :as bar]))
(comment
  (bar/keep-me))

Clj-kondo takes the approach that any require that does not have an alias is presumably there for side effects.

(ns foo
  (:require [foo.bar]
            foo.bar.baz))

In this form both foo.bar and foo.bar.baz will remain because they do not have aliases.

This analysis also powers how clojure-lsp will clean the ns form.

It would be nice if refactor-nrepl would align to this style. Clj-kondo is by far the most popular linter and will complain about the unresolved symbols in the bar/keep-me form. Clojure-lsp is powered by clj-kondo's analysis and will count those alias use sites as usages and keep the requires they intend to keep, but will also keep them if the alias is dropped since it uses clj-kondo's analysis. Adopting this style in refactor-nrepl would mean less work for users of refactor-nrepl and no tension with the other tooling.

@borkdude
Copy link

Note that there are two conditions: no alias or no referred vars.

@magnars
Copy link
Contributor

magnars commented Jan 21, 2022

Agreed. I remember this being discussed here before, but I don't remember why it wasn't changed. Maybe @expez has a clue?

@borkdude
Copy link

Found an issue here: #292

@expez
Copy link
Member

expez commented Jan 23, 2022

Agreed. I remember this being discussed here before, but I don't remember why it wasn't changed. Maybe @expez has a clue?

Nobody's added the setting. That's the only reason 🙂 Personally I prefer the workaround in #292 (and I don't write much cljs, where it's not available!) because I think it's helpful to indicate that the required ns is special and I also always add a comment about why we need those side-effects.

@vemv
Copy link
Member

vemv commented Jan 23, 2022

From clj-kondo/clj-kondo#241 which dates back to 2019 a few things can be observed:

  • The most-voted option was ignored
  • The chosen option was acknowledged to yield false negatives
  • These days, projects can have kondo config that would be propagated to lib consumers
    • This can be used to mark side-effectful namespaces, which could be part of clj-kondo's logic

I'm particularly concerned about intentionally introducing false negatives in other tools.

And also trying to follow any 'conventions' that aren't described in https://clojure.org/ seems a pretty bad precedent - it would be similar to making other tools honor #_:clj-kondo/ignore.

I'd be happy if we could revisit the topic in a particularly productive fashion - maybe opening a poll on the internet isn't exactly bound to give optimal results. Surely it should be a matter of "who's going to adhere by this convention", which is ultimately a tooling maker's concern.

As a quick note I like .cljfmt.edn files (which appear to be a clojure-lsp thing), couldn't a simiar edn file be a good cross-tooling standard? Similar to EditorConfig.

@vemv
Copy link
Member

vemv commented Jan 23, 2022

Refactor nrepl will remove them unconditionally and there seems to be an agreed upon workaround to include a [(comment)] form

@dpsutton: this is incorrect, there's the :libspec-whitelist global option and also you can use top-level requires in JVM clojure.

@vemv
Copy link
Member

vemv commented Jan 23, 2022

Closing in face of the post above, using :libspec-whitelist removes the practical conflict that is described. i.e. you can use clj-kondo's convention + :libspec-whitelist, no part is going to complain.

There's a slight room for a PR (inferring the whitelist if the convention is followed), however that would intentionally introduce false negatives, so I don't think it would be a great addition (at least not without pinging again a variety of tool makers).

Cheers - V

@vemv vemv closed this as completed Jan 23, 2022
@vemv
Copy link
Member

vemv commented Jan 24, 2022

...One interesting thing refactor-nrepl could start doing is reading .clj-kondo/config files in search of https://github.com/clj-kondo/clj-kondo/blob/504077db186c00e09ec16bd85c01ebafa2cb86f1/doc/linters.md#unused-namespace config.

It's essentially the same as our own :libspec-whitelist, but in a better-known format which is just data, keeping things DRY between different tools, for end users.

Will hammock and likely impl.

@vemv
Copy link
Member

vemv commented Feb 8, 2022

Today's refactor-nrepl + clj-refactor releases honor Kondo :unused-namespace config.

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