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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/notes/2.25.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ python_requirement(name="black", resolve="your-resolve-name", requirements=["bla

The previously deprecated `[shell-setup].tailor` option has now been removed. See [`[shell-setup].tailor_sources`](https://www.pantsbuild.org/2.25/reference/subsystems/shell-setup#tailor_sources) and [`[shell-setup].tailor_shunit2_tests`](https://www.pantsbuild.org/2.25/reference/subsystems/shell#tailor_shunit2_tests) to update.

#### Javascript

The NodeJS subsystem now supports configuring additional tools that should be available in the NodeJS process environment:

- [`[nodejs].tools`](https://www.pantsbuild.org/2.25/reference/subsystems/nodejs#tools): List any additional executable tools required for node processes to work.
- [`[nodejs].macos_tools`](https://www.pantsbuild.org/2.25/reference/subsystems/nodejs#macos_tools): List any additional executable tools required specifically for macOS.

The paths to these tools will be included in the PATH used in the execution sandbox, so that they may be used by NodeJS processes during execution.

### Plugin API changes

The version of Python used by Pants itself is now [3.11](https://docs.python.org/3/whatsnew/3.11.html) (up from 3.9).
Expand Down
49 changes: 46 additions & 3 deletions src/python/pants/backend/javascript/subsystems/nodejs.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,38 @@ def default_package_manager(self) -> str | None:
return f"{self.package_manager}@{self.package_managers[self.package_manager]}"
return self.package_manager

_tools = StrListOption(
default=[],
help=softwrap(
"""
List any additional executable tools required for node processes to work. The paths to
these tools will be included in the PATH used in the execution sandbox, so that
they may be used by nodejs processes execution.
"""
),
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.

default=[],
help=softwrap(
"""
List any additional executable tools required for node processes to work on macOS.
The paths to these tools will be included in the PATH used in the execution sandbox,
so that they may be used by nodejs processes execution.
"""
),
advanced=True,
)

@property
def tools(self) -> tuple[str, ...]:
return tuple(sorted(set(self._tools)))

@property
def macos_tools(self) -> tuple[str, ...]:
return tuple(sorted(set(self._macos_tools)))

class EnvironmentAware(ExecutableSearchPathsOptionMixin, Subsystem.EnvironmentAware):
search_path = StrListOption(
default=["<PATH>"],
Expand Down Expand Up @@ -350,22 +382,33 @@ async def add_corepack_shims_to_digest(

@rule(level=LogLevel.DEBUG)
async def node_process_environment(
binaries: NodeJSBinaries, nodejs: NodeJS.EnvironmentAware
binaries: NodeJSBinaries,
nodejs: NodeJS,
nodejs_environment: NodeJS.EnvironmentAware,
platform: Platform,
) -> NodeJSProcessEnvironment:
default_required_tools = ["sh", "bash"]
tools_used_by_setup_scripts = ["mkdir", "rm", "touch", "which"]
pnpm_shim_tools = ["sed", "dirname"]

additional_tools = nodejs.tools
if platform.is_macos:
additional_tools.extend(nodejs.macos_tools)

binary_shims = await Get(
BinaryShims,
BinaryShimsRequest.for_binaries(
*default_required_tools,
*tools_used_by_setup_scripts,
*pnpm_shim_tools,
*additional_tools,
rationale="execute a nodejs process",
search_path=nodejs.executable_search_path,
search_path=nodejs_environment.executable_search_path,
),
)
corepack_env_vars = await Get(EnvironmentVars, EnvironmentVarsRequest(nodejs.corepack_env_vars))
corepack_env_vars = await Get(
EnvironmentVars, EnvironmentVarsRequest(nodejs_environment.corepack_env_vars)
)
binary_digest_with_shims = await add_corepack_shims_to_digest(
binaries, binary_shims, corepack_env_vars
)
Expand Down