Skip to content

BAZEL_TEST env variable is not sent on Windows #21420

@cristiandonosoc

Description

@cristiandonosoc

Description of the bug:

According to https://bazel.build/reference/test-encyclopedia#initial-conditions, the BAZEL_TEST environment variable is sent to the underlying program to mark is as being run by bazel test. This works on Linux, but on Windows that environment variable is not sent.

Which category does this issue belong to?

No response

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Wrote the following test on linux, which passes:

#include <algorithm>
#include <iostream>
#include <unistd.h>
#include <vector>

extern char **environ;

int main() {
  std::vector<std::string> envs;
  for (char **env = environ; *env != 0; env++) {
    envs.push_back(*env);
  }

  std::sort(envs.begin(), envs.end(),
            [](const auto &lhs, const auto &rhs) { return lhs < rhs; });

  // Search for BAZEL_TEST.
  bool found = false;
  for (const auto &env : envs) {
    std::cout << env << std::endl;
    size_t pos = env.find("BAZEL_TEST");
    if (pos != std::string::npos) {
      found = true;
      break;
    }
  }

  if (!found) {
    return 1;
  }

  return 0;
}

But the equivalent test on Windows does not pass:

#include <iostream>
#include <vector>
#include <windows.h>

int main() {
  // Get a pointer to the environment block.
  LPCH lpvEnv = GetEnvironmentStrings();

  // If the returned pointer is NULL, exit.
  if (lpvEnv == NULL) {
    std::cerr << "Failed to retrieve environment strings." << std::endl;
    return 1;
  }

  std::vector<std::string> envs;

  // Variable to hold each environment variable.
  LPSTR lpszVariable;
  for (lpszVariable = (LPSTR)lpvEnv; *lpszVariable; lpszVariable++) {
    // lpszVariable now points to the beginning of an environment string.
    envs.push_back(lpszVariable);

    // Move to the next environment string.
    while (*lpszVariable)
      lpszVariable++;
  }

  // Free the memory allocated for the environment strings.
  FreeEnvironmentStrings(lpvEnv);

  std::sort(envs.begin(), envs.end(),
            [](const auto &lhs, const auto &rhs) { return lhs < rhs; });

  // Search for BAZEL_TEST.
  bool found = false;
  for (const auto &env : envs) {
    std::cout << env << std::endl;

    size_t pos = env.find("BAZEL_TEST");
    if (pos != std::string::npos) {
      found = true;
      break;
    }
  }

  if (!found) {
    return 1;
  }

  return 0;
}

Which operating system are you running Bazel on?

Windows and Linux

What is the output of bazel info release?

Windows & Linux: release 7.0.2 (via Bazelisk)

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse HEAD ?

This was not run in any git repository.

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No response

Have you found anything relevant by searching the web?

I did google for this, but could not find any relevant information. If there is a known issue, I apologize for not finding it.

Any other information, logs, or outputs that you want to share?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions