forked from facebook/buck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuck_tool.py
More file actions
1297 lines (1098 loc) · 49.9 KB
/
buck_tool.py
File metadata and controls
1297 lines (1098 loc) · 49.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) Facebook, Inc. and its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
import contextlib
import errno
import glob
import json
import logging
import os
import platform
import shlex
import signal
import subprocess
import sys
import tempfile
import textwrap
import time
import traceback
import uuid
from collections import namedtuple
from subprocess import CalledProcessError, check_output
from ng import NailgunConnection, NailgunException
from programs.file_locks import exclusive_lock
from programs.subprocutils import which
from programs.timing import monotonic_time_nanos
from programs.tracing import Tracing
BUCKD_CLIENT_TIMEOUT_MILLIS = 180000
BUCKD_STARTUP_TIMEOUT_MILLIS = 10000
GC_MAX_PAUSE_TARGET = 15000
JAVA_MAX_HEAP_SIZE_MB = 1000
# Files that are used to invoke programs after running buck. Used for buck run, and
# to automatically invoke buck fix scripts
PostRunFiles = namedtuple("PostRunFiles", ["command_args_file"])
class MovableTemporaryFile(object):
def __init__(self, *args, **kwargs):
self.file = None
self._temp_file = None
self._should_delete = True
self._named_temporary_args = args
self._named_temporary_kwargs = kwargs
@property
def name(self):
if self.file:
return self.file.name
else:
raise AttributeError(
"`file` is not set. You cannot call name on a moved file"
)
def close(self):
if self.file:
self.file.close()
def move(self):
new_wrapper = MovableTemporaryFile(
self._named_temporary_args, self._named_temporary_kwargs
)
new_wrapper.file = self.file
new_wrapper._temp_file = self._temp_file
self.file = None
self._temp_file = None
return new_wrapper
def __enter__(self):
if not self._temp_file:
# Due to a bug in python (https://bugs.python.org/issue14243), we need to
# be able to close() the temporary file without deleting it. So, set
# delete=False, and cleanup manually on __exit__() rather than using
# NamedTemproaryFile's built in delete logic
self._temp_file = tempfile.NamedTemporaryFile(
*self._named_temporary_args,
delete=False,
**self._named_temporary_kwargs
)
self.file = self._temp_file.__enter__()
return self
def __exit__(self, exc_type, exc_value, traceback):
if self._temp_file:
self._temp_file.__exit__(exc_type, exc_value, traceback)
if self.file:
try:
os.remove(self.file.name)
except OSError as e:
# It's possible this fails because of a race with another buck
# instance has removed the entire resource_path, so ignore
# 'file not found' errors.
if e.errno != errno.ENOENT:
raise
class Resource(object):
"""Describes a resource used by this driver.
:ivar name: logical name of the resources
:ivar executable: whether the resource should/needs execute permissions
:ivar basename: required basename of the resource
"""
def __init__(self, name, executable=False, basename=None):
self.name = name
self.executable = executable
self.basename = name if basename is None else basename
# Resource that get propagated to buck via system properties.
EXPORTED_RESOURCES = [
Resource("legacy_fix_script", executable=True),
Resource("testrunner_classes"),
Resource("logging_config_file"),
Resource("path_to_python_dsl"),
Resource("path_to_pathlib_py", basename="pathlib.py"),
Resource("path_to_pywatchman"),
Resource("path_to_typing"),
Resource("path_to_six_py", basename="six.py"),
Resource("path_to_sh_binary_template"),
Resource("path_to_isolated_trampoline"),
Resource("jacoco_agent_jar"),
Resource("report_generator_jar"),
Resource("dx"),
Resource("android_agent_path"),
Resource("buck_build_type_info"),
Resource("native_exopackage_fake_path"),
]
class CommandLineArgs:
def __init__(self, cmdline):
self.args = cmdline[1:]
self.buck_options = []
self.command = None
self.command_options = []
for arg in self.args:
if arg == "--":
break
elif self.command is not None:
self.command_options.append(arg)
elif (arg[:1]) == "-":
self.buck_options.append(arg)
else:
self.command = arg
# Whether this is a help command that doesn't run a build
# n.b. 'buck --help clean' is *not* currently a help command
# n.b. 'buck --version' *is* a help command
def is_help(self):
# --help or -h can be anywhere except after ""--"
return (
self.command is None
or any(v in self.command_options for v in ["--help", "-h"])
or any(v in self.buck_options for v in ["--help", "-h"])
)
def is_version(self):
return self.command is None and any(
v in self.buck_options for v in ["--version", "-V"]
)
class ExitCode(object):
"""Python equivalent of com.facebook.buck.util.ExitCode"""
SUCCESS = 0
COMMANDLINE_ERROR = 3
FATAL_GENERIC = 10
FATAL_BOOTSTRAP = 11
FATAL_IO = 13
FATAL_DISK_FULL = 14
FIX_FAILED = 16
SIGNAL_INTERRUPT = 130
SIGNAL_PIPE = 141
class BuckToolException(Exception):
pass
class BuckDaemonErrorException(BuckToolException):
pass
class ExitCodeCallable(object):
""" Simple callable class that just returns a given exit code """
def __init__(self, exit_code):
self.exit_code = exit_code
def __call__(self):
return self.exit_code
class ExecuteTarget(ExitCodeCallable):
""" Callable that executes a given target after the build finishes """
def __init__(self, exit_code, path, argv, envp, cwd):
super(ExecuteTarget, self).__init__(exit_code)
self._path = path
self._argv = argv
self._envp = envp
self._cwd = cwd
def __call__(self):
if self.exit_code != ExitCode.SUCCESS:
return self.exit_code
# Restore default handling of SIGPIPE. See https://bugs.python.org/issue1652.
if os.name != "nt":
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
os.execvpe(self._path, self._argv, self._envp)
else:
args = [self._path]
args.extend(self._argv[1:])
child = subprocess.Popen(args, env=self._envp, cwd=self._cwd)
child.wait()
sys.exit(child.returncode)
class ExecuteFixScript(ExitCodeCallable):
"""
Callable that tries to execute a "fix" script, returning a different error
if the fix script fails
"""
def __init__(self, exit_code, path, argv, envp, cwd):
super(ExecuteFixScript, self).__init__(exit_code)
self._path = path
self._argv = argv
self._envp = envp
self._cwd = cwd
@contextlib.contextmanager
def _disable_signal_handlers(self):
if os.name == "nt":
yield
else:
original_signal = signal.getsignal(signal.SIGINT)
try:
signal.signal(signal.SIGINT, signal.SIG_IGN)
yield
finally:
signal.signal(signal.SIGINT, original_signal)
def __call__(self):
exit_code = self.exit_code
extra_kwargs = {}
if os.name != "nt":
# Restore default handling of SIGPIPE. See https://bugs.python.org/issue1652.
extra_kwargs["preexec_fn"] = lambda: signal.signal(
signal.SIGPIPE, signal.SIG_DFL
)
# Do not respond to ctrl-c while this is running. Users will expect this to go
# to the fix script. Make sure we restore the signal handler afterward
# We could have done this with tcsetpgrp and the like, but it also leads to
# weird behavior with ctrl-z when the processes are in different pgroups
# (to set the foreground job) and both processes don't suspend.
with self._disable_signal_handlers():
args = [self._path]
args.extend(self._argv[1:])
proc = subprocess.Popen(args, env=self._envp, cwd=self._cwd, **extra_kwargs)
proc.wait()
# If the script failed, return a different code to indicate that
if proc.returncode != ExitCode.SUCCESS:
exit_code = ExitCode.FIX_FAILED
return exit_code
class BuckStatusReporter(object):
""" Add custom logic to log Buck completion statuses or errors including
critical ones like JVM crashes and OOMs. This object is fully mutable with
all fields optional which get populated on the go. Only safe operations
are allowed in the reporter except for report() function which can throw
"""
def __init__(self, argv):
self.argv = argv
self.build_id = None
self.buck_version = None
self.is_buckd = False
self.no_buckd_reason = None
self.status_message = None
self.repository = None
self.start_time = time.time()
def report(self, exit_code):
""" Add custom code here to track Buck invocations """
pass
class JvmCrashLogger(object):
def __init__(self, buck_tool, project_root):
self._buck_tool = buck_tool
self._project_root = project_root
def __enter__(self):
self.jvm_errors_before = self._get_jvm_errors()
def __exit__(self, type, value, traceback):
new_errors = self._get_jvm_errors() - self.jvm_errors_before
if new_errors:
self._log_jvm_errors(new_errors)
def _get_jvm_errors(self):
return set(glob.glob(os.path.join(self._project_root, "hs_err_pid*.log")))
@staticmethod
def _format_jvm_errors(fp):
errors = []
keep = False
for line in fp:
if line.startswith("Stack:"):
keep = True
if line.startswith("--------------- P R O C E S S ---------------"):
break
if keep:
errors.append(line)
message = "JVM native crash: " + (errors[2] if len(errors) > 2 else "")
return message, "".join(errors)
def _log_jvm_errors(self, errors):
for file in errors:
with open(file, "r") as f:
message, loglines = self._format_jvm_errors(f)
logging.error(loglines)
class BuckTool(object):
def __init__(self, buck_project, buck_reporter):
self._reporter = buck_reporter
self._package_info = self._get_package_info()
self._init_timestamp = int(round(time.time() * 1000))
self._command_line = CommandLineArgs(sys.argv)
self._buck_project = buck_project
self._tmp_dir = platform_path(buck_project.tmp_dir)
self._fake_buck_version = os.environ.get("BUCK_FAKE_VERSION")
if self._fake_buck_version:
logging.info("Using fake buck version: {}".format(self._fake_buck_version))
self._pathsep = os.pathsep
if sys.platform == "cygwin":
self._pathsep = ";"
def _get_package_info(self):
raise NotImplementedError()
def _has_resource(self, resource):
"""Check whether the given resource exists."""
raise NotImplementedError()
def _get_resource(self, resource):
"""Return an on-disk path to the given resource.
This may cause implementations to unpack the resource at this point.
"""
raise NotImplementedError()
def _get_resource_lock_path(self):
"""Return the path to the file used to determine if another buck process is using the same
resources folder (used for cleanup coordination).
"""
raise NotImplementedError()
def _get_buck_version_uid(self):
if self._fake_buck_version:
return self._fake_buck_version
return self._package_info["version"]
def _get_buck_version_timestamp(self):
return self._package_info["timestamp"]
def _get_buck_git_commit(self):
raise NotImplementedError()
def _get_buck_repo_dirty(self):
return self._package_info["is_dirty"]
def get_buck_compiled_java_version(self):
return self._package_info["java_version"]
def _get_bootstrap_classpath(self):
raise NotImplementedError()
def _get_buckfilesystem_classpath(self):
raise NotImplementedError()
def _get_java_classpath(self):
raise NotImplementedError()
def _get_buck_binary_hash(self):
"""
Returns Buck binary hash
This hash reflects the code that can affect the content of artifacts.
"""
raise NotImplementedError()
def _unpack_modules(self):
raise NotImplementedError()
def _get_extra_java_args(self):
return []
def _get_exported_resources(self):
return EXPORTED_RESOURCES
@property
def _use_buckd(self):
return not os.environ.get("NO_BUCKD") or os.environ.get("NO_BUCKD") == "0"
def _environ_for_buck(self):
env = os.environ.copy()
classpath = str(self._get_bootstrap_classpath())
# On Java 9 and higher, the BuckFileSystem jar gets loaded via the bootclasspath and doesn't
# need to be added here.
if self.get_buck_compiled_java_version() < 9:
classpath += os.pathsep + str(self._get_buckfilesystem_classpath())
env["CLASSPATH"] = classpath
env["BUCK_CLASSPATH"] = str(self._get_java_classpath())
env["BUCK_TTY"] = str(int(sys.stdin.isatty()))
if os.name == "posix":
# It will fallback on default on Windows platform.
try:
# Get the number of columns in the terminal.
with open(os.devnull, "w") as devnull:
# Make sure we get a string back in py3, but encoding isn't
# valid in py2, so don't send in that case
stty_size_str = (
check_output(["stty", "size"], stderr=devnull)
.decode("utf-8")
.strip()
)
stty_size = stty_size_str.split(" ")
if len(stty_size) >= 2:
env["BUCK_TERM_COLUMNS"] = stty_size[1]
except CalledProcessError:
# If the call to tput fails, we use the default.
pass
return env
def _handle_isolation_args(self, args):
try:
pos = args.index("--isolation_prefix")
# Allow for the argument to --isolation_prefix
if (pos + 1) < len(args):
new_args = args[:pos] + args[pos + 2 :]
new_args.append("--config")
new_args.append(
"buck.base_buck_out_dir={0}".format(
self._buck_project.get_buck_out_relative_dir()
)
)
return new_args
else:
return args # buck will error out on unrecognized option
except ValueError:
return args
def _handle_buck_fix_args(self, argv, post_run_files):
additional_args = ["--command-args-file", post_run_files.command_args_file]
insert_idx = 1
# Cover the case where someone runs `buck --isolation_prefix foo`, which would
# make the first arg `--config`, not a subcommand
if len(argv) and argv[0].startswith("--"):
insert_idx = 0
ret = argv[:insert_idx] + additional_args + argv[insert_idx:]
return ret
def _add_args(self, argv, args, post_run_files):
self._adjust_help_args(argv, args)
"""
Add new arguments to the end of arguments string
But before optional arguments to test runner ("--")
"""
if len(args) == 0:
return self._handle_buck_fix_args(
self._handle_isolation_args(argv), post_run_files
)
try:
pos = argv.index("--")
except ValueError:
# "--" not found, just add to the end of the list
pos = len(argv)
return self._handle_buck_fix_args(
self._handle_isolation_args(argv[:pos] + args + argv[pos:]), post_run_files
)
def _adjust_help_args(self, argv, args):
# allow --help right after "buck" then followed by subcommand, in order for java code
# to process this as help, move --help to after subcommand
if len(argv) and argv[0] == "--help":
del argv[0]
args.append("--help")
else:
# '-h' need to be replaced by '--help' for java code to process as help
try:
pos_h = argv.index("-h")
except ValueError:
pos_h = -1
if pos_h >= 0:
try:
# don't do the replacement if '-h' is after '--', it's for external runner
pos_ex = argv.index("--")
except ValueError:
pos_ex = -1
if pos_ex < 0 or pos_h < pos_ex:
del argv[pos_h]
args.append("--help")
def _add_args_from_env(self, argv, post_run_files):
"""
Implicitly add command line arguments based on environmental variables. This is a bad
practice and should be considered for infrastructure / debugging purposes only
"""
args = []
if os.environ.get("BUCK_NO_CACHE") == "1" and "--no-cache" not in argv:
args.append("--no-cache")
if os.environ.get("BUCK_CACHE_READONLY") == "1":
args.append("-c")
args.append("cache.http_mode=readonly")
return self._add_args(argv, args, post_run_files)
def _run_with_nailgun(self, java_path, argv, env, post_run_files):
"""
Run the command using nailgun. If the daemon is busy, block until it becomes free.
"""
exit_code = 2
busy_diagnostic_displayed = False
while exit_code == 2:
try:
with NailgunConnection(
self._buck_project.get_buckd_transport_address(),
cwd=self._buck_project.root,
) as c:
now = int(round(time.time() * 1000))
env["BUCK_PYTHON_SPACE_INIT_TIME"] = str(now - self._init_timestamp)
exit_code = c.send_command(
"com.facebook.buck.cli.MainWithNailgun",
self._add_args_from_env(argv, post_run_files),
env=env,
cwd=self._buck_project.root,
)
if exit_code == 2:
env["BUCK_BUILD_ID"] = str(uuid.uuid4())
if busy_diagnostic_displayed:
sys.stderr.write(".")
sys.stderr.flush()
else:
logging.info(
"You can use 'buck kill' to kill buck "
+ "if you suspect buck is stuck."
)
busy_diagnostic_displayed = True
env["BUCK_BUSY_DISPLAYED"] = "1"
sys.stderr.write("Waiting for Buck Daemon to become free")
sys.stderr.flush()
time.sleep(3)
except NailgunException as nex:
if nex.code == NailgunException.CONNECTION_BROKEN:
message = (
"Connection is lost to Buck daemon! This usually indicates that"
+ " daemon experienced an unrecoverable error. Here is what you can do:\n"
+ " - check if the machine has enough disk space and filesystem is"
+ " accessible\n"
+ " - check if the machine does not run out of physical memory\n"
+ " - try to run Buck in serverless mode:"
+ " buck kill && NO_BUCKD=1 buck <command>\n"
)
transport_address = self._buck_project.get_buckd_transport_address()
if not transport_address.startswith("local:"):
message += (
" - check if connection specified by "
+ transport_address
+ " is stable\n"
)
raise BuckDaemonErrorException(message)
else:
raise nex
return exit_code
def _run_without_nailgun(self, java_path, argv, env, post_run_files):
"""
Run the command by directly invoking `java` (rather than by sending a command via nailgun)
"""
command = ["buck"]
extra_default_options = [
"-Dbuck.is_buckd=false",
"-Djava.io.tmpdir={0}".format(self._tmp_dir),
"-Dfile.encoding=UTF-8",
"-XX:SoftRefLRUPolicyMSPerMB=0",
"-XX:+UseG1GC",
]
command.extend(
self._get_java_args(self._get_buck_version_uid(), extra_default_options)
)
command.append("com.facebook.buck.cli.bootstrapper.ClassLoaderBootstrapper")
command.append("com.facebook.buck.cli.MainWithoutNailgun")
command.extend(self._add_args_from_env(argv, post_run_files))
now = int(round(time.time() * 1000))
env["BUCK_PYTHON_SPACE_INIT_TIME"] = str(now - self._init_timestamp)
with Tracing("buck", args={"command": command}):
return subprocess.call(
command, cwd=self._buck_project.root, env=env, executable=java_path
)
def _execute_command_and_maybe_run_target(self, run_fn, java_path, env, argv):
"""
Run a buck command using the specified `run_fn`.
If the command writes out to --command-args-file, return a callable that
executes the program in the --command-args-file
"""
with Tracing("buck", args={"command": sys.argv[1:]}):
if os.name == "nt":
# Windows now support the VT100 sequences that are used by the Superconsole, but our
# process must opt into this behavior to enable it.
import ctypes
kernel32 = ctypes.windll.kernel32
STD_OUTPUT_HANDLE = -11
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
# These calls can fail because we're not output to a terminal or we're not on a new
# enough version of Windows. That's fine because we fail silently, and the
# Superconsole wasn't going to work anyways.
handle = kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
console_mode = ctypes.c_uint(0)
if kernel32.GetConsoleMode(handle, ctypes.byref(console_mode)):
kernel32.SetConsoleMode(
handle, console_mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING
)
argv = argv[1:]
# The argsfile can go in the project tmpdir as we read it back immediately,
# the fix spec file needs to be somewhere else, as we cleanup the tmpdir
# before we run the actual fix script, and we need to make sure that the
# build details are not deleted before that.
with MovableTemporaryFile(dir=self._tmp_dir) as argsfile:
argsfile.close()
post_run_files = PostRunFiles(command_args_file=argsfile.name)
exit_code = run_fn(java_path, argv, env, post_run_files)
if os.path.getsize(argsfile.name) == 0:
# No file was requested to be run by the daemon. Exit normally.
return ExitCodeCallable(exit_code)
with open(argsfile.name, "r") as reopened_argsfile:
cmd = json.load(reopened_argsfile)
path = cmd["path"].encode("utf8")
argv = [arg.encode("utf8") for arg in cmd["argv"]]
envp = {
k.encode("utf8"): v.encode("utf8") for k, v in cmd["envp"].items()
}
cwd = cmd["cwd"].encode("utf8")
if cmd["is_fix_script"]:
return ExecuteFixScript(exit_code, path, argv, envp, cwd)
else:
return ExecuteTarget(exit_code, path, argv, envp, cwd)
def launch_buck(self, build_id, client_cwd, java_path, argv):
with Tracing("BuckTool.launch_buck"):
with JvmCrashLogger(self, self._buck_project.root):
self._reporter.build_id = build_id
try:
repository = self._get_repository()
self._reporter.repository = repository
except Exception as e:
# _get_repository() is only for reporting,
# so skip on error
logging.warning("Failed to get repo name: " + str(e))
if (
self._command_line.command == "clean"
and not self._command_line.is_help()
):
self.kill_buckd()
buck_version_uid = self._get_buck_version_uid()
self._reporter.buck_version = buck_version_uid
if self._command_line.is_version():
print("buck version {}".format(buck_version_uid))
return ExitCodeCallable(ExitCode.SUCCESS)
use_buckd = self._use_buckd
if not use_buckd:
logging.warning("Not using buckd because NO_BUCKD is set.")
self._reporter.no_buckd_reason = "explicit"
if use_buckd and self._command_line.is_help():
use_buckd = False
self._reporter.no_buckd_reason = "help"
if use_buckd:
has_watchman = bool(which("watchman"))
if not has_watchman:
use_buckd = False
self._reporter.no_buckd_reason = "watchman"
logging.warning(
"Not using buckd because watchman isn't installed."
)
if use_buckd:
with exclusive_lock(
self._buck_project.get_section_lock_path("buckd_start_stop"),
wait=True,
):
need_start = True
running_version = self._buck_project.get_running_buckd_version()
running_jvm_args = (
self._buck_project.get_running_buckd_jvm_args()
)
jvm_args = self._get_java_args(buck_version_uid)
if running_version is None:
logging.info("Starting new Buck daemon...")
elif running_version != buck_version_uid:
logging.info(
"Restarting Buck daemon because Buck version has "
"changed..."
)
elif not self._is_buckd_running():
logging.info(
"Unable to connect to Buck daemon, restarting it..."
)
elif jvm_args != running_jvm_args:
logging.info(
"Restarting Buck daemon because JVM args have "
"changed..."
)
else:
need_start = False
if need_start:
self.kill_buckd()
if not self.launch_buckd(
java_path, jvm_args, buck_version_uid=buck_version_uid
):
use_buckd = False
self._reporter.no_buckd_reason = "daemon_failure"
logging.warning(
"Not using buckd because daemon failed to start."
)
env = self._environ_for_buck()
env["BUCK_BUILD_ID"] = build_id
env["BUCK_CLIENT_PWD"] = client_cwd
self._reporter.is_buckd = use_buckd
run_fn = (
self._run_with_nailgun if use_buckd else self._run_without_nailgun
)
self._unpack_modules()
exit_code_callable = self._execute_command_and_maybe_run_target(
run_fn, java_path, env, argv
)
# Most shells return process termination with signal as
# 128 + N, where N is the signal. However Python's subprocess
# call returns them as negative numbers. Buck binary protocol
# uses shell's convention, so convert
if exit_code_callable.exit_code < 0:
exit_code_callable.exit_code = 128 + (
-1 * exit_code_callable.exit_code
)
return exit_code_callable
def launch_buckd(self, java_path, jvm_args, buck_version_uid=None):
with Tracing("BuckTool.launch_buckd"):
setup_watchman_watch()
if buck_version_uid is None:
buck_version_uid = self._get_buck_version_uid()
self._buck_project.create_buckd_dir()
# Override self._tmp_dir to a long lived directory.
buckd_tmp_dir = self._buck_project.create_buckd_tmp_dir()
ngserver_output_path = os.path.join(buckd_tmp_dir, "ngserver-out")
"""
Use SoftRefLRUPolicyMSPerMB for immediate GC of javac output.
Set timeout to 60s (longer than the biggest GC pause seen for a 2GB
heap) and GC target to 15s. This means that the GC has to miss its
target by 100% or many 500ms heartbeats must be missed before a client
disconnection occurs. Specify port 0 to allow Nailgun to find an
available port, then parse the port number out of the first log entry.
"""
command = ["buckd"]
extra_default_options = [
"-Dbuck.is_buckd=true",
"-Dbuck.buckd_launch_time_nanos={0}".format(monotonic_time_nanos()),
"-Dfile.encoding=UTF-8",
"-XX:MaxGCPauseMillis={0}".format(GC_MAX_PAUSE_TARGET),
"-XX:SoftRefLRUPolicyMSPerMB=0",
# Stop Java waking up every 50ms to collect thread
# statistics; doing it once every five seconds is much
# saner for a long-lived daemon.
"-XX:PerfDataSamplingInterval=5000",
# Do not touch most signals
"-Xrs",
# Likewise, waking up once per second just in case
# there's some rebalancing to be done is silly.
"-XX:+UnlockDiagnosticVMOptions",
"-XX:GuaranteedSafepointInterval=5000",
"-Djava.io.tmpdir={0}".format(buckd_tmp_dir),
"-Dcom.facebook.nailgun.NGServer.outputPath={0}".format(
ngserver_output_path
),
"-XX:+UseG1GC",
"-XX:MaxHeapFreeRatio=40",
]
command.extend(extra_default_options)
command.extend(jvm_args)
command.append("com.facebook.buck.cli.bootstrapper.ClassLoaderBootstrapper")
command.append("com.facebook.buck.cli.BuckDaemon")
command.append(self._buck_project.get_buckd_transport_address())
command.append("{0}".format(BUCKD_CLIENT_TIMEOUT_MILLIS))
buckd_transport_file_path = (
self._buck_project.get_buckd_transport_file_path()
)
if os.name == "nt":
# https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863.aspx#DETACHED_PROCESS
DETACHED_PROCESS = 0x00000008
creationflags = DETACHED_PROCESS
# do not redirect output for Windows as it deadlocks
stdin = None
stdout = None
stderr = None
close_fds = True
else:
"""
Change the process group of the child buckd process so that when this
script is interrupted, it does not kill buckd.
"""
creationflags = 0
stdin = open(os.devnull, mode="r")
stdout = open(
self._buck_project.get_buckd_stdout(), mode="w+b", buffering=0
)
stderr = open(
self._buck_project.get_buckd_stderr(), mode="w+b", buffering=0
)
close_fds = False
process = subprocess.Popen(
command,
executable=java_path,
cwd=self._buck_project.root,
env=self._environ_for_buck(),
creationflags=creationflags,
close_fds=close_fds,
stdin=stdin,
stdout=stdout,
stderr=stderr,
)
self._buck_project.save_buckd_version(buck_version_uid)
self._buck_project.save_buckd_jvm_args(
self._get_java_args(buck_version_uid)
)
# Give Java some time to create the listening socket.
wait_seconds = 0.01
repetitions = int(BUCKD_STARTUP_TIMEOUT_MILLIS / 1000.0 / wait_seconds)
for _idx in range(repetitions):
if transport_exists(buckd_transport_file_path):
break
time.sleep(wait_seconds)
if not transport_exists(buckd_transport_file_path):
return False
returncode = process.poll()
# If the process has exited then daemon failed to start
if returncode is not None:
return False
# Save pid of running daemon
self._buck_project.save_buckd_pid(process.pid)
logging.info("Buck daemon started.")
return True
def _get_repository(self):
arcconfig = os.path.join(self._buck_project.root, ".arcconfig")
if os.path.isfile(arcconfig):
with open(arcconfig, "r") as fp:
return json.load(fp).get("project_id", None)
return os.path.basename(self._buck_project.root)
def kill_buckd(self):
with Tracing("BuckTool.kill_buckd"), exclusive_lock(
self._buck_project.get_section_lock_path("buckd_kill"), wait=True
):
buckd_transport_file_path = (
self._buck_project.get_buckd_transport_file_path()
)
wait_for_termination = False
if transport_exists(buckd_transport_file_path):
logging.debug("Shutting down buck daemon.")
try:
with NailgunConnection(
self._buck_project.get_buckd_transport_address(),
cwd=self._buck_project.root,
) as c:
c.send_command("ng-stop")
wait_for_termination = True
except NailgunException as e:
if e.code not in (
NailgunException.CONNECT_FAILED,
NailgunException.CONNECTION_BROKEN,
NailgunException.UNEXPECTED_CHUNKTYPE,
):
raise BuckToolException(
"Unexpected error shutting down nailgun server: " + str(e)
)
if os.name == "posix":
buckd_pid = self._buck_project.get_running_buckd_pid()
if pid_exists_posix(buckd_pid):
# If termination command was sent successfully then allow some time for the
# daemon to finish gracefully. Otherwise kill it hard.
if not wait_for_termination or not wait_for_process_posix(
buckd_pid, 5000
):
# There is a possibility that daemon is dead for some time but pid file
# still exists and another process is assigned to the same pid. Ideally we
# should check first which process we are killing but so far let's pretend
# this will never happen and kill it with fire anyways.
try:
force_kill_process_posix(buckd_pid)
except Exception as e:
# In the worst case it keeps running multiple daemons simultaneously
# consuming memory. Not the good place to be, but let's just issue a
# warning for now.
logging.warning(
"Error killing running Buck daemon " + str(e)
)
# it is ok to have a socket file still around, as linux domain sockets should be
# unlinked explicitly
if transport_exists(buckd_transport_file_path):
force_close_transport_posix(buckd_transport_file_path)
elif os.name == "nt":
# for Windows, we rely on transport to be closed to determine the process is done
# TODO(buck_team) implement wait for process and hard kill for Windows
for _idx in range(0, 300):
if not transport_exists(buckd_transport_file_path):
break
time.sleep(0.01)
self._buck_project.clean_up_buckd()