6
votes

lein cljsbuild is having trouble finding a namespace/file that's defined right alongside another namespace/file unless I make sure they're compiled in a certain order.

My directory layout looks like this:

project/
  project.clj
  src/
    cljs/
      contempo/
        common/
          defs.cljs
        view/
          core.cljs
          navigation.cljs

navigation.cljs has some stuff to build Om components for navigating around the page, and core.cljs is the main entry point for this particular page. navigation.cljs starts with:

(ns contempo.view.navigation (:require ...))

core.cljs starts with:

(ns contempo.view.core (:require [contempo.view.navigation :as navigation]))

When I run lein cljsbuild, I get:

solace:Groov jfischer$ lein cljsbuild auto
Compiling ClojureScript.
Compiling "war/view/js/app.js" from ["src/cljs/contempo/common" "src/cljs/contempo/view"]...
Compiling "war/view/js/app.js" failed.
clojure.lang.ExceptionInfo: failed compiling file:src/cljs/contempo/view/core.cljs

... snipped stacktrace ...

Caused by: clojure.lang.ExceptionInfo: No such namespace: contempo.view.navigation at line 1 src/cljs/contempo/view/core.cljs

I can get it to work by removing all references to contempo.view.navigation from core.cljs, running lein cljsbuild auto and letting the compile finish, then putting them back and saving (so cljsbuild picks up the changes), but that's silly and shouldn't be necessary.

My project.clj looks like:

(defproject contempo "0.0.0-SNAPSHOT"
  :description "Experimenting with ClojureScript and Om"
  :dependencies [[org.clojure/clojure "1.6.0"]
                 [org.clojure/clojurescript "0.0-2740"]
                 [org.clojure/core.async "0.1.346.0-17112a-alpha"]
                 [org.omcljs/om "0.8.7"]]

  :plugins [[lein-cljsbuild "1.0.4"]]

  :clean-targets ^{:protect false} ["war/view/js/app.js"
                                    "war/view/js/out"]

  :cljsbuild {:builds [{:id "view-dev"
                        :source-paths ["src/cljs/contempo/common"
                                       "src/cljs/contempo/view"]
                        :compiler {:output-to "war/view/js/app.js"
                                   :output-dir "war/view/js/out"
                                   :optimizations :none
                                   :cache-analysis true
                                   :source-map true}}]})

Is there anything obvious I'm doing wrong? This looks pretty similar to every ClojureScript project I've looked at.

Update: Tiny skeleton project that shows the error is up here: namespace-error-demo.zip

2
Works for me without errors/warnings. I suspect that either you have circular dependencies or one of the source files fail to compile. Also, it might be worth trying using single source path "src/cljs/contempo/" - Jarlax
Checked for circular dependencies, collapsed it down to just one source-path, and actually removed almost all the code, and I still get the error. A tiny project that demonstrates it is available here: namespace-error-demo.zip - Jonathan
I just downloaded and compiled the demo without problems. Try with lein clean. If that doesn't work go to your ~/.m2/ directory and delete your Clojurescript and Clojure dependencies, they might be in a bad state. - sbensu

2 Answers

6
votes

The issue ended up being: I wasn't obeying the rules for how namespaces are resolved.

With source paths of src/cljs/contempo/common and src/cljs/contempo/view, if I required the contempo.view.whatever namespace, the compiler would look for it at src/cljs/contempo/common/contempo/view/whatever.cljs and src/cljs/contempo/view/contempo/view/whatever.cljs.

I had to use src/cljs as the source directory. What I wanted to pull off (leaving out code for a given page that didn't need it) was kind of silly (since it'd be pulling in all of ClojureScript anyway) and is now properly addressed thanks to proper Google Closure Module support in ClojureScript.

4
votes

I was having this same problem all of today. In my case, the root cause was having .cljs files with "-" in the name. I only discovered this was the problem after switching to 0.0-3030, which provides better error messages for the strict file path to namespace conventions that more recent versions of the cljs compiler have.

You might want to try changing :source-paths to ["src/cljs"]