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

fix: handle malformed npm packages gracefully in extract action #1794

Merged
merged 2 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
# Input hashes for repository rule npm_translate_lock(name = "npm", pnpm_lock = "@@//:pnpm-lock.yaml").
# This file should be checked into version control along with the pnpm-lock.yaml file.
.npmrc=-2065072158
pnpm-lock.yaml=-481966245
pnpm-lock.yaml=-1309835144
examples/npm_deps/patches/meaning-of-life@1.0.0-pnpm.patch=-442666336
package.json=-275319675
pnpm-workspace.yaml=-1178830835
examples/js_binary/package.json=-41174383
examples/linked_empty_node_modules/package.json=-1039372825
examples/macro/package.json=857146175
examples/npm_deps/package.json=-1377141392
examples/npm_package/libs/lib_a/package.json=-1377103079
examples/npm_package/packages/pkg_a/package.json=1006424040
examples/npm_package/packages/pkg_b/package.json=1041247977
examples/webpack_cli/package.json=1911342006
js/private/coverage/bundle/package.json=-1543718929
js/private/image/package.json=-1260474848
jbedard marked this conversation as resolved.
Show resolved Hide resolved
js/private/test/image/package.json=-687546763
js/private/test/js_run_devserver/package.json=-260856079
js/private/worker/src/package.json=1608383745
npm/private/test/package.json=1756993924
npm/private/test/package.json=600650131
npm/private/test/vendored/lodash-4.17.21.tgz=-1206623349
npm/private/test/npm_package/package.json=-1991705133
npm/private/test/vendored/is-odd/package.json=1041695223
npm/private/test/vendored/semver-max/package.json=578664053
examples/linked_empty_node_modules/package.json=-1039372825
examples/npm_package/packages/pkg_d/package.json=1110895851
js/private/image/package.json=-1260474848
js/private/test/image/package.json=-687546763
js/private/test/js_run_devserver/package.json=-260856079
30 changes: 14 additions & 16 deletions npm/private/npm_package_store.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -205,25 +205,23 @@ def _npm_package_store_impl(ctx):
else:
package_store_directory = ctx.actions.declare_directory(package_store_directory_path)
if utils.is_tarball_extension(src.extension):
# npm packages are always published with one top-level directory inside the tarball, tho the name is not predictable
# we can use the --strip-components 1 argument with tar to strip one directory level
args = ctx.actions.args()
args.add("--extract")
args.add("--no-same-owner")
args.add("--no-same-permissions")
args.add("--strip-components")
args.add(str(1))
args.add("--file")
args.add(src.path)
args.add("--directory")
args.add(package_store_directory.path)

# npm packages are always published with one top-level directory inside the tarball,
# tho the name is not predictable we can use the --strip-components 1 argument with
# tar to strip one directory level. Some packages have directory permissions missing
# executable which make the directories not listable (pngjs@5.0.0 for example). Run
# `chmod -R a+X` to fix up these packages (https://stackoverflow.com/a/14634721).
# See https://github.com/aspect-build/rules_js/issues/1637 for more info.
bsdtar = ctx.toolchains["@aspect_bazel_lib//lib:tar_toolchain_type"]
ctx.actions.run(
executable = bsdtar.tarinfo.binary,
ctx.actions.run_shell(
tools = [bsdtar.tarinfo.binary],
inputs = depset(direct = [src], transitive = [bsdtar.default.files]),
outputs = [package_store_directory],
arguments = [args],
gregmagolan marked this conversation as resolved.
Show resolved Hide resolved
command = "$1 --extract --no-same-owner --no-same-permissions --strip-components 1 --file $2 --directory $3 && chmod -R a+X $3",
arguments = [
bsdtar.tarinfo.binary.path,
gregmagolan marked this conversation as resolved.
Show resolved Hide resolved
src.path,
package_store_directory.path,
],
mnemonic = "NpmPackageExtract",
progress_message = "Extracting npm package {}@{}".format(package, version),
)
Expand Down
3 changes: 2 additions & 1 deletion npm/private/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"typescript": "*",
"unused": "latest",
"test-npm_package": "workspace:*",
"webpack-bundle-analyzer": "4.5.0"
"webpack-bundle-analyzer": "4.5.0",
"pngjs": "5.0.0"
}
}
Loading
Loading