Skip to content

Commit

Permalink
Work around a RuntimeMXBean bug.
Browse files Browse the repository at this point in the history
RuntimeMXBean.getInputArguments is buggy when an input argument contains spaces.
For an input argument of "-Dmyprop=hello world" it returns two arguments, split at the
space: "-Dmyprop=hello" and "world".

This causes eval-in-project to fail in spectacular ways when leiningen.original.pwd
contains spaces or when path to the clojure jar that gets passed to -Xbootclasspath
contains spaces (which it does by default on Windows XP).
  • Loading branch information
mtyaka committed Nov 18, 2010
1 parent 1c166b1 commit 2fe29e5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/leiningen/compile.clj
Expand Up @@ -110,10 +110,25 @@
f
nil))))

(defn- get-jvm-args
[project]
(concat (.getInputArguments (ManagementFactory/getRuntimeMXBean))
(:jvm-opts project)))
;; Split this function out for better testability.
(defn- get-raw-input-args []
(.getInputArguments (ManagementFactory/getRuntimeMXBean)))

(defn- get-input-args
"Returns a vector of input arguments, accounting for a bug in RuntimeMXBean
that splits arguments which contain spaces."
[]
;; RuntimeMXBean.getInputArguments() is buggy when an input argument
;; contains spaces. For an input argument of -Dprop="hello world" it
;; returns ["-Dprop=hello", "world"]. Try to work around this bug.
(letfn [(join-broken-args [v arg] (if (= \- (first arg))
(conj v arg)
(conj (vec (butlast v))
(format "%s %s" (last v) arg))))]
(reduce join-broken-args [] (get-raw-input-args))))

(defn- get-jvm-args [project]
(concat (get-input-args) (:jvm-opts project)))

(defn get-readable-form [java project form init]
(let [cp (str (.getClasspath (.getCommandLine java)))
Expand Down Expand Up @@ -218,6 +233,7 @@ those given as command-line arguments."
(javac project))
(if (seq (compilable-namespaces project))
(if-let [namespaces (seq (stale-namespaces project))]

(binding [*skip-auto-compile* true]
(try
(if (zero? (eval-in-project project
Expand Down
6 changes: 6 additions & 0 deletions test/test_compile.clj
Expand Up @@ -41,3 +41,9 @@
(is (some (partial re-find r) classes) (format "missing %s" r))))
(is (not (.exists (file "test_projects" "sample"
"classes" "sample2" "core.class")))))

(deftest test-spaces-in-project-path
(binding [leiningen.compile/get-raw-input-args
(fn [] ["-Dleiningen.original.pwd=/path/with" "spaces/got-broken"])]
(is (zero? (eval-in-project (make-project "test_projects/sample")
`(System/exit 0))))))

0 comments on commit 2fe29e5

Please sign in to comment.