cli: ensure code tunnel service is headless on windows (#184621)

Fixes #184058
This commit is contained in:
Connor Peet 2023-06-08 10:50:44 -07:00 committed by GitHub
parent 68fdecfdc1
commit 6f568d087d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View file

@ -133,7 +133,8 @@ fn make_logger(core: &args::CliCore) -> log::Logger {
let tracer = SdkTracerProvider::builder().build().tracer("codecli");
let mut log = log::Logger::new(tracer, log_level);
if let Some(f) = &core.global_options.log_to_file {
log = log.tee(log::FileLogSink::new(log_level, f).expect("expected to make file logger"))
log = log
.with_sink(log::FileLogSink::new(log_level, f).expect("expected to make file logger"))
}
log

View file

@ -5,10 +5,12 @@
use async_trait::async_trait;
use shell_escape::windows::escape as shell_escape;
use std::os::windows::process::CommandExt;
use std::{
path::PathBuf,
process::{Command, Stdio},
};
use winapi::um::winbase::{CREATE_NEW_PROCESS_GROUP, DETACHED_PROCESS};
use winreg::{enums::HKEY_CURRENT_USER, RegKey};
use crate::{
@ -21,6 +23,8 @@ use crate::{
use super::service::{tail_log_file, ServiceContainer, ServiceManager as CliServiceManager};
const DID_LAUNCH_AS_HIDDEN_PROCESS: &str = "VSCODE_CLI_DID_LAUNCH_AS_HIDDEN_PROCESS";
pub struct WindowsService {
log: log::Logger,
tunnel_lock: PathBuf,
@ -90,7 +94,24 @@ impl CliServiceManager for WindowsService {
launcher_paths: LauncherPaths,
mut handle: impl 'static + ServiceContainer,
) -> Result<(), AnyError> {
handle.run_service(self.log, launcher_paths).await
if std::env::var(DID_LAUNCH_AS_HIDDEN_PROCESS).is_ok() {
return handle.run_service(self.log, launcher_paths).await;
}
// Start as a hidden subprocess to avoid showing cmd.exe on startup.
// Fixes https://github.com/microsoft/vscode/issues/184058
// I also tried the winapi ShowWindow, but that didn't yield fruit.
Command::new(std::env::current_exe().unwrap())
.args(std::env::args().skip(1))
.env(DID_LAUNCH_AS_HIDDEN_PROCESS, "1")
.stderr(Stdio::null())
.stdout(Stdio::null())
.stdin(Stdio::null())
.creation_flags(CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS)
.spawn()
.map_err(|e| wrap(e, "error starting nested process"))?;
Ok(())
}
async fn unregister(&self) -> Result<(), AnyError> {