2016-12-26 36 views
2

我試圖在我的Swift 3.0.2(與3.0.1一起發生)服務器應用程序在Heroku上運行它(它在Ubuntu上運行它)中嘗試調用String(contentsOfFile:encoding:),但函數中止程序。顯示在heroku logs以下錯誤:String(contentsOfFile:encoding :)以退出狀態中止139

2016-12-26T01:59:01.304535+00:00 heroku[web.1]: Process exited with status 139 

有比其它任何錯誤消息。我試圖讀取的文件與應用程序的源文件位於同一目錄樹中。下面是實際的代碼的功能上等同的收縮(在main.swift頂層):

let sqlDirectory = URL(fileURLWithPath: #file).deletingLastPathComponent().path + "/Models/SQL/" 

do { 
    print("reading") 
    fflush(stdout) 
    let a = try String(contentsOfFile: sqlDirectory + "createTables.sql", encoding: .utf8) 
    print("done") 
    fflush(stdout) 
} catch { 
    print("error") 
    fflush(stdout) 
} 

sqlDirectory的運行時間值是/tmp/build_514c5587b46ef976e60d4f039b4d048d/Sources/App/Models/SQL/(散列是在每個構建不同)。它在打印reading後崩潰。 doneerror從不打印。 (標準輸出刷新僅用於調試,以確保所有內容都寫入標準輸出。刪除它們不會影響崩潰。)

這是怎麼回事?我該如何解決?

我也報告過這個錯誤here,因爲它似乎這可能是一個錯誤。

+0

狀態139是'段錯誤'(SIGSEGV)。 – user28434

+0

'String(contentsOfFile:encoding:)'拋出,你在do/try/catch上下文中調用它嗎?你如何指定文件名和路徑?你傳遞給該呼叫的是什麼編碼?我對Heroku並不熟悉,但源文件的位置可能不是該進程的當前工作目錄。 –

+0

@MartinR修改了問題的細節。 – emlai

回答

0

我檢查了FileManager.default.fileExists(atPath: sqlDirectory + "createTables.sql")的結果,它的結果是false

因此,這就解釋了爲什麼String(contentsOfFile:encoding:)崩潰。雖然它應該拋出一個錯誤而不是崩潰。這是潛在的錯誤:https://bugs.swift.org/browse/SR-1547,這似乎是由這個錯誤引起的:https://bugs.swift.org/browse/SR-585

我仍然需要找出Heroku中不存在路徑的原因。也許Heroku在構建之後移動或刪除所有源文件?