Command line "Launch path unavailable"

I recently learned that I can create Swift command line scripts.

I decided to see if I could build my Xamarin project using it.

Unfortunately, I am getting the following error and I do not know how to fix it.

*** Application termination due to an uncaught exception "NSInvalidArgumentException", reason: "Launch path unavailable"

Here is my script:

#!/usr/bin/env swift import Foundation print("Building Script") let fileManager = NSFileManager.defaultManager() let path = fileManager.currentDirectoryPath func shell(launchPath: String, arguments: [String] = []) -> NSString? { let task = NSTask() task.launchPath = launchPath task.arguments = arguments let pipe = NSPipe() task.standardOutput = pipe task.launch() let data = pipe.fileHandleForReading.readDataToEndOfFile() let output = NSString(data: data, encoding: NSUTF8StringEncoding) return output } if let output = shell("/Applications/Xamarin\\ Studio.app/Contents/MacOS/mdtool", arguments: ["-v build", "\"--configuration:Beta|iPhone\"", "MyApp.iOS.sln"]) { print(output) } 

Any thoughts?

+9
source share
2 answers

I think the problem is that you really want to execute the shell and execute its mdtool, and not directly execute mdtool

Try passing "/ bin / bash" as the startup path, and then include the path to mdtool as part of the argument string.

+8
source

I recently encountered a similar problem, but my solution is different.

I fix the problem by changing the script resolution mode. In particular, chmod 777 xxx . The key must give permission to execute.

Let them conduct a controlled experiment to verify this:

  1. prepare a script with the path /tmp/a.sh and resolution 666

    Contents /tmp/a.sh :

 #!/bin/sh echo aaa 
  1. prepare the swift script to run the script:
 import Foundation let fileManager = FileManager.default let path = fileManager.currentDirectoryPath func shell(launchPath: String, arguments: [String] = []) -> NSString? { let task = Process() task.launchPath = launchPath task.arguments = arguments let pipe = Pipe() task.standardOutput = pipe task.launch() let data = pipe.fileHandleForReading.readDataToEndOfFile() let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue) return output } if let output = shell(launchPath: "/tmp/a.sh", arguments: []) { print(output) } 

Output:

 ... *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible' 
  1. change the permission of /tmp/a.sh to 777 (giving permission to execute), and then re-run the swift script:
 aaa 
0
source

Source: https://habr.com/ru/post/1247485/


All Articles