Welcome! Please see the About page for a little more info on how this works.

0 votes
in Syntax and reader by

Reproducible test case:

(ns bug)
(require '[clojure.data.xml :as xml] #?@(:cljs [[clojure.data.xml.js.dom :as dom]]))
(def opts {:features #{:clj} :read-cond :allow})
(read-string opts "#?(:cljs {:a ::dom/Element})")
; Execution error at bug/eval30984 (REPL:534).
; Invalid token: ::dom/Element

Stack trace:

clojure.lang.Util/runtimeException (Util.java:221)
clojure.lang.LispReader/interpretToken (LispReader.java:412)
clojure.lang.LispReader/read (LispReader.java:305)
clojure.lang.LispReader/readDelimitedList (LispReader.java:1398)
clojure.lang.LispReader$MapReader/invoke (LispReader.java:1355)
clojure.lang.LispReader/read (LispReader.java:285)
clojure.lang.LispReader/access$900 (LispReader.java:41)
clojure.lang.LispReader$ConditionalReader/readCondDelimited (LispReader.java:1584)
clojure.lang.LispReader$ConditionalReader/invoke (LispReader.java:1659)
clojure.lang.LispReader$DispatchReader/invoke (LispReader.java:853)
clojure.lang.LispReader/read (LispReader.java:285)
clojure.lang.RT/readString (RT.java:1876)
clojure.core/read-string (core.clj:3817)
clojure.core/read-string (core.clj:3806)
bug/eval30984 (NO_SOURCE_FILE:534)
clojure.lang.Compiler/eval (Compiler.java:7194)

Real-world scenario:

Reading data.xml-0.2.0-alpha8/clojure/data/xml/spec.cljc errors in line 45: https://github.com/clojure/data.xml/blob/v0.2.0-alpha8/src/main/resources/clojure/data/xml/spec.cljc#L45

Note that stack trace is different there:

; Caused by: clojure.lang.ExceptionInfo: [line 45, col 28] Invalid keyword: ::dom/Element. {:type :reader-exception, :ex-kind :reader-error, :file nil, :line 45, :col 28}
; 	at clojure.tools.reader.impl.errors$throw_ex.invokeStatic(errors.clj:34)
; 	at clojure.tools.reader.impl.errors$throw_ex.doInvoke(errors.clj:24)
; 	at clojure.lang.RestFn.invoke(RestFn.java:442)
; 	at clojure.tools.reader.impl.errors$reader_error.invokeStatic(errors.clj:40)
; 	at clojure.tools.reader.impl.errors$reader_error.doInvoke(errors.clj:36)
; 	at clojure.lang.RestFn.invoke(RestFn.java:516)
; 	at clojure.tools.reader.impl.errors$throw_invalid.invokeStatic(errors.clj:97)
; 	at clojure.tools.reader.impl.errors$throw_invalid.invoke(errors.clj:96)
; 	at clojure.tools.reader$read_keyword.invokeStatic(reader.clj:358)
; 	at clojure.tools.reader$read_keyword.invoke(reader.clj:344)
; 	at clojure.tools.reader$read_STAR_.invokeStatic(reader.clj:935)
; 	at clojure.tools.reader$read_STAR_.invoke(reader.clj:917)
; 	at clojure.tools.reader$read_delimited.invokeStatic(reader.clj:198)
; 	at clojure.tools.reader$read_delimited.invoke(reader.clj:191)
; 	at clojure.tools.reader$read_list.invokeStatic(reader.clj:209)
; 	at clojure.tools.reader$read_list.invoke(reader.clj:205)
; 	at clojure.tools.reader$read_STAR_.invokeStatic(reader.clj:935)
; 	at clojure.tools.reader$read_STAR_.invoke(reader.clj:917)
; 	at clojure.tools.reader$read_delimited.invokeStatic(reader.clj:198)
; 	at clojure.tools.reader$read_delimited.invoke(reader.clj:191)
; 	at clojure.tools.reader$read_list.invokeStatic(reader.clj:209)
; 	at clojure.tools.reader$read_list.invoke(reader.clj:205)
; 	at clojure.tools.reader$read_STAR_.invokeStatic(reader.clj:935)
; 	at clojure.tools.reader$read_STAR_.invoke(reader.clj:917)
; 	at clojure.tools.reader$read_suppress.invokeStatic(reader.clj:451)
; 	at clojure.tools.reader$read_suppress.invoke(reader.clj:447)
; 	at clojure.tools.reader$match_feature.invokeStatic(reader.clj:474)
; 	at clojure.tools.reader$match_feature.invoke(reader.clj:458)
; 	at clojure.tools.reader$read_cond_delimited$fn__8556.invoke(reader.clj:485)
; 	at clojure.tools.reader$read_cond_delimited.invokeStatic(reader.clj:480)
; 	at clojure.tools.reader$read_cond_delimited.invoke(reader.clj:477)
; 	at clojure.tools.reader$read_cond.invokeStatic(reader.clj:522)
; 	at clojure.tools.reader$read_cond.invoke(reader.clj:506)
; 	at clojure.tools.reader$read_dispatch.invokeStatic(reader.clj:72)
; 	at clojure.tools.reader$read_dispatch.invoke(reader.clj:68)
; 	at clojure.tools.reader$read_STAR_.invokeStatic(reader.clj:935)
; 	at clojure.tools.reader$read_STAR_.invoke(reader.clj:917)
; 	at clojure.tools.reader$read.invokeStatic(reader.clj:988)
; 	at clojure.tools.reader$read.invoke(reader.clj:961)

1 Answer

0 votes
by

Autoresolved keywords require namespace resolution (the main feature of the reader that requires external environment).

Because reader conditionals "read" all of the parts of the conditional, it is necessary that anything in the conditional is readable in every platform, and thus autoresolved keywords in reader conditionals should not rely on conditional namespaces.

This should be considered the expected behavior, so don't do that.

We have considered whether it would be possible to suspend autoresolved keyword resolution in the context of reader conditionals but it is somewhat tricky, and I don't think we're going to do that anytime soon.

by
I created a data.xml jira to stop doing that. https://clojure.atlassian.net/browse/DXML-72
by
Interesting, thanks for the explanation. Is there any way to workaround this temporarily?

Can we make clojure evaluator raise an error or a warning if this isn't supported? Or maybe clj-kondo and other linters should be highlighting that as an error?

OTOH, why can't we simply ignore anything that's suppressed?
by
We can ignore it, but we have to read it to know what to ignore. You can work around it by simply not using auto resolved keywords in reader conditionals.
by
Thanks! I meant a workaround for the situation when I want to read a 3rd party code containing them.
by
No, I don't think there is any workaround, unless maybe you created the namespace yourself first and created the alias to a different (existing) namespace before loading.
...