[#934] Environment variables not picked up by tools.deps

This commit is contained in:
Michiel Borkent 2021-07-13 11:31:36 +02:00
parent e996351dc9
commit 588cda9eac
5 changed files with 46 additions and 16 deletions

View file

@ -16,6 +16,7 @@ Babashka proper:
- Compatibility with `org.clojure/data.json {:mvn/version "2.4.0}"` - Compatibility with `org.clojure/data.json {:mvn/version "2.4.0}"`
- Support passing `GITLIBS` via `:extra-env` in `clojure` to set git lib dir: - Support passing `GITLIBS` via `:extra-env` in `clojure` to set git lib dir:
`(clojure {:extra-env {"GITLIBS" ".gitlib"}} ,,,) [#934](https://github.com/babashka/babashka/issues/934)` `(clojure {:extra-env {"GITLIBS" ".gitlib"}} ,,,) [#934](https://github.com/babashka/babashka/issues/934)`
- Add `--force` option to force recomputation of bababashka deps classpath.
Deps.clj: Deps.clj:

View file

@ -56,7 +56,7 @@
keywords) which will used to calculate classpath. The classpath is keywords) which will used to calculate classpath. The classpath is
then used to resolve dependencies in babashka." then used to resolve dependencies in babashka."
([deps-map] (add-deps deps-map nil)) ([deps-map] (add-deps deps-map nil))
([deps-map {:keys [:aliases :extra-env]}] ([deps-map {:keys [:aliases :env :extra-env :force]}]
(when-let [paths (:paths deps-map)] (when-let [paths (:paths deps-map)]
(cp/add-classpath (str/join cp/path-sep paths))) (cp/add-classpath (str/join cp/path-sep paths)))
(when-let [deps-map (not-empty (dissoc deps-map :paths :tasks :raw :min-bb-version))] (when-let [deps-map (not-empty (dissoc deps-map :paths :tasks :raw :min-bb-version))]
@ -65,12 +65,13 @@
:classpath-overrides {org.clojure/clojure "" :classpath-overrides {org.clojure/clojure ""
org.clojure/spec.alpha "" org.clojure/spec.alpha ""
org.clojure/core.specs.alpha ""}}) org.clojure/core.specs.alpha ""}})
args ["-Srepro" ;; do not include deps.edn from user config args (list "-Srepro" ;; do not include deps.edn from user config
"-Spath" "-Sdeps" (str deps-map) "-Spath" "-Sdeps" (str deps-map)
"-Sdeps-file" "" ;; we reset deps file so the local deps.edn isn't used "-Sdeps-file" "") ;; we reset deps file so the local deps.edn isn't used
,] args (if force (cons "-Sforce" args) args)
args (conj args (str "-A:" (str/join ":" (cons ":org.babashka/defaults" aliases)))) args (concat args [(str "-A:" (str/join ":" (cons ":org.babashka/defaults" aliases)))])
cp (with-out-str (binding [deps/*extra-env* extra-env] cp (with-out-str (binding [deps/*env* env
deps/*extra-env* extra-env]
(apply deps/-main args))) (apply deps/-main args)))
cp (str/trim cp) cp (str/trim cp)
cp (str/replace cp (re-pattern (str cp/path-sep "+$")) "")] cp (str/replace cp (re-pattern (str cp/path-sep "+$")) "")]
@ -111,6 +112,7 @@
*out* @sci/out *out* @sci/out
*err* @sci/err *err* @sci/err
deps/*dir* (:dir opts) deps/*dir* (:dir opts)
deps/*env* (:env opts)
deps/*extra-env* (:extra-env opts) deps/*extra-env* (:extra-env opts)
deps/*process-fn* (fn deps/*process-fn* (fn
([cmd] (p/process cmd opts)) ([cmd] (p/process cmd opts))

View file

@ -124,8 +124,9 @@ Substrate VM opts:
Global opts: Global opts:
-cp, --classpath Classpath to use. Overrides bb.edn classpath. -cp, --classpath Classpath to use. Overrides bb.edn classpath.
--debug Print debug information and internal stacktrace in case of exception. --debug Print debug information and internal stacktrace in case of exception.
--force Passes -Sforce to deps.clj, forcing recalculation of the classpath.
Help: Help:
@ -505,9 +506,13 @@ Use bb run --help to show this help output.
("--doc") ("--doc")
{:doc true {:doc true
:command-line-args (rest options)} :command-line-args (rest options)}
;; renamed to --debug
("--verbose") (recur (next options) ("--verbose") (recur (next options)
(assoc opts-map (assoc opts-map
:verbose? true)) :verbose? true))
("--force") (recur (next options)
(assoc opts-map
:force? true))
("--describe") (recur (next options) ("--describe") (recur (next options)
(assoc opts-map (assoc opts-map
:describe? true)) :describe? true))
@ -675,7 +680,7 @@ Use bb run --help to show this help output.
:help :file :command-line-args :help :file :command-line-args
:expressions :stream? :expressions :stream?
:repl :socket-repl :nrepl :repl :socket-repl :nrepl
:debug :classpath :debug :classpath :force?
:main :uberscript :describe? :main :uberscript :describe?
:jar :uberjar :clojure :jar :uberjar :clojure
:doc :run :list-tasks]} :doc :run :list-tasks]}
@ -703,7 +708,7 @@ Use bb run --help to show this help output.
_ (if classpath _ (if classpath
(cp/add-classpath classpath) (cp/add-classpath classpath)
;; when classpath isn't set, we calculate it from bb.edn, if present ;; when classpath isn't set, we calculate it from bb.edn, if present
(when-let [bb-edn @common/bb-edn] (deps/add-deps bb-edn))) (when-let [bb-edn @common/bb-edn] (deps/add-deps bb-edn {:force force?})))
abs-path (when file abs-path (when file
(let [abs-path (.getAbsolutePath (io/file file))] (let [abs-path (.getAbsolutePath (io/file file))]
(vars/bindRoot sci/file abs-path) (vars/bindRoot sci/file abs-path)

View file

@ -1,5 +1,6 @@
(ns babashka.deps-test (ns babashka.deps-test
(:require (:require
[babashka.fs :as fs]
[babashka.test-utils :as test-utils] [babashka.test-utils :as test-utils]
[clojure.edn :as edn] [clojure.edn :as edn]
[clojure.test :as test :refer [deftest is testing]])) [clojure.test :as test :refer [deftest is testing]]))
@ -10,7 +11,8 @@
:eof nil} :eof nil}
(apply test-utils/bb nil (map str args)))) (apply test-utils/bb nil (map str args))))
(deftest dependency-test (is (= #{:a :c :b} (bb " (deftest dependency-test
(is (= #{:a :c :b} (bb "
(require '[babashka.deps :as deps]) (require '[babashka.deps :as deps])
(deps/add-deps '{:deps {com.stuartsierra/dependency {:mvn/version \"1.0.0\"}}}) (deps/add-deps '{:deps {com.stuartsierra/dependency {:mvn/version \"1.0.0\"}}})
@ -24,7 +26,19 @@
(dep/depend :d :c))) (dep/depend :d :c)))
(dep/transitive-dependencies g1 :d) (dep/transitive-dependencies g1 :d)
")))) ")))
(testing "GITLIBS can set location of .gitlibs dir"
(let [tmp-dir (fs/create-temp-dir)
libs-dir (fs/file tmp-dir ".gitlibs")
libs-dir2 (fs/file tmp-dir ".gitlibs2")]
(bb (pr-str `(do (babashka.deps/add-deps '{:deps {babashka/process {:git/url "https://github.com/babashka/process" :sha "4c6699d06b49773d3e5c5b4c11d3334fb78cc996"}}}
{:force true
:env {"GITLIBS" ~(str libs-dir)}}) nil)))
(bb (pr-str `(do (babashka.deps/add-deps '{:deps {babashka/process {:git/url "https://github.com/babashka/process" :sha "4c6699d06b49773d3e5c5b4c11d3334fb78cc996"}}}
{:force true
:extra-env {"GITLIBS" ~(str libs-dir2)}}) nil)))
(is (fs/exists? libs-dir))
(is (fs/exists? libs-dir2)))))
(deftest clojure-test (deftest clojure-test
(testing "-Stree prints to *out*" (testing "-Stree prints to *out*"
@ -60,4 +74,12 @@ true
(testing "start from other directory" (testing "start from other directory"
(is (= {1 {:id 1}, 2 {:id 2}} (is (= {1 {:id 1}, 2 {:id 2}}
(edn/read-string (bb " (edn/read-string (bb "
(:out @(babashka.deps/clojure [\"-M\" \"-e\" \"(require 'medley.core) (medley.core/index-by :id [{:id 1} {:id 2}])\"] {:out :string :dir \"test-resources/clojure-dir-test\"}))")))))) (:out @(babashka.deps/clojure [\"-M\" \"-e\" \"(require 'medley.core) (medley.core/index-by :id [{:id 1} {:id 2}])\"] {:out :string :dir \"test-resources/clojure-dir-test\"}))")))))
(testing "GITLIBS can set location of .gitlibs dir"
(let [tmp-dir (fs/create-temp-dir)
libs-dir (fs/file tmp-dir ".gitlibs")
libs-dir2 (fs/file tmp-dir ".gitlibs2")]
(bb (pr-str `(do (babashka.deps/clojure ["-Sforce" "-Spath" "-Sdeps" "{:deps {babashka/process {:git/url \"https://github.com/babashka/process\" :sha \"4c6699d06b49773d3e5c5b4c11d3334fb78cc996\"}}}"] {:out :string :env {"GITLIBS" ~(str libs-dir)}}) nil)))
(bb (pr-str `(do (babashka.deps/clojure ["-Sforce" "-Spath" "-Sdeps" "{:deps {babashka/process {:git/url \"https://github.com/babashka/process\" :sha \"4c6699d06b49773d3e5c5b4c11d3334fb78cc996\"}}}"] {:out :string :extra-env {"GITLIBS" ~(str libs-dir2)}}) nil)))
(is (fs/exists? libs-dir))
(is (fs/exists? libs-dir2)))))

View file

@ -53,8 +53,8 @@
(is (= '("-e" "1") (bb nil "-e" "*command-line-args*" "--" "-e" "1"))) (is (= '("-e" "1") (bb nil "-e" "*command-line-args*" "--" "-e" "1")))
(let [v (bb nil "--describe")] (let [v (bb nil "--describe")]
(is (:babashka/version v)) (is (:babashka/version v))
(is (:feature/xml v)))) (is (:feature/xml v)))
(is (= {:force? true} (main/parse-opts ["--force"]))))
(deftest version-test (deftest version-test
(is (= [1 0 0] (main/parse-version "1.0.0-SNAPSHOT"))) (is (= [1 0 0] (main/parse-version "1.0.0-SNAPSHOT")))