From f144cb82f60bb7b622bd8bd8dc9952abd3e6419a Mon Sep 17 00:00:00 2001 From: Romaric Parmentier Date: Tue, 20 May 2025 16:18:57 -0400 Subject: [PATCH 1/2] chore(py_lambda): add init_file parent dirs to mtree --- aws/private/py_lambda.bzl | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/private/py_lambda.bzl b/aws/private/py_lambda.bzl index f5c5cb1..2bcc24d 100644 --- a/aws/private/py_lambda.bzl +++ b/aws/private/py_lambda.bzl @@ -46,6 +46,7 @@ def _py_lambda_tar_impl(ctx): path = "" for dir in ctx.attr.init_files.split("/"): path = path + "/" + dir + mtree.append(_mtree_line(ctx.attr.prefix + path, type = "dir", mode = "0755")) mtree.append(_mtree_line(ctx.attr.prefix + path + "/__init__.py", type = "file")) mtree.append("") From 72bd77011389194f72d879e4cd35eeabec3e56fc Mon Sep 17 00:00:00 2001 From: Romaric Parmentier Date: Tue, 20 May 2025 16:19:18 -0400 Subject: [PATCH 2/2] fix(py_lambda): add parent dirs to mtree --- aws/private/py_lambda.bzl | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/aws/private/py_lambda.bzl b/aws/private/py_lambda.bzl index 2bcc24d..d46d1a1 100644 --- a/aws/private/py_lambda.bzl +++ b/aws/private/py_lambda.bzl @@ -1,6 +1,7 @@ "Rule to produce tar files with py_binary deps and app" load("@aspect_bazel_lib//lib:tar.bzl", "tar") +load("@bazel_skylib//lib:new_sets.bzl", "sets") # Write these two separate layers, so application changes are a small delta when pushing to a registry _LAYERS = ["app", "deps"] @@ -15,6 +16,23 @@ def _short_path(file_): short_path = short_path[second_slash + 1:] return short_path + +def _get_mtree_dirs_list_to_file(path): + results = [] + if "/" not in path: + return results + + parts = "" + for dir_part in path.split("/")[:-1]: + if len(parts) == 0: + parts += dir_part + else: + parts += "/" + dir_part + + results.append(_mtree_line(parts, type = "dir", mode = "0755")) + + return results + # Copied from aspect-bazel-lib/lib/private/tar.bzl def _mtree_line(file, type, content = None, uid = "0", gid = "0", time = "1672560000", mode = "0644"): spec = [ @@ -32,15 +50,17 @@ def _mtree_line(file, type, content = None, uid = "0", gid = "0", time = "167256 def _py_lambda_tar_impl(ctx): deps = ctx.attr.target[DefaultInfo].default_runfiles.files - # NB: this creates one of the parent directories, but others are implicit - mtree = [_mtree_line(ctx.attr.prefix, type = "dir", mode = "0755")] - + mtree = [] for dep in deps.to_list(): short_path = _short_path(dep) + mtree_path = None if dep.owner.workspace_name == "" and ctx.attr.kind == "app": - mtree.append(_mtree_line(ctx.attr.prefix + "/" + dep.short_path, type = "file", content = dep.path)) + mtree_path = ctx.attr.prefix + "/" + dep.short_path elif short_path.startswith("site-packages") and ctx.attr.kind == "deps": - mtree.append(_mtree_line(ctx.attr.prefix + short_path[len("site-packages"):], type = "file", content = dep.path)) + mtree_path = ctx.attr.prefix + short_path[len("site-packages"):] + if mtree_path: + mtree.extend(_get_mtree_dirs_list_to_file(mtree_path)) + mtree.append(_mtree_line(mtree_path, type = "file", content = dep.path)) if ctx.attr.kind == "app" and ctx.attr.init_files: path = "" @@ -49,6 +69,8 @@ def _py_lambda_tar_impl(ctx): mtree.append(_mtree_line(ctx.attr.prefix + path, type = "dir", mode = "0755")) mtree.append(_mtree_line(ctx.attr.prefix + path + "/__init__.py", type = "file")) + mtree = sets.to_list(sets.make(mtree)) + mtree = sorted(mtree, key = lambda x: len(x.split("/"))) mtree.append("") ctx.actions.write(ctx.outputs.output, "\n".join(mtree))