Skip to content

Commit

Permalink
Never use .mkdirs directly; it silently masks errors.
Browse files Browse the repository at this point in the history
Use leiningen.core.utils/mkdirs so that problems will be found when
they happen.
  • Loading branch information
technomancy committed May 16, 2017
1 parent 163887d commit 04d5001
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 16 deletions.
12 changes: 6 additions & 6 deletions leiningen-core/src/leiningen/core/classpath.clj
Expand Up @@ -29,8 +29,8 @@
:when (.startsWith (.getName entry) native-prefix)]
(let [f (io/file native-path (subs (.getName entry) (count native-prefix)))]
(if (.isDirectory entry)
(.mkdirs f)
(do (.mkdirs (.getParentFile f))
(utils/mkdirs f)
(do (utils/mkdirs (.getParentFile f))
(io/copy (.getInputStream jar entry) f))))))

(defn extract-native-dep!
Expand All @@ -47,8 +47,8 @@
(vreset! native? true)
(let [f (io/file native-path (subs (.getName entry) (count native-prefix)))]
(if (.isDirectory entry)
(.mkdirs f)
(do (.mkdirs (.getParentFile f))
(utils/mkdirs f)
(do (utils/mkdirs (.getParentFile f))
(io/copy (.getInputStream jar entry) f)))))
@native?))

Expand Down Expand Up @@ -167,7 +167,7 @@
(warn "Could not read the old stale value for" identifier ", rerunning stale task"))
(when (or (= ::error file-content)
(not= old-cmp-val cmp-val))
(.mkdirs (.getParentFile file))
(utils/mkdirs (.getParentFile file))
(let [result (apply f outdated-val args)]
(spit file (pr-str [cmp-val result]))
result)))))
Expand All @@ -189,7 +189,7 @@
(when (and (:name project) (:target-path project)
(not= current-value old-value))
(apply f args)
(.mkdirs (.getParentFile file))
(utils/mkdirs (.getParentFile file))
(spit file (doall current-value))
true)))

Expand Down
6 changes: 3 additions & 3 deletions leiningen-core/src/leiningen/core/eval.clj
Expand Up @@ -37,7 +37,7 @@
(when (and (:root project) (:write-pom-properties project true))
(let [path (format "%s/META-INF/maven/%s/%s/pom.properties"
compile-path group name)]
(.mkdirs (.getParentFile (io/file path)))
(utils/mkdirs (.getParentFile (io/file path)))
(spit path (project/make-project-properties project)))))

(defn run-prep-tasks
Expand Down Expand Up @@ -76,11 +76,11 @@
[project]
;; These must exist before the project is launched.
(when (:root project)
(.mkdirs (io/file (:compile-path project "/tmp")))
(utils/mkdirs (io/file (:compile-path project "/tmp")))
;; hack to not create default projects. For now only.
(doseq [path (mapcat #(remove-default-paths project %)
((juxt :source-paths :test-paths :resource-paths) project))]
(.mkdirs (io/file path))))
(utils/mkdirs (io/file path))))
(write-pom-properties project)
(classpath/resolve-managed-dependencies :dependencies :managed-dependencies project)
(run-prep-tasks project)
Expand Down
2 changes: 1 addition & 1 deletion leiningen-core/src/leiningen/core/user.clj
Expand Up @@ -24,7 +24,7 @@
(let [lein-home (getenv "LEIN_HOME")
lein-home (or (and lein-home (io/file lein-home))
(io/file (System/getProperty "user.home") ".lein"))]
(.getAbsolutePath (doto lein-home .mkdirs))))
(.getAbsolutePath (doto lein-home utils/mkdirs))))

;; TODO: move all these memoized fns into delays
(def init
Expand Down
6 changes: 6 additions & 0 deletions leiningen-core/src/leiningen/core/utils.clj
Expand Up @@ -63,6 +63,12 @@
(not= (.getCanonicalFile canon)
(.getAbsoluteFile canon))))

(defn mkdirs
"Make a given directory and its parents, but throw an Exception on failure."
[f] ; whyyyyy does .mkdirs fail silently ugh
(when-not (.mkdirs (io/file f))
(throw (Exception. (str "Couldn't create directories: " (io/file f))))))

