Repository: pantsbuild/pants

Issue:
RunTracker regressions -cmd_line doesn't show full command line
[cmd_line](https://github.com/pantsbuild/pants/blob/main/src/python/pants/goal/run_tracker.py#L91https://github.com/pantsbuild/pants/blob/main/src/python/pants/goal/run_tracker.py#L91) no longer captures the actual command line pants was executed with.

@stuhood 


Discussion:

Resolving patch:
diff --git a/src/python/pants/bin/local_pants_runner.py b/src/python/pants/bin/local_pants_runner.py
--- a/src/python/pants/bin/local_pants_runner.py
+++ b/src/python/pants/bin/local_pants_runner.py
@@ -115,7 +115,7 @@ def create(
             options_bootstrapper, env, raise_=True
         )
 
-        run_tracker = RunTracker(options)
+        run_tracker = RunTracker(options_bootstrapper.args, options)
         union_membership = UnionMembership.from_rules(build_config.union_rules)
 
         # If we're running with the daemon, we'll be handed a warmed Scheduler, which we use
diff --git a/src/python/pants/goal/run_tracker.py b/src/python/pants/goal/run_tracker.py
--- a/src/python/pants/goal/run_tracker.py
+++ b/src/python/pants/goal/run_tracker.py
@@ -8,13 +8,12 @@
 import os
 import platform
 import socket
-import sys
 import time
 import uuid
 from collections import OrderedDict
 from hashlib import sha256
 from pathlib import Path
-from typing import Any, Dict, List, Optional
+from typing import Any, Dict, List, Optional, Tuple
 
 from pants.base.build_environment import get_buildroot
 from pants.base.exiter import PANTS_SUCCEEDED_EXIT_CODE, ExitCode
@@ -45,7 +44,7 @@ def default(self, o):
 class RunTracker:
     """Tracks and times the execution of a single Pants run."""
 
-    def __init__(self, options: Options):
+    def __init__(self, args: Tuple[str, ...], options: Options):
         """
         :API: public
         """
@@ -59,6 +58,7 @@ def __init__(self, options: Options):
         millis = int((run_timestamp * 1000) % 1000)
         self.run_id = f"pants_run_{str_time}_{millis}_{run_uuid}"
 
+        self._args = args
         self._all_options = options
         info_dir = os.path.join(self._all_options.for_global_scope().pants_workdir, "run-tracker")
         self._run_info: Dict[str, Any] = {}
@@ -88,7 +88,7 @@ def start(self, run_start_time: float, specs: List[str]) -> None:
         self._run_start_time = run_start_time
 
         datetime = time.strftime("%A %b %d, %Y %H:%M:%S", time.localtime(run_start_time))
-        cmd_line = " ".join(["pants"] + sys.argv[1:])
+        cmd_line = " ".join(("pants",) + self._args[1:])
 
         self._run_info.update(
             {


Test patch:
diff --git a/src/python/pants/engine/internals/engine_test.py b/src/python/pants/engine/internals/engine_test.py
--- a/src/python/pants/engine/internals/engine_test.py
+++ b/src/python/pants/engine/internals/engine_test.py
@@ -291,7 +291,8 @@ def __call__(self, **kwargs) -> None:
 def new_run_tracker() -> RunTracker:
     # NB: A RunTracker usually observes "all options" (`full_options_for_scopes`), but it only
     # actually directly consumes bootstrap options.
-    return RunTracker(create_options_bootstrapper([]).bootstrap_options)
+    ob = create_options_bootstrapper([])
+    return RunTracker(ob.args, ob.bootstrap_options)
 
 
 @pytest.fixture
diff --git a/src/python/pants/goal/run_tracker_test.py b/src/python/pants/goal/run_tracker_test.py
--- a/src/python/pants/goal/run_tracker_test.py
+++ b/src/python/pants/goal/run_tracker_test.py
@@ -21,7 +21,8 @@
 def test_run_tracker_timing_output(**kwargs) -> None:
     with temporary_dir() as buildroot:
         with environment_as(PANTS_BUILDROOT_OVERRIDE=buildroot):
-            run_tracker = RunTracker(create_options_bootstrapper([]).bootstrap_options)
+            ob = create_options_bootstrapper([])
+            run_tracker = RunTracker(ob.args, ob.bootstrap_options)
             run_tracker.start(run_start_time=time.time(), specs=["::"])
             frozen_time = kwargs["frozen_time"]
             frozen_time.tick(delta=datetime.timedelta(seconds=1))
@@ -40,9 +41,11 @@ def test_run_tracker_timing_output(**kwargs) -> None:
 def test_run_information(exit_code, expected, **kwargs) -> None:
     with temporary_dir() as buildroot:
         with environment_as(PANTS_BUILDROOT_OVERRIDE=buildroot):
-            run_tracker = RunTracker(create_options_bootstrapper([]).bootstrap_options)
+            spec = "test/example.py"
+            ob = create_options_bootstrapper(["list", spec])
+            run_tracker = RunTracker(ob.args, ob.bootstrap_options)
 
-            specs = ["src/python/pants/goal/run_tracker_test.py"]
+            specs = [spec]
             run_tracker.start(run_start_time=time.time(), specs=specs)
 
             run_information = run_tracker.run_information()
@@ -58,10 +61,8 @@ def test_run_information(exit_code, expected, **kwargs) -> None:
             assert run_information["timestamp"] == 1578657601.0
             assert run_information["user"] == getpass.getuser()
             assert run_information["version"] == VERSION
-            assert re.match("pants.*run_tracker_test.py", run_information["cmd_line"])
-            assert run_information["specs_from_command_line"] == [
-                "src/python/pants/goal/run_tracker_test.py"
-            ]
+            assert re.match(f"pants.*{spec}", run_information["cmd_line"])
+            assert run_information["specs_from_command_line"] == [spec]
 
             frozen_time = kwargs["frozen_time"]
             frozen_time.tick(delta=datetime.timedelta(seconds=1))
@@ -75,9 +76,10 @@ def test_run_information(exit_code, expected, **kwargs) -> None:
 def test_anonymous_telemetry(monkeypatch, **kwargs) -> None:
     with temporary_dir() as buildroot:
         with environment_as(PANTS_BUILDROOT_OVERRIDE=buildroot):
-            opts = create_options_bootstrapper([]).bootstrap_options
+            ob = create_options_bootstrapper([])
+            opts = ob.bootstrap_options
             monkeypatch.setattr(opts, "_goals", ["test", "customgoal", "lint"])
-            run_tracker = RunTracker(opts)
+            run_tracker = RunTracker(ob.args, opts)
             run_tracker.start(run_start_time=time.time(), specs=[])
             kwargs["frozen_time"].tick(delta=datetime.timedelta(seconds=1))
             run_tracker.end_run(PANTS_SUCCEEDED_EXIT_CODE)
@@ -113,8 +115,8 @@ def test_anonymous_telemetry(monkeypatch, **kwargs) -> None:
 def test_anonymous_telemetry_with_no_repo_id() -> None:
     with temporary_dir() as buildroot:
         with environment_as(PANTS_BUILDROOT_OVERRIDE=buildroot):
-            opts = create_options_bootstrapper([]).bootstrap_options
-            run_tracker = RunTracker(opts)
+            ob = create_options_bootstrapper([])
+            run_tracker = RunTracker(ob.args, ob.bootstrap_options)
             run_tracker.start(run_start_time=time.time(), specs=[])
             run_tracker.end_run(PANTS_SUCCEEDED_EXIT_CODE)
             repo_id = ""