Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

[GIT-81733] Handle jar native filesystem paths (#81927)

Tyler Plude40a15bc48537
diff --git a/deps.edn b/deps.edn
index 684b56105a9..8375c040e8a 100644
--- a/deps.edn
+++ b/deps.edn
@@ -263,6 +263,7 @@
{:mvn/version "0.4.0"} ; Enables easy memory measurement
com.gfredericks/test.chuck {:mvn/version "0.2.15"} ; generating strings from regexes (useful with malli)
com.github.erosb/json-sKema {:mvn/version "0.25.0"} ; validating MBQL JSON Schema
+ com.google.jimfs/jimfs {:mvn/version "1.3.2"} ; in-memory NIO filesystems (e.g. Windows) for testing path handling
criterium/criterium {:mvn/version "0.4.6"} ; benchmarking library
djblue/portal {:mvn/version "0.62.0"} ; ui for inspecting values
integrant/integrant {:mvn/version "1.0.1"} ; component lifecycle management
diff --git a/src/metabase/util/files.clj b/src/metabase/util/files.clj
index 7fe037ff085..22b62cde70a 100644
--- a/src/metabase/util/files.clj
+++ b/src/metabase/util/files.clj
@@ -14,7 +14,7 @@
[metabase.util.log :as log])
(:import
(java.io FileNotFoundException)
- (java.net URL)
+ (java.net URI URL)
(java.nio.file CopyOption Files FileSystem FileSystemAlreadyExistsException
FileSystems LinkOption OpenOption Path Paths StandardCopyOption)
(java.nio.file.attribute FileAttribute)
@@ -208,6 +208,13 @@
(getResource ".keep-me")
getProtocol)))
+(defn code-location->path
+ "Convert a code-source location URI into a native filesystem path string."
+ ^String [^URI uri]
+ ;; Paths/get decodes %-escapes AND yields the platform-native form. URI.getPath only decodes: on Windows it returns
+ ;; `/C:/...`, which java.nio.file.Path rejects (#81733).
+ (str (Paths/get uri)))
+
(defn get-jar-path
"Returns the path to the currently running jar file.
@@ -218,8 +225,8 @@
(.getProtectionDomain)
(.getCodeSource)
(.getLocation)
- (.toURI) ;; avoid problems with special characters in path.
- (.getPath)))
+ (.toURI)
+ (code-location->path)))
(defn find-in-current-jar
"Find matching files in the current jar. See
diff --git a/test/metabase/util/files_test.clj b/test/metabase/util/files_test.clj
index b668c471935..873ad07efc3 100644
--- a/test/metabase/util/files_test.clj
+++ b/test/metabase/util/files_test.clj
@@ -5,7 +5,9 @@
[metabase.util :as u]
[metabase.util.files :as u.files])
(:import
+ (com.google.common.jimfs Configuration Jimfs)
(java.io FileOutputStream)
+ (java.net URI)
(java.nio.file ClosedFileSystemException FileSystem Files)
(java.util.zip ZipEntry ZipOutputStream)))
@@ -52,3 +54,13 @@
;; Subsequent opens still work after previous instance closes — not in a permanently-broken state.
(with-open [fs (u.files/nio-fs zip-path)]
(is (= "hi" (read-hello fs)))))))
+
+(deftest ^:parallel code-location->path-test
+ (testing "%-escapes decode to native paths"
+ (is (= "/tmp/jar location/x.jar"
+ (u.files/code-location->path (URI. "file:/tmp/jar%20location/x.jar")))))
+ (testing "windows-semantics filesystems yield drive-letter native form (#81733)"
+ (with-open [fs (Jimfs/newFileSystem (Configuration/windows))]
+ (let [uri (.toUri (.getPath fs "C:\\jar location\\x.jar" (u/varargs String)))]
+ (is (= "C:\\jar location\\x.jar"
+ (u.files/code-location->path uri)))))))