Error msgs from Xcode vs Command Line

I am writing an app that creates Swift code. Part of what it does is pass that code to Xcode using a "shell" function.
The problem is a given line of Swift gives the same error code if pasted into Xcode as it does from the command line (shell), with the exception that Xcode tells me MORE

for example :

`Cannot convert value of type 'Int' to 'Double'
Try replacing 'X+1' with 'Double(x+1)'

Both Xcode and shell give me the first line, but only Xcode itself gives me the 2nd... My app needs that 2nd line to possibly "patch" the code it created...

I am calling the CLI using

let s = shell("cd ~/desktop;swift \(test) --verbose")

where test is the filename, and shell is defined as below

func shell(_ command: String) -> String {
    var output : String = ""
    let task   = Process()
    let pipe   = Pipe()
    task.executableURL  = URL(fileURLWithPath: "/bin/bash")
    task.arguments      = ["-c", command]
    task.standardOutput = pipe
    task.standardError  = pipe
    do {
        let handle = pipe.fileHandleForReading
        let data = handle.readDataToEndOfFile()
        output = String(data: data, encoding: .utf8) ?? "no result"
    } catch {
       output = "'command' failed."
    return output 

Darn... it seems the CLI does return it, but not expanded to make it noticeable

syntax_check.swift:1862:20: error: cannot convert value of type 'Int' to expected argument type 'Double'
s = format ( x + y + 1 , #"000000000000000000000000"# ) 
             Double(  )

seems the "Double( )" is expanded by the Xcode editor when it displays the messages

Seems like you're going to have to do the "formatting" you want just like does. It's taking the info from the compiler and re-formatting it as a Fix-it dialog box. Since, xcodebuild, etc., are all proprietary to Apple, you'll probably have to reverse-engineer to meet your needs.

There are two ways of getting the compiler fix-its in a structured format that I know of. The first is to compile the code using -serialize-diagnostics and an output file map which specifies where the compiler should create .dia serialized diagnostics files for each compiler input. swift-tools-support-core and LLVM both have utilities for reading these files, but it's still kind of a pain since they use a binary format.

The easier, but less well-supported way is to compile using -fixit-all -Xfrontend -emit-fixits-path -Xfrontend /output-path/fixits.json. Frontend options passed using -Xfrontend aren't officially supported and could break in the future, but this will give you a JSON representation of each text replacement that's much easier to parse.

Thanks I'll look into those options

swift syntax_check.swift -fixit-all -Xfrontend -emit-fixits-path -Xfrontend /output-path/fixits.json

this did nothing "extra"

nor did

swift syntax_check.swift -serialize-diagnostics

both gave the same as if I just used

swift syntax_check.swift 
Terms of Service

Privacy Policy

Cookie Policy