From c1dcfea6f65f6f3b67f22e7c4d69339b07fc90e1 Mon Sep 17 00:00:00 2001 From: Ivan Pashchenko Date: Thu, 14 Mar 2024 21:18:21 +0100 Subject: [PATCH] RDCT-1171: restore console for remote dev scenario GitOrigin-RevId: fff34b60f31aa68ff2179e4c82fd00988a1709ff --- native/XPlatLauncher/Cargo.toml | 4 ++-- native/XPlatLauncher/src/lib.rs | 20 +++++++++++++++++++- native/XPlatLauncher/src/main.rs | 3 +++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/native/XPlatLauncher/Cargo.toml b/native/XPlatLauncher/Cargo.toml index 64dbe1a803be..22b53041d7b2 100644 --- a/native/XPlatLauncher/Cargo.toml +++ b/native/XPlatLauncher/Cargo.toml @@ -25,7 +25,7 @@ va_list = "0.1.4" [target.'cfg(target_os = "windows")'.dependencies.windows] version = "0.51.1" -features = ["Win32_Foundation", "Win32_UI_Shell", "Win32_UI_WindowsAndMessaging", "Win32_System_Environment", "Win32_System_Services", "Win32_Security"] +features = ["Win32_Foundation", "Win32_UI_Shell", "Win32_UI_WindowsAndMessaging", "Win32_System_Environment", "Win32_System_Services", "Win32_Security", "Win32_System_Console"] [target.'cfg(target_family = "unix")'.dependencies] libc = "0.2.142" @@ -48,7 +48,7 @@ cargo-about = "0.5.7" # generates license report winresource = "0.1.17" [dev-dependencies] -tempfile = "3" +tempfile = "3.8.0" junction = "1.0.0" [features] diff --git a/native/XPlatLauncher/src/lib.rs b/native/XPlatLauncher/src/lib.rs index 5d1bddcf64e6..0c3964e0ffcb 100644 --- a/native/XPlatLauncher/src/lib.rs +++ b/native/XPlatLauncher/src/lib.rs @@ -41,7 +41,8 @@ use serde::{Deserialize, Serialize}; use { windows::core::{GUID, PWSTR}, windows::Win32::Foundation, - windows::Win32::UI::Shell + windows::Win32::UI::Shell, + windows::Win32::System::Console::{AllocConsole, ATTACH_PARENT_PROCESS, AttachConsole}, }; #[cfg(target_family = "unix")] @@ -67,6 +68,10 @@ const CLASS_PATH_SEPARATOR: &str = ":"; pub fn main_lib() { let exe_path = env::current_exe().unwrap_or_else(|_| PathBuf::from(env::args().next().unwrap())); let remote_dev = exe_path.file_name().unwrap().to_string_lossy().starts_with("remote-dev-server"); + if remote_dev { + attach_console(); + } + let debug_mode = remote_dev || env::var(DEBUG_MODE_ENV_VAR).is_ok(); if let Err(e) = main_impl(exe_path, remote_dev, debug_mode) { @@ -75,6 +80,19 @@ pub fn main_lib() { } } +#[cfg(target_os = "windows")] +fn attach_console() { + unsafe { + match AttachConsole(ATTACH_PARENT_PROCESS) { + Ok(_) => {} + Err(_) => AllocConsole().expect("Failed to attach to existing console or allocate a new one") + } + } +} + +#[cfg(not(target_os = "windows"))] +fn attach_console() { } + fn main_impl(exe_path: PathBuf, remote_dev: bool, debug_mode: bool) -> Result<()> { let level = if debug_mode { LevelFilter::Debug } else { LevelFilter::Error }; mini_logger::init(level).expect("Cannot initialize the logger"); diff --git a/native/XPlatLauncher/src/main.rs b/native/XPlatLauncher/src/main.rs index fab54844a95d..298d13b69342 100644 --- a/native/XPlatLauncher/src/main.rs +++ b/native/XPlatLauncher/src/main.rs @@ -1,5 +1,8 @@ // Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// this attribute doesn't do anything on macos/linux +// same as /SUBSYSTEM:WINDOWS, doesn't allocate/attach to a console by default +#![windows_subsystem = "windows"] use xplat_launcher::main_lib; fn main() {