Skip to content

suggest-libspec is suggesting clojure.math as clj only in cljc files #408

Open
@dgtized

Description

@dgtized

Expected behavior

Typing math/ in a CLJC file should insert the libspec [clojure.math :as math] into the ns form.

Actual behavior

The libspec inserted is #?(:clj [clojure.math :as math]).

Steps to reproduce the problem

Using clj-refactor in a CLJC file type math/ in a namespace with no existing alias named math.

Commentary

This is using a default cljr-magic-require-namespaces which does not specify clojure.math as :only ("clj") but does include it as a magic namespace. This works correctly for clojure.set :as set and clojure.string :as str and even clojure.walk and clojure.zip, which are also listed in the magic namespaces, so it's confusing that clojure.math behaves differently. The project I'm testing with has a mixture of cljs and cljc files but has no instances where the math libspec is wrapped in a reader conditional. It does correctly return clojure.math :as math in cljs files in that project, but add the unnecessary :clj language conditional if math/ invokes cljr-slash in a CLJC file.

Note that clojure.math is one of the CLJS namespaces that is magically mapped to the equivalent namespace in Clojure as described in https://clojurescript.org/guides/ns-forms#_clojure_namespace_aliasing. I don't know if that's related but it seems like it might be.

Environment & Version information

clj-refactor.el and refactor-nrepl version information

clj-refactor 3.11.3, refactor-nrepl 3.10.0

CIDER version information

;; CIDER 1.14.0-snapshot (package: 20240510.1436), nREPL 1.1.1
;; Clojure 1.11.1, Java 17.0.10

Emacs version

GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.38, cairo version 1.18.0) of 2024-05-08

Operating system

Ubuntu 23.10

Activity

vemv

vemv commented on May 12, 2024

@vemv
Member

Hi @dgtized , good to see you here.

Can you attach the nrepl logs as well?

Thanks - V

dgtized

dgtized commented on May 13, 2024

@dgtized
Author
(-->
  id                           "416"
  op                           "cljr-suggest-libspecs"
  session                      "a8396f47-6c61-46f7-81dc-320bc33add03"
  time-stamp                   "2024-05-13 14:20:49.421752067"
  buffer-language-context      "cljc"
  debug                        "false"
  input-language-context       "cljc"
  insert-newline-after-require "true"
  language-context             "cljc"
  lib-prefix                   "math"
  preferred-aliases            "((\"edn\" . \"clojure.edn\") (\"io\" \"clojure.java.io\" :only (\"clj\")) (\"math\" . \"clojure.math\") (\"set\" . \"clojure.set\") (\"str\" . \"clojure.string\") (\"walk\" . \"clojure.walk\") (\"zip\" . \"clojure.zip\"))"
  prefix-rewriting             "false"
)
(<--
  id          "416"
  session     "a8396f47-6c61-46f7-81dc-320bc33add03"
  time-stamp  "2024-05-13 14:20:49.507559109"
  status      ("done")
  suggestions "[\"#?(:clj [clojure.math :as math])\"]"
)

Not sure if this is the right log, I toggled nrepl-toggle-message-logging and grabbed the output that seemed related. Is that what you meant by the nrepl logs?

vemv

vemv commented on May 13, 2024

@vemv
Member

Yes, that's very useful as it includes the entire preferred-aliases value, besides from language-context, etc.

I'll try to give it a shot, if there are no news, feel free to nudge.

Or if you'd be willing, you can try reproducing the bug by adding that data to

#_lib-prefix #_buffer-lc #_input-lc #_preferred-aliases #_project-libspecs #_expected
;; Some basic examples. I chose 'donkey' as a unique alias. Note that `project-libspecs` is {}, so a real project analysis is performed
;; (Which is why I chose a unique ns segment)
"donkey" "clj" "clj" [] {} ["[donkey.jvm :as donkey]"]
"donkey" "cljs" "cljs" [] {} ["[donkeyscript :as donkey]"]
"donkey" "cljc" "clj" [] {} ["#?(:clj [donkey.jvm :as donkey])"]
"donkey" "cljc" "cljs" [] {} ["#?(:cljs [donkeyscript :as donkey])"]
"donkey" "cljc" "cljc" [] {} ["#?(:clj [donkey.jvm :as donkey]\n :cljs [donkeyscript :as donkey])"]
- should be an easy way to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @dgtized@vemv

        Issue actions

          suggest-libspec is suggesting clojure.math as clj only in cljc files · Issue #408 · clojure-emacs/refactor-nrepl