(defn relativize
"Makes the filepath path relative to base. Assumes base is an ancestor to
path, and that the path contains no '..'."
Expand Down
2 changes: 1 addition & 1 deletion src/leiningen/jar.clj
Expand Up @@ -217,7 +217,7 @@

;; Split out backwards-compatibility. Collapse into get-jar-filename for 3.0
(defn get-classified-jar-filename [project classifier]
(let [target (doto (io/file (:target-path project)) .mkdirs)
(let [target (doto (io/file (:target-path project)) utils/mkdirs)
suffix (if classifier (str "-" (name classifier) ".jar") ".jar")
name-kw (if (= classifier :standalone) :uberjar-name :jar-name)
jar-name (or (project name-kw) (str (:name project) "-%s" suffix))
Expand Down
3 changes: 2 additions & 1 deletion src/leiningen/javac.clj
Expand Up @@ -3,6 +3,7 @@
(:require [leiningen.classpath :as classpath]
[leiningen.core.eval :as eval]
[leiningen.core.main :as main]
[leiningen.core.utils :as utils]
[leiningen.core.project :as project]
[clojure.java.io :as io]
[clojure.string :as string])
Expand Down Expand Up @@ -91,7 +92,7 @@
~(when main/*info*
`(binding [*out* *err*]
(println "Compiling" ~(count files) "source files to" ~compile-path)))
(.mkdirs (clojure.java.io/file ~compile-path))
(utils/mkdirs (clojure.java.io/file ~compile-path))
(when-not (zero?
(.run compiler# nil nil nil
(into-array java.lang.String ~javac-opts)))
Expand Down
5 changes: 3 additions & 2 deletions src/leiningen/new/templates.clj
Expand Up @@ -13,6 +13,7 @@
[clojure.string :as string]
[leiningen.core.eval :as eval]
[leiningen.core.user :as user]
[leiningen.core.utils :as utils]
[leiningen.core.main :as main]
[stencil.core :as stencil])
(:import (java.util Calendar)))
Expand Down Expand Up @@ -191,11 +192,11 @@
(if (or (= "." dir) (.mkdir (io/file dir)) *force?*)
(doseq [path paths]
(if (string? path)
(.mkdirs (template-path dir path data))
(utils/mkdirs (template-path dir path data))
(let [[path content & options] path
path (template-path dir path data)
options (apply hash-map options)]
(.mkdirs (.getParentFile path))
(utils/mkdirs (.getParentFile path))
(io/copy content (io/file path))
(when (:executable options)
(.setExecutable path true)))))
Expand Down
3 changes: 2 additions & 1 deletion src/leiningen/pom.clj
Expand Up @@ -2,6 +2,7 @@
"Write a pom.xml file to disk for Maven interoperability."
(:import java.io.IOException)
(:require [leiningen.core.main :as main]
[leiningen.core.utils :as utils]
[leiningen.core.project :as project]
[clojure.java.io :as io]
[clojure.set :as set]
Expand Down Expand Up @@ -402,7 +403,7 @@
([project pom-location-or-properties]
(let [pom (make-pom project true)
pom-file (io/file (:root project) pom-location-or-properties)]
(.mkdirs (.getParentFile pom-file))
(utils/mkdirs (.getParentFile pom-file))
(with-open [pom-writer (io/writer pom-file)]
(.write pom-writer pom))
(main/info "Wrote" (str pom-file))
Expand Down
3 changes: 2 additions & 1 deletion src/leiningen/trampoline.clj
Expand Up @@ -4,6 +4,7 @@
(:require [clojure.string :as string]
[leiningen.core.eval :as eval]
[leiningen.core.main :as main]
[leiningen.core.utils :as utils]
[leiningen.core.project :as project]
[clojure.java.io :as io]
[clojure.pprint :as pprint]))
Expand Down Expand Up @@ -34,7 +35,7 @@
(let [command (trampoline-command-string project forms profiles)
trampoline (trampoline-file)]
(main/debug "Trampoline command:" command)
(.mkdirs (.getParentFile (io/file trampoline)))
(utils/mkdirs (.getParentFile (io/file trampoline)))
(spit trampoline command)))

(defn ^:higher-order trampoline
Expand Down

0 comments on commit 04d5001

Please sign in to comment.