Skip to content

Commit

Permalink
Retrieve RunTracker args from the OptionsBootstrapper. (#11931)
Browse files Browse the repository at this point in the history
Since #11641, `sys.argv` has not been the right way to get the args for a particular run of pants.

Explicitly pass in the args via the `OptionsBootstrapper`, and fix the test (which was actually testing its _own_ `sys.argv`).

Fixes #11930.

[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
stuhood authored Apr 16, 2021
1 parent e251883 commit c62d05d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/python/pants/bin/local_pants_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/python/pants/engine/internals/engine_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/python/pants/goal/run_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
"""
Expand All @@ -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] = {}
Expand Down Expand Up @@ -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(
{
Expand Down
24 changes: 13 additions & 11 deletions src/python/pants/goal/run_tracker_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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()
Expand All @@ -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))
Expand All @@ -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)
Expand Down Expand Up @@ -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 = ""
Expand Down

0 comments on commit c62d05d

Please sign in to comment.