asciidoctor / asciidoctor-intellij-plugin

AsciiDoc plugin for products on the IntelliJ platform (IDEA, RubyMine, etc)
https://intellij-asciidoc-plugin.ahus1.de/
Apache License 2.0
354 stars 145 forks source link

/opt/local/bin/dot not found on homebrew-installed graphviz on Apple Silicon #972

Open sdavids opened 2 years ago

sdavids commented 2 years ago

Observed vs. expected behavior

Observed

Screen Shot 2022-01-24 at 12 28 03

Expected

Screen Shot 2022-01-24 at 12 35 08

Suggestion

Maybe introduce an environment variable GRAPHVIZ_DOT and/or let the user select it via UI like the PlantUML integration does:

Screen Shot 2022-01-24 at 12 39 50

Steps to reproduce

= dot test

[plantuml]

----
@startuml
!include https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Context.puml
!include https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Container.puml

Container(t, "Test Container")
Person(p, "Test Person")
@enduml
----

Environment

Plugin Version: 0.36.13

WebStorm Details: 2021.3.1

% sw_vers
ProductName:    macOS
ProductVersion: 12.1
BuildVersion:   21C52
% uname -m
arm64
% brew --prefix
/opt/homebrew
% which dot
/opt/homebrew/bin/dot

https://docs.brew.sh/FAQ#why-is-the-default-installation-prefix-opthomebrew-on-apple-silicon

Screen Shot 2022-01-24 at 12 46 38
ahus1 commented 2 years ago

Thanks for reporting this and providing all the details.

There is a documentation with different approaches in the docs of the plugin, although it could be better explained (my fault, I'm sorry).

Please use the following steps to give it a try:

I would have hoped that when which dot works, the plugin in IntelliJ would find it as well. This must be my lack of macOS experience.

Please let me know if this works for you. If it does, I'll update the docs accordingly.

sdavids commented 2 years ago

I tried setting graphvizdot=/opt/homebrew/bin/dot and it worked.

sdavids commented 2 years ago

I have two suggestions:

@@ Attributes @@

As the UI is right now, I thought the attributes are for Kroki.

Yes, in that case the Attributes block would be indented, but I still think as it is right now it it is a little misleading.

I suggest adding a spacer or horizontal line like the PlantUML plugin does in the screenshot above.

@@ graphvizdot Attribute @@

I would have hoped that when which dot works, the plugin in IntelliJ would find it as well. This must be my lack of macOS experience.

Mac applications launched through Launch Services use a different environment that the ones launched via command line.

https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html#//apple_ref/doc/uid/20001431-106825

Info.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>LSEnvironment</key>
    <dict>
      <key>MY_KEY</key>
      <string>MY_VALUE</string>
    </dict>
  </dict>
</plist>

That is why neither PATH norGRAPHVIZ_DOT is picked up.

One would have to modify the file supplied by JetBrains /Applications/JetBrains\ Toolbox/IntelliJ\ IDEA\ Ultimate.app/Content/Info.plist -- and each update would overwrite that file.

Maybe after downloading the dependencies for diagrams you could check whether dot can be found?

If no, you could present a dialog which would then in turn add the graphvizdot attribute -- or just a popup with a link to your FAQ?

ahus1 commented 2 years ago

The next version of the plugin will include horizontal rules in the settings, thank you for the suggestion.

image

ahus1 commented 2 years ago

I am also marking this issue as "help wanted" to add more documentation about how to use environment variables on macOS. As I am not using macOS, I find it difficult to write those myself.

The docs are in this project in the folder /doc/user-guide: They are plain AsciiDoc, in the directory layout of Antora. I'm happy answer any questions that arise.

LloydFinch commented 1 year ago

I copy /opt/homebrew/bin/dot to /opt/local/bin/dot, then anything is well

daimingzhong commented 1 year ago

Not sure if it is related, but to make the Plantuml plugin work in M1 mac, need to specify the dot path explicitly, which is /opt/homebrew/bin/dot.

nonnenmacher commented 5 months ago

suggestion from @LloydFinch is working and here it is why.

Problem is that from JetBrain tools (Rubymine in my case), the launch profile doesn't include the proper '$PATH` you do expect.

you could see this finding the environment from wich the IDE is launched.

launchctl list | grep jet
78249   0   application.com.jetbrains.rubymine.1391464.10228395

launchctl print gui/502/application.com.jetbrains.rubymine.1391464.10228395

gui/502/application.com.jetbrains.rubymine.1391464.10228395 = {
    active count = 5
    path = (submitted by runningboardd.589)
    type = Submitted
    managed_by = com.apple.runningboard
    state = running
    bundle id = com.jetbrains.rubymine

    program = /Applications/RubyMine.app/Contents/MacOS/rubymine
    arguments = {
        /Applications/RubyMine.app/Contents/MacOS/rubymine
    }

    working directory = /
        ...
    default environment = {
        PATH => /usr/bin:/bin:/usr/sbin:/sbin
    }

    environment = {
        ...
        __CFBundleIdentifier => com.jetbrains.rubymine
        PATH => /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
        ...
    }
....

So as you see the best available option is to place the 'dot' executable in usr/local/bin

Didn't tried to make of from the symlink like $(brew prefix graphviz)/bin/dot but directly to its full path.

I added this to my .zshrc (or your bash or .profile)

export GRAPHVIZ_DOT="/opt/homebrew/Cellar/graphviz/10.0.1/bin/dot"

So in usr/local/bin

added a symbolink link

sudo ln -s $GRAPHVIZ_DOT dot

# to get this

ls -la
total 2248
...
lrwxr-xr-x   1 root  wheel       44 Apr 16 12:07 dot -> /opt/homebrew/Cellar/graphviz/10.0.1/bin/dot
...

And from that everything went smoothly

I spend an entire day, checking that the Ruby gem Asciidoctor-diagrams works well with graphviz bin in path and proper venv to install python diagrams (and graphviz) packages.

However JetBrain IDEs don't use the home PATH env when launching 'ruby' then 'python' and the latter doesn't find the 'dot' executable inside the package line in

.../lib/python/site-packages/graphviz/backend/dot_command.py lines 12-14

DOT_BINARY = pathlib.Path('dot')

the dot executable is then launched from the JetBrain process as a subprocess.run(cmd) which call the OS to launch a sub-process (and resolve it from 'usr/local/bin` in your case.

Do not know if the new JetBrains IDE setting in Appearance/Path Variables could help here (sound promising)

To check that the dot executable is found you can try to execute (from your IDE) the following quick test (that simulate the python's Graphviz package and code in the 'backend' folder.

import os
import pathlib
import shutil
import subprocess

paths = os.environ["PATH"]

DOT_BINARY = pathlib.Path('dot')

DOT_BINARY2 = pathlib.Path(shutil.which("dot"))

cmd = [DOT_BINARY]

cmd.append('--version')

try:
    proc = subprocess.run(cmd)
except OSError as e:
    if e.errno == errno.ENOENT:
        raise ExecutableNotFound(cmd) from e
    raise

proc.check_returncode()

print('found {} dot at {}'.format(cmd[0].exists(),cmd[0].resolve()))

you could try both DOTFILE and DOTFILE2 to convince yourself, that this DOT_BINARY = pathlib.Path('dot') doesn't really matter, only the OS launch part will find the executable.

BTW I'm not a python guy, I don't know why this shutil.wich isn't used from this pathlib.Path('dot') that doesn't work at all !

sdavids commented 5 months ago

A different solution …


Prepend Homebrew's /opt/homebrew/bin:

$ sudo launchctl config user path '/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'

While you are at it I suggest adding ~/.local/bin as well:

$ sudo launchctl config user path '~/.local/bin:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'

Reboot:

$ sudo reboot

After reboot:

$ launchctl getenv PATH

UI and Spotlight started apps will have ~/.local/bin and /opt/homebrew/bin in their path now.


Note: Even though it says "user" it is system-wide.


More information

How to revert launchctl PATH to defaults?

nonnenmacher commented 3 months ago

sound nice !

I'll give it a try