dalcantara7 / obsidian-auto-moc

GNU General Public License v3.0
40 stars 1 forks source link

Bug: "Command failed to execute: auto-moc:add-missing-linked-mentions" #2

Closed avirosso closed 2 years ago

avirosso commented 2 years ago

Hi,

I tested the plugin (Obsidian Version 0.1.4.6 / Plugin Version 1.0.1):

It shows a notice "Linking mentions" but nothing happens.

Developer Console:

Command failed to execute: auto-moc:add-missing-linked-mentions

app.js:1 TypeError: Cannot read properties of undefined (reading 'children') at getLinkedMentions (eval at (app.js:1:1494667), :111:48) at runAutoMOC (eval at (app.js:1:1494667), :138:26) at Object.checkCallback (eval at (app.js:1:1494667), :74:15) at Mz (app.js:1:1618005) at e.executeCommandById (app.js:1:1619289) at eval (eval at (app.js:1:1494667), :74:11954) at e.o [as executeCommandById] (eval at (app.js:1:1494667), :23:77227) at T.eval (eval at (app.js:1:1494667), :15:16522) at Generator.next () at eval (eval at (app.js:1:1494667), :15:363)

dalcantara7 commented 2 years ago

Could you give me some context on your environment? OS, enabled plugins, etc.

avirosso commented 2 years ago

OS: Windows 10 Pro

Enables Plugins:

dalcantara7 commented 2 years ago

Could you try disabling all plugins and running AutoMOC? I just want to confirm another plugin isn't interfering with this it.

avirosso commented 2 years ago

I disabled all other plugins (exept AutoMOC) and restarted Obsidian.

The problem persists, nothing happens.

Developer Console:

VM148:111 Uncaught TypeError: Cannot read properties of undefined (reading 'children') at getLinkedMentions (eval at (app.js:1:1494667), :111:48) at runAutoMOC (eval at (app.js:1:1494667), :138:26) at HTMLDivElement.eval (eval at (app.js:1:1494667), :64:11)

One hint (maybe it helps): I don't use Wikilinks. All my links are markdown links ('shortest path if possible')

dalcantara7 commented 2 years ago

This is really weird. I use the fileManager.generateMarkdownLink() function which uses the user's preferred format for links.

I even changed my link type to markdown links to check and it still functions properly.

Might be worth trying on your end with Wikilinks enabled. Just to see if there's a bug in the API.

The only other idea I have right now would be for you to go into the main.ts in your plugins folder and add a console.log(key) inside the forEach part of the getLinkedMentions() function which is throwing the error to see if you have a specific file that is causing the problem. But I understand that's a bit tedious.

I'm also confused on what "children" its reading. None of my functions access a children property.

andrezgz commented 2 years ago

I'm having the exact same issue.

I'm also confused on what "children" its reading. None of my functions access a children property.

I've followed the console log error and it showed me the following code (check the line maked with THE ERROR IS POINTING HERE):

(function anonymous(require,module,exports){/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/

...

function getLinkedMentions() {
  const workspaceContainer = this.app.workspace.containerEl;
  const activePane = workspaceContainer.getElementsByClassName("workspace-leaf mod-active")[0];
  const backlinkPane = activePane.getElementsByClassName("backlink-pane")[0];
  const linkedMentionsContainer = backlinkPane.children[1];  // <-------- THE ERROR IS POINTING HERE
  const linkedMentions = linkedMentionsContainer.children[0].children;
  let linkNamesList = [];
  for (let i = 0; i < linkedMentions.length; i++) {
    let inner = linkedMentions[i].children;
    if (inner.length > 0) {
      let linkName = inner[0].innerText.slice(0, -1).replace(/(\r\n|\n|\r)/gm, "");
      linkNamesList.push(linkName);
    }
  }
  return linkNamesList;
}

As you proposed, I've added a console.log to this plugin main.ts:

function getLinkedMentions() {
  const workspaceContainer = this.app.workspace.containerEl;
  const activePane = workspaceContainer.getElementsByClassName("workspace-leaf mod-active")[0];
  const backlinkPane = activePane.getElementsByClassName("backlink-pane")[0];
  console.log(backlinkPane);
  ...

It shows undefined. That's because activePane.getElementsByClassName("backlink-pane") is an empty HTMLCollection

dalcantara7 commented 2 years ago

Oh I see. That code is the actual first ever submission I created. I worked with one of the plugin approvers to update the code and make it more robust (and less hacky) and I was under the impression the changes made it into PR, but I guess they did not. The original version of this plugin required the backlink pane to be active in order to find linked mentions, but the new version does not.

I have updated the main.js in the release to use the new version. Could you guys uninstall and reinstall the plugin and try again? I may ultimately need to submit a new release in order for the changes to make it in, but want to be sure.

avirosso commented 2 years ago

I uninstalled, installed again (Version is still 1.01), but now IT WORKS!

Thanks!

andrezgz commented 2 years ago

Great! Did the same thing as @avirosso and it's working :)

dalcantara7 commented 2 years ago

Awesome! Thanks for pointing out the error. Hope you guys file the plugin useful. Going to close this now.

avirosso commented 2 years ago

Thanks - and: Yes, this plugin is really usefull! (so far I've done this with a dataview script, showing me backlinks which are not linked from the current note ...)