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

Typescript Not Working #87

Open
aidan-gibson opened this issue Oct 3, 2022 · 26 comments
Open

Typescript Not Working #87

aidan-gibson opened this issue Oct 3, 2022 · 26 comments
Labels
bug Something isn't working

Comments

@aidan-gibson
Copy link

With ts-node path set to /opt/homebrew/bin/ts-node, I get this error:
Screen Shot 2022-10-02 at 18 00 58

With ts-node path set to /opt/homebrew/bin/npx ts-node I get this error:
Screen Shot 2022-10-02 at 18 02 47
Also tried setting ts-node as an argument

Also tried just ts-node, and settings ts-node as an argument under npx.

Everything works fine from the terminal. Any ideas?

Thanks for a brilliant plugin, I absolutely love this!

Running macOS

@twibiral twibiral added the bug Something isn't working label Oct 5, 2022
@chlohal
Copy link
Collaborator

chlohal commented Oct 13, 2022

Hi! Sorry for the late response! I can help troubleshoot. When you say that everything works fine from the terminal, what are you entering into the terminal (i.e. are you entering ts-node, or the full path)?

@aidan-gibson
Copy link
Author

Thanks for looking into this!

Either way works, ts-node is on path

image

@chlohal
Copy link
Collaborator

chlohal commented Oct 14, 2022

Thanks!

I think that the issue is that your node binary is available on the path to your shell, but not available to Obsidian. Could you try running these commands in a bash block, through the plugin? Like:

```bash
which node
which ts-node
which npx
```

How did you install Node? If homebrew, try running brew link --overwrite node.

@aidan-gibson
Copy link
Author

That does seem like the issue! Not sure how to remedy though.
image

image

@aidan-gibson
Copy link
Author

Also tried relinking via brew unlink node && brew link node, same outcome

@chlohal
Copy link
Collaborator

chlohal commented Oct 15, 2022

Alright. I think that the easiest solution would be to create symlinks from /usr/local/bin/foo to /opt/homebrew/bin/foo for each foo of npx, ts-node, node, etc.

@aidan-gibson
Copy link
Author

image
Still no dice 😬

If I set ts-node path to usr/local/bin/npx ts-node or usr/local/bin/ts-node in settings I get this error
image

But in terminal, it works
image
image

(Tried rebooting OS & Obsidian etc)

Sorry to burden you with this, once again I super appreciate the extension and the work you do! Even if we can't figure this one out, thanks again.

@chlohal
Copy link
Collaborator

chlohal commented Oct 15, 2022

Oh dear :( That is unfortunate, sorry.

To tell the truth, it doesn't have to be /usr/local/bin; just somewhere on your PATH. If you re-run the which node, which ts-node, etc. blocks, does it show the correct filepath? If it's still foo not found, could you run echo "$PATH" in a bash block and send that? (of course, feel free to censor any sensitive sections)

Thank you! I'm happy to help out :) Even if we can't figure it out, thank you for working with me to try and fix the problem. If it ends up being a problem that we can fix from the plugin, this has been super helpful for future users! Thanks :)

@aidan-gibson
Copy link
Author

(From Terminal)
image
(From Obsidian)
image

@chlohal
Copy link
Collaborator

chlohal commented Oct 16, 2022

Oh! I'm surprised that cargo is on there; everything else seems very... bare, in the PATH that Obsidian has.

Do you know if your zshrc is being ran?

What happens if you run whoami and pwd in a bash block?

@aidan-gibson
Copy link
Author

Messed around and realized .zshenv is being sourced but .zshrc is not (for run-shell blocks). (My .zshenv has pretty much nothing and my .zshrc has a ton).

Added export PATH=/opt/homebrew/bin:$PATH to .zshenv and voila
image

However, run-ts blocks are still failing in the same manner:

With full file path specified in settings:
image

With just ts-node or npx ts-node as file path:
image

@aidan-gibson
Copy link
Author

Additionally tested
image

image

@aidan-gibson
Copy link
Author

image

@twibiral
Copy link
Owner

There already were a few issues by people using the combination MaxOS + Homebrew. We don't have a general solution yet but we there seems to be a common problem.

@devpixde
Copy link

Same problem here. Have a homebrew installed nvm. Solution would be great!

@chlohal
Copy link
Collaborator

chlohal commented Nov 1, 2022

As of now, I'm not sure what to do with this issue. If you're willing to help debug, that would be great; I'm not that familiar with Macs, so it's really good to have someone to test. What I'd advise you do:

  • Try to follow the steps in this issue and see if they help
  • If not, retry the symlinking step, but link from /usr/bin instead of from /usr/local/bin-- seems like PATH doesn't contain /usr/local/bin on MacOS?

@devpixde
Copy link

devpixde commented Nov 3, 2022

