Skip to content

Commit

Permalink
Add setrlimit filter to coursier invocations (#14296)
Browse files Browse the repository at this point in the history
After a few attempts to close #13942, I have emerged from a giant pile of yaks and discovered that the solution was actually attached to a sheep.

Annoyingly, a large number of our coursier invocations are used to look at outputs from stderr, which makes using `grep` (as suggested as an easy fix in #13942) to filter out spurious warnings quite difficult. Instead, this change adds another wrapper in the form of a Python script, which preserves separation of stdout and stderr for all Coursier invocations, but removes all instances of the setrlimit warning from stderr before outputting it. 

I've also renamed `coursier_wrapper`* to `coursier_fetch_wrapper`* to indicate that only `coursier fetch` calls are wrapped; calls like `coursier java-home` continue to be called more directly.
  • Loading branch information
Christopher Neugebauer committed Jan 28, 2022
1 parent d53e08e commit c09289d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
8 changes: 1 addition & 7 deletions src/python/pants/jvm/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,7 @@ def prep_output(s: bytes) -> str:
return strip_v2_chroot_path(s) if strip_chroot_path else s.decode()

exit_code = process_result.exit_code
# TODO: Coursier renders this line on macOS.
# see https://github.com/pantsbuild/pants/issues/13942.
stderr = "\n".join(
line
for line in prep_output(process_result.stderr).splitlines()
if line != "setrlimit to increase file descriptor limit failed, errno 22"
)
stderr = prep_output(process_result.stderr)
return cls(
description=description,
result=(CompileResult.SUCCEEDED if exit_code == 0 else CompileResult.FAILED),
Expand Down
42 changes: 36 additions & 6 deletions src/python/pants/jvm/resolve/coursier_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"""
)

COURSIER_WRAPPER_SCRIPT = textwrap.dedent(
COURSIER_FETCH_WRAPPER_SCRIPT = textwrap.dedent(
"""\
set -eux
Expand All @@ -75,6 +75,23 @@
)


# TODO: Coursier renders setrlimit error line on macOS.
# see https://github.com/pantsbuild/pants/issues/13942.
POST_PROCESS_COURSIER_STDERR_SCRIPT = textwrap.dedent(
"""\
#!{python_path}
import sys
from subprocess import run, PIPE
proc = run(sys.argv[1:], stdout=PIPE, stderr=PIPE)
sys.stdout.buffer.write(proc.stdout)
sys.stderr.buffer.write(proc.stderr.replace(b"setrlimit to increase file descriptor limit failed, errno 22\\n", b""))
sys.exit(proc.returncode)
"""
)


class CoursierSubsystem(TemplatedExternalTool):
options_scope = "coursier"
name = "coursier"
Expand Down Expand Up @@ -125,14 +142,20 @@ class Coursier:
_digest: Digest

bin_dir: ClassVar[str] = "__coursier"
wrapper_script: ClassVar[str] = f"{bin_dir}/coursier_wrapper_script.sh"
fetch_wrapper_script: ClassVar[str] = f"{bin_dir}/coursier_fetch_wrapper_script.sh"
post_processing_script: ClassVar[str] = f"{bin_dir}/coursier_post_processing_script.py"
post_process_stderr: ClassVar[str] = f"{bin_dir}/coursier_post_process_stderr.py"
cache_name: ClassVar[str] = "coursier"
cache_dir: ClassVar[str] = ".cache"
working_directory_placeholder: ClassVar[str] = "___COURSIER_WORKING_DIRECTORY___"

def args(self, args: Iterable[str], *, wrapper: Iterable[str] = ()) -> tuple[str, ...]:
return tuple((*wrapper, os.path.join(self.bin_dir, self.coursier.exe), *args))
return (
self.post_process_stderr,
*wrapper,
os.path.join(self.bin_dir, self.coursier.exe),
*args,
)

@property
def env(self) -> dict[str, str]:
Expand Down Expand Up @@ -174,7 +197,7 @@ async def invoke_coursier_wrapper(
return Process(
argv=coursier.args(
request.args,
wrapper=[bash.path, coursier.wrapper_script],
wrapper=[bash.path, coursier.fetch_wrapper_script],
),
input_digest=request.input_digest,
immutable_input_digests=coursier.immutable_input_digests,
Expand All @@ -193,13 +216,15 @@ async def setup_coursier(
python: PythonBinary,
) -> Coursier:
repos_args = " ".join(f"-r={shlex.quote(repo)}" for repo in coursier_subsystem.options.repos)
coursier_wrapper_script = COURSIER_WRAPPER_SCRIPT.format(
coursier_wrapper_script = COURSIER_FETCH_WRAPPER_SCRIPT.format(
repos_args=repos_args,
coursier_working_directory=Coursier.working_directory_placeholder,
python_path=python.path,
coursier_bin_dir=Coursier.bin_dir,
)

post_process_stderr = POST_PROCESS_COURSIER_STDERR_SCRIPT.format(python_path=python.path)

downloaded_coursier_get = Get(
DownloadedExternalTool,
ExternalToolRequest,
Expand All @@ -210,7 +235,7 @@ async def setup_coursier(
CreateDigest(
[
FileContent(
os.path.basename(Coursier.wrapper_script),
os.path.basename(Coursier.fetch_wrapper_script),
coursier_wrapper_script.encode("utf-8"),
is_executable=True,
),
Expand All @@ -219,6 +244,11 @@ async def setup_coursier(
COURSIER_POST_PROCESSING_SCRIPT.encode("utf-8"),
is_executable=True,
),
FileContent(
os.path.basename(Coursier.post_process_stderr),
post_process_stderr.encode("utf-8"),
is_executable=True,
),
]
),
)
Expand Down

0 comments on commit c09289d

Please sign in to comment.