Fix #1661: follow symlink when reading adjacent bb.edn (#1663)

This commit is contained in:
Michiel Borkent 2024-01-30 11:56:22 +01:00 committed by GitHub
parent 35e17bd46a
commit df477adbb0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 29 additions and 14 deletions

View file

@ -11,6 +11,7 @@ A preview of the next release can be installed from
- [#1660](https://github.com/babashka/babashka/issues/1660): add `:deps-root` as part of hash to avoid caching issue with `deps.clj` - [#1660](https://github.com/babashka/babashka/issues/1660): add `:deps-root` as part of hash to avoid caching issue with `deps.clj`
- [#1632](https://github.com/babashka/babashka/issues/1632): fix `(.readPassword (System/console))` by upgrading GraalVM to `21.0.2` - [#1632](https://github.com/babashka/babashka/issues/1632): fix `(.readPassword (System/console))` by upgrading GraalVM to `21.0.2`
- [#1661](https://github.com/babashka/babashka/issues/1661): follow symlink when reading adjacent bb.edn
## 1.3.188 (2023-01-12) ## 1.3.188 (2023-01-12)

View file

@ -23,6 +23,9 @@ cache:
clone_script: clone_script:
- cmd: git config --global core.autocrlf true - cmd: git config --global core.autocrlf true
- cmd: git config --global core.symlinks true
- ps: >- - ps: >-
if(-not $env:APPVEYOR_PULL_REQUEST_NUMBER) { if(-not $env:APPVEYOR_PULL_REQUEST_NUMBER) {
git clone -q --branch=$env:APPVEYOR_REPO_BRANCH https://github.com/$env:APPVEYOR_REPO_NAME.git $env:APPVEYOR_BUILD_FOLDER git clone -q --branch=$env:APPVEYOR_REPO_BRANCH https://github.com/$env:APPVEYOR_REPO_NAME.git $env:APPVEYOR_BUILD_FOLDER
@ -36,6 +39,8 @@ clone_script:
} }
- cmd: git submodule update --init --recursive - cmd: git submodule update --init --recursive
- cmd: git reset --hard
build_script: build_script:
# TODO: Extract the zip by removing the top level folder to remove the hardcoded path for GRAALVM_HOME # TODO: Extract the zip by removing the top level folder to remove the hardcoded path for GRAALVM_HOME
- cmd: >- - cmd: >-

View file

@ -1140,29 +1140,36 @@ Use bb run --help to show this help output.
(catch Exception _ false)) (catch Exception _ false))
bin)))))) bin))))))
(defn resolve-symbolic-link [f]
(if (and f (fs/exists? f))
(str (fs/real-path f))
f))
(defn main [& args] (defn main [& args]
(let [bin-jar (binary-invoked-as-jar) (let [bin-jar (binary-invoked-as-jar)
args (if bin-jar args (if bin-jar
(list* "--jar" bin-jar "--" args) (list* "--jar" bin-jar "--" args)
args) args)
[args opts] (parse-global-opts args) [args opts] (parse-global-opts args)
[args {:keys [jar file config merge-deps debug] :as opts}] [args {:keys [config merge-deps debug] :as opts}]
(if-not (or (:file opts) (if-not (or (:file opts)
(:jar opts)) (:jar opts))
(parse-file-opt args opts) (parse-file-opt args opts)
[args opts]) [args opts])
;; _ (prn :args args :opts opts) {:keys [jar file]} opts
abs-path #(-> % io/file .getAbsolutePath) abs-path resolve-symbolic-link
config (cond config (cond
config (if (fs/exists? config) (abs-path config) config (if (fs/exists? config) (abs-path config)
(when debug (when debug
(binding [*out* *err*] (binding [*out* *err*]
(println "[babashka] WARNING: config file does not exist:" config)) (println "[babashka] WARNING: config file does not exist:" config))
nil)) nil))
jar (some-> [jar] cp/new-loader (cp/resource "META-INF/bb.edn") .toString) jar (let [jar (resolve-symbolic-link jar)]
(some-> [jar] cp/new-loader (cp/resource "META-INF/bb.edn") .toString))
:else (if (and file (fs/exists? file)) :else (if (and file (fs/exists? file))
;; file relative to bb.edn ;; file relative to bb.edn
(let [rel-bb-edn (fs/file (fs/parent file) "bb.edn")] (let [file (abs-path file) ;; follow symlink
rel-bb-edn (fs/file (fs/parent file) "bb.edn")]
(if (fs/exists? rel-bb-edn) (if (fs/exists? rel-bb-edn)
(abs-path rel-bb-edn) (abs-path rel-bb-edn)
;; fall back to local bb.edn ;; fall back to local bb.edn

View file

@ -0,0 +1 @@
adjacent_bb/medley.bb

View file

@ -8,8 +8,7 @@
[borkdude.deps] [borkdude.deps]
[clojure.edn :as edn] [clojure.edn :as edn]
[clojure.string :as str] [clojure.string :as str]
[clojure.test :as test :refer [deftest is testing]] [clojure.test :as test :refer [deftest is testing]]))
[babashka.test-utils :as tu]))
(defn bb [& args] (defn bb [& args]
(let [args (map str args) (let [args (map str args)
@ -321,8 +320,8 @@
(testing "call to run in missing dir gives 'cannot run program' message" (testing "call to run in missing dir gives 'cannot run program' message"
(test-utils/with-config (test-utils/with-config
(pr-str '{:tasks {foo (clojure {:dir "../missingdir"} "-M" "-r")}}) (pr-str '{:tasks {foo (clojure {:dir "../missingdir"} "-M" "-r")}})
; check rough text of error message, specific message about missing directory is OS-dependent ;; check rough text of error message, specific message about missing directory is OS-dependent
(is (thrown-with-msg? Exception #"Cannot run program .* \(in directory \"\.\.[/\\]missingdir\"\)" (is (thrown-with-msg? Exception #"Cannot run program .* \(in directory \"\.\.[/\\]missingdir\"\)"
(bb "run" "foo")))))) (bb "run" "foo"))))))
(deftest list-tasks-test (deftest list-tasks-test
@ -410,7 +409,7 @@ even more stuff here\"
entries (cp/split-classpath out) entries (cp/split-classpath out)
entry (first entries)] entry (first entries)]
(is (= 1 (count entries))) (is (= 1 (count entries)))
(is (= (fs/parent config) (fs/parent entry))) (is (= (fs/real-path (fs/parent config)) (fs/real-path (fs/parent entry))))
(is (str/ends-with? entry "src")))))) (is (str/ends-with? entry "src"))))))
(deftest without-deps-test (deftest without-deps-test
@ -514,7 +513,7 @@ even more stuff here\"
(testing "default deps-root path is same as bb.edn" (testing "default deps-root path is same as bb.edn"
(let [out (bb "--config" config "cp") (let [out (bb "--config" config "cp")
entries (cp/split-classpath out)] entries (cp/split-classpath out)]
(is (= (fs/parent f) (fs/parent (first entries)))))) (is (= (fs/real-path(fs/parent f)) (fs/real-path (fs/parent (first entries)))))))
(spit config (spit config
'{:paths ["src"] '{:paths ["src"]
:deps {local/dep {:local/root "local-dep"}} :deps {local/dep {:local/root "local-dep"}}
@ -524,11 +523,13 @@ even more stuff here\"
_ (spit (str (fs/file root "deps.edn")) {}) _ (spit (str (fs/file root "deps.edn")) {})
out (bb "--config" config "cp") out (bb "--config" config "cp")
entries (cp/split-classpath out)] entries (cp/split-classpath out)]
(is (= (fs/parent f) (fs/parent (first entries))))))))) (is (= (fs/real-path (fs/parent f)) (fs/real-path (fs/parent (first entries))))))))))
(deftest adjacent-bb-edn-test (deftest adjacent-bb-edn-test
(is (= {1 {:id 1}} (bb "test-resources/adjacent_bb/medley.bb"))) (is (= {1 {:id 1}} (bb "test-resources/adjacent_bb/medley.bb")))
(is (= {1 {:id 1}} (bb "-f" "test-resources/adjacent_bb/medley.bb")))) (is (= {1 {:id 1}} (bb "-f" "test-resources/adjacent_bb/medley.bb")))
(testing "symlink"
(is (= {1 {:id 1}} (bb (str (fs/file "test-resources" "symlink-adjacent-bb")))))))
(deftest non-existing-tasks-in-run-gives-exit-code-1 (deftest non-existing-tasks-in-run-gives-exit-code-1
(is (thrown? Exception (bb "-Sdeps" "{:tasks {foo {:task (run (quote bar))}}}" "foo")))) (is (thrown? Exception (bb "-Sdeps" "{:tasks {foo {:task (run (quote bar))}}}" "foo"))))
@ -537,6 +538,6 @@ even more stuff here\"
(is (= 6 (bb "-Sdeps" "" "-e" "(+ 1 2 3)")))) (is (= 6 (bb "-Sdeps" "" "-e" "(+ 1 2 3)"))))
(deftest warning-on-override-task (deftest warning-on-override-task
(when-not tu/native? (when-not test-utils/native?
(binding [*out* *err*] (binding [*out* *err*]
(is (str/includes? (with-out-str (bb "-Sdeps" "{:tasks {run {:task 1}}}" "run")) "'run' override"))))) (is (str/includes? (with-out-str (bb "-Sdeps" "{:tasks {run {:task 1}}}" "run")) "'run' override")))))