Well, node and ts-node versions are controlled by nvm, so it might not be a good idea to set symlinks.

But i have another observation:

If i set brew and nvm init scripts in .zshenv (which for some unknow reason is loaded by obsidians shell, while the other zsh configs are not loaded) this will work within obsidian and give proper values:

   ``shell
 node -v
 which node

 ts-node -v
 which ts-node
  ``

Output:

18.12.0
/Users/xxx/.nvm/versions/node/v18.12.0/bin/node
v10.9.1
/Users/xxx/.nvm/versions/node/v18.12.0/bin/ts-node

BUT: It does not help with the problem, calling some typescript code in obsidian nevertheless results in

"env: node: No such file or directory"

@chlohal
Copy link
Collaborator

chlohal commented Nov 3, 2022

ts-node calls the same version of node which was used to run it. Now that which node works, could you try node for the path and /path/to/ts-node for the first argument? e.g:

image

@devpixde
Copy link

devpixde commented Nov 4, 2022

thanks for your support, but sorry, still not working.

@isr413
Copy link

isr413 commented Nov 5, 2022

If you need a quick fix, copy and paste the output of which node into ts-node path followed by the output of which ts-node. They both need to be in the ts-node path field and the arguments field should be left blank.

The problem is that the shell environment executing the provided commands isn't loading the local .zshrc or .bashrc. It also only includes bin, /usr/bin, and /usr/sbin. These folders are protected by MacOS and, by default, should not be written to even if it's just for the purpose of adding symlinks.

It would be much better if we could ensure that /usr/local/bin gets added to the shell environment before commands are executed.

@twibiral
Copy link
Owner

twibiral commented Nov 5, 2022

The problem is that the shell environment executing the provided commands isn't loading the local .zshrc or .bashrc. It also only includes bin, /usr/bin, and /usr/sbin. These folders are protected by MacOS and, by default, should not be written to even if it's just for the purpose of adding symlinks.

@isr413 Do you know how we can make sure that the .*shrc files are loaded?

@devpixde
Copy link

devpixde commented Nov 5, 2022

If you need a quick fix, copy and paste the output of which node into ts-node path followed by the output of which ts-node. They both need to be in the ts-node path field and the arguments field should be left blank.

You made my day! - For me a quick fix is fine.
Btw.: I wasn't aware that the path field sets $PATH, i always thought it was the path the plugin calls directly, so i did not try it this way.

Thanks!

@chlohal
Copy link
Collaborator

chlohal commented Nov 7, 2022

The path field doesn't set $PATH. I believe that this works because it directly calls npx ts-node, without any indirection or dependence on the $PATH. This is a good quick-fix; thanks, @isr413!

@aidan-gibson pinging for a fix for your issue! :)

@skdamico
Copy link

skdamico commented Nov 8, 2022

The problem is that the shell environment executing the provided commands isn't loading the local .zshrc or .bashrc. It also only includes bin, /usr/bin, and /usr/sbin. These folders are protected by MacOS and, by default, should not be written to even if it's just for the purpose of adding symlinks.

@isr413 Do you know how we can make sure that the .*shrc files are loaded?

https://unix.stackexchange.com/questions/71253/what-should-shouldnt-go-in-zshenv-zshrc-zlogin-zprofile-zlogout

Suggest everyone to read the top two answers to this StackExchange. But TL;DR:
.zshrc is for interactive shells and is read once per shell creation
.zshenv is for applications, etc and is read every time.
export your $PATH, $EDITOR, other application level vars here.

@isr413
Copy link

isr413 commented Nov 8, 2022

@skdamico I haven't looked through all of the code yet to see how the ts executor is being formed, but I assume that somewhere along the road, a child process is forked off that executes the provided commands. If that's correct, then the child process only has access to the environmental variables within the scope of the Obsidian process, which isn't much.

You would need to do one of two things. Either, you would need to source select rc, env, or profile files before executing the provided command or add additional directories to the path yourself. I would recommend the latter.

You would need an input field to accept additional directories, such as what the Shell Commands plugin uses https://publish.obsidian.md/shellcommands/Environments/Additions+to+the+PATH+environment+variable

You would need to concatenate those with the PATH variable and pass that into the child process, the child process would need the additional argument { env: dirs.join(":") + ":" + process.env.PATH }. You would of course need to ensure that some dir is provided, otherwise, you wouldn't pass this argument at all.

This would at least allow users to specify which directories they want included in their PATH. For most UNIX users, adding /usr/local/bin would be enough but others may also want to include their homebrew dir, etc.

@zieka
Copy link

zieka commented Apr 24, 2024

I ran into this same issue and my workaround was to just forgo using ts-node completely and instead for the ts-node path put my path to the bun executable. Works and is fast.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants