2

I have problems with Swedish national characters when using Rust in Visual Studio Code in Windows 11. It can be shown with the following program:

fn main() {
let abc = " ååå
ööö
äää";
println!("<---{}--->", abc);
}

When the program is run from the command line using cargo run, the output is as follows:

<--- ååå
     ööö
     äää--->

Strangely, spaces are added at the beginning of lines 2 and 3. However, when the program is run in Visual Studio Code, the Swedish characters get distorted.

<--- ååå
     ├╢├╢├╢
     äää--->

How can I solve it? I work with text processing and this is a major problem.

EDIT: Since the problem doesn't appear on many systems, I add the technical data: Windows 11 Pro Version 10.0.22621 Build 22621; Visual Studio Code Version: 1.73.1 (user setup) Date: 2022-11-09 Chromium: 102.0.5005.167 Node.js: 16.14.2 Sandboxed: No.

kmdreko
  • 42,554
  • 6
  • 57
  • 106
Kalle Svensson
  • 353
  • 1
  • 10
  • Which operating system? Which version? Which shell? What do you mean with "run in VSCode"? Through the integrated shell? Through clicking the "run" button of rust-analyzer? – Finomnis Dec 03 '22 at 09:01
  • It works on my Windows 11 on Ubuntu 22.04 WSL on integrated bash and also when hitting the "run" button. It also works on my Windows11 native with built-in powershell and on built-in "cmd". It also works when I hit the "run" button of rust-analyzer. It even works in git-bash (nested in VSCode). – Finomnis Dec 03 '22 at 09:06
  • I tried every combination of OS/Shell/run method that I have available, and it works on all of them. – Finomnis Dec 03 '22 at 09:07
  • Which shell are you using in VSCode? Cmd? Powershell? Git Bash? Does it happen on all of those, or just on one specific one? – Finomnis Dec 03 '22 at 09:12
  • Does switching between software/hardware rendering change anything? (Settings -> Features -> Terminal -> Integrated: Gpu Acceleration) – Finomnis Dec 03 '22 at 09:13
  • *"Strangely, spaces are added at the beginning of lines 2 and 3."* - That shouldn't happen. See: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4013bdc7e2ef652904f895cc1f9ece9e – Finomnis Dec 03 '22 at 09:14
  • Did you try updating Rust to the newest version? (Don't think it will change anything, but it's worth at try) – Finomnis Dec 03 '22 at 09:16
  • Might be related: https://github.com/Microsoft/vscode/issues/19837 – Finomnis Dec 03 '22 at 09:17
  • Said thread suggests you add this to your vscode config (Settings->Click on top right button "Open Settings (JSON)"), then add this line if you are using CMD: `"terminal.integrated.shellArgs.windows": ["/K", "chcp 65001"],`. Alternatively, if you are using PowerShell, use this line: `"terminal.integrated.shellArgs.windows": ["-NoExit", "/c", "chcp.com 65001"],`. Of course restart your VSCode in between. Does this make a difference? – Finomnis Dec 03 '22 at 09:20
  • @Finomnis: see the technical data added to the question. I've tried both the commands in the VSC terminal but they produce error output `"Unexpected token ':' in expression or statement."` – Kalle Svensson Dec 03 '22 at 09:38
  • They aren't supposed to go in your terminal, but in your VSCode settings page. Either way, they are deprecated. I added the new version in my answer. – Finomnis Dec 03 '22 at 09:41
  • Btw, your error output strongly indicates that you are using Powershell. CMD would say `'"terminal.integrated.shellArgs.windows":' is not recognized as an internal or external command, operable program or batch file.`. – Finomnis Dec 03 '22 at 09:51
  • @Finomnis: it is Powershell, I see it now. – Kalle Svensson Dec 03 '22 at 11:48
  • Out of curiousity: You said it worked if you execute the program in the shell directly. Did it also work if you execute it in **Powershell** directly? Because I have a suspicion that the reason why it worked is that you used Powershell in VSCode, but CMD outside of VSCode. – Finomnis Dec 04 '22 at 08:24
  • @Finomnis: I used CMD outside VCS and Powershell inside VSC. I'm not sure what would have happen if I had run the program in Powershell outside VSC instead of CMD. The VCS settings are changed now so Powershell works certainly as well, I hope. – Kalle Svensson Dec 04 '22 at 10:40

1 Answers1

7

- This answer is Windows specific. -

INFO: This answer describes you how can change your VSCode settings to force UTF-8 in your console. An alternative to this answer would be to force UTF-8 system-wide, as described here: Using UTF-8 Encoding (CHCP 65001) in Command Prompt / Windows Powershell (Windows 10)


It seems that sometimes the Windows shell doesn't use the correct UTF-8 code page.

You can tell VSCode to force a codepage in its shell using the following settings.

  • Open the Settings page (Shortkey: Ctrl+,)
  • Click on the button on the top right whose mouse-over text reads "Open Settings (JSON)"
  • Add the following lines:
   "terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "PowerShell",
            "icon": "terminal-powershell",
            "args": [
                "-NoExit",
                "/c",
                "chcp.com 65001"
            ]
        },
        "Command Prompt": {
            "path": [
                "${env:windir}\\Sysnative\\cmd.exe",
                "${env:windir}\\System32\\cmd.exe"
            ],
            "args": [
                "/K",
                "chcp 65001"
            ],
            "icon": "terminal-cmd"
        },
    },

This will force the UTF-8 code page.

If it worked, opening a new shell should display Active code page: 65001.

Source: https://github.com/microsoft/vscode/issues/19837


Previous, deprecated settings:

  • If your shell is "CMD":
    "terminal.integrated.shellArgs.windows": ["/K", "chcp 65001"],
    
  • If your shell is "Powershell":
    "terminal.integrated.shellArgs.windows": ["-NoExit", "/c", "chcp.com 65001"],
    
Finomnis
  • 18,094
  • 1
  • 20
  • 27