Repository: vscode
Issue: Line normalization in `Terminal.sendText()` (IPython)
Labels: under-discussion

<!-- 💡 Instead of creating your report here, use 'Report Issue' from the 'Help' menu in VS Code to pre-fill useful information. -->
<!-- 🔧 Launch with `code --disable-extensions` to check. -->
Does this issue occur when all extensions are disabled?: Yes

<!-- 🪓 If you answered No above, use 'Help: Start Extension Bisect' from Command Palette to try to identify the cause. -->
<!-- 📣 Issues caused by an extension need to be reported directly to the extension publisher. The 'Help > Report Issue' dialog can assist with this. -->

<details>

<summary>Show System Information</summary>

- VSCode: Version: 1.96.4 (user setup)
Commit: cd4ee3b1c348a13bafd8f9ad8060705f6d4b9cba
Date: 2025-01-16T00:16:19.038Z
Electron: 32.2.6
ElectronBuildId: 10629634
Chromium: 128.0.6613.186
Node.js: 20.18.1
V8: 12.8.374.38-electron.0
OS: Windows_NT x64 10.0.19045

- I'm using Windows 10, Version 22H2 with WSL2 (Ubuntu 22.04)

</details>

---

### 🎈 `Terminal.sendText()`

The `Terminal.sendText()` function currently normalizes line endings as follows:

https://github.com/microsoft/vscode/blob/0d57cf5304672f3cd0c2776aaec1e5dedfead053/src/vs/workbench/contrib/terminal/browser/terminalInstance.ts#L1271-L1275

With regards to cross-platform operability, I am wondering **why we don't append any `\n` here and instead just a single `\r`**? E.g. the default line separation on Windows is `\r\n` and not `\r`.

Git Blame: The mentioned code was first proposed in [this comment](https://github.com/microsoft/vscode/issues/22887#issuecomment-296505048) and then introduced via [this commit](https://github.com/microsoft/vscode/commit/d01c71579d6690e7a7c8311f8477275d8b9b0203). During this, the `os.EOL` was replaced by `\r` and I don't see the reason to do so.

---

### 🎈 Possible symptoms

I suspect this normalization to have caused [this problem](https://github.com/ipython/ipython/issues/13054#issuecomment-2599055070) for us in the role of VSCode extension authors. For that, assume that the extension user has opened a VSCode-integrated terminal and started an IPython shell inside it (IPython: interactive Python shell, feels like a REPL for Python). Assume further that the user has defined a Python method `def hello_world()` inside that IPython shell.

In our extension, we send the string `hello_world()` to that terminal and expect that the `hello_world()` method is run. What we actually find, is that IPython enters the multi-line mode, so we see the following:

```py
In [2]: hello_world()
    ..:
```

This is only the case on Windows. On macOS and Unix it works fine.

I'm not entirely sure whether this problem is due to this normalization or due to some IPython behavior. [This issue](https://github.com/ipython/ipython/issues/13054) is already open for it.

Furthermore, because of this issue, the VSCode Python extension had to add a manual timeout, which might fail on slower machines. See their `sendText()` method [here](https://github.com/saponas/vscode-python/blob/1597f84ec29f320fa707355d340282994417f407/src/client/common/terminal/service.ts#L48-L56) and my [summary here](https://github.com/ipython/ipython/issues/13054#issuecomment-2598897658).

```ts
this.terminal!.sendText(`${text}`);
// send carriage return to make code execute
await new Promise(resolve => setTimeout(resolve, 100));
this.terminal.sendText('\u000D', false);
```