Skip to content

Commit

Permalink
javascript: fix running scripts with yarn (pantsbuild#20543)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Oliveira authored Feb 14, 2024
1 parent b39e2c4 commit a7f86a0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/python/pants/backend/javascript/run/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ async def run_node_build_script(
target_env_vars = await Get(
EnvironmentVars, EnvironmentVarsRequest(field_set.extra_env_vars.value or ())
)

prefix_arg = "--prefix"
if installation.project_env.project.package_manager == "yarn":
prefix_arg = "--cwd"

process = await Get(
Process,
NodeJsProjectEnvironmentProcess(
installation.project_env,
args=("--prefix", "{chroot}", "run", str(field_set.entry_point.value)),
args=(prefix_arg, "{chroot}", "run", str(field_set.entry_point.value)),
description=f"Running {str(field_set.entry_point.value)}.",
input_digest=installation.digest,
extra_env=target_env_vars,
Expand Down
45 changes: 44 additions & 1 deletion src/python/pants/backend/javascript/run/rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def rule_runner() -> RuleRunner:
return rule_runner


def test_creates_run_requests_package_json_scripts(rule_runner: RuleRunner) -> None:
def test_creates_npm_run_requests_package_json_scripts(rule_runner: RuleRunner) -> None:
rule_runner.write_files(
{
"src/js/BUILD": dedent(
Expand Down Expand Up @@ -77,6 +77,49 @@ def test_creates_run_requests_package_json_scripts(rule_runner: RuleRunner) -> N
assert result.args == ("npm", "--prefix", "{chroot}", "run", script)


def test_creates_yarn_run_requests_package_json_scripts(rule_runner: RuleRunner) -> None:
rule_runner.write_files(
{
"src/js/BUILD": dedent(
"""\
package_json(
scripts=[
node_build_script(entry_point="build", output_directories=["dist"]),
node_build_script(entry_point="compile", output_directories=["dist"]),
node_build_script(entry_point="transpile", output_directories=["dist"]),
]
)
"""
),
"src/js/package.json": json.dumps(
{
"name": "ham",
"version": "0.0.1",
"browser": "lib/index.mjs",
"scripts": {
"build": "swc ./lib -d dist",
"transpile": "babel ./lib -d dist",
"compile": "tsc ./lib --emit -d bin",
},
"packageManager": "[email protected]",
}
),
"src/js/yarn.lock": "",
"src/js/lib/BUILD": dedent(
"""\
javascript_sources()
"""
),
"src/js/lib/index.mjs": "",
}
)
for script in ("build", "compile", "transpile"):
tgt = rule_runner.get_target(Address("src/js", generated_name=script))
result = rule_runner.request(RunRequest, [RunNodeBuildScriptFieldSet.create(tgt)])

assert result.args == ("yarn", "--cwd", "{chroot}", "run", script)


def test_extra_envs(rule_runner: RuleRunner) -> None:
rule_runner.write_files(
{
Expand Down

0 comments on commit a7f86a0

Please sign in to comment.