From a5e18d6bb7d7fcfc0f617de97dc98de09cb8f03c Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 29 Jan 2024 13:57:49 +0100 Subject: [PATCH] Fix #1661: follow symlink when reading adjacent bb.edn --- CHANGELOG.md | 1 + src/babashka/main.clj | 16 +++++++++++----- test-resources/symlink-adjacent-bb | 1 + test/babashka/bb_edn_test.clj | 13 +++++++------ 4 files changed, 20 insertions(+), 11 deletions(-) create mode 120000 test-resources/symlink-adjacent-bb diff --git a/CHANGELOG.md b/CHANGELOG.md index 807f3411..343d96c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` - [#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) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 3dc4bb22..f02ebdd8 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -1140,29 +1140,35 @@ Use bb run --help to show this help output. (catch Exception _ false)) bin)))))) +(defn resolve-symbolic-link [f] + (if (and f (fs/exists? f)) + (str (fs/real-path f)) + f)) + (defn main [& args] (let [bin-jar (binary-invoked-as-jar) args (if bin-jar (list* "--jar" bin-jar "--" args) 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) (:jar opts)) (parse-file-opt args opts) [args opts]) - ;; _ (prn :args args :opts opts) - abs-path #(-> % io/file .getAbsolutePath) + {:keys [jar file]} opts + abs-path resolve-symbolic-link config (cond config (if (fs/exists? config) (abs-path config) (when debug (binding [*out* *err*] (println "[babashka] WARNING: config file does not exist:" config)) 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)) ;; file relative to bb.edn - (let [file (fs/real-path file) ;; follow symlink + (let [file (abs-path file) ;; follow symlink rel-bb-edn (fs/file (fs/parent file) "bb.edn")] (if (fs/exists? rel-bb-edn) (abs-path rel-bb-edn) diff --git a/test-resources/symlink-adjacent-bb b/test-resources/symlink-adjacent-bb new file mode 120000 index 00000000..40b5d4bf --- /dev/null +++ b/test-resources/symlink-adjacent-bb @@ -0,0 +1 @@ +adjacent_bb/medley.bb \ No newline at end of file diff --git a/test/babashka/bb_edn_test.clj b/test/babashka/bb_edn_test.clj index 424c17b7..f2f8a313 100644 --- a/test/babashka/bb_edn_test.clj +++ b/test/babashka/bb_edn_test.clj @@ -8,8 +8,7 @@ [borkdude.deps] [clojure.edn :as edn] [clojure.string :as str] - [clojure.test :as test :refer [deftest is testing]] - [babashka.test-utils :as tu])) + [clojure.test :as test :refer [deftest is testing]])) (defn bb [& args] (let [args (map str args) @@ -321,8 +320,8 @@ (testing "call to run in missing dir gives 'cannot run program' message" (test-utils/with-config (pr-str '{:tasks {foo (clojure {:dir "../missingdir"} "-M" "-r")}}) - ; 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\"\)" + ;; 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\"\)" (bb "run" "foo")))))) (deftest list-tasks-test @@ -528,7 +527,9 @@ even more stuff here\" (deftest adjacent-bb-edn-test (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 "test-resources/symlink-adjacent-bb"))))) (deftest non-existing-tasks-in-run-gives-exit-code-1 (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)")))) (deftest warning-on-override-task - (when-not tu/native? + (when-not test-utils/native? (binding [*out* *err*] (is (str/includes? (with-out-str (bb "-Sdeps" "{:tasks {run {:task 1}}}" "run")) "'run' override")))))