Skip to content

Commit f86db9c

Browse files
scgilardistuarthalloway
authored andcommitted
restore detection of cyclic load dependencies
Signed-off-by: Stuart Halloway <[email protected]>
1 parent eb85cf9 commit f86db9c

File tree

9 files changed

+123
-8
lines changed

9 files changed

+123
-8
lines changed

src/clj/clojure/core.clj

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4668,8 +4668,8 @@
46684668

46694669
(defonce
46704670
^{:private true
4671-
:doc "the set of paths currently being loaded by this thread"}
4672-
*pending-paths* #{})
4671+
:doc "A stack of paths currently being loaded by this thread"}
4672+
*pending-paths* ())
46734673

46744674
(defonce
46754675
^{:private true :doc
@@ -4803,8 +4803,20 @@
48034803
(doseq [arg args]
48044804
(apply load-lib prefix (prependss arg opts))))))))
48054805

4806-
;; Public
4806+
(defn- check-cyclic-dependency
4807+
"Detects and rejects non-trivial cyclic load dependencies. The
4808+
exception message shows the dependency chain with the cycle
4809+
highlighted. Ignores the trivial case of a file attempting to load
4810+
itself because that can occur when a gen-class'd class loads its
4811+
implementation."
4812+
[path]
4813+
(when (some #{path} (rest *pending-paths*))
4814+
(let [pending (map #(if (= % path) (str "[ " % " ]") %)
4815+
(cons path *pending-paths*))
4816+
chain (apply str (interpose "->" pending))]
4817+
(throw (Exception. (str "Cyclic load dependency: " chain))))))
48074818

4819+
;; Public
48084820

48094821
(defn require
48104822
"Loads libs, skipping any that are already loaded. Each argument is
@@ -4897,12 +4909,10 @@
48974909
(when *loading-verbosely*
48984910
(printf "(clojure.core/load \"%s\")\n" path)
48994911
(flush))
4900-
; (throw-if (*pending-paths* path)
4901-
; "cannot load '%s' again while it is loading"
4902-
; path)
4903-
(when-not (*pending-paths* path)
4912+
(check-cyclic-dependency path)
4913+
(when-not (= path (first *pending-paths*))
49044914
(binding [*pending-paths* (conj *pending-paths* path)]
4905-
(clojure.lang.RT/load (.substring path 1)))))))
4915+
(clojure.lang.RT/load (.substring path 1)))))))
49064916

49074917
(defn compile
49084918
"Compiles the namespace named by the symbol lib into a set of

test/clojure/test_clojure/load.clj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; Copyright (c) Rich Hickey. All rights reserved.
2+
; The use and distribution terms for this software are covered by the
3+
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
; which can be found in the file epl-v10.html at the root of this distribution.
5+
; By using this software in any fashion, you are agreeing to be bound by
6+
; the terms of this license.
7+
; You must not remove this notice, or any other, from this software.
8+
9+
(ns clojure.test-clojure.load
10+
(:use clojure.test))
11+
12+
(deftest test-load
13+
(testing "Should ignore self-loads without comment"
14+
(is (nil? (require 'clojure.test-clojure.load.cyclic0))))
15+
(testing "Should reject cyclic dependencies"
16+
(testing "a->b->a"
17+
(is (thrown-with-msg? Exception #".*Cyclic load dependency.*"
18+
(require 'clojure.test-clojure.load.cyclic1))))
19+
(testing "a->b->c->d->b"
20+
(is (thrown-with-msg? Exception #".*Cyclic load dependency.*"
21+
(require 'clojure.test-clojure.load.cyclic3))))))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; Copyright (c) Rich Hickey. All rights reserved.
2+
; The use and distribution terms for this software are covered by the
3+
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
; which can be found in the file epl-v10.html at the root of this distribution.
5+
; By using this software in any fashion, you are agreeing to be bound by
6+
; the terms of this license.
7+
; You must not remove this notice, or any other, from this software.
8+
;
9+
; Author: Stephen C. Gilardi
10+
11+
(ns clojure.test-clojure.load.cyclic0
12+
(:require clojure.test-clojure.load.cyclic0))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; Copyright (c) Rich Hickey. All rights reserved.
2+
; The use and distribution terms for this software are covered by the
3+
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
; which can be found in the file epl-v10.html at the root of this distribution.
5+
; By using this software in any fashion, you are agreeing to be bound by
6+
; the terms of this license.
7+
; You must not remove this notice, or any other, from this software.
8+
;
9+
; Author: Stephen C. Gilardi
10+
11+
(ns clojure.test-clojure.load.cyclic1
12+
(:require clojure.test-clojure.load.cyclic2))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; Copyright (c) Rich Hickey. All rights reserved.
2+
; The use and distribution terms for this software are covered by the
3+
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
; which can be found in the file epl-v10.html at the root of this distribution.
5+
; By using this software in any fashion, you are agreeing to be bound by
6+
; the terms of this license.
7+
; You must not remove this notice, or any other, from this software.
8+
;
9+
; Author: Stephen C. Gilardi
10+
11+
(ns clojure.test-clojure.load.cyclic2
12+
(:require clojure.test-clojure.load.cyclic1))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; Copyright (c) Rich Hickey. All rights reserved.
2+
; The use and distribution terms for this software are covered by the
3+
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
; which can be found in the file epl-v10.html at the root of this distribution.
5+
; By using this software in any fashion, you are agreeing to be bound by
6+
; the terms of this license.
7+
; You must not remove this notice, or any other, from this software.
8+
;
9+
; Author: Stephen C. Gilardi
10+
11+
(ns clojure.test-clojure.load.cyclic3
12+
(:require clojure.test-clojure.load.cyclic4))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; Copyright (c) Rich Hickey. All rights reserved.
2+
; The use and distribution terms for this software are covered by the
3+
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
; which can be found in the file epl-v10.html at the root of this distribution.
5+
; By using this software in any fashion, you are agreeing to be bound by
6+
; the terms of this license.
7+
; You must not remove this notice, or any other, from this software.
8+
;
9+
; Author: Stephen C. Gilardi
10+
11+
(ns clojure.test-clojure.load.cyclic4
12+
(:require clojure.test-clojure.load.cyclic5))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; Copyright (c) Rich Hickey. All rights reserved.
2+
; The use and distribution terms for this software are covered by the
3+
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
; which can be found in the file epl-v10.html at the root of this distribution.
5+
; By using this software in any fashion, you are agreeing to be bound by
6+
; the terms of this license.
7+
; You must not remove this notice, or any other, from this software.
8+
;
9+
; Author: Stephen C. Gilardi
10+
11+
(ns clojure.test-clojure.load.cyclic5
12+
(:require clojure.test-clojure.load.cyclic6))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; Copyright (c) Rich Hickey. All rights reserved.
2+
; The use and distribution terms for this software are covered by the
3+
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
; which can be found in the file epl-v10.html at the root of this distribution.
5+
; By using this software in any fashion, you are agreeing to be bound by
6+
; the terms of this license.
7+
; You must not remove this notice, or any other, from this software.
8+
;
9+
; Author: Stephen C. Gilardi
10+
11+
(ns clojure.test-clojure.load.cyclic6
12+
(:require clojure.test-clojure.load.cyclic4))

0 commit comments

Comments
 (0)