Skip to content

Commit

Permalink
rustc_action: always use LF line-endings
Browse files Browse the repository at this point in the history
Summary: CRLF is a pain. Avoid it.

Reviewed By: shayne-fletcher

Differential Revision: D47553486

fbshipit-source-id: e137526acb72b429f5dd29ff31f30431363783d5
  • Loading branch information
zertosh authored and facebook-github-bot committed Jul 18, 2023
1 parent 2c16b6f commit ed1e136
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions prelude/rust/tools/rustc_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@
import sys
import tempfile
from pathlib import Path
from typing import Dict, IO, List, NamedTuple, Optional, Tuple
from typing import Any, Dict, IO, List, NamedTuple, Optional, Tuple

DEBUG = False


def eprint(*args: Any, **kwargs: Any) -> None:
print(*args, end="\n", file=sys.stderr, flush=True, **kwargs)


if sys.version_info[:2] < (3, 7):
print("Python 3.7 or newer is required!", file=sys.stderr)
print(
"Using {} from {}".format(platform.python_version(), sys.executable),
file=sys.stderr,
)
eprint("Python 3.7 or newer is required!")
eprint("Using {} from {}".format(platform.python_version(), sys.executable))
sys.exit(1)


Expand All @@ -47,14 +49,14 @@ def key_value_arg(s: str) -> Tuple[str, str]:


class Args(NamedTuple):
diag_json: Optional[IO[str]]
diag_txt: Optional[IO[str]]
diag_json: Optional[IO[bytes]]
diag_txt: Optional[IO[bytes]]
env: Optional[List[Tuple[str, str]]]
path_env: Optional[List[Tuple[str, str]]]
remap_cwd_prefix: Optional[str]
crate_map: Optional[List[Tuple[str, str]]]
buck_target: Optional[str]
failure_filter: Optional[IO[str]]
failure_filter: Optional[IO[bytes]]
required_output: Optional[List[Tuple[str, str]]]
rustc: List[str]

Expand All @@ -64,13 +66,13 @@ def arg_parse() -> Args:
parser = argparse.ArgumentParser(fromfile_prefix_chars="@")
parser.add_argument(
"--diag-json",
type=argparse.FileType("w"),
type=argparse.FileType("wb"),
help="Json-formatted diagnostic output "
"(assumes compiler is invoked with --error-format=json)",
)
parser.add_argument(
"--diag-txt",
type=argparse.FileType("w"),
type=argparse.FileType("wb"),
help="Rendered text diagnostic output (also streamed to stderr)",
)
parser.add_argument(
Expand Down Expand Up @@ -104,7 +106,7 @@ def arg_parse() -> Args:
)
parser.add_argument(
"--failure-filter",
type=argparse.FileType(mode="w"),
type=argparse.FileType("wb"),
help="Consider a failure as success so long as we got some usable diagnostics",
metavar="build-status.json",
)
Expand Down Expand Up @@ -149,7 +151,7 @@ async def handle_output( # noqa: C901
continue

if DEBUG:
print(f"diag={repr(diag)}")
print(f"diag={repr(diag)}", end="\n")

if diag.get("level") == "error":
got_error_diag = True
Expand Down Expand Up @@ -182,14 +184,16 @@ async def handle_output( # noqa: C901

# Emit json
if args.diag_json:
args.diag_json.write(json.dumps(diag, separators=(",", ":")) + "\n")
args.diag_json.write(
json.dumps(diag, separators=(",", ":")).encode() + b"\n"
)

# Emit rendered text version
if "rendered" in diag:
rendered = diag["rendered"] + "\n"
rendered = diag["rendered"].encode() + b"\n"
if args.diag_txt:
args.diag_txt.write(rendered)
sys.stderr.write(rendered)
sys.stderr.buffer.write(rendered)

if args.diag_json:
args.diag_json.close()
Expand Down Expand Up @@ -248,7 +252,7 @@ async def main() -> int:
crate_map = dict(args.crate_map) if args.crate_map else {}

if DEBUG:
print(f"args {repr(args)} env {env} crate_map {crate_map}")
print(f"args {repr(args)} env {env} crate_map {crate_map}", end="\n")

rustc_cmd = args.rustc[:1]
rustc_args = args.rustc[1:]
Expand Down Expand Up @@ -288,7 +292,8 @@ async def main() -> int:
print(
f"res={repr(res)} "
f"got_error_diag={got_error_diag} "
f"args.failure_filter {args.failure_filter}"
f"args.failure_filter {args.failure_filter}",
end="\n",
)

# If rustc is reporting a silent error, make it loud
Expand All @@ -298,10 +303,7 @@ async def main() -> int:
# Check for death by signal - this is always considered a failure
if res < 0:
cmdline = " ".join(shlex.quote(arg) for arg in args.rustc)
print(
f"Command exited with signal {-res}: command line: {cmdline}",
file=sys.stderr,
)
eprint(f"Command exited with signal {-res}: command line: {cmdline}")
elif args.failure_filter:
# If failure filtering is enabled, then getting an error diagnostic is also
# considered a success. That is, if rustc exited with an error status, but
Expand All @@ -320,7 +322,9 @@ async def main() -> int:
"status": res,
"files": [short for short, path in required_output if Path(path).exists()],
}
json.dump(build_status, args.failure_filter)
args.failure_filter.write(
json.dumps(build_status, separators=(",", ":")).encode() + b"\n"
)

# OK to actually report success, but keep buck happy by making sure all
# the required outputs are present
Expand Down

0 comments on commit ed1e136

Please sign in to comment.