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

[Bug] `this` Bound Incorrectly When Populating Field Using Inline Query

Open iamrecursion opened this issue 2 years ago • 8 comments

What happened?

When writing a dataview query in file Foo.md that depends on a field F:: in Bar.md that is populated by a dataview query, the value of the this variable is bound incorrectly. Define F as follows:

F:: `= this.file.name`

When the query runs within the file Bar, the result is "Bar", which is correct. When the query is run as part of evaluating a query in Foo (in my case a table), this gets bound to Foo, and the value in the corresponding column is incorrect.

This can be trivially reproduced as follows:

  1. Create Bar.md as follows:
Baz:: `= this.file.name`

- [[Foo]]
  1. Preview Bar and note that the value of the field Baz is "Bar".
  2. Create Foo.md as follows:
```dataview
table without id
  link(file.name) as Name,
  Baz
from [[Foo]]
```
  1. Preview Foo and see that the value in column "Baz" that corresponds to the file name "Bar" is "Foo" and not "Bar".

This may not be a bug. In that case I submit that it's counter-intuitive to bind this at query execution time, rather than at query definition time. The late binding leads to behaviours like this, where it's difficult to correctly nest queries in an extensible fashion.

DQL

table without id
  link(file.name) as Name,
  Baz
from [[Foo]]

JS

No response

Dataview Version

0.4.26

Obsidian Version

0.14.2

OS

MacOS

iamrecursion avatar Mar 18 '22 17:03 iamrecursion

I should note that this is not platform specific. I've reproduced it on iOS too.

iamrecursion avatar Mar 18 '22 17:03 iamrecursion

Yeah, I don't think this kind of recursion is possible yet. Out of curiosity, can you talk about the use case here a bit?

AB1908 avatar Mar 18 '22 18:03 AB1908

I often have notes where fields that I may want to query later can be gleaned from the title with a little bit of code (e.g. pulling an author out with a regex). It makes far more sense from a maintenance perspective to have those fields created once (in the note), rather than having to pull them out each time the note is accessed. While Templater is a good fit for when these things are static and unlikely to change, when they are more dynamic it makes far more sense to me to use dataview queries.

I should note that the recursion does work in the sense that the correct expression is evaluated. It's just evaluated in the wrong context.

iamrecursion avatar Mar 18 '22 21:03 iamrecursion

Can you repro this with DVJS?

AB1908 avatar Mar 20 '22 05:03 AB1908

To clarify, making the inline query, the main query, or both with DVJS?

iamrecursion avatar Mar 21 '22 01:03 iamrecursion

The main query. I'm just curious to see what happens. At best, I suspect we'll still see the wrong results.

AB1908 avatar Mar 21 '22 14:03 AB1908

This does actually work correctly if you write the main query in DVJS. If you write the inline query in DVJS it still fails, much as you would expect.

iamrecursion avatar Mar 25 '22 22:03 iamrecursion

#1023 also related

AB1908 avatar Jun 14 '22 08:06 AB1908