[Sublime Text] Perforce 外掛無法正常運作,與 iTerm2 shell integration 衝突

[Sublime Text] Perforce 外掛無法正常運作,與 iTerm2 shell integration 衝突

之前寫過一篇 在 Sublime Text 裡使用 Perforce Plugin

今天想照著在新 Mac 上也裝好 Perforce plugin,

可是裝好之後,卻發現一直不能成功運作,沒辦法連上 perforce….

在 Sublime Text > View > Show Console 裡看一下,結果有如下的錯誤訊息:

Unable to open /Users/testuser/Library/Application Support/Sublime Text 3/Packages/User/Perforce.sublime-settings
error: Perforce Plugin: p4 info didn't supply a valid clientspec, launching p4 client

 

怪的是直接在 Terminal 裡執行 p4 info,又很正常…

實在找不出原因,只好去翻了這個外掛的原始碼來碰碰運氣,

結果 p4 info didn’t supply a valid clientspec 的錯誤訊息是從 GetClientRoot() 出來的:

def GetClientRoot(in_dir):
    # check if the file is in the depot
    command = ConstructCommand('p4 info')
    p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=global_folder, shell=True)
    result, err = p.communicate()
    result = result.decode("utf-8")
    err = err.decode("utf-8")

    if(err):
        WarnUser(err.strip())
        return -1

    # locate the line containing "Client root: " and extract the following path
    startindex = result.find("Client root: ")
    if(startindex == -1):
        # sometimes the clientspec is not displayed
        sublime.error_message("Perforce Plugin: p4 info didn't supply a valid clientspec, launching p4 client");

 

從程式來看,p4 info 這個指令的結果裡面,

找不到 “Client root:” 的字串,才會印出那個錯誤訊息 。

但我直接執行 p4 info 又會正常的吐出 Client root 的資訊,那是哪邊的問題呢?

 

瞄了一下 ConstructCommand() 這個函式,結果看到了這一段:

def ConstructCommand(in_command):
    # ...
    elif(sublime.platform() == "osx"):
        command = '. ~/.bash_profile && {0}'.format(p4Path)

 

也就是說在 Mac 環境下,會執行的是 “. ~/.bash_profile && p4 info”,

直接執行看看,果真沒有吐出任何資訊…

看了一下 ~/.bash_profile,最後發現是 iTerm2 的 shell integration,

這個功能會在 ~/.bash_profile 裡加入下面這一行:

test -e "${HOME}/.iterm2_shell_integration.bash" && source "${HOME}/.iterm2_shell_integration.bash"

 

如果這一行存在時,”. ~/.bash_profile && echo xxx” 就不會印出任何東西,

表示 .iterm2_shell_integration.bash 可能最後吐出了一個非零的 exit code…

 

為了修正這個問題,最簡單的方法是把這一行直接刪掉,

因為我也沒有在用 iTerm2 的 shell integration 的功能…

移除掉之後,Perforce plugin 也就回復正常了~

 

心得:看別人的原始碼雖然很累,不過有時候真的可以解決問題呀~

 

(本頁面已被瀏覽過 284 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料