Subject: bug#66683: 30.0.50; xref-find-references returns duplicates

From: Dmitry Gutov <[email-masked]>
Date: Mon, 23 Oct 2023 22:11:51 +0300

On 22/10/2023 18:29, Petteri Hintsanen wrote:
> [This is a bug report from an old issue discussed in
> [email-masked]  (dated 19 Dec 2022, Message-ID
> <[email-masked]>).  Dmitry Gutov asked me
> to file a bug, which I unfortunately forgot to do that time.  Hence
> filing now.]
> 
> I discovered a somewhat peculiar case where xref-find-references returns
> duplicate references for elisp symbols.
> 
> Recipe:
> 
> 1. Create a file ~/foo/bar/bar.el with contents:
> 
>    (defun bar-f ())
> 
>    (defun bar-g ()
>      (bar-f))
> 
>    (provide 'bar)
> 
> 2. Create a git repo in ~/foo/bar and commit bar.el.  This is just to
> set up a project that Emacs can recognize.
> 
> 3. Start emacs -Q and eval
> 
>    (add-to-list 'load-path "~/foo")
>    (add-to-list 'load-path "~/foo/bar")
>    (require 'bar)
> 
> 4. C-x C-f ~/foo/bar/bar.el
> 
> 5. M-x xref-find-references RET bar-f RET
> 
> On my machine I get
> 
>    bar.el
>    1: (defun bar-f ())
>    4:   (bar-f))
>    1: (defun bar-f ())
>    4:   (bar-f))
> 
> i.e. references are duplicated.  The issue seems to be in load-path: if
> I remove (add-to-list 'load-path "~/foo") from step (3) above, then I
> don't see any duplicates.

Hi! Thanks for the report and the reminder.

Could you try this little patch below?

diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index fd788ec8f32..81618428bf3 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -281,9 +281,10 @@ xref-backend-references
           (xref-references-in-directory identifier dir)
         (message "Searching %s... done" dir)))
     (let ((pr (project-current t)))
-     (cons
-      (xref--project-root pr)
-      (project-external-roots pr)))))
+     (project-combine-directories
+      (cons
+       (xref--project-root pr)
+       (project-external-roots pr))))))

  (cl-defgeneric xref-backend-apropos (backend pattern)
    "Find all symbols that match PATTERN string.