Skip to content

Commit 837e1b3

Browse files
laszlocsomordslomov
authored andcommitted
Windows, sh_bin. launcher: export runfiles envvars
Fix the exe launcher of sh_binary rules to export the RUNFILES_MANIFEST_ONLY and RUNFILES_MANIFEST_FILE environment variables. Fixes bazelbuild#3492 Change-Id: I8507565f44c8b59f8218570306375cc083a41e03 PiperOrigin-RevId: 164095286
1 parent d4fa181 commit 837e1b3

File tree

7 files changed

+34
-15
lines changed

7 files changed

+34
-15
lines changed

src/test/py/bazel/launcher_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ def testShBinaryLauncher(self):
112112
foo_sh = self.ScratchFile('foo/foo.sh', [
113113
'#!/bin/bash',
114114
'echo hello shell',
115+
'echo runfiles_manifest_only=${RUNFILES_MANIFEST_ONLY:-}',
116+
'echo runfiles_manifest_file=${RUNFILES_MANIFEST_FILE:-}',
115117
])
116118
foo_cmd = self.ScratchFile('foo/foo.cmd', ['@echo hello batch'])
117119
self.ScratchFile('bar/BUILD', ['exports_files(["bar.txt"])'])
@@ -182,7 +184,18 @@ def testShBinaryLauncher(self):
182184

183185
exit_code, stdout, stderr = self.RunProgram([bin1])
184186
self.AssertExitCode(exit_code, 0, stderr)
187+
self.assertEqual(len(stdout), 3)
185188
self.assertEqual(stdout[0], 'hello shell')
189+
if self.IsWindows():
190+
self.assertEqual(stdout[1], 'runfiles_manifest_only=1')
191+
self.assertRegexpMatches(stdout[2], r'^runfiles_manifest_file.*MANIFEST$')
192+
else:
193+
# TODO(laszlocsomor): Find out whether the runfiles-related envvars should
194+
# be set on Linux (e.g. $RUNFILES, $RUNFILES_MANIFEST_FILE). Currently
195+
# they aren't, and that may be a bug. If it's indeed a bug, fix that bug
196+
# and update this test.
197+
self.assertEqual(stdout[1], 'runfiles_manifest_only=')
198+
self.assertEqual(stdout[2], 'runfiles_manifest_file=')
186199

187200
if self.IsWindows():
188201
exit_code, stdout, stderr = self.RunProgram([bin2])

