Skip to content
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

User-defined functions break if tools installed with Homebrew are used #512

Closed
christyray opened this issue Jan 14, 2022 · 8 comments
Closed
Assignees
Labels

Comments

@christyray
Copy link

@christyray christyray commented Jan 14, 2022

Plugin informations (please complete the following information):

  • OS: macOS Monterey
  • Templater version: 1.9.11
  • Obsidian version: 0.13.19
  • Templater settings: Template folder location: "templates/", Timeout: "5", Trigger on New File Creation, User System Command Functions enabled

Describe the bug
When I create a user function that relies on a tool installed with Homebrew, I get the error command not found. If I specify the path to the alias created by Homebrew (e.g., /user/local/bin/toolname), I get the error env: node: No such file or directory.

It appears to be specific to tools installed with Homebrew. I have other user functions that only use standard shell tools, and they work fine, but I've tried multiple Homebrew tools, and they all give the same error.

Two of the functions that I tried were:

  • curl -s https://api.weather.gov/gridpoints/LWX/109,90/forecast | /usr/local/bin/fx '.properties.periods[0].detailedForecast' - fx is a command-line tool I installed with Homebrew
  • /usr/local/bin/tldr curl- tldr is another command-line tool I installed with Homebrew (the expected output of the command is a plain-text summary of the curl documentation)

These both gave the env: node: No such file or directory error.

Expected behavior
I expected the shell command to work inside Templater like it does when I execute it at the command line.

Additional context
It looks like I'm having the same issue that was described in this forum post, if that helps give more information!

@shabegom
Copy link
Collaborator

@shabegom shabegom commented Jan 14, 2022

@christyray Please fill out the bug report template.

@christyray
Copy link
Author

@christyray christyray commented Jan 14, 2022

@shabegom - I'm sorry for the confusion, but I thought I did? I updated it to add the rest of my settings and two of the functions I tried - is there anything else that would be helpful for me to add?

@shabegom
Copy link
Collaborator

@shabegom shabegom commented Jan 15, 2022

@christyray hrm this is all I see right now...

image

I'm not sure what's happening, maybe repost as a comment?

@christyray
Copy link
Author

@christyray christyray commented Jan 15, 2022

@shabegom - that is odd! I pasted the text below, hopefully it works this time!

Plugin information:

  • OS: macOS Monterey
  • Templater version: 1.9.11
  • Obsidian version: 0.13.19
  • Templater settings: Template folder location: "templates/", Timeout: "5", Trigger on New File Creation, User System Command Functions enabled

Describe the bug
When I create a user function that relies on a tool installed with Homebrew, I get the error command not found. If I specify the path to the alias created by Homebrew (e.g., /user/local/bin/toolname), I get the error env: node: No such file or directory.

It appears to be specific to tools installed with Homebrew. I have other user functions that only use standard shell tools, and they work fine, but I've tried multiple Homebrew tools, and they all give the same error.

Two of the functions that I tried were:

  • curl -s https://api.weather.gov/gridpoints/LWX/109,90/forecast | /usr/local/bin/fx '.properties.periods[0].detailedForecast' - fx is a command-line tool I installed with Homebrew
  • /usr/local/bin/tldr curl- tldr is another command-line tool I installed with Homebrew (the expected output of the command is a plain-text summary of the curl documentation)

These both gave the env: node: No such file or directory error.

Expected behavior
I expected the shell command to work inside Templater like it does when I execute it at the command line.

Additional context
It looks like I'm having the same issue that was described in this forum post, if that helps give more information!

@christyray
Copy link
Author

@christyray christyray commented Jan 20, 2022

Update: based on this issue with the Obsidian Git plugin, it looks like the problem is that the obsidian process doesn't include /usr/local/bin in its PATH, and that's where Homebrew places symlinks to any packages it installs.

Based on the suggestions in that issue, I rewrote my user-defined function as a bash script with PATH=$PATH:/usr/local/bin at the top, and then created a function in Templater to run that script. This worked to fix my issue without needing to reinstall the packages separate from Homebrew.

I'm not sure if this is fixable within the plugin since it seems to be a problem with Obsidian itself (although I could be totally off-base), but I'm not bothered by just writing separate scripts instead of directly pasting the code in Templater.

@shabegom
Copy link
Collaborator

@shabegom shabegom commented Jan 21, 2022

Yeah I noticed that the $PATH being used when triggering templater is different from the $PATH defined in a .bashrc.

Templater is using Node's child_process to run the shell script. If the issue is with templater that would be where.

@christyray
Copy link
Author

@christyray christyray commented Jan 21, 2022

It looks like child_process uses the path set in process.env.PATH if one isn't explicitly provided in options.env.PATH. It looks like process.env.PATH is /usr/bin:/bin:/usr/sbin:/sbin in Obsidian on my computer. I couldn't determine how that path was set, if it was specified by Obsidian or the default for Electron.

If it was something you wanted to add in Templater, you could probably add a setting for the user to specify any additional search paths and then pass that to options.env.PATH, but I think that's more of a feature request (and I don't know how many people would actually need it).

I made a small plugin for myself to add /usr/local/bin to the end of process.env.PATH when Obsidian is loaded, and that works to make the Homebrew-installed tools available inside my Templater functions, so it definitely was just a problem with the $PATH.

Since this isn't actually a bug with Templater, I can close this unless there's anything else that would be helpful to add!

@shabegom
Copy link
Collaborator

@shabegom shabegom commented Jan 24, 2022

Great sleuthing. This problem comes up more than I would expect so we should either add the feature to Templater, fix it in obsidian, or point towards another solution. I'll close this, but if you choose to push your plugin to github let me know so I can link it as a workaround.

@shabegom shabegom closed this Jan 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants