obsidian-tasks-group / obsidian-tasks

Task management for the Obsidian knowledge base.
https://publish.obsidian.md/tasks/
MIT License
2.48k stars 230 forks source link

Query for all tasks in the query's path, folder, filename etc does not work in canvas cards #2971

Open psigurdsson opened 3 months ago

psigurdsson commented 3 months ago

Please check that this issue hasn't been reported before.

Expected Behavior

Expected is that the following query will work when placed inside a canvas card, since it works inside a normal markdown note:

```tasks  
filter by function task.file.folder.includes(query.file.folder)
```

Current behaviour

Instead of showing the task contained inside the note in the subfolder, the canvas card containing this query shows the following error message:

Tasks query: Error: Search failed.
The error message was:
    "TypeError: Cannot read properties of null (reading 'file')"

Steps to reproduce

From @claremacrae: there is also a shorter reproduction in https://github.com/obsidian-tasks-group/obsidian-tasks/issues/2971#issuecomment-2238535676...

Full description of the steps to reproduce:

  1. Create a new Obsidian vault
    • Click the line with the name of your vault, at the bottom next to the gear icon
    • Select "Manage vaults"
    • Select Create new vault
    • Give the vault a name and click Create
  2. In the new vault, ensure that Canvas plugin is enabled
    • Click the gear icon
    • Select Core plugins
    • Verify that the slider next to Canvas is showing that it's active
  3. Get the tasks plugin
    • Click the gear icon again, if needed
    • Select Community plugins from the list
    • Click the "Turn on community plugins" button
    • Click the "Browse" button
    • Find and open "Tasks" by Martin Schenck and Clare Macrae
    • Click Install
    • Click Enable
  4. Create a two-level folder structure
    • Navigate back to the main window by closing the plugin and settings windows
    • Click the new folder icon on the left side of the interface
    • Give the folder a name, e.g. "My projects".
    • Right-click the folder and select "New folder" to create a subfolder
    • Give the subfolder a name, e.g. "Home projects".
  5. Create a new note with a task in the subfolder
    • Right-click the subfolder and click "New note"
    • Give the note a name, if desired, e.g. "Project A", by replacing "Untitled" with "Project A".
    • In the area below, a task, by entering "- [ ] First task"
  6. Create a note in the first level folder which shows all tasks in the folder and subfolders below
    • Right-click the first level folder ("My projects") and click "New note"
    • Give the note a name, if desired, e.g. "Projects list", by replacing "Untitled" with "Project list"
    • In the area below the title, enter the following
      filter by function task.file.folder.includes(query.file.folder)

      The note should now show the task contained inside the note in the subfolder and it does.

  7. Create a new canvas inside the first level folder with a card which shows all tasks in the folder and subfolders below
    • Right-click the first level folder ("My projects") and click "New canvas"
    • Right-click an empty area in the canvas and click "Add card"
    • Inside the card, enter the same task query as in step 6 above.

I expected this card now to show the task contained inside the note in the subfolder, but it didn't.

Which Operating Systems are you using?

Obsidian Version

1.6.7

Tasks Plugin Version

7.6.1

Checks

Possible solution

I don't know how to fix this but there is a workaround, which is to add the note which already contains the query to the canvas:

Note from @claremacrae: there is a slightly simpler workaround in https://github.com/obsidian-tasks-group/obsidian-tasks/issues/2971#issuecomment-2238520505...

This workaround does not adequately achieve what I want. I want to copy the canvas into many different folders, which already exist, and utilize the same tasks query dynamically. If the functionality would work as expected, I would not need to change anything in the copies in the canvas whereas in the workaround I will have to recreate many of the cards for each copy of the canvas.

claremacrae commented 3 months ago

Thank you very much for finding and reporting the problem.

Having a list of reproduction steps is also very valuable, and much appreciated.

(For future reference, if you ever report another bug in this project, you can save yourself a lot of time by assuming knowledge of basic use of Obsidian, like in this example.)

claremacrae commented 3 months ago

I can reproduce the problem, and will write up what I've found.

I also note that dataview doesn't recognise the folder containing a query in a canvas file:

```dataview
TASK
WHERE file.folder = this.file.folder
```
claremacrae commented 3 months ago

The relevant code is the context.sourcePath value in line 48 here:

https://github.com/obsidian-tasks-group/obsidian-tasks/blob/63ee976eeab5d7aa0cf29dcb7332828a07b3c6a4/src/Renderer/QueryRenderer.ts#L41-L52

When the Tasks query block is in an ordinary Markdown file, the sourcePath is supplied:

image

When the Tasks query block is in a card inside a Canvas file, the sourcePath is empty:

image
claremacrae commented 3 months ago

I have asked for help on Obsidian Discord.

claremacrae commented 3 months ago

I will have to wait and see if the Obsidian team offers a workaround. Currently Obsidian is just not giving plugins the file path of the canvas, so there's nothing I can see to do to fix this.

Possible solution

I don't know how to fix this but there is a workaround, which is to add the note which already contains the query to the canvas:

  • Right-click, an empty area in the canvas and click "Add note from vault"
  • Select the note that was created in step 6, above. This will now show a canvas card with the list of tasks.

@psigurdsson You can do that a lot more easily than the Right-click option...

  1. Open the canvas
  2. Drag the note from the Explorer into an empty area in the Canvas
claremacrae commented 3 months ago

A simpler reproduction is:

  1. Create or open a vault with the Tasks plugin installed and enabled
  2. Create an empty test folder, called test folder
  3. Create a markdown note called test task, in that folder:
    • View it in Source or Live Preview mode
    • Run Tasks: Create or edit task
    • Enter any old data and click Apply
  4. Create a new Canvas called test canvas
  5. Add a card to the Canvas and paste in the following text:
```tasks
ignore global query
filter by function task.file.folder.includes(query.file.folder)
explain
```
  1. Click away from the card, so that it is rendered.

Actual result:

image

Expected result (when the same query is used in a Markdown file:

image

psigurdsson commented 3 months ago

Clare, thanks for the super-quick responses and for the tips, especially on how I can report bugs more efficiently in the future. I'm happy that my bug-report helped develop the tasks plugin, which I'm a great fan of. I'm right now experimenting with creating dashboards for the various aspects of my life, including tasks that I've registered in notes inside folders which represent my areas of focus and that's where this came from. I should be able to use the workaround but there are also other ways to create dashboards than canvas and I might explore those also.

claremacrae commented 2 months ago

I've renamed this to make it clear that the problem is specifically querying using the folder that the query is in, rather than any old folder...