Skip to content

Commit 2dd2448

Browse files
authored
Run commands without creating a console window on Windows (#266)
Running a console program (like `python.exe`) from a GUI application (such as an IDE) will create a new console window by default. This will cause a console window to briefly appear whenever the IDE uses PET. To fix this, tell Windows to hide this console window.
1 parent e97b950 commit 2dd2448

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

crates/pet-conda/src/conda_info.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use log::{error, trace, warn};
55
use pet_fs::path::resolve_symlink;
6+
use pet_python_utils::executable::new_silent_command;
67
use std::path::PathBuf;
78

89
#[derive(Debug, serde::Deserialize)]
@@ -45,7 +46,7 @@ impl CondaInfo {
4546
resolve_symlink(&executable).unwrap_or(executable)
4647
};
4748

48-
let result = std::process::Command::new(&executable)
49+
let result = new_silent_command(&executable)
4950
.arg("info")
5051
.arg("--json")
5152
.output();

crates/pet-poetry/src/environment_locations_spawn.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use lazy_static::lazy_static;
55
use log::{error, trace};
66
use pet_core::python_environment::PythonEnvironment;
7+
use pet_python_utils::executable::new_silent_command;
78
use regex::Regex;
89
use std::{path::PathBuf, time::SystemTime};
910

@@ -36,7 +37,7 @@ pub fn list_environments(
3637

3738
fn get_environments(executable: &PathBuf, workspace_dir: &PathBuf) -> Option<Vec<PathBuf>> {
3839
let start = SystemTime::now();
39-
let result = std::process::Command::new(executable)
40+
let result = new_silent_command(executable)
4041
.arg("env")
4142
.arg("list")
4243
.arg("--full-path")

crates/pet-python-utils/src/env.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{
99
time::SystemTime,
1010
};
1111

12-
use crate::cache::create_cache;
12+
use crate::{cache::create_cache, executable::new_silent_command};
1313

1414
const PYTHON_INFO_JSON_SEPARATOR: &str = "093385e9-59f7-4a16-a604-14bf206256fe";
1515
const PYTHON_INFO_CMD:&str = "import json, sys; print('093385e9-59f7-4a16-a604-14bf206256fe');print(json.dumps({'version': '.'.join(str(n) for n in sys.version_info), 'sys_prefix': sys.prefix, 'executable': sys.executable, 'is64_bit': sys.maxsize > 2**32}))";
@@ -92,7 +92,7 @@ fn get_interpreter_details(executable: &Path) -> Option<ResolvedPythonEnv> {
9292
let executable = executable.to_str()?;
9393
let start = SystemTime::now();
9494
trace!("Executing Python: {} -c {}", executable, PYTHON_INFO_CMD);
95-
let result = std::process::Command::new(executable)
95+
let result = new_silent_command(executable)
9696
.args(["-c", PYTHON_INFO_CMD])
9797
.output();
9898
match result {

crates/pet-python-utils/src/executable.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use lazy_static::lazy_static;
55
use log::trace;
66
use regex::Regex;
7+
use std::ffi::OsStr;
78
use std::{
89
fs,
910
path::{Path, PathBuf},
@@ -159,6 +160,22 @@ pub fn should_search_for_environments_in_path<P: AsRef<Path>>(path: &P) -> bool
159160
true
160161
}
161162

163+
#[cfg(target_os = "windows")]
164+
pub fn new_silent_command(program: impl AsRef<OsStr>) -> std::process::Command {
165+
use std::os::windows::process::CommandExt;
166+
167+
const CREATE_NO_WINDOW: u32 = 0x08000000;
168+
169+
let mut command = std::process::Command::new(program);
170+
command.creation_flags(CREATE_NO_WINDOW);
171+
command
172+
}
173+
174+
#[cfg(not(target_os = "windows"))]
175+
pub fn new_silent_command(program: impl AsRef<OsStr>) -> std::process::Command {
176+
std::process::Command::new(program)
177+
}
178+
162179
#[cfg(test)]
163180
mod tests {
164181
use super::*;

0 commit comments

Comments
 (0)