Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c4fb218

Browse files
laszlocsomorbuchgr
authored andcommitted
Windows: detect if started from Windows Explorer
Bazel now detects whether it was started from a command line (or as a subprocess), or from Windows Explorer (by clicking its icon). In the latter case it displays an error message asking the user to run it from a command line, and waiting for a keypress so the user has time to read this message. Change-Id: I4b0430e30d2f1f243cec6ff63cb3abac907e60e3 PiperOrigin-RevId: 163338527
1 parent 195a7a8 commit c4fb218

4 files changed

Lines changed: 33 additions & 0 deletions

File tree

src/main/cpp/blaze.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,13 @@ int Main(int argc, const char *argv[], WorkspaceLayout *workspace_layout,
13231323

13241324
globals = new GlobalVariables(option_processor);
13251325
blaze::SetupStdStreams();
1326+
if (argc == 1 && blaze::WarnIfStartedFromDesktop()) {
1327+
// Only check and warn for from-desktop start if there were no args.
1328+
// In this case the user probably clicked Bazel's icon (as opposed to either
1329+
// starting it from a terminal, or as a subprocess with args, or on Windows
1330+
// from a ".lnk" file with some args).
1331+
return blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR;
1332+
}
13261333

13271334
// Best-effort operation to raise the resource limits from soft to hard. We
13281335
// do this early during the main program instead of just before execing the

src/main/cpp/blaze_util_platform.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ void SetEnv(const std::string& name, const std::string& value);
198198

199199
void UnsetEnv(const std::string& name);
200200

201+
// Returns true and prints a warning if Bazel was started by clicking its icon.
202+
// This is typical on Windows. Other platforms should return false, unless they
203+
// wish to handle this case too.
204+
bool WarnIfStartedFromDesktop();
205+
201206
// Ensure we have open file descriptors for stdin/stdout/stderr.
202207
void SetupStdStreams();
203208

src/main/cpp/blaze_util_posix.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@ void UnsetEnv(const string& name) {
512512
unsetenv(name.c_str());
513513
}
514514

515+
bool WarnIfStartedFromDesktop() { return false; }
516+
515517
void SetupStdStreams() {
516518
// Set non-buffered output mode for stderr/stdout. The server already
517519
// line-buffers messages where it makes sense, so there's no need to do set

src/main/cpp/blaze_util_windows.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,25 @@ void SetEnv(const string& name, const string& value) {
11661166

11671167
void UnsetEnv(const string& name) { SetEnv(name, ""); }
11681168

1169+
bool WarnIfStartedFromDesktop() {
1170+
// GetConsoleProcessList returns:
1171+
// 0, if no console attached (Bazel runs as a subprocess)
1172+
// 1, if Bazel was started by clicking on its icon
1173+
// 2, if Bazel was started from the command line (even if its output is
1174+
// redirected)
1175+
DWORD dummy[2] = {0};
1176+
if (GetConsoleProcessList(dummy, 2) != 1) {
1177+
return false;
1178+
}
1179+
printf(
1180+
"Bazel is a command line tool.\n\n"
1181+
"Try opening a console, such as the Windows Command Prompt (cmd.exe) "
1182+
"or PowerShell, and running \"bazel help\".\n\n"
1183+
"Press Enter to close this window...");
1184+
ReadFile(GetStdHandle(STD_INPUT_HANDLE), dummy, 1, dummy, NULL);
1185+
return true;
1186+
}
1187+
11691188
#ifndef ENABLE_PROCESSED_OUTPUT
11701189
// From MSDN about BOOL SetConsoleMode(HANDLE, DWORD).
11711190
#define ENABLE_PROCESSED_OUTPUT 0x0001

0 commit comments

Comments
 (0)