Skip to content
Open
Changes from all commits
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
11 changes: 11 additions & 0 deletions bazel/container_structure_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ def _structure_test_impl(ctx):
test_bin = ctx.toolchains["@container_structure_test//bazel:structure_test_toolchain_type"].st_info.binary
jq_bin = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"].jqinfo.bin

# On Windows the downloaded binary has no file extension. Windows requires
# a .exe extension to recognise a file as directly executable — without it,
# invoking the binary from a batch script triggers an "Open With" dialog
# instead of running it. Symlink the binary to <name>.exe so the generated
# launcher scripts can call it without this problem.
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
if is_windows and not test_bin.basename.endswith(".exe"):
test_bin_exe = ctx.actions.declare_file(test_bin.basename + ".exe")
ctx.actions.symlink(output = test_bin_exe, target_file = test_bin)
test_bin = test_bin_exe
Comment on lines +60 to +64

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

On Windows, reassigning test_bin = test_bin_exe means that only the symlink (test_bin_exe) is added to the test's runfiles (on line 119), while the original binary (test_bin) is omitted.

In sandboxed or remote execution environments, this will result in a dangling symlink at runtime because the target of the symlink is not present in the sandbox, causing the test to fail.

To fix this, both the symlink and the original binary must be included in the runfiles. Additionally, is_windows is calculated twice (here and on line 114); we should define it once and reuse it.

Please also update the runfiles definition around line 119 to include original_test_bin as well:

    runfiles = ctx.runfiles(
        files = image_files + ctx.files.configs + [
            bash_launcher,
            test_bin,
            original_test_bin,
            jq_bin,
        ],
    ).merge(ctx.attr._runfiles.default_runfiles)

And you can remove the duplicate is_windows definition on line 114.

Suggested change
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
if is_windows and not test_bin.basename.endswith(".exe"):
test_bin_exe = ctx.actions.declare_file(test_bin.basename + ".exe")
ctx.actions.symlink(output = test_bin_exe, target_file = test_bin)
test_bin = test_bin_exe
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
original_test_bin = test_bin
if is_windows and not test_bin.basename.endswith(".exe"):
test_bin_exe = ctx.actions.declare_file(test_bin.basename + ".exe")
ctx.actions.symlink(output = test_bin_exe, target_file = test_bin)
test_bin = test_bin_exe


default_info = ctx.attr.image[DefaultInfo] if DefaultInfo in ctx.attr.image else None
output_group_info = ctx.attr.image[OutputGroupInfo] if OutputGroupInfo in ctx.attr.image else None
image = None
Expand Down