find-system.lisp 29.2 KB
Newer Older
1 2 3
;;;; -------------------------------------------------------------------------
;;;; Finding systems

4
(uiop/package:define-package :asdf/find-system
5
  (:recycle :asdf/find-system :asdf)
6
  (:use :uiop/common-lisp :uiop :asdf/upgrade
7
    :asdf/session :asdf/component :asdf/operation :asdf/action :asdf/system)
8
  (:export
9
   #:remove-entry-from-registry #:coerce-entry-to-directory
10
   #:coerce-name #:primary-system-name #:coerce-filename
11
   #:find-system #:locate-system #:load-asd #:define-op
12 13
   #:system-registered-p #:registered-system #:register-system
   #:registered-systems* #:registered-systems
14
   #:clear-system #:map-systems
15
   #:missing-component #:missing-requires #:missing-parent
16 17 18
   #:load-system-definition-error #:error-name #:error-pathname #:error-condition
   #:*system-definition-search-functions* #:search-for-system-definition
   #:*central-registry* #:probe-asd #:sysdef-central-registry-search
19
   #:find-system-if-being-defined
20
   #:contrib-sysdef-search #:sysdef-find-asdf ;; backward compatibility symbols, functions removed
21
   #:sysdef-preloaded-system-search #:register-preloaded-system #:*preloaded-systems*
22
   #:mark-component-preloaded ;; forward reference to asdf/operate
23 24
   #:sysdef-immutable-system-search #:register-immutable-system #:*immutable-systems*
   #:*defined-systems* #:clear-defined-systems
25
   ;; defined in source-registry, but specially mentioned here:
26
   #:initialize-source-registry #:sysdef-source-registry-search))
27 28
(in-package :asdf/find-system)

29
(with-upgradability ()
30
  (declaim (ftype (function (&optional t) t) initialize-source-registry)) ; forward reference
31 32 33 34 35 36 37 38 39 40 41 42

  (define-condition missing-component (system-definition-error)
    ((requires :initform "(unnamed)" :reader missing-requires :initarg :requires)
     (parent :initform nil :reader missing-parent :initarg :parent)))

  (define-condition load-system-definition-error (system-definition-error)
    ((name :initarg :name :reader error-name)
     (pathname :initarg :pathname :reader error-pathname)
     (condition :initarg :condition :reader error-condition))
    (:report (lambda (c s)
               (format s (compatfmt "~@<Error while trying to load definition for system ~A from pathname ~A: ~3i~_~A~@:>")
                       (error-name c) (error-pathname c) (error-condition c)))))
43

44 45
  ;;; Canonicalizing system names

46
  (defun coerce-name (name)
47 48 49
    "Given a designator for a component NAME, return the name as a string.
The designator can be a COMPONENT (designing its name; note that a SYSTEM is a component),
a SYMBOL (designing its name, downcased), or a STRING (designing itself)."
50 51
    (typecase name
      (component (component-name name))
52
      (symbol (string-downcase name))
53 54 55 56
      (string name)
      (t (sysdef-error (compatfmt "~@<Invalid component designator: ~3i~_~A~@:>") name))))

  (defun primary-system-name (name)
57 58 59
    "Given a system designator NAME, return the name of the corresponding primary system,
after which the .asd file is named. That's the first component when dividing the name
as a string by / slashes."
60 61
    (first (split-string (coerce-name name) :separator "/")))

62
  (defun coerce-filename (name)
63 64 65 66
    "Coerce a system designator NAME into a string suitable as a filename component.
The (current) transformation is to replace characters /:\\ each by --,
the former being forbidden in a filename component.
NB: The onus is unhappily on the user to avoid clashes."
67 68
    (frob-substrings (coerce-name name) '("/" ":" "\\") "--"))

69 70 71

  ;;; Registry of Defined Systems

72
  (defvar *defined-systems* (make-hash-table :test 'equal)
73 74
    "This is a hash table whose keys are strings -- the
names of systems -- and whose values are pairs, the first
75 76
element of which is a universal-time indicating when the
system definition was last updated, and the second element
77 78 79
of which is a system object.
  A system is referred to as \"registered\" if it is present
in this table.")
80

81
  (defun system-registered-p (name)
82 83 84
    "Return a generalized boolean that is true if a system of given NAME was registered already.
NAME is a system designator, to be normalized by COERCE-NAME.
The value returned if true is a pair of a timestamp and a system object."
85 86
    (gethash (coerce-name name) *defined-systems*))

87
  (defun registered-system (name)
Robert Goldman's avatar
Robert Goldman committed
88 89 90 91
    "Return a system of given NAME that was registered already,
if such a system exists.  NAME is a system designator, to be
normalized by COERCE-NAME. The value returned is a system object,
or NIL if not found."
92 93
    (cdr (system-registered-p name)))

94
  (defun registered-systems* ()
95
    "Return a list containing every registered system (as a system object)."
96
    (loop :for registered :being :the :hash-values :of *defined-systems*
97 98 99
          :collect (cdr registered)))

  (defun registered-systems ()
100
    "Return a list of the names of every registered system."
101
    (mapcar 'coerce-name (registered-systems*)))
102 103

  (defun register-system (system)
104
    "Given a SYSTEM object, register it."
105 106 107
    (check-type system system)
    (let ((name (component-name system)))
      (check-type name string)
108
      (asdf-message (compatfmt "~&~@<; ~@;Registering system ~3i~_~A~@:>~%") name)
109
      (unless (eq system (registered-system name))
110
        (setf (gethash name *defined-systems*)
111
              (cons (ignore-errors (get-file-stamp (system-source-file system)))
112 113
                    system)))))

114 115 116 117 118 119 120 121
  (defun map-systems (fn)
    "Apply FN to each defined system.

FN should be a function of one argument. It will be
called with an object of type asdf:system."
    (loop :for registered :being :the :hash-values :of *defined-systems*
          :do (funcall fn (cdr registered))))

122 123 124

  ;;; Preloaded systems: in the image even if you can't find source files backing them.

125 126
  (defvar *preloaded-systems* (make-hash-table :test 'equal)
    "Registration table for preloaded systems.")
127

128
  (declaim (ftype (function (t) t) mark-component-preloaded)) ; defined in asdf/operate
129

130
  (defun make-preloaded-system (name keys)
131
    "Make a preloaded system of given NAME with build information from KEYS"
132 133 134 135 136
    (let ((system (apply 'make-instance (getf keys :class 'system)
                         :name name :source-file (getf keys :source-file)
                         (remove-plist-keys '(:class :name :source-file) keys))))
      (mark-component-preloaded system)
      system))
137 138

  (defun sysdef-preloaded-system-search (requested)
139 140
    "If REQUESTED names a system registered as preloaded, return a new system
with its registration information."
141 142 143 144 145
    (let ((name (coerce-name requested)))
      (multiple-value-bind (keys foundp) (gethash name *preloaded-systems*)
        (when foundp
          (make-preloaded-system name keys)))))

146 147 148 149 150 151 152
  (defun ensure-preloaded-system-registered (name)
    "If there isn't a registered _defined_ system of given NAME,
and a there is a registered _preloaded_ system of given NAME,
then define and register said preloaded system."
    (if-let (system (and (not (registered-system name)) (sysdef-preloaded-system-search name)))
      (register-system system)))

153 154 155 156 157 158 159 160 161 162 163
  (defun register-preloaded-system (system-name &rest keys &key (version t) &allow-other-keys)
    "Register a system as being preloaded. If the system has not been loaded from the filesystem
yet, or if its build information is later cleared with CLEAR-SYSTEM, a dummy system will be
registered without backing filesystem information, based on KEYS (e.g. to provide a VERSION).
If VERSION is the default T, and a system was already loaded, then its version will be preserved."
    (let ((name (coerce-name system-name)))
      (when (eql version t)
        (if-let (system (registered-system name))
          (setf (getf keys :version) (component-version system))))
      (setf (gethash name *preloaded-systems*) keys)
      (ensure-preloaded-system-registered system-name)))
164

165

166
  ;;; Immutable systems: in the image and can't be reloaded from source.
167 168

  (defvar *immutable-systems* nil
169
    "A hash-set (equal hash-table mapping keys to T) of systems that are immutable,
170 171 172
i.e. already loaded in memory and not to be refreshed from the filesystem.
They will be treated specially by find-system, and passed as :force-not argument to make-plan.

173 174 175 176 177 178 179 180
For instance, to can deliver an image with many systems precompiled, that *will not* check the
filesystem for them every time a user loads an extension, what more risk a problematic upgrade
 or catastrophic downgrade, before you dump an image, you may use:
   (map () 'asdf:register-immutable-system (asdf:already-loaded-systems))

Note that direct access to this variable from outside ASDF is not supported.
Please call REGISTER-IMMUTABLE-SYSTEM to add new immutable systems, and
contact maintainers if you need a stable API to do more than that.")
181 182 183 184

  (defun sysdef-immutable-system-search (requested)
    (let ((name (coerce-name requested)))
      (when (and *immutable-systems* (gethash name *immutable-systems*))
185
        (or (registered-system requested)
186
            (error 'formatted-system-definition-error
187
                   :format-control "Requested system ~A registered as an immutable-system, ~
188
but not even registered as defined"
189 190
                   :format-arguments (list name))))))

Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
191 192
  (defun register-immutable-system (system-name &rest keys)
    "Register SYSTEM-NAME as preloaded and immutable.
193 194 195
It will automatically be considered as passed to FORCE-NOT in a plan."
    (let ((system-name (coerce-name system-name)))
      (apply 'register-preloaded-system system-name keys)
196 197
      (unless *immutable-systems*
        (setf *immutable-systems* (list-to-hash-set nil)))
198 199 200 201
      (setf (gethash system-name *immutable-systems*) t)))


  ;;; Making systems undefined.
202 203

  (defun clear-system (system)
204 205 206
    "Clear the entry for a SYSTEM in the database of systems previously defined.
However if the system was registered as PRELOADED (which it is if it is IMMUTABLE),
then a new system with the same name will be defined and registered in its place
207
from which build details will have been cleared.
208
Note that this does NOT in any way cause any of the code of the system to be unloaded.
209
Returns T if system was or is now undefined, NIL if a new preloaded system was redefined."
210 211 212
    ;; There is no "unload" operation in Common Lisp, and
    ;; a general such operation cannot be portably written,
    ;; considering how much CL relies on side-effects to global data structures.
213
    (let ((name (coerce-name system)))
214 215 216
      (remhash name *defined-systems*)
      (unset-asdf-cache-entry `(find-system ,name))
      (not (ensure-preloaded-system-registered name))))
217

218
  (defun clear-defined-systems ()
219 220
    "Clear all currently registered defined systems.
Preloaded systems (including immutable ones) will be reset, other systems will be de-registered."
221
    (loop :for name :being :the :hash-keys :of *defined-systems*
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
222
          :unless (member name '("asdf" "uiop") :test 'equal) :do (clear-system name)))
223

224

225 226 227 228 229 230 231 232 233 234 235 236 237
  ;;; Searching for system definitions

  ;; For the sake of keeping things reasonably neat, we adopt a convention that
  ;; only symbols are to be pushed to this list (rather than e.g. function objects),
  ;; which makes upgrade easier. Also, the name of these symbols shall start with SYSDEF-
  (defvar *system-definition-search-functions* '()
    "A list that controls the ways that ASDF looks for system definitions.
It contains symbols to be funcalled in order, with a requested system name as argument,
until one returns a non-NIL result (if any), which must then be a fully initialized system object
with that name.")

  ;; Initialize and/or upgrade the *system-definition-search-functions*
  ;; so it doesn't contain obsolete symbols, and does contain the current ones.
238 239 240 241
  (defun cleanup-system-definition-search-functions ()
    (setf *system-definition-search-functions*
          (append
           ;; Remove known-incompatible sysdef functions from old versions of asdf.
242 243 244 245
           ;; Order matters, so we can't just use set-difference.
           (let ((obsolete
                  '(contrib-sysdef-search sysdef-find-asdf sysdef-preloaded-system-search)))
             (remove-if #'(lambda (x) (member x obsolete)) *system-definition-search-functions*))
246 247
           ;; Tuck our defaults at the end of the list if they were absent.
           ;; This is imperfect, in case they were removed on purpose,
248
           ;; but then it will be the responsibility of whoever removes these symmbols
249 250 251
           ;; to upgrade asdf before he does such a thing rather than after.
           (remove-if #'(lambda (x) (member x *system-definition-search-functions*))
                      '(sysdef-central-registry-search
252
                        sysdef-source-registry-search)))))
253 254
  (cleanup-system-definition-search-functions)

255 256
  ;; This (private) function does the search for a system definition using *s-d-s-f*;
  ;; it is to be called by locate-system.
257
  (defun search-for-system-definition (system)
258 259 260 261 262 263 264
    ;; Search for valid definitions of the system available in the current session.
    ;; Previous definitions as registered in *defined-systems* MUST NOT be considered;
    ;; they will be reconciled by locate-system then find-system.
    ;; There are two special treatments: first, specially search for objects being defined
    ;; in the current session, to avoid definition races between several files;
    ;; second, specially search for immutable systems, so they cannot be redefined.
    ;; Finally, use the search functions specified in *system-definition-search-functions*.
265 266 267
    (let ((name (coerce-name system)))
      (flet ((try (f) (if-let ((x (funcall f name))) (return-from search-for-system-definition x))))
        (try 'find-system-if-being-defined)
268
        (try 'sysdef-immutable-system-search)
269
        (map () #'try *system-definition-search-functions*))))
270

271 272 273 274 275

  ;;; The legacy way of finding a system: the *central-registry*

  ;; This variable contains a list of directories to be lazily searched for the requested asd
  ;; by sysdef-central-registry-search.
276 277
  (defvar *central-registry* nil
    "A list of 'system directory designators' ASDF uses to find systems.
278 279 280 281 282 283 284 285 286

A 'system directory designator' is a pathname or an expression
which evaluates to a pathname. For example:

    (setf asdf:*central-registry*
          (list '*default-pathname-defaults*
                #p\"/home/me/cl/systems/\"
                #p\"/usr/share/common-lisp/systems/\"))

287 288
This variable is for backward compatibility.
Going forward, we recommend new users should be using the source-registry.")
289

290 291 292 293
  ;; Function to look for an asd file of given NAME under a directory provided by DEFAULTS.
  ;; Return the truename of that file if it is found and TRUENAME is true.
  ;; Return NIL if the file is not found.
  ;; On Windows, follow shortcuts to .asd files.
294
  (defun probe-asd (name defaults &key truename)
295
    (block nil
296 297 298 299 300 301 302 303 304
      (when (directory-pathname-p defaults)
        (if-let (file (probe-file*
                       (ensure-absolute-pathname
                        (parse-unix-namestring name :type "asd")
                        #'(lambda () (ensure-absolute-pathname defaults 'get-pathname-defaults nil))
                        nil)
                       :truename truename))
          (return file))
        #-(or clisp genera) ; clisp doesn't need it, plain genera doesn't have read-sequence(!)
305 306 307 308 309 310 311 312 313 314
        (os-cond
         ((os-windows-p)
          (when (physical-pathname-p defaults)
            (let ((shortcut
                    (make-pathname
                     :defaults defaults :case :local
                     :name (strcat name ".asd")
                     :type "lnk")))
              (when (probe-file* shortcut)
                (ensure-pathname (parse-windows-shortcut shortcut) :namestring :native)))))))))
315

316
  ;; Function to push onto *s-d-s-f* to use the *central-registry*
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
  (defun sysdef-central-registry-search (system)
    (let ((name (primary-system-name system))
          (to-remove nil)
          (to-replace nil))
      (block nil
        (unwind-protect
             (dolist (dir *central-registry*)
               (let ((defaults (eval dir))
                     directorized)
                 (when defaults
                   (cond ((directory-pathname-p defaults)
                          (let* ((file (probe-asd name defaults :truename *resolve-symlinks*)))
                            (when file
                              (return file))))
                         (t
                          (restart-case
                              (let* ((*print-circle* nil)
                                     (message
                                       (format nil
                                               (compatfmt "~@<While searching for system ~S: ~3i~_~S evaluated to ~S which is not an absolute directory.~@:>")
                                               system dir defaults)))
                                (error message))
                            (remove-entry-from-registry ()
                              :report "Remove entry from *central-registry* and continue"
                              (push dir to-remove))
                            (coerce-entry-to-directory ()
                              :test (lambda (c) (declare (ignore c))
                                      (and (not (directory-pathname-p defaults))
                                           (directory-pathname-p
                                            (setf directorized
                                                  (ensure-directory-pathname defaults)))))
                              :report (lambda (s)
                                        (format s (compatfmt "~@<Coerce entry to ~a, replace ~a and continue.~@:>")
                                                directorized dir))
                              (push (cons dir directorized) to-replace))))))))
          ;; cleanup
          (dolist (dir to-remove)
            (setf *central-registry* (remove dir *central-registry*)))
          (dolist (pair to-replace)
            (let* ((current (car pair))
                   (new (cdr pair))
                   (position (position current *central-registry*)))
              (setf *central-registry*
                    (append (subseq *central-registry* 0 position)
                            (list new)
                            (subseq *central-registry* (1+ position))))))))))

364 365 366 367

  ;;; Methods for find-system

  ;; Reject NIL as a system designator.
368 369 370 371
  (defmethod find-system ((name null) &optional (error-p t))
    (when error-p
      (sysdef-error (compatfmt "~@<NIL is not a valid system name~@:>"))))

372
  ;; Default method for find-system: resolve the argument using COERCE-NAME.
373 374 375 376
  (defmethod find-system (name &optional (error-p t))
    (find-system (coerce-name name) error-p))

  (defun find-system-if-being-defined (name)
377 378 379 380 381 382 383
    ;; This function finds systems being defined *in the current ASDF session*, as embodied by
    ;; its session cache, even before they are fully defined and registered in *defined-systems*.
    ;; The purpose of this function is to prevent races between two files that might otherwise
    ;; try overwrite each other's system objects, resulting in infinite loops and stack overflow.
    ;; This function explicitly MUST NOT find definitions merely registered in previous sessions.
    ;; NB: this function depends on a corresponding side-effect in parse-defsystem;
    ;; the precise protocol between the two functions may change in the future (or not).
384
    (first (gethash `(find-system ,(coerce-name name)) (asdf-cache))))
385

386 387 388 389 390 391 392 393 394 395 396
  (defclass define-op (non-propagating-operation) ()
    (:documentation "An operation to record dependencies on loading a .asd file."))

  (defmethod component-depends-on ((o define-op) (s system))
    `((load-op ,@(system-defsystem-depends-on s))
      ,@(loop* :for (o . c) :in (definition-dependencies s) :collect (list o c))
      ,@(call-next-method)))

  (defmethod component-depends-on ((o operation) (c undefined-system))
    (sysdef-error "Trying to use undefined or incompletely defined system ~A" (coerce-name c)))

397 398 399
  (defun load-asd (pathname
                   &key name (external-format (encoding-external-format (detect-encoding pathname)))
                   &aux (readtable *readtable*) (print-pprint-dispatch *print-pprint-dispatch*))
400
    "Load system definitions from PATHNAME.
401 402 403
NAME if supplied is the name of a system expected to be defined in that file.

Do NOT try to load a .asd file directly with CL:LOAD. Always use ASDF:LOAD-ASD."
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    (nest
     (with-asdf-session ())
     (with-standard-io-syntax)
     (let* ((primary-name (primary-system-name (or name (pathname-name pathname))))
            (*package* (find-package :asdf-user))
            ;; Note that our backward-compatible *readtable* is
            ;; a global readtable that gets globally side-effected. Ouch.
            ;; Same for the *print-pprint-dispatch* table.
            ;; We should do something about that for ASDF3 if possible, or else ASDF4.
            (*readtable* readtable)
            (*print-pprint-dispatch* print-pprint-dispatch)
            (*print-readably* nil)
            (*default-pathname-defaults*
             ;; resolve logical-pathnames so they won't wreak havoc in parsing namestrings.
             (pathname-directory-pathname (physicalize-pathname pathname)))
            (operation (make-operation 'define-op))
            (system (or (registered-system primary-name)
                        (let ((system
                               (make-instance 'undefined-system
                                              :name primary-name :source-file pathname)))
                          (register-system system)
                          system)))))
     (while-visiting-action (operation system))
     (handler-bind
         (((and error (not missing-component))
           #'(lambda (condition)
               (error 'load-system-definition-error
                      :name name :pathname pathname :condition condition))))
       (asdf-message (compatfmt "~&~@<; ~@;Loading system definition~@[ for ~A~] from ~A~@:>~%")
                     name pathname)
       (load* pathname :external-format external-format))))
435

436 437
  (defvar *old-asdf-systems* (make-hash-table :test 'equal))

438 439 440
  ;; (Private) function to check that a system that was found isn't an asdf downgrade.
  ;; Returns T if everything went right, NIL if the system was an ASDF of the same or older version,
  ;; that shall not be loaded. Also issue a warning if it was a strictly older version of ASDF.
441 442 443 444 445 446 447
  (defun check-not-old-asdf-system (name pathname)
    (or (not (equal name "asdf"))
        (null pathname)
        (let* ((version-pathname (subpathname pathname "version.lisp-expr"))
               (version (and (probe-file* version-pathname :truename nil)
                             (read-file-form version-pathname)))
               (old-version (asdf-version)))
448 449 450 451 452 453 454
          (cond
            ((version< old-version version) t) ;; newer version: good!
            ((equal old-version version) nil) ;; same version: don't load, but don't warn
            (t ;; old version: bad
             (ensure-gethash
              (list (namestring pathname) version) *old-asdf-systems*
              #'(lambda ()
455
                 (let ((old-pathname (system-source-file (registered-system "asdf"))))
456
                   (warn "~@<~
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
        You are using ASDF version ~A ~:[(probably from (require \"asdf\") ~
        or loaded by quicklisp)~;from ~:*~S~] and have an older version of ASDF ~
        ~:[(and older than 2.27 at that)~;~:*~A~] registered at ~S. ~
        Having an ASDF installed and registered is the normal way of configuring ASDF to upgrade itself, ~
        and having an old version registered is a configuration error. ~
        ASDF will ignore this configured system rather than downgrade itself. ~
        In the future, you may want to either: ~
        (a) upgrade this configured ASDF to a newer version, ~
        (b) install a newer ASDF and register it in front of the former in your configuration, or ~
        (c) uninstall or unregister this and any other old version of ASDF from your configuration. ~
        Note that the older ASDF might be registered implicitly through configuration inherited ~
        from your system installation, in which case you might have to specify ~
        :ignore-inherited-configuration in your in your ~~/.config/common-lisp/source-registry.conf ~
        or other source-registry configuration file, environment variable or lisp parameter. ~
        Indeed, a likely offender is an obsolete version of the cl-asdf debian or ubuntu package, ~
        that you might want to upgrade (if a recent enough version is available) ~
        or else remove altogether (since most implementations ship with a recent asdf); ~
        if you lack the system administration rights to upgrade or remove this package, ~
        then you might indeed want to either install and register a more recent version, ~
        or use :ignore-inherited-configuration to avoid registering the old one. ~
        Please consult ASDF documentation and/or experts.~@:>~%"
478 479
                         old-version old-pathname version pathname))))
             nil))))) ;; only issue the warning the first time, but always return nil
480

481 482
  (defun locate-system (name)
    "Given a system NAME designator, try to locate where to load the system from.
483 484 485
Returns five values: FOUNDP FOUND-SYSTEM PATHNAME PREVIOUS PREVIOUS-TIME
FOUNDP is true when a system was found,
either a new unregistered one or a previously registered one.
Robert P. Goldman's avatar
Robert P. Goldman committed
486 487
FOUND-SYSTEM when not null is a SYSTEM object that may be REGISTER-SYSTEM'ed.
PATHNAME when not null is a path from which to load the system,
488 489 490
either associated with FOUND-SYSTEM, or with the PREVIOUS system.
PREVIOUS when not null is a previously loaded SYSTEM object of same name.
PREVIOUS-TIME when not null is the time at which the PREVIOUS system was loaded."
491
    (with-asdf-session () ;; NB: We don't cache the results. We once used to, but it wasn't useful,
492 493 494 495 496 497 498 499 500 501 502
      ;; and keeping a negative cache was a bug (see lp#1335323), which required
      ;; explicit invalidation in clear-system and find-system (when unsucccessful).
      (let* ((name (coerce-name name))
             (in-memory (system-registered-p name)) ; load from disk if absent or newer on disk
             (previous (cdr in-memory))
             (previous (and (typep previous 'system) previous))
             (previous-time (car in-memory))
             (found (search-for-system-definition name))
             (found-system (and (typep found 'system) found))
             (pathname (ensure-pathname
                        (or (and (typep found '(or pathname string)) (pathname found))
503 504
                            (system-source-file found-system)
                            (system-source-file previous))
505 506 507 508
                        :want-absolute t :resolve-symlinks *resolve-symlinks*))
             (foundp (and (or found-system pathname previous) t)))
        (check-type found (or null pathname system))
        (unless (check-not-old-asdf-system name pathname)
509 510
          (check-type previous system) ;; asdf is preloaded, so there should be a previous one.
          (setf found-system nil pathname nil))
511
        (values foundp found-system pathname previous previous-time))))
512

513 514 515 516 517
  ;; Main method for find-system: first, make sure the computation is memoized in a session cache.
  ;; unless the system is immutable, use locate-system to find the primary system;
  ;; reconcile the finding (if any) with any previous definition (in a previous session,
  ;; preloaded, with a previous configuration, or before filesystem changes), and
  ;; load a found .asd if appropriate. Finally, update registration table and return results.
518
  (defmethod find-system ((name string) &optional (error-p t))
519
    (with-asdf-session (:key `(find-system ,name))
520 521 522
      (let ((primary-name (primary-system-name name)))
        (unless (equal name primary-name)
          (find-system primary-name nil)))
523
      (or (and *immutable-systems* (gethash name *immutable-systems*) (registered-system name))
524 525 526
          (multiple-value-bind (foundp found-system pathname previous previous-time)
              (locate-system name)
            (assert (eq foundp (and (or found-system pathname previous) t)))
527
            (let ((previous-pathname (system-source-file previous))
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
                  (system (or previous found-system)))
              (when (and found-system (not previous))
                (register-system found-system))
              (when (and system pathname)
                (setf (system-source-file system) pathname))
              (when (and pathname
                         (let ((stamp (get-file-stamp pathname)))
                           (and stamp
                                (not (and previous
                                          (or (pathname-equal pathname previous-pathname)
                                              (and pathname previous-pathname
                                                   (pathname-equal
                                                    (physicalize-pathname pathname)
                                                    (physicalize-pathname previous-pathname))))
                                          (stamp<= stamp previous-time))))))
543
                ;; Only load when it's a pathname that is different or has newer content.
544
                (load-asd pathname :name name)))
545 546
            ;; Try again after having loaded from disk if needed
            (let ((in-memory (system-registered-p name)))
547 548 549 550 551 552 553
              (cond
                (in-memory
                 (when pathname
                   (setf (car in-memory) (get-file-stamp pathname)))
                 (cdr in-memory))
                (error-p
                 (error 'missing-component :requires name))
554
                (t
555
                 (return-from find-system nil)))))))))