Skip to content

Commit 6f568d0

Browse files
authored
cli: ensure code tunnel service is headless on windows (microsoft#184621)
Fixes microsoft#184058
1 parent 68fdecf commit 6f568d0

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

cli/src/bin/code/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ fn make_logger(core: &args::CliCore) -> log::Logger {
133133
let tracer = SdkTracerProvider::builder().build().tracer("codecli");
134134
let mut log = log::Logger::new(tracer, log_level);
135135
if let Some(f) = &core.global_options.log_to_file {
136-
log = log.tee(log::FileLogSink::new(log_level, f).expect("expected to make file logger"))
136+
log = log
137+
.with_sink(log::FileLogSink::new(log_level, f).expect("expected to make file logger"))
137138
}
138139

139140
log

cli/src/tunnels/service_windows.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
use async_trait::async_trait;
77
use shell_escape::windows::escape as shell_escape;
8+
use std::os::windows::process::CommandExt;
89
use std::{
910
path::PathBuf,
1011
process::{Command, Stdio},
1112
};
13+
use winapi::um::winbase::{CREATE_NEW_PROCESS_GROUP, DETACHED_PROCESS};
1214
use winreg::{enums::HKEY_CURRENT_USER, RegKey};
1315

1416
use crate::{
@@ -21,6 +23,8 @@ use crate::{
2123

2224
use super::service::{tail_log_file, ServiceContainer, ServiceManager as CliServiceManager};
2325

26+
const DID_LAUNCH_AS_HIDDEN_PROCESS: &str = "VSCODE_CLI_DID_LAUNCH_AS_HIDDEN_PROCESS";
27+
2428
pub struct WindowsService {
2529
log: log::Logger,
2630
tunnel_lock: PathBuf,
@@ -90,7 +94,24 @@ impl CliServiceManager for WindowsService {
9094
launcher_paths: LauncherPaths,
9195
mut handle: impl 'static + ServiceContainer,
9296
) -> Result<(), AnyError> {
93-
handle.run_service(self.log, launcher_paths).await
97+
if std::env::var(DID_LAUNCH_AS_HIDDEN_PROCESS).is_ok() {
98+
return handle.run_service(self.log, launcher_paths).await;
99+
}
100+
101+
// Start as a hidden subprocess to avoid showing cmd.exe on startup.
102+
// Fixes https://github.com/microsoft/vscode/issues/184058
103+
// I also tried the winapi ShowWindow, but that didn't yield fruit.
104+
Command::new(std::env::current_exe().unwrap())
105+
.args(std::env::args().skip(1))
106+
.env(DID_LAUNCH_AS_HIDDEN_PROCESS, "1")
107+
.stderr(Stdio::null())
108+
.stdout(Stdio::null())
109+
.stdin(Stdio::null())
110+
.creation_flags(CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS)
111+
.spawn()
112+
.map_err(|e| wrap(e, "error starting nested process"))?;
113+
114+
Ok(())
94115
}
95116

96117
async fn unregister(&self) -> Result<(), AnyError> {

0 commit comments

Comments
 (0)