2024年4月11日

VSCodeの統合ターミナルでPowerShellのコードページをUTF-8に変える

VSCodeの統合ターミナルのPowerShellで«»が文字化けした。

PS C:\Users\user\work\hello-lean-doc-gen4> lake build
info: ツォhello-lean-doc-gen4ツサ: no previous manifest, creating one from scratch
[0/6] Building HelloLeanDocGen4.Basic
[1/6] Compiling HelloLeanDocGen4.Basic
[1/6] Building HelloLeanDocGen4
[3/6] Compiling HelloLeanDocGen4
[3/6] Building Main
[5/6] Compiling Main
[6/6] Linking hello-lean-doc-gen4.exe
PS C:\Users\user\work\hello-lean-doc-gen4> 

VSCodeの設定でterminal.integrated.profiles.windowsPowerShellの項目を、PowerShellが-NoExit -Command chcp 65001の引数付きで実行されるようにすれば良い。

    "terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "PowerShell",
            "args": [
                "-NoExit",
                "-Command",
                "chcp",
                "65001"
            ],
            "icon": "terminal-powershell"
        },
PS C:\Users\user\work\hello-lean-doc-gen4> rm .\lake-manifest.json
PS C:\Users\user\work\hello-lean-doc-gen4> lake build
info: «hello-lean-doc-gen4»: no previous manifest, creating one from scratch
PS C:\Users\user\work\hello-lean-doc-gen4>

これだけだとタスクの実行でも引数付きになってしまうようで、(当サイトのソースリポジトリで)npm run devのタスクを実行したら以下のようなエラーになった。

パラメーターの書式が違います - -Command

Visual Studio Code公式ドキュメントによれば、タスク実行時のシェルにはterminal.integrated.automationProfile.windowsを設定すればそちらの設定が使われるとのこと。 以下のように引数を追加する前の設定を追加した。

    "terminal.integrated.automationProfile.windows": {
        "path": "pwsh.exe",
        "icon": "terminal-powershell"
    }

こちらの設定にはsourceというフィールドはないようだった。

参考文献

  1. VSCodeの統合ターミナルでPowerShellのコードページをUTF-8に変える
    1. 参考文献