src/tools/launcher/bash_launcher.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ExitCode BashBinaryLauncher::Launch() {
3030
string bash_binary = this->GetLaunchInfoByKey(BASH_BIN_PATH);
3131
// If specified bash binary path doesn't exist, then fall back to
3232
// bash.exe and hope it's in PATH.
33-
if (!DoesFilePathExist(bash_binary)) {
33+
if (!DoesFilePathExist(bash_binary.c_str())) {
3434
bash_binary = "bash.exe";
3535
}
3636

src/tools/launcher/launcher.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,28 @@ using std::vector;
3434

3535
BinaryLauncherBase::BinaryLauncherBase(
3636
const LaunchDataParser::LaunchInfo& _launch_info, int argc, char* argv[])
37-
: launch_info(_launch_info) {
38-
this->workspace_name = GetLaunchInfoByKey(WORKSPACE_NAME);
37+
: launch_info(_launch_info),
38+
manifest_file(FindManifestFile(argv[0])),
39+
workspace_name(GetLaunchInfoByKey(WORKSPACE_NAME)) {
3940
for (int i = 0; i < argc; i++) {
4041
this->commandline_arguments.push_back(argv[i]);
4142
}
42-
ParseManifestFile(&this->manifest_file_map, FindManifestFile());
43+
ParseManifestFile(&this->manifest_file_map, this->manifest_file);
4344
}
4445

45-
string BinaryLauncherBase::FindManifestFile() const {
46+
string BinaryLauncherBase::FindManifestFile(const char* argv0) {
4647
// Get the name of the binary
47-
string binary = GetBinaryPathWithoutExtension(this->commandline_arguments[0]);
48+
string binary = GetBinaryPathWithoutExtension(argv0);
4849

4950
// Try to find <path to binary>.runfiles/MANIFEST
5051
string manifest_file = binary + ".runfiles\\MANIFEST";
51-
if (DoesFilePathExist(manifest_file)) {
52+
if (DoesFilePathExist(manifest_file.c_str())) {
5253
return manifest_file;
5354
}
5455

5556
// Also try to check if <path to binary>.runfiles_manifest exists
5657
manifest_file = binary + ".runfiles_manifest";
57-
if (DoesFilePathExist(manifest_file)) {
58+
if (DoesFilePathExist(manifest_file.c_str())) {
5859
return manifest_file;
5960
}
6061

@@ -125,6 +126,8 @@ void BinaryLauncherBase::CreateCommandLine(
125126

126127
ExitCode BinaryLauncherBase::LaunchProcess(
127128
const string& executable, const vector<string>& arguments) const {
129+
SetEnvironmentVariableA("RUNFILES_MANIFEST_ONLY", "1");
130+
SetEnvironmentVariableA("RUNFILES_MANIFEST_FILE", manifest_file.c_str());
128131
CmdLine cmdline;
129132
CreateCommandLine(&cmdline, executable, arguments);
130133
PROCESS_INFORMATION processInfo = {0};

src/tools/launcher/launcher.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,15 @@ class BinaryLauncherBase {
6666
// A map to store all the launch information.
6767
const LaunchDataParser::LaunchInfo& launch_info;
6868

69+
// Absolute path to the runfiles manifest file.
70+
const std::string manifest_file;
71+
6972
// The commandline arguments recieved.
7073
// The first argument is the path of this launcher itself.
7174
std::vector<std::string> commandline_arguments;
7275

7376
// The workspace name of the repository this target belongs to.
74-
std::string workspace_name;
77+
const std::string workspace_name;
7578

7679
// A map to store all entries of the manifest file.
7780
std::unordered_map<std::string, std::string> manifest_file_map;
@@ -89,7 +92,7 @@ class BinaryLauncherBase {
8992
// Expect the manifest file to be at
9093
// 1. <path>/<to>/<binary>/<target_name>.runfiles/MANIFEST
9194
// or 2. <path>/<to>/<binary>/<target_name>.runfiles_manifest
92-
std::string FindManifestFile() const;
95+
static std::string FindManifestFile(const char* argv0);
9396

9497
// Parse manifest file into a map
9598
static void ParseManifestFile(ManifestFileMap* manifest_file_map,

src/tools/launcher/util/launcher_util.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ void PrintError(const char* format, ...) {
6767
fputc('\n', stderr);
6868
}
6969

70-
bool DoesFilePathExist(const string& path) {
71-
DWORD dwAttrib = GetFileAttributes(path.c_str());
70+
bool DoesFilePathExist(const char* path) {
71+
DWORD dwAttrib = GetFileAttributes(path);
7272

7373
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
7474
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));

src/tools/launcher/util/launcher_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ std::string GetBinaryPathWithExtension(const std::string& binary);
4848
std::string GetEscapedArgument(const std::string& argument);
4949

5050
// Check if a file exists at a given path.
51-
bool DoesFilePathExist(const std::string& path);
51+
bool DoesFilePathExist(const char* path);
5252

5353
} // namespace launcher
5454
} // namespace bazel

src/tools/launcher/util/launcher_util_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ TEST_F(LaunchUtilTest, DoesFilePathExistTest) {
8686
string file1 = GetTmpDir() + "/foo";
8787
string file2 = GetTmpDir() + "/bar";
8888
CreateEmptyFile(file1);
89-
ASSERT_TRUE(DoesFilePathExist(file1));
90-
ASSERT_FALSE(DoesFilePathExist(file2));
89+
ASSERT_TRUE(DoesFilePathExist(file1.c_str()));
90+
ASSERT_FALSE(DoesFilePathExist(file2.c_str()));
9191
}
9292

9393
} // namespace launcher

0 commit comments

Comments
 (0)