Problem with command line in gradle
已回答
I have a very odd problem in gradle when running inside Intellij on a Macbook
I'm executing a Gradle task as an exec trying to run yarn in the following way (simplified)
tasks.register('yarn', Exec) {
commandLine 'yarn'
}
This runs from a zsh command line, but fails when run from Intellij, with the following error:
Caused by: java.io.IOException: Cannot run program "yarn" (in directory "/Users/..."): error=2, No such file or directory
at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:25)
... 7 more
Caused by: java.io.IOException: error=2, No such file or directory
... 8 more
I thought this must be a PATH problem, but yarn is definitely on the path and a similar execute of:
tasks.register('whichyarn', Exec) {
commandLine 'which', 'yarn'
}
Executes successfully returning the expected location of yarn as ‘/opt/homebrew/bin/yarn
’
Does anyone have any suggestions as to what could be going wrong here, or any additional steps to diagnose the problem?
请先登录再写评论。
Some more data points:
This also happens trying to run
commandLine ‘node’, …
it works from a terminal, but fails when run inside gradlew, but node (and yarn) are clearly on the PATH in gradle in Intellij gradle environment.
Also, if I execute the exact path to node or yarn (for example) it works, so this succeeds.
GUI apps and terminal apps normally have a different set of environment variables on macOS(it's an Apple system limitation). If you start IntelliJ IDEA from the terminal per https://intellij-support.jetbrains.com/hc/en-us/articles/360011901879-How-to-start-IDE-from-the-command-line, it should have the same environment in its built-in Terminal.
See also https://stackoverflow.com/a/26586170/104891 for the ways to set env variables on macOS so that they are the same for all the apps.
Try running
/Applications/IntelliJ\ IDEA.app/Contents/MacOS/idea
instead.idea.sh
is not designed for Mac and will not work without some manual changes.Another option is to create the command line launcher: Tools | Create Command-line Launcher.
If you are using Toolbox, it provides a way to create the command launcher automatically.
see also:
https://stackoverflow.com/questions/7836492/command-line-intellij-on-mac-os-x
https://stackoverflow.com/questions/25385934/setting-environment-variables-via-launchd-conf-no-longer-works-in-os-x-yosemite/26586170#26586170
Jacky Liu, thanks for the reply it's definitely worth looking into.
What I don't understand if it's a simple matter of Intellij not having the correct environment is why:
commandLine: ‘which’, ‘yarn’
returns the correct yarn, orprintln System.getEnv('PATH')
returns a path with both node and yarn on the path (it also seems to be the exact path for my user profile)This seems to be a problem with Intellij not passing the path to the executor
Hello, this is not a bug but a macOS GUI app limitation. Please check the instructions at my post https://intellij-support.jetbrains.com/hc/en-us/community/posts/23773523371154/comments/23917952543762
Double click this app via the finder or using Terminal will fix the issue: /Applications/IntelliJ\ IDEA.app/Contents/MacOS/idea
Jacky Liu your suggestion worked but there is one other quirk…
When using Java 17 as project SDK, the error doesn't seem to occur regardless of whether opening via terminal or via GUI
When using Java 21 however, the error appears again. So it doesn't always appear to be MacOS limitation but rather JDK dependent as well
Ryanhealy1992 This might be another quirk of macOS: depending where each JDK is located and/or how they were deployed (direct download, sdkman, IDEA, etc.).