diff --git a/README.md b/README.md index 7daaa548..2f96a52c 100644 --- a/README.md +++ b/README.md @@ -568,7 +568,7 @@ Babashka sets the following system properties: - `babashka.version`: the version string, e.g. `"1.2.0"` - `babashka.main`: the `--main` argument -- `babashka.file`: the `--file` argument (normalized using `.getCanonicalPath`) +- `babashka.file`: the `--file` argument (normalized using `.getAbsolutePath`) ## `__name__ == "__main__"` pattern diff --git a/sci b/sci index 12ab6c48..2e4d6e02 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 12ab6c489e0a2a6eba56c108e63b86c53287ab7e +Subproject commit 2e4d6e027688a9518b9cb25b9f26e274321ac8ab diff --git a/src/babashka/impl/classpath.clj b/src/babashka/impl/classpath.clj index 63b595e7..d1cd286b 100644 --- a/src/babashka/impl/classpath.clj +++ b/src/babashka/impl/classpath.clj @@ -19,8 +19,8 @@ (when (.exists f) (if url? ;; manual conversion, faster than going through .toURI - (java.net.URL. "file" nil (.getCanonicalPath f)) - {:file (.getCanonicalPath f) + (java.net.URL. "file" nil (.getAbsolutePath f)) + {:file (.getAbsolutePath f) :source (slurp f)})))) resource-paths))) @@ -32,7 +32,7 @@ (if url? ;; manual conversion, faster than going through .toURI (java.net.URL. "jar" nil - (str "file:" (.getCanonicalPath jar-file) "!/" path)) + (str "file:" (.getAbsolutePath jar-file) "!/" path)) {:file path :source (slurp (.getInputStream jar entry))}))) resource-paths))) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index fc99e1ab..eeca9582 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -296,7 +296,7 @@ If neither -e, -f, or --socket-repl are specified, then the first argument that (let [f (io/file f) s (slurp f)] (sci/with-bindings {sci/ns @sci/ns - sci/file (.getCanonicalPath f)} + sci/file (.getAbsolutePath f)} (sci/eval-string* sci-ctx s)))) {:sci.impl/op :needs-ctx})) @@ -484,9 +484,9 @@ If neither -e, -f, or --socket-repl are specified, then the first argument that (when uberscript (swap! uberscript-sources conj (:source res))) res))) _ (when file - (let [canonical-path (.getCanonicalPath (io/file file))] - (vars/bindRoot sci/file canonical-path) - (System/setProperty "babashka.file" canonical-path))) + (let [abs-path (.getAbsolutePath (io/file file))] + (vars/bindRoot sci/file abs-path) + (System/setProperty "babashka.file" abs-path))) ;; TODO: pull more of these values to compile time opts {:aliases aliases :namespaces (-> namespaces diff --git a/test/babashka/file_var_test.clj b/test/babashka/file_var_test.clj index 6aeb7577..e601523a 100644 --- a/test/babashka/file_var_test.clj +++ b/test/babashka/file_var_test.clj @@ -1,8 +1,9 @@ (ns babashka.file-var-test (:require [babashka.test-utils :as tu] + [clojure.java.io :as io] [clojure.string :as str] - [clojure.test :as t :refer [deftest is]])) + [clojure.test :as t :refer [deftest is testing]])) (defn bb [input & args] (apply tu/bb (when (some? input) (str input)) (map str args))) @@ -15,4 +16,9 @@ (is (str/ends-with? f1 "file_var_classpath.bb")) (is (str/ends-with? f2 "loaded_by_file_var.bb")) (is (str/ends-with? f3 "file_var.bb")) - (is (str/ends-with? f4 "file_var.bb")))) + (is (str/ends-with? f4 "file_var.bb"))) + (testing "file var uses absolute path" + (is (str/includes? + (bb nil (io/file "test" ".." "test" + "babashka" "scripts" "simple_file_var.bb")) + "..")))) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 84c32256..cf89d592 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -519,7 +519,13 @@ (is (= "true\nfalse\n" (test-utils/bb nil (.getPath (io/file "test-resources" "babashka" "file_property1.clj"))))) (is (= "true\n" - (test-utils/bb nil (.getPath (io/file "test-resources" "babashka" "file_property2.clj")))))) + (test-utils/bb nil (.getPath (io/file "test-resources" "babashka" "file_property2.clj"))))) + (is (apply = + (bb nil (.getPath (io/file "test" "babashka" "scripts" "simple_file_var.bb"))))) + (let [res (bb nil (.getPath (io/file "test" ".." "test" "babashka" + "scripts" "simple_file_var.bb")))] + (is (apply = res)) + (is (str/includes? (first res) "..")))) ;;;; Scratch diff --git a/test/babashka/scripts/simple_file_var.bb b/test/babashka/scripts/simple_file_var.bb new file mode 100644 index 00000000..4a0a2691 --- /dev/null +++ b/test/babashka/scripts/simple_file_var.bb @@ -0,0 +1 @@ +[*file* (System/getProperty "babashka.file")]