tools-build 2021-10-05

anyone tried to use tools.build on a Mac M1?

with io.github.clojure/tools.build {:git/tag "v0.5.1" :git/sha "21da7d4"} I've tried to see that if it is in my classpath

Alex Miller (Clojure team) 2021-10-05T20:53:28.449100Z

Why would it matter?

it is on my classpath

(slurp (http://clojure.java.io/resource "clojure/tools/build/api.clj ")) Execution error (IllegalArgumentException) at user/eval26714 (form-init3758113223764110517.clj:1). Cannot open <nil> as a Reader.

Alex Miller (Clojure team) 2021-10-05T20:57:15.453100Z

There's a trailing space in that string btw

Alex Miller (Clojure team) 2021-10-05T20:57:36.453800Z

If you really have that, this won't work

when I do this at the repl with this deps

I don't know why it would matter

Alex Miller (Clojure team) 2021-10-05T20:54:24.450500Z

How did you start the repl?

by IntelliJ classpath, with tool.deps adding the alias that has the build

Alex Miller (Clojure team) 2021-10-05T20:56:00.452200Z

What is the full alias in deps.edn?

{:paths ["src" "test" "resources"]
 :deps {org.clojure/clojure {:mvn/version "1.10.3"}
        io.pedestal/pedestal.service {:mvn/version "0.5.9"}
        io.pedestal/pedestal.jetty   {:mvn/version "0.5.9"}
        reagent/reagent              {:mvn/version "1.1.0"}
        nubank/workspaces            {:mvn/version "1.1.1"}
        binaryage/devtools           {:mvn/version "1.0.3"}
        borkdude/sci                 {:mvn/version "0.2.6"}
        thheller/shadow-cljs         {:mvn/version "2.15.10"}
        org.slf4j/slf4j-simple       {:mvn/version "1.7.30"}
        com.google.guava/guava {:mvn/version "31.0.1-jre"}}
 :aliases {:build {:extra-deps  {io.github.clojure/tools.build {:git/tag "v0.5.1"
                                                                :git/sha "21da7d4"}}}}}

Alex Miller (Clojure team) 2021-10-05T21:00:06.454700Z

Seems like that should work

Alex Miller (Clojure team) 2021-10-05T21:00:20.455200Z

Can you check that trailing space I asked about in thread?

👍 1

the build ns needs to be in another folder from the classpath?

instead of being in "src"?

@d.ian.b When you run clojure -T:build ... the classpath has just the deps from :build and the path ["."]

So the assumption is build.clj is in the project root.

(and you'd normally have :ns-default build in the :build alias so you can invoke clojure -T:build my-fn and it will run build/my-fn)

I don't think Cursive currently supports running build scripts tho'...

You can start a REPL in a build.clj function though and then connect to it, probably (I've done this from emacs, don't know about Cursive)

I have this on build.clj

(ns build
  (:require [clojure.tools.build.api :as b]
            [shadow.cljs.devtools.api :as shadow.api]
            [shadow.cljs.devtools.server :as shadow.server]))

(def lib 'ianffcs/project)
(def class-dir "target/classes")
(def uber-file "target/project.jar")

(defn -main
  [& _]
  (let [basis (b/create-basis {:project "deps.edn"})]
    (b/delete {:path "target"})
    (shadow.server/start!)
    (shadow.api/release :project)
    (shadow.server/stop!)
    (b/write-pom {:class-dir class-dir
                  :lib       lib
                  :version   "1.0.0"
                  :basis     basis
                  :src-dirs  (:paths basis)})
    (b/compile-clj {:basis       basis
                    :src-dirs    (:paths basis)
                    :class-dir   class-dir})
    (b/uber {:class-dir class-dir
             :main      'main
             :uber-file uber-file
             :basis     basis})
    (shutdown-agents)))

(-main)

FWIW you do not need shadow.server/start! or stop! when only using release

👍 1

and I've tried to run the (-main)

[main] INFO org.eclipse.jetty.util.log - Logging initialized @5438ms to org.eclipse.jetty.util.log.Slf4jLog
Execution error (FileNotFoundException) at build/loading (build.clj:1).
Could not locate clojure/tools/build/api__init.class, clojure/tools/build/api.clj or clojure/tools/build/api.cljc on classpath.

Full report at:
/var/folders/vn/25ww_nmx7h7450y1c35wg8780000gn/T/clojure-12857228039568925233.edn
Syntax error (ExceptionInfo) compiling at (build.clj:31:1).
Clojure compilation failed

Full report at:
/var/folders/vn/25ww_nmx7h7450y1c35wg8780000gn/T/clojure-1488074269378056351.edn

with this clojure -A:build -M -m build , the same error

with liberica-11, liberica-16 and liberica-17 on macbook m1

this is strange because a friend of mine is doing same stuff on linux x86 itself and it went well

like, I'm giving this info because my machine is "not canonical"

@d.ian.b You can add this function to build.clj:

(defn socket-repl [{:keys [port]}]
  ((requiring-resolve 'clojure.core.server/start-server)
   {:port port :name :repl :accept 'clojure.core.server/repl})
  (println "Started socket REPL on port" port)
  @(promise))
And then call from the command line:
clojure -T:build socket-repl :port 1337
And then connect to this server from Cursive to develop your build functions.

clojure -T:build socket-repl :port 1337
gives:
Unqualified function can't be resolved: socket-repl

clojure -T:build build/socket-repl :port 1337
gets:
Namespace could not be loaded: build

@d.ian.b I wonder if your ~/.gitlibs folder is broken somehow and your git deps are not working because of that?

my build is in ./src/build.clj is this correct?

or you put into the root folder of the project?

oh, it's in the root folder

thanks @seancorfield it went right 😄

👍🏻 1

build.clj is not part of your application so it shouldn't live in src. Do you think the tools.build guide could be clearer about that? (and also to use :deps instead of :extra-deps and -T:build instead of -A/`-M` or was that just your attempt to satisfy Cursive?)

I think it needs to be more clear about that

@d.ian.b There's already an issue for this https://github.com/clojure/clojure-site/issues/550 so I've tried to summarize what I think you found confusing/missing there. Feel free to add more comments if I missed anything.

1

deleted .cpcache and ˜/.gitlibs and ran with clj -A:build -M -m build

[main] INFO org.eclipse.jetty.util.log - Logging initialized @4872ms to org.eclipse.jetty.util.log.Slf4jLog
Execution error (FileNotFoundException) at build/loading (build.clj:1).
Could not locate clojure/tools/build/api__init.class, clojure/tools/build/api.clj or clojure/tools/build/api.cljc on classpath.

I think the project deps are interfering with your build script and its deps. Use clojure -T:build <function> to invoke functions in build.clj -- they will be passed a single argument, a hash map, like the -X option does.

You'll need the shadow-clj dependency added to :build for that to work -- since your build.clj depends on it and :ns-default build added.

👍 1

Here's what works:

(! 950)-> cat deps.edn 
{:paths ["src" "test" "resources"]
 :deps {org.clojure/clojure {:mvn/version "1.10.3"}
        io.pedestal/pedestal.service {:mvn/version "0.5.9"}
        io.pedestal/pedestal.jetty   {:mvn/version "0.5.9"}
        reagent/reagent              {:mvn/version "1.1.0"}
        nubank/workspaces            {:mvn/version "1.1.1"}
        binaryage/devtools           {:mvn/version "1.0.3"}
        borkdude/sci                 {:mvn/version "0.2.6"}
        thheller/shadow-cljs         {:mvn/version "2.15.10"}
        org.slf4j/slf4j-simple       {:mvn/version "1.7.30"}
        com.google.guava/guava {:mvn/version "31.0.1-jre"}}
 :aliases {:build {:deps {io.github.clojure/tools.build {:git/tag "v0.5.1" :git/sha "21da7d4"}
                          thheller/shadow-cljs {:mvn/version "2.15.10"}}
                   :ns-default build}}}

Tue Oct 05 15:23:46
(sean)-(jobs:0)-(~/clojure/fresh/fernandez)
(! 951)-> cat build.clj 
(ns build
  (:require [clojure.tools.build.api :as b]
            [shadow.cljs.devtools.api :as shadow.api]
            [shadow.cljs.devtools.server :as shadow.server]))

(def lib 'ianffcs/project)
(def class-dir "target/classes")
(def uber-file "target/project.jar")

(defn uberjar
  [_]
  (let [basis (b/create-basis {:project "deps.edn"})]
    (b/delete {:path "target"})
    (shadow.server/start!)
    (shadow.api/release :project)
    (shadow.server/stop!)
    (b/write-pom {:class-dir class-dir
                  :lib       lib
                  :version   "1.0.0"
                  :basis     basis
                  :src-dirs  (:paths basis)})
    (b/compile-clj {:basis       basis
                    :src-dirs    (:paths basis)
                    :class-dir   class-dir})
    (b/uber {:class-dir class-dir
             :main      'main
             :uber-file uber-file
             :basis     basis})
    (shutdown-agents)))

Tue Oct 05 15:23:49
(sean)-(jobs:0)-(~/clojure/fresh/fernandez)
(! 952)-> clojure -A:deps -T:build help/doc
-------------------------
build/uberjar
([_])

Tue Oct 05 15:24:10
(sean)-(jobs:0)-(~/clojure/fresh/fernandez)
(! 953)-> clojure -T:build uberjar
Oct 05, 2021 3:24:25 PM io.undertow.Undertow start
INFO: starting server: Undertow - 2.2.4.Final
Oct 05, 2021 3:24:25 PM org.xnio.Xnio <clinit>
INFO: XNIO version 3.8.0.Final
Oct 05, 2021 3:24:25 PM org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.8.0.Final
Oct 05, 2021 3:24:25 PM org.jboss.threads.Version <clinit>
INFO: JBoss Threads version 3.1.0.Final
shadow-cljs - server version: 2.15.10 running at 
shadow-cljs - nREPL server started on port 61002
No configuration for build ":project" found.
Oct 05, 2021 3:24:28 PM io.undertow.Undertow stop
INFO: stopping server: Undertow - 2.2.4.Final
Skipping paths: test resources

Tue Oct 05 15:25:19
(sean)-(jobs:0)-(~/clojure/fresh/fernandez)
(! 954)-> 

(obvs it couldn't find the :project build coz I don't have the shadow-cljs config)

👍 1

Maybe delete that folder and also the .cpcache folder in your project and try that clojure -M:build -m build command again (you don't need -A:build separately, you can put the alias on -M).

clojure -M:build -m build => I got the Could not locate clojure/tools/build/api__init.class, clojure/tools/build/api.clj or clojure/tools/build/api.cljc on classpath.

Also, confirm that your installed clojure command is recent enough: clojure -version -- what version do you get?

Clojure CLI version 1.10.3.986

OK, so you could use -T and follow the guide -- which means having an exec function style entry point into your build.clj instead of -main.

Does -M work with :deps -- is :deps a synonym for :replace-deps now?

He isn't using -T to invoke it and he doesn't have :deps in :build -- he has :extra-deps.

Also, @d.ian.b you have a call to -main at the bottom of your build.clj so that is going to run -main when the ns is loaded which is going to defeat almost everything you try to do with it.

I did that to try it on cursive repl only