obsidian-dataview icon indicating copy to clipboard operation
obsidian-dataview copied to clipboard

Task path is not updated after note has been renamed

Open kim-fehl opened this issue 1 year ago • 13 comments

What happened?

I use dataview objects in Templater code. This code snippet copies all the tasks with a scheduled date into the daily note, adding a link to a project note where these tasks belong to.
When the project note (containing the task) is renamed, task.path still contains original note name.

Probably has to do with this issue

DQL

No response

JS

<%*
const targetDate = tp.date.now("YYYY-MM-DD");
const dv = this.app.plugins.plugins["dataview"].api;

// Query all tasks in the vault
const scheduled_tasks = dv.pages().file.tasks
  .where(t => !t.completed && t.text.includes(`⏳ ${targetDate}`));

if (scheduled_tasks.length != 0) {
  scheduled_tasks.forEach(task => {
    // Extract the note title from the task's file path:
    let noteTitle = task.path.split("/").pop().replace(".md", "");
    // Create markdown checkbox format for each task, using the note title in the link.
    tR += `- [ ] ${task.text.replace(/⏳\\s*${targetDate}/, "").trim()} [[${task.path}|🗂️ ${noteTitle}]]\n`;
  });
}
%>

Dataview Version

0.5.67

Obsidian Version

1.8.9

OS

Linux

kim-fehl avatar Mar 18 '25 11:03 kim-fehl

After a template has been executed, it is considered static text, and you've lost any lookup of any part which was used in the template.

In other words, when you insert this template, and it uses Dataview to produce the output, you loose the link to the Dataview query, and all links and other text are just normal text, which will follow the normal rules if you rename or move any files related to that text.

So I'm not sure, what you're considering to be the bug here? Is it just a misconception from your part, that the template would be re-evaluated every now and then?

holroy avatar Mar 18 '25 16:03 holroy

@holroy, I understand that Templater produces its output once and it remains static. Below I show that renaming the project note before Templater invocation leads to incorrect paths obtained via Dataview. I thought maybe it is a caching issue, but 10 minutes after renaming the bug is still there. Here's the video:

Image

Some of my settings:

Image

Image

kim-fehl avatar Mar 19 '25 09:03 kim-fehl

I was curious so had a look.

I'm finding it hard to tell from the video what exactly the bug is.

Is there a simpler workaround with fewer moving parts? Like, assuming it really is a dataview issue, it should be possible to provide a minimal reproduction that demonstrates the problem with just a Dataview script.

Otherwise, how can you be sure it's not a Templater issue?

claremacrae avatar Mar 19 '25 17:03 claremacrae

@claremacrae I'm sorry I didn't bother to make a minimal reproduction in the first place. Here's the one, showing that the issue persists without Templater:

const all_tasks = dv.pages('"DataView Bug/Test Projects"').file.tasks;

all_tasks.forEach(task => {
    let noteTitle = task.path.split("/").pop().replace(".md", "");
    dv.paragraph(`- [ ] ${task.text} [[${task.path}|🗂️ ${noteTitle}]]`);
});

Image

kim-fehl avatar Mar 20 '25 09:03 kim-fehl

Thanks - that's a lot simpler.

I think that dataview has some settings to control how often it updates its results. I wonder if that could be affecting the behaviour?

claremacrae avatar Mar 20 '25 09:03 claremacrae

@claremacrae After three hours, it's still the same. Looking at the source code, I see that .path property is derived from the .linkproperty. I tried to used it instead -- the bug is till here. I think you are right, it is likely an indexing/caching issue, as this source file reads, "Renames do not set off the metadata cache" and handles them separately.

kim-fehl avatar Mar 20 '25 13:03 kim-fehl

Good find.

This sounds like the sort of thing that might have been reported before then.

Maybe earlier reports include workarounds?

claremacrae avatar Mar 20 '25 13:03 claremacrae

@claremacrae Yes, in the issue I have linked earlier there is a workaround relying on Obsidian's own cache, not the DataView index.

kim-fehl avatar Mar 20 '25 13:03 kim-fehl

Thank you. So what was the purpose of creating a duplicate bug report then?

claremacrae avatar Mar 20 '25 14:03 claremacrae

Ah, maybe it was only simplifying the reproduction that revealed whether you are encountering the same issue as earlier????

What do you think, is it now the same as the earlier issue? Would the fix for the earlier issue fix this one?

claremacrae avatar Mar 20 '25 14:03 claremacrae

They're not the same as far as I can see. The other issue is more related to changing the name of the file you're currently viewing, and how dataview handles that.

This issue seems to be more an issue related to how the data used in a query had been invalidated by a rename operation without Dataview detecting the rename. This is, and will be an issue, when we rely on the cache to get data from.

I've not looked into which cache has been invalidated, and whether this needs to be handled by dataview or obsidian. That is whether there should be some propagation related to the rename event somehow.

@claremacrae, do you have (or have had) any similar issue related to queries relying on the {{query.file.path}} variable and renaming of that file?

holroy avatar Mar 20 '25 17:03 holroy

Great stuff - thanks @holroy.

Funnily enough I did something in this area in Tasks recently.

This is the one specific commit:

https://github.com/obsidian-tasks-group/obsidian-tasks/commit/5fc9fba31f11d1b6ef388b4ec090df0137e06fe1

Here's the latest code, after a bunch of refactoring:

https://github.com/obsidian-tasks-group/obsidian-tasks/blob/84ff5070059ff4c99cbe61f5e3492f551cbcbec9/src/Renderer/QueryRenderer.ts#L152-L176

Code is MIT - free free to use...

claremacrae avatar Mar 20 '25 18:03 claremacrae

If you updated the text of the task then dataview will in fact recognize that event and update static content. It seems specific with the file rename that it doesn't seem to see that event as a trigger to refresh its content. If you close obsidian entirely and then reopen it....the paths will be updated and point to the renamed note.

rudyolph avatar Sep 30 '25 17:09 rudyolph