Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAZEL_TEST env variable is not sent on Windows #21420

Closed
cristiandonosoc opened this issue Feb 20, 2024 · 4 comments
Closed

BAZEL_TEST env variable is not sent on Windows #21420

cristiandonosoc opened this issue Feb 20, 2024 · 4 comments
Labels
team-Core Skyframe, bazel query, BEP, options parsing, bazelrc type: bug untriaged

Comments

@cristiandonosoc
Copy link
Contributor

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

@fmeum
Copy link
Collaborator

fmeum commented Feb 20, 2024

This was added in #15393, which simply missed the case of Windows.

Would you be interested in submitting a PR? You would need to add a SetEnv call around here

!PrintTestLogStartMarker() || !GetCwd(&exec_root) || !ExportUserName() ||

cristiandonosoc added a commit to cristiandonosoc/bazel that referenced this issue Feb 21, 2024
This was an oversight from when exporting BAZEL_TEST on Linux.

fixes: bazelbuild#21420
@meisterT meisterT added team-Core Skyframe, bazel query, BEP, options parsing, bazelrc and removed team-Local-Exec Issues and PRs for the Execution (Local) team labels Feb 21, 2024
@fmeum
Copy link
Collaborator

fmeum commented Feb 26, 2024

@bazel-io fork 7.1.0

bazel-io pushed a commit to bazel-io/bazel that referenced this issue Feb 26, 2024
This was an oversight from when exporting BAZEL_TEST on Linux.

fixes: bazelbuild#21420

Closes bazelbuild#21444.

PiperOrigin-RevId: 610338496
Change-Id: Icbbdc42b6ea92a2de2b0c558aea47a24493c9d8a
github-merge-queue bot pushed a commit that referenced this issue Feb 26, 2024
This was an oversight from when exporting BAZEL_TEST on Linux.

fixes: #21420

Closes #21444.

Commit
0a9d612

PiperOrigin-RevId: 610338496
Change-Id: Icbbdc42b6ea92a2de2b0c558aea47a24493c9d8a

Co-authored-by: Cristin Donoso <cristiandonosoc@gmail.com>
@iancha1992
Copy link
Member

A fix for this issue has been included in Bazel 7.1.0 RC2. Please test out the release candidate and report any issues as soon as possible.
If you're using Bazelisk, you can point to the latest RC by setting USE_BAZEL_VERSION=7.1.0rc2. Thanks!

@cristiandonosoc
Copy link
Contributor Author

Used bazelisk with .bazelversion set to 7.1.0ec2 and seems to work with the test C++ program I wrote.
I would consider this fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-Core Skyframe, bazel query, BEP, options parsing, bazelrc type: bug untriaged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants