          (cond ((not err)
                 (dolist (file files)
                   (when (equal (cdr (assq 'type file)) "full")
-                    (with-current-buffer (find-file-noselect (expand-file-name (cdr (assq 'name file)) tern-project-dir))
+                    (with-current-buffer (find-file-noselect (expand-file-name (cdr (assq 'name file)) (tern-project-dir)))
                       (setf tern-buffer-is-dirty nil))))
                 (funcall f data))
                ((not (eq mode :silent)) (tern-message "Request failed: %s" err)))))

        (funcall c nil json)))))

(defun tern-project-dir ()
  (or tern-project-dir
      (and (not (buffer-file-name)) (setf tern-project-dir ""))
      (let ((project-dir (file-name-directory (buffer-file-name))))
        (cl-loop for cur = project-dir then (let ((shorter (file-name-directory (directory-file-name cur))))
                                              (and (< (length shorter) (length cur)) shorter))
                 while cur do
                 (when (file-exists-p (expand-file-name ".tern-project" cur))
                   (cl-return (setf project-dir cur))))
        (setf tern-project-dir project-dir))))

(defun tern-find-server (c &optional ignore-port)
  (cl-block nil

(defvar tern-known-port nil)
(defvar tern-server nil)
(defvar tern-explicit-port nil)

(defun tern-message (fmt &rest objects)
  (apply 'message fmt objects))
