             (let (limit (point))
               (goto-char (point-max))
               (when (re-search-backward gerbil-error-locat-rx limit t)
-                (let ((loc (gerbil-extract-locat (buffer-substring (point) (point-max)))))
-                  (switch-to-buffer (car loc))
+                (let* ((loc (gerbil-extract-locat (buffer-substring (point) (point-max))))
+                       (fname (if gerbil-build-directory
+                                  (concat gerbil-build-directory (car loc))
+                                (car loc))))
+                  (find-file fname)
                   (goto-line (cadr loc))
                   (forward-char (- (caddr loc) 1))
                   (mark-sexp)))))))

  (gerbil-reload-file buffer-file-name))

(defvar gerbil-compile-optimize t)

(defun gerbil-compile-current-buffer ()
  (interactive)

    (with-current-buffer buf
      (goto-char (point-max))
      (insert "> build " build "\n"))
    (setq gerbil-build-directory build-dir)
    (let ((proc (start-process "build" buf "sh" "-c"
                               (concat "cd " build-dir " && ./" build-script))))
      (set-process-sentinel proc 'gerbil-compile-sentinel)
