Skip to content

Commit

Permalink
Work around Bazel launcher issues on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
phst committed Nov 23, 2021
1 parent 26d6a2b commit ca3ba21
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
19 changes: 18 additions & 1 deletion elisp/process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,26 @@ absl::StatusOr<int> Run(std::string binary,
absl::StatusOr<NativeString> resolved_binary = runfiles.Resolve(binary);
if (!resolved_binary.ok()) return resolved_binary.status();
std::vector<NativeString> final_args{*resolved_binary};
final_args.insert(final_args.end(), args.begin(), args.end());
absl::StatusOr<Environment> map = runfiles.Environment();
if (!map.ok()) return map.status();
std::vector<NativeString> runfiles_args;
// Pass the runfiles environment variables as separate arguments. This is
// necessary because the binary launcher unconditionally sets the runfiles
// environment variables based on its own argv[0]; see
// https://github.com/bazelbuild/bazel/blob/6.0.0-pre.20211110.1/src/tools/launcher/launcher.cc.
// We also can’t set argv[0] to this binary because the launcher uses it to
// find and its own binary; see
// https://github.com/bazelbuild/bazel/blob/6.0.0-pre.20211110.1/src/tools/launcher/launcher_main.cc.
for (const auto& p : *map) {
runfiles_args.push_back(PHST_RULES_ELISP_NATIVE_LITERAL("--runfiles_env=") +
p.first + PHST_RULES_ELISP_NATIVE_LITERAL('=') +
p.second);
}
// Sort entries for hermeticity.
absl::c_sort(runfiles_args);
final_args.insert(final_args.end(), runfiles_args.begin(),
runfiles_args.end());
final_args.insert(final_args.end(), args.begin(), args.end());
map->insert(orig_env.begin(), orig_env.end());
std::vector<NativeString> final_env;
for (const auto& p : *map) {
Expand Down
10 changes: 8 additions & 2 deletions elisp/run_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import pathlib
import subprocess
import sys
from typing import Iterable, Mapping, Optional, Sequence
from typing import Iterable, Mapping, Optional, Sequence, Tuple

from phst_rules_elisp.elisp import load
from phst_rules_elisp.elisp import manifest
Expand All @@ -32,6 +32,8 @@
def main() -> None:
"""Main function."""
parser = argparse.ArgumentParser(allow_abbrev=False)
parser.add_argument('--runfiles_env', action='append', type=_env_var,
default=[])
parser.add_argument('--wrapper', type=pathlib.PurePosixPath, required=True)
parser.add_argument('--mode', choices=('direct', 'wrap'), required=True)
parser.add_argument('--rule-tag', action='append', default=[])
Expand All @@ -46,7 +48,7 @@ def main() -> None:
parser.add_argument('argv', nargs='+')
opts = parser.parse_args()
orig_env = dict(os.environ)
run_files = runfiles.Runfiles()
run_files = runfiles.Runfiles(dict(opts.runfiles_env))
emacs = run_files.resolve(opts.wrapper)
args = [opts.argv[0]]
with manifest.add(opts.mode, args) as manifest_file:
Expand Down Expand Up @@ -103,5 +105,9 @@ def _arg_files(argv: Sequence[str], root: pathlib.Path,
result.append(file)
return tuple(result)

def _env_var(arg: str) -> Tuple[str, str]:
key, _, value = arg.partition('=')
return key, value

if __name__ == '__main__':
main()
10 changes: 8 additions & 2 deletions elisp/run_emacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@
import subprocess
import sys
import tempfile
from typing import Iterable
from typing import Iterable, Tuple

from phst_rules_elisp.elisp import runfiles

def main() -> None:
"""Main function."""
parser = argparse.ArgumentParser(allow_abbrev=False)
parser.add_argument('--runfiles_env', action='append', type=_env_var,
default=[])
parser.add_argument('--install', type=pathlib.PurePosixPath, required=True)
parser.add_argument('--archive', type=pathlib.PurePosixPath, required=True)
parser.add_argument('--dump-mode', choices=('portable', 'unexec'),
required=True)
parser.add_argument('argv', nargs='+')
opts = parser.parse_args()
run_files = runfiles.Runfiles()
run_files = runfiles.Runfiles(dict(opts.runfiles_env))
try:
install = run_files.resolve(opts.install)
remove = False
Expand Down Expand Up @@ -99,5 +101,9 @@ def _check_codepage(description: str, values: Iterable[str]) -> None:
raise ValueError(
f'can’t encode {description}{value}” for Windows') from ex

def _env_var(arg: str) -> Tuple[str, str]:
key, _, value = arg.partition('=')
return key, value

if __name__ == '__main__':
main()
10 changes: 8 additions & 2 deletions elisp/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import pathlib
import subprocess
import sys
from typing import List
from typing import List, Tuple
import urllib.parse
import warnings

Expand All @@ -34,6 +34,8 @@
def main() -> None:
"""Main function."""
parser = argparse.ArgumentParser(allow_abbrev=False)
parser.add_argument('--runfiles_env', action='append', type=_env_var,
default=[])
parser.add_argument('--wrapper', type=pathlib.PurePosixPath, required=True)
parser.add_argument('--mode', choices=('direct', 'wrap'), required=True)
parser.add_argument('--rule-tag', action='append', default=[])
Expand All @@ -48,7 +50,7 @@ def main() -> None:
parser.add_argument('argv', nargs='+')
opts = parser.parse_args()
orig_env = dict(os.environ)
run_files = runfiles.Runfiles()
run_files = runfiles.Runfiles(dict(opts.runfiles_env))
emacs = run_files.resolve(opts.wrapper)
args = [opts.argv[0]]
with manifest.add(opts.mode, args) as manifest_file:
Expand Down Expand Up @@ -124,6 +126,10 @@ def _fix_coverage_manifest(manifest_file: pathlib.Path,
for file in files:
stream.write(file + '\n')

def _env_var(arg: str) -> Tuple[str, str]:
key, _, value = arg.partition('=')
return key, value

_WINDOWS = os.name == 'nt'

if __name__ == '__main__':
Expand Down

0 comments on commit ca3ba21

Please sign in to comment.