I've had this problem for a very long time (which makes it a little hard to know what originally caused it), but it was mostly something I could ignore until now, however my workflow would be a lot simpler now if I could just solve this.
For a long time I noticed that if I called org-open-at-point (C-c C-o) on a headline in the agenda, after selecting to link I wished to open, it would fail due to wrong-type-argument, I recently discovered this wasn't an issue with the agenda as I had thought, but actually happens whenever I call this function on a headline even in the org-file directly.
However if I call the function directly on a link, even a link contained in the headline, it works as expected (although not when called directly on a link that is contained in the property drawer).
So far I've narrowed this down to the fact that when called on a headline, org-open-at-point calls org-link-open-from-string, which does not appear to be the case when called directly on a link. Presumably feeding the link I select from the prompt to this function as a string, while it doesn't need that rigmarole when called directly on a link itself. (Although I don't understand why this errors still when called directly on links in the properties drawer).
Here is the output of the emacs-lisp debugger cleaned up very slightly for minor readability improvement. The only thing I can think of is that something in the org-mode-hook is doing something weird?
(wrong-type-argument stringp nil)
file-name-directory(nil)
(string-match-p (concat (getenv "DATA_DIR") "/Notes") (file-name-directory (buffer-file-name)))
(if (string-match-p (concat (getenv "DATA_DIR") "/Notes") (file-name-directory (buffer-file-name))) (howm-mode))
(lambda nil (if (string-match-p (concat (getenv "DATA_DIR") "/Notes") (file-name-directory (buffer-file-name))) (howm-mode)))()
run-hooks(change-major-mode-after-body-hook text-mode-hook outline-mode-hook org-mode-hook)
apply(run-hooks (change-major-mode-after-body-hook text-mode-hook outline-mode-hook org-mode-hook))
run-mode-hooks(org-mode-hook)
org-mode()
org-link-open-from-string("[[pdftools:/data/Reading.bib]]")
eval((org-link-open-from-string "[[pdftools:/data/Reading.bib]]") t)
eval-expression((org-link-open-from-string "[[pdftools:/data/Reading.bib]]") nil nil 127)
funcall-interactively(eval-expression (org-link-open-from-string "[[pdftools:/data/Reading.bib]]") nil nil 127)
call-interactively(eval-expression nil nil)
command-execute(eval-expression)
I'm truly stumped here guys, spent a few hours on this, and also "Googling" occasionally over the last few months. Nobody else that I've found seems to have a comparable issue.
Please ignore the fact that I'm calling a pdftools link on something that isn't a PDF here, that has nothing to do with it, the issue is replicable regardless of the format of the link, and isn't exclusive to pdftools links or mismatched files to link type or anything like that.
What's causing this problem? I've spent tens of hours on this, to no avail. I'm still new to the debugger in Emacs to be honest.
UPDATE: Putting my thoughts down like this made me realise the offending call to (buffer-file-name) was being called due to this hook I'd added
(add-hook 'org-mode-hook '(lambda ()
(if (string-match-p (concat (getenv "DATA_DIR") "/Notes") (file-name-directory (buffer-file-name)))
(howm-mode))))
I've still no idea what the reason for buffer-file-name being nil is, but removing this piece of code, did indeed resolve the immediate issue.
Now I guess the question I still need answered, is why? Is it possible that this function is being called when the value is actually nil, i.e., there is no buffer? I had always thought there was always a buffer. This hook is also pretty important to my workflow.
ANOTHER UPDATE: Just realised that I can use a conditional to get around this issue:
(add-hook 'org-mode-hook '(lambda ()
(if
(string-match-p (concat (getenv "DATA_DIR") "/Notes") (if
(buffer-file-name) (file-name-directory (buffer-file-name)) (message "No buffer file name.")))
(howm-mode))))
(Yes I realise this is really inefficient and not very functional, I'll clean it up later ;).)
However if anybody has an explanation for exactly how buffer-file-name was nil, I'd love to know as I'm still learning Emacs' internals slowly slowly.
YET ANOTHER EDIT: It's obvious now that it's occurred to me but even if one IS always in a buffer as I suspect, it's also true that not all buffers have a file associated. Hence the nil value. Why I was ending up in a non-file buffer at precisely the moment org-hooks were called I'm not 100% sure.