Skw*_*ggs 5 command-line-interface swift
I'm building a CLI tool as a Swift Package. Part of the process requires me to invoke some processes (such as gsutil). When I try to invoke those, I always end up with
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible'
Some further analysis revealed that regular commands suchs as pwd or which work just fine. As pictured below, if I try to echo $PATH, I get an incomplete result.
The value of $PATH while running the project directly
/Applications/Xcode-beta.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin\n
is completely different from my actual $PATH variable:
echo $PATH
/Users/skwiggs/google-cloud-sdk/bin:/Users/skwiggs/.npm-global/bin:/Users/skwiggs/.rbenv/bin:/Users/skwiggs/.rbenv/shims:/Users/skwiggs/google-cloud-sdk/bin:/Users/skwiggs/.npm-global/bin:/Users/skwiggs/.rbenv/bin:/Users/skwiggs/.rbenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands
Run Code Online (Sandbox Code Playgroud)
How can I ensure that my env variables are correctly set while debugging my CLI tool?
直接从 shell 运行该工具将向其传递预期$PATH值,因此此问题仅在调试时发生。
如果您想使用正确的$PATH值调试程序,您可以在活动方案中添加环境变量。该变量仅在调试时使用,因此可以安全地覆盖它(从 shell 调用产品时它将被忽略)。
echo $PATH然后,打开XCode
方案->编辑方案->运行->环境变量->添加。
名称 PATH 值 <paste the value from terminal>
构建并运行,您现在可以像从 shell 调用项目一样进行调试。
(请记住,您必须对需要此行为的每个新项目执行此操作,并且对您的意愿的任何更改都$PATH必须对您的项目进行改造)