Skip to content

Commit a7f86a0

Browse files
author
Kevin Oliveira
authored
javascript: fix running scripts with yarn (#20543)
Fixes #20542
1 parent b39e2c4 commit a7f86a0

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/python/pants/backend/javascript/run/rules.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,16 @@ async def run_node_build_script(
4545
target_env_vars = await Get(
4646
EnvironmentVars, EnvironmentVarsRequest(field_set.extra_env_vars.value or ())
4747
)
48+
49+
prefix_arg = "--prefix"
50+
if installation.project_env.project.package_manager == "yarn":
51+
prefix_arg = "--cwd"
52+
4853
process = await Get(
4954
Process,
5055
NodeJsProjectEnvironmentProcess(
5156
installation.project_env,
52-
args=("--prefix", "{chroot}", "run", str(field_set.entry_point.value)),
57+
args=(prefix_arg, "{chroot}", "run", str(field_set.entry_point.value)),
5358
description=f"Running {str(field_set.entry_point.value)}.",
5459
input_digest=installation.digest,
5560
extra_env=target_env_vars,

src/python/pants/backend/javascript/run/rules_test.py

+44-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def rule_runner() -> RuleRunner:
3535
return rule_runner
3636

3737

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

7979

80+
def test_creates_yarn_run_requests_package_json_scripts(rule_runner: RuleRunner) -> None:
81+
rule_runner.write_files(
82+
{
83+
"src/js/BUILD": dedent(
84+
"""\
85+
package_json(
86+
scripts=[
87+
node_build_script(entry_point="build", output_directories=["dist"]),
88+
node_build_script(entry_point="compile", output_directories=["dist"]),
89+
node_build_script(entry_point="transpile", output_directories=["dist"]),
90+
]
91+
)
92+
"""
93+
),
94+
"src/js/package.json": json.dumps(
95+
{
96+
"name": "ham",
97+
"version": "0.0.1",
98+
"browser": "lib/index.mjs",
99+
"scripts": {
100+
"build": "swc ./lib -d dist",
101+
"transpile": "babel ./lib -d dist",
102+
"compile": "tsc ./lib --emit -d bin",
103+
},
104+
"packageManager": "[email protected]",
105+
}
106+
),
107+
"src/js/yarn.lock": "",
108+
"src/js/lib/BUILD": dedent(
109+
"""\
110+
javascript_sources()
111+
"""
112+
),
113+
"src/js/lib/index.mjs": "",
114+
}
115+
)
116+
for script in ("build", "compile", "transpile"):
117+
tgt = rule_runner.get_target(Address("src/js", generated_name=script))
118+
result = rule_runner.request(RunRequest, [RunNodeBuildScriptFieldSet.create(tgt)])
119+
120+
assert result.args == ("yarn", "--cwd", "{chroot}", "run", script)
121+
122+
80123
def test_extra_envs(rule_runner: RuleRunner) -> None:
81124
rule_runner.write_files(
82125
{

0 commit comments

Comments
 (0)