From 8493606150e3da59ba8ef7eacffcfb8a39411344 Mon Sep 17 00:00:00 2001 From: Jonah Iden Date: Fri, 23 Jun 2023 14:41:54 +0200 Subject: [PATCH] Fix command execution in terminal on Windows (#12620) --- packages/core/src/browser/common-frontend-contribution.ts | 4 ++-- packages/core/src/browser/preloader.ts | 1 + packages/core/src/common/os.ts | 5 ++++- packages/monaco/src/browser/monaco-text-model-service.ts | 2 +- .../src/browser/search-in-workspace-result-tree-widget.tsx | 4 ++-- packages/task/src/node/task-abstract-line-matcher.ts | 4 ++-- packages/terminal/src/browser/terminal-widget-impl.ts | 2 +- packages/workspace/src/browser/workspace-commands.ts | 4 ++-- 8 files changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/core/src/browser/common-frontend-contribution.ts b/packages/core/src/browser/common-frontend-contribution.ts index d94d690d47d8a..8821bc6501b02 100644 --- a/packages/core/src/browser/common-frontend-contribution.ts +++ b/packages/core/src/browser/common-frontend-contribution.ts @@ -32,7 +32,7 @@ import { AboutDialog } from './about-dialog'; import * as browser from './browser'; import URI from '../common/uri'; import { ContextKey, ContextKeyService } from './context-key-service'; -import { OS, isOSX, isWindows } from '../common/os'; +import { OS, isOSX, isWindows, EOL } from '../common/os'; import { ResourceContextKey } from './resource-context-key'; import { UriSelection } from '../common/selection'; import { StorageService } from './storage-service'; @@ -736,7 +736,7 @@ export class CommonFrontendContribution implements FrontendApplicationContributi isEnabled: uris => Array.isArray(uris) && uris.some(uri => uri instanceof URI), execute: async uris => { if (uris.length) { - const lineDelimiter = isWindows ? '\r\n' : '\n'; + const lineDelimiter = EOL; const text = uris.map(resource => resource.path.fsPath()).join(lineDelimiter); await this.clipboardService.writeText(text); } else { diff --git a/packages/core/src/browser/preloader.ts b/packages/core/src/browser/preloader.ts index 7e17315c7f93c..5cff49270219f 100644 --- a/packages/core/src/browser/preloader.ts +++ b/packages/core/src/browser/preloader.ts @@ -55,6 +55,7 @@ async function loadBackendOS(): Promise { OS.backend.isOSX = isOSX; OS.backend.isWindows = isWindows; OS.backend.type = () => osType; + OS.backend.EOL = isWindows ? '\r\n' : '\n'; } function initBackground(): void { diff --git a/packages/core/src/common/os.ts b/packages/core/src/common/os.ts index a1a5942ddcbce..8b8529cd7b1be 100644 --- a/packages/core/src/common/os.ts +++ b/packages/core/src/common/os.ts @@ -29,6 +29,8 @@ function is(userAgent: string, platform: string): boolean { export const isWindows = is('Windows', 'win32'); export const isOSX = is('Mac', 'darwin'); +export const EOL = isWindows ? '\r\n' : '\n'; + export type CMD = [string, string[]]; export function cmd(command: string, ...args: string[]): CMD { return [ @@ -65,7 +67,8 @@ export namespace OS { export const backend = { type, isWindows, - isOSX + isOSX, + EOL }; } diff --git a/packages/monaco/src/browser/monaco-text-model-service.ts b/packages/monaco/src/browser/monaco-text-model-service.ts index f8a7ac2cc95ec..8c7bd41749762 100644 --- a/packages/monaco/src/browser/monaco-text-model-service.ts +++ b/packages/monaco/src/browser/monaco-text-model-service.ts @@ -88,7 +88,7 @@ export class MonacoTextModelService implements ITextModelService { if (eol && eol !== 'auto') { return eol; } - return OS.backend.isWindows ? '\r\n' : '\n'; + return OS.backend.EOL; }; } } diff --git a/packages/search-in-workspace/src/browser/search-in-workspace-result-tree-widget.tsx b/packages/search-in-workspace/src/browser/search-in-workspace-result-tree-widget.tsx index 7f09ac20416b3..cf5fbb4fbc13e 100644 --- a/packages/search-in-workspace/src/browser/search-in-workspace-result-tree-widget.tsx +++ b/packages/search-in-workspace/src/browser/search-in-workspace-result-tree-widget.tsx @@ -33,7 +33,7 @@ import { codicon, TopDownTreeIterator } from '@theia/core/lib/browser'; -import { CancellationTokenSource, Emitter, Event, isWindows, ProgressService } from '@theia/core'; +import { CancellationTokenSource, Emitter, EOL, Event, ProgressService } from '@theia/core'; import { EditorManager, EditorDecoration, TrackedRangeStickiness, OverviewRulerLane, EditorWidget, EditorOpenerOptions, FindMatch @@ -1210,7 +1210,7 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget { strings.push(string); } } - return strings.join(isWindows ? '\r\n' : '\n'); + return strings.join(EOL); } } diff --git a/packages/task/src/node/task-abstract-line-matcher.ts b/packages/task/src/node/task-abstract-line-matcher.ts index a4c650a730b6e..9aa4796b425b7 100644 --- a/packages/task/src/node/task-abstract-line-matcher.ts +++ b/packages/task/src/node/task-abstract-line-matcher.ts @@ -19,7 +19,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { isWindows } from '@theia/core/lib/common/os'; +import { EOL } from '@theia/core/lib/common/os'; import { Diagnostic, DiagnosticSeverity, Range } from '@theia/core/shared/vscode-languageserver-protocol'; import { FileLocationKind, ProblemMatcher, ProblemPattern, @@ -31,7 +31,7 @@ import { URI as vscodeURI } from '@theia/core/shared/vscode-uri'; import { Severity } from '@theia/core/lib/common/severity'; import { MAX_SAFE_INTEGER } from '@theia/core/lib/common/numbers'; -const endOfLine: string = isWindows ? '\r\n' : '\n'; +const endOfLine: string = EOL; export interface ProblemData { kind?: ProblemLocationKind; diff --git a/packages/terminal/src/browser/terminal-widget-impl.ts b/packages/terminal/src/browser/terminal-widget-impl.ts index ae3d9fbf8a01c..0fb4695889033 100644 --- a/packages/terminal/src/browser/terminal-widget-impl.ts +++ b/packages/terminal/src/browser/terminal-widget-impl.ts @@ -706,7 +706,7 @@ export class TerminalWidgetImpl extends TerminalWidget implements StatefulWidget } async executeCommand(commandOptions: CommandLineOptions): Promise { - this.sendText(this.shellCommandBuilder.buildCommand(await this.processInfo, commandOptions) + '\n'); + this.sendText(this.shellCommandBuilder.buildCommand(await this.processInfo, commandOptions) + OS.backend.EOL); } scrollLineUp(): void { diff --git a/packages/workspace/src/browser/workspace-commands.ts b/packages/workspace/src/browser/workspace-commands.ts index 6f1c944f9972a..2d54cec2d7302 100644 --- a/packages/workspace/src/browser/workspace-commands.ts +++ b/packages/workspace/src/browser/workspace-commands.ts @@ -34,7 +34,7 @@ import { WorkspaceCompareHandler } from './workspace-compare-handler'; import { FileDownloadCommands } from '@theia/filesystem/lib/browser/download/file-download-command-contribution'; import { FileSystemCommands } from '@theia/filesystem/lib/browser/filesystem-frontend-contribution'; import { WorkspaceInputDialog } from './workspace-input-dialog'; -import { Emitter, Event, isWindows, OS } from '@theia/core/lib/common'; +import { Emitter, EOL, Event, OS } from '@theia/core/lib/common'; import { FileService } from '@theia/filesystem/lib/browser/file-service'; import { FileStat } from '@theia/filesystem/lib/common/files'; import { nls } from '@theia/core/lib/common/nls'; @@ -313,7 +313,7 @@ export class WorkspaceCommandContribution implements CommandContribution { isEnabled: uris => !!uris.length, isVisible: uris => !!uris.length, execute: async uris => { - const lineDelimiter = isWindows ? '\r\n' : '\n'; + const lineDelimiter = EOL; const text = uris.map((uri: URI) => { const workspaceRoot = this.workspaceService.getWorkspaceRootUri(uri); if (workspaceRoot) {