Skip to content

Commit 9bc6f08

Browse files
Refactor pkg_npm to support caching and better reference package contents (#43)
1 parent 4af5b4b commit 9bc6f08

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

internal/pkg_npm/npm_script_generator.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,27 @@
2323
const fs = require('fs');
2424

2525
function main(args) {
26-
const
27-
[outDir, packPath, publishPath, runNpmTemplatePath, packBatPath, publishBatPath,
28-
runNpmBatTemplatePath] = args;
26+
const [
27+
packPath,
28+
publishPath,
29+
runNpmTemplatePath,
30+
packBatPath,
31+
publishBatPath,
32+
runNpmBatTemplatePath,
33+
] = args;
2934
const cwd = process.cwd();
3035
if (/[\//]sandbox[\//]/.test(cwd)) {
3136
console.error('Error: npm_script_generator must be run with no sandbox');
3237
process.exit(1);
3338
}
3439

35-
const npmTemplate = fs.readFileSync(runNpmTemplatePath, {encoding: 'utf-8'});
36-
fs.writeFileSync(packPath, npmTemplate.replace('TMPL_args', `pack "${cwd}/${outDir}"`));
37-
fs.writeFileSync(publishPath, npmTemplate.replace('TMPL_args', `publish "${cwd}/${outDir}"`));
40+
const npmTemplate = fs.readFileSync(runNpmTemplatePath, { encoding: 'utf-8' });
41+
fs.writeFileSync(packPath, npmTemplate.replace('TMPL_args', `pack`));
42+
fs.writeFileSync(publishPath, npmTemplate.replace('TMPL_args', `publish`));
3843

39-
const npmBatTemplate = fs.readFileSync(runNpmBatTemplatePath, {encoding: 'utf-8'});
40-
fs.writeFileSync(packBatPath, npmBatTemplate.replace('TMPL_args', `pack "${cwd}/${outDir}"`));
41-
fs.writeFileSync(
42-
publishBatPath, npmBatTemplate.replace('TMPL_args', `publish "${cwd}/${outDir}"`));
44+
const npmBatTemplate = fs.readFileSync(runNpmBatTemplatePath, { encoding: 'utf-8' });
45+
fs.writeFileSync(packBatPath, npmBatTemplate.replace('TMPL_args', `pack`));
46+
fs.writeFileSync(publishBatPath, npmBatTemplate.replace('TMPL_args', `publish`));
4347
}
4448

4549
if (require.main === module) {

internal/pkg_npm/pkg_npm.bzl

+18-14
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ See the section on stamping in the [README](stamping)
137137
),
138138
"_npm_script_generator": attr.label(
139139
default = Label("//internal/pkg_npm:npm_script_generator"),
140-
cfg = "host",
140+
cfg = "exec",
141141
executable = True,
142142
),
143143
"_packager": attr.label(
144144
default = Label("//internal/pkg_npm:packager"),
145-
cfg = "host",
145+
cfg = "exec",
146146
executable = True,
147147
),
148148
"_run_npm_bat_template": attr.label(
@@ -206,7 +206,7 @@ def create_package(ctx, deps_files, nested_packages):
206206
# that single directory and there is no work to do
207207
package_dir = all_files[0]
208208

209-
_create_npm_scripts(ctx, package_dir)
209+
_create_npm_scripts(ctx)
210210

211211
return package_dir
212212

@@ -260,15 +260,14 @@ def create_package(ctx, deps_files, nested_packages):
260260
arguments = [args],
261261
)
262262

263-
_create_npm_scripts(ctx, package_dir)
263+
_create_npm_scripts(ctx)
264264

265265
return package_dir
266266

267-
def _create_npm_scripts(ctx, package_dir):
267+
def _create_npm_scripts(ctx):
268268
args = ctx.actions.args()
269269

270270
args.add_all([
271-
package_dir.path,
272271
ctx.outputs.pack_sh.path,
273272
ctx.outputs.publish_sh.path,
274273
ctx.file._run_npm_template.path,
@@ -281,13 +280,16 @@ def _create_npm_scripts(ctx, package_dir):
281280
progress_message = "Generating npm pack & publish scripts",
282281
mnemonic = "GenerateNpmScripts",
283282
executable = ctx.executable._npm_script_generator,
284-
inputs = [ctx.file._run_npm_template, ctx.file._run_npm_bat_template, package_dir],
283+
inputs = [ctx.file._run_npm_template, ctx.file._run_npm_bat_template],
285284
outputs = [ctx.outputs.pack_sh, ctx.outputs.publish_sh, ctx.outputs.pack_bat, ctx.outputs.publish_bat],
286285
arguments = [args],
287-
# Must be run local (no sandbox) so that the pwd is the actual execroot
288-
# in the script which is used to generate the path in the pack & publish
289-
# scripts.
290-
execution_requirements = {"local": "1"},
286+
# Cannot be built remotely (template contains absolute path to npm executable).
287+
# Cannot be built with sandboxing (references files outside sandbox).
288+
# Can be cached, because absolute path is a standard input from a repo rule.
289+
execution_requirements = {
290+
"no-remote-exec": "",
291+
"no-sandbox": "",
292+
},
291293
)
292294

293295
def _pkg_npm(ctx):
@@ -326,7 +328,6 @@ def _pkg_npm(ctx):
326328
result = [
327329
DefaultInfo(
328330
files = package_dir_depset,
329-
runfiles = ctx.runfiles([package_dir]),
330331
),
331332
]
332333

@@ -385,15 +386,18 @@ def pkg_npm_macro(name, tgz = None, **kwargs):
385386
outs = [tgz],
386387
# NOTE(mattem): on windows, it seems to output a buch of other stuff on stdout when piping, so pipe to tail
387388
# and grab the last line
388-
cmd = "$(location :%s.pack) 2>/dev/null | tail -1 | xargs -I {} cp {} $@" % name,
389+
# pass in package path
390+
cmd = "$(location :%s.pack) $(location :%s) | tail -1 | xargs -I {} cp {} $@" % (name, name),
389391
srcs = [
390392
name,
391393
],
392394
tools = [
393395
":%s.pack" % name,
394396
],
395397
tags = [
396-
"local",
398+
# Inherits limitations of %s.pack (due to absolute path to npm)
399+
"no-remote-exec",
400+
"no-sandbox",
397401
],
398402
visibility = kwargs.get("visibility"),
399403
)

0 commit comments

Comments
 (0)