blacksmithgu / obsidian-dataview

A data index and query language over Markdown files, for https://obsidian.md/.
https://blacksmithgu.github.io/obsidian-dataview/
MIT License
7.11k stars 418 forks source link

When opening a note, dv.current() and this.file are stale in Inline Queries (LP mode) #1980

Open GitOnHub opened 1 year ago

GitOnHub commented 1 year ago

What happened?

Inline queries (and inline JS queries) that use dv.current() or this.file sometimes reflect the previously opened file instead of the newly opened file that defines the running query. Such Inline queries are therefore unreliable to the point of being unusable for some situations. This is in Live Preview mode.

This looks to be a race condition.

Setup

In the screenshot, queries are defined as indicated. The situation was created by having NoteA active, and then opening NoteB (possibly with ctrl-shift-T). image

Initial Assessment

I've traced the problem through dv.current() to inlineRender(), which does app.workspace.getActiveFile() for every inline query. It seems obsidian is 'slow' to switch to the newly opened file. But they function call result may actually be correct in the sense that during rendering the active editor might not have switched yet; it's still rendering so can you really blame it?

I don't know Dataview sources and the Obsidian API well enough to come up with a solution. From a design perspective my hunch is that the currently active file is not necessarily the same file as is being rendered, so Dataview should not rely on that mechanism. It would be great if during rendering, Obsidian would somehow provide information about the file that is being rendered to the widgets, and Dataview could use that to back dv.current().

@blacksmithgu thanks for making this excellent Plugin! It surely makes for half of my Obsidian experience.

NoteB.md

DQL

= this.file.path

JS

`$= dv.current().file.path`

Dataview Version

0.5.56

Obsidian Version

1.3.5

OS

Windows

dotKokott commented 2 weeks ago

Having the same issue, still exists.

In my case I am creating the note via Calendar "Open weekly note". If I have queries in the weekly note template that refer to this.file or dv.current().file they do not work. I have to reload the note or enter the query with. my cursor and exit again for them to work.

In the inline.js query it throws the exception. In an inline DQ query it just displays -

image

In my case it happens half of the time I create from this note template.

Dataview Version 0.5.67

Obsidian Version 1.7.5

OS MacOS