Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

additional tools during nodejs processes invocation #21833

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

kevin-oliveira-zocdoc
Copy link

Description

During nodejs processes execution, such as yarn install, some additional tools might need to be present on the PATH, e.g. make, cp, c++, etc, specially when the npm dependencies uses node-gyp under the hood, so these additional tools are necessary to compile binaries. The current implementation doesn’t allow us to specify more tools than the default.

This adds tools and macos_tools as options to the nodejs subsystem so that these additional tools can be added to the sandbox during nodejs processes execution.

There might be cases where macos specific tools might be needed - mostly when node-gyp is used - such as xcrun and xcodebuild, which are necessary to compile native stuff, and these tools are not available on linux based os, so I've added the macos_tools where these specific tools can be set.

Fixes #21638.

Copy link
Contributor

@huonw huonw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable overall. Thanks for contributing!

advanced=True,
)

_macos_tools = StrListOption(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach we've used with Docker is optional_tools: tools that should be included if they're available. https://www.pantsbuild.org/stable/reference/subsystems/docker#optional_tools

The code that's used for filtering the tools is at:

if optional_tools:
optional_tools_requests = [
BinaryPathRequest(binary_name=binary_name, search_path=search_path)
for binary_name in optional_tools
]
optional_tools_paths = await MultiGet(
Get(BinaryPaths, BinaryPathRequest, optional_tools_request)
for optional_tools_request in optional_tools_requests
)
all_binary_first_paths.extend(
[
cast(BinaryPath, path.first_path) # safe since we check for non-empty paths below
for path in optional_tools_paths
if path.paths
]
)

Maybe a similar approach could work here and be a bit more flexible?

It is a bit more fiddly to get set-up, though.

@huonw
Copy link
Contributor

huonw commented Jan 16, 2025

Ah, it'd also be good to add a test for this, to ensure the functionality keeps working in future. Again the docker version might be handy inspiration:

def test_get_docker_with_tools(rule_runner: RuleRunner) -> None:
def mock_get_binary_path(request: BinaryPathRequest) -> BinaryPaths:
if request.binary_name == "docker":
return BinaryPaths("docker", paths=[BinaryPath("/bin/docker")])
elif request.binary_name == "real-tool":
return BinaryPaths("real-tool", paths=[BinaryPath("/bin/a-real-tool")])
else:
return BinaryPaths(request.binary_name, ())
def mock_get_binary_shims(request: BinaryShimsRequest) -> BinaryShims:
return BinaryShims(EMPTY_DIGEST, "cache_name")
def run(tools: list[str], optional_tools: list[str]) -> None:
docker_options = create_subsystem(
DockerOptions,
experimental_enable_podman=False,
tools=tools,
optional_tools=optional_tools,
)
docker_options_env_aware = mock.MagicMock(spec=DockerOptions.EnvironmentAware)
nonlocal mock_get_binary_path
nonlocal mock_get_binary_shims
run_rule_with_mocks(
get_docker,
rule_args=[docker_options, docker_options_env_aware],
mock_gets=[
MockGet(
output_type=BinaryPaths,
input_types=(BinaryPathRequest,),
mock=mock_get_binary_path,
),
MockGet(
output_type=BinaryShims,
input_types=(BinaryShimsRequest,),
mock=mock_get_binary_shims,
),
],
)
run(tools=["real-tool"], optional_tools=[])
with pytest.raises(BinaryNotFoundError, match="Cannot find `nonexistent-tool`"):
run(tools=["real-tool", "nonexistent-tool"], optional_tools=[])
# Optional non-existent tool should still succeed.
run(tools=[], optional_tools=["real-tool", "nonexistent-tool"])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow additional tools during nodejs processes invocation
2 participants