-
-
Notifications
You must be signed in to change notification settings - Fork 239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: protect null frontmatter.tags in TaskFile.ts #3082
base: main
Are you sure you want to change the base?
Conversation
In my vault I get an uncaught exception from this line of code, where the tags is null.
…-matter Protect null tags on frontmatter
Hi @evolutioned, many thanks for contributing to Tasks. Before we talk about the code change, we need to understand how to reproduce the bug. Then we can test the change, and work out how to add an automated test to ensure the problem never returns. Can you give us a Markdown file that causes this problem please. |
hi @claremacrae |
Thanks @evolutioned. It is still worth your giving us the broken FrontMatter of your file so we can fix this for others. Can you use the File Recovery core plugin, or Sync History or similar, to get the original content? |
Hi @claremacrae Chronology7.60 worked great. So, I am unable to reproduce the conditions again to create that issue giving this anomaly. Is there anything here that gives a clue? Notes/DailyNotes/2021/May-2021/📅-Week-18-Review-(3-May-to-9-May)/3-May-2021-----Monday
|
Thanks for the info, but sorry, I was unclear. As I understand, this PR is to fix a particular query that fails on a particular frontmatter content. So please supply, as plain text in fenced code blocks:
It needs to be fenced code blocks so that I can click on the Copy button and copy the content to write tests... |
If you can't get back the original file, please try to recreate the situation. |
I have this query on my Daily Note, so every day this is pulling back tasks. This is what failed with that message in the Debug Console.
The offending frontmatter was:
As I mentioned before, it was not occurring in 7.60 or below, then broke in .7.7 and when I tried 7.10. As soon as I opened the offending file, the Obsidian Tasks plugin worked once turned off and back on again. So I've tried for hours over the weekend to reproduce the conditions but have been unable to. My only guess is that the cache of this file's frontmatter was corrupt and by opening the file, Obsidian corrected it? |
Great - thanks very much indeed. |
Reopening as draft so I remember to look at this later. |
Quality Gate passedIssues Measures |
Happy to help if I can. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am unsure how to proceed with this.
I have tried hard with the code on main
to make Obsidian trigger the error that this PR works around, and in all cases of invalid frontmatter that I have tried rawFrontMatter
has been undefined
, so the JSON.parse(JSON.stringify(rawFrontmatter))
call is never triggered.
This in turn makes it impossible to write a failing test that demonstrates the problem - and in turn, confirms that the PR behaves as intended.
I am not comfortable merging changes without tests.
I wonder whether Obsidian has become stricter in its parsing of formatter since the original problem happened. (I am on 1.7.1 Insider version)
// protect against null tags (happens in my vault) | ||
if (this._frontmatter) { | ||
this._frontmatter.tags = parseFrontMatterTags(rawFrontmatter) ?? []; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the following lines:
obsidian-tasks/src/Scripting/TasksFile.ts
Lines 11 to 12 in e0bb9bd
// Always make TasksFile.frontmatter.tags exist and be empty, even if no frontmatter present: | |
private readonly _frontmatter = { tags: [] } as any; |
line 21 of this code is wrong, as it always overwrites this._frontmatter
, including if JSON.parse(JSON.stringify(rawFrontmatter))
returns a null
or undefined
value.
I suspect that this always-overwriting is the root cause of the issue you have found.
And you have worked around it by only updating this._frontmatter.tags
if this._frontmatter
is valid.
But if this._frontmatter
is not valid, the TasksFile
object is left in an invalid state.
As such, I don't think I can accept this PR as-is.
A fuller fix would not overwrite the this._frontmatter
value if JSON.parse(JSON.stringify(rawFrontmatter))
returns invalid data.
/CC @ilandikov for comment too...
Example file that I have been experimenting with:
|
On my obsidian (1.6.7) running 7.10 tasks plugin, that example file parses correctly. I'm unable to reproduce that issue I had (screen shot before), so can only suspect that the original markdown file has been fixed by opening. Happy to reject to PR and accept the issue as no longer applying. |
Types of changes
Changes visible to users:
feat
- non-breaking change which adds functionality)feat!!
orfix!!
- fix or feature that would cause existing functionality to not work as expected)fix
- non-breaking change which fixes an issue)docs
- improvements to any documentation content for users)vault
- improvements to the Tasks-Demo sample vault)contrib
- any improvements to documentation content for contributors - see Contributing to Tasks)Internal changes:
refactor
- non-breaking change which only improves the design or structure of existing code, and making no changes to its external behaviour)test
- additions and improvements to unit tests and the smoke tests)chore
- examples include GitHub Actions, issue templates)Description
Motivation and Context
How has this been tested?
Screenshots (if appropriate)
Checklist
yarn run lint
.Terms