diff --git a/MODULE.bazel b/MODULE.bazel index 0199434..bbb81a7 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -1,6 +1,6 @@ module( name = "rules_mayhem", - version = "0.8.7", + version = "0.8.8", ) bazel_dep(name = "bazel_skylib", version = "1.9.0") diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index e4164e3..e1f282c 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -192,7 +192,7 @@ "//mayhem:extensions.bzl%rules_mayhem_extension": { "general": { "bzlTransitiveDigest": "SUFcWZq0Nc0sjSX728mHdDz01rb3+thUWiQBxjMbYsA=", - "usagesDigest": "rrpQRkerDYioRqTE9+dhc3hb9COdGQVTTq1fgQ07pZk=", + "usagesDigest": "Oe8goIySCz0esqxbJE2IWDiez9LcX7Lz3532P77zhgE=", "recordedInputs": [ "REPO_MAPPING:,bazel_tools bazel_tools", "REPO_MAPPING:,rules_mayhem ", diff --git a/README.md b/README.md index c00364f..9103d00 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ You can add the following snippet: ``` ## MODULE.bazel -bazel_dep(name = "rules_mayhem", version = "0.8.7") +bazel_dep(name = "rules_mayhem", version = "0.8.8") rules_mayhem_extension = use_extension("@rules_mayhem//mayhem:extensions.bzl", "rules_mayhem_extension") use_repo(rules_mayhem_extension, "bazel_skylib", "mayhem_cli_linux", "mayhem_cli_windows", "platforms", "yq_cli_linux", "yq_cli_windows") @@ -21,8 +21,8 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "rules_mayhem", strip_prefix = "rules_mayhem", - urls = ["https://github.com/ForAllSecure/rules_mayhem/releases/download/0.8.7/rules_mayhem-0.8.7.tar.gz"], - sha256 = "4279b38113f223c1ab32cd1e393be59ad0295ad3f5ea2f017f4333c1f95e8fe6", + urls = ["https://github.com/ForAllSecure/rules_mayhem/releases/download/0.8.8/rules_mayhem-0.8.8.tar.gz"], + sha256 = "f5fbe3d4178f91d31153e1fd47224ffe8609ab81026a5f83c31ad9d40b867c65", ) load("@rules_mayhem//mayhem:repositories.bzl", "rules_mayhem_repositories") diff --git a/mayhem/mayhem.bzl b/mayhem/mayhem.bzl index 5972444..9b67306 100644 --- a/mayhem/mayhem.bzl +++ b/mayhem/mayhem.bzl @@ -110,6 +110,8 @@ def _mayhem_run_impl(ctx): run_args.append(ctx.attr.verbosity) run_args.append("run") + package_basename = "" + target_path = "" if ctx.file.mayhemfile: mayhemfile_path = ctx.file.mayhemfile.short_path run_args.append(".") @@ -117,10 +119,8 @@ def _mayhem_run_impl(ctx): run_args.append(mayhemfile_path) runfiles = runfiles.merge(ctx.runfiles(files=[ctx.file.mayhemfile])) elif ctx.file.target_path: - mayhemfile_path = ctx.file.target_path.short_path + "/Mayhemfile" - run_args.append(ctx.file.target_path.short_path) - run_args.append("-f") - run_args.append(mayhemfile_path) + package_basename = ctx.file.target_path.basename + target_path = str(ctx.file.target_path.short_path) runfiles = runfiles.merge(ctx.runfiles(files=[ctx.file.target_path])) elif ctx.attr.image: run_args.append(ctx.attr.image) @@ -252,27 +252,47 @@ import subprocess import sys import os +runfiles_enabled = os.path.exists("{mayhem_cli}") + def resolve_runfile_mayhem_cli_path(mayhem_cli_short_path): - # Bazel sets the RUNFILES_MANIFEST_FILE environment variable on Windows - # which we can use to resolve the path to the mayhem CLI in the case that - # runfiles/symlinks are not available - manifest_path = os.environ.get("RUNFILES_MANIFEST_FILE") - if manifest_path and os.name == "nt": + if runfiles_enabled: + # Runfiles are enabled, so no need to resolve the path manually + return mayhem_cli_short_path + + manifest_path = None + work_dir = os.path.dirname(os.path.abspath(__file__)) + print("Looking for manifest file in runfiles directory: " + work_dir) + for file in os.listdir(work_dir): + if file.endswith(".runfiles_manifest"): + manifest_path = os.path.join(work_dir, file) + print("Found manifest file: " + manifest_path) + break + + if manifest_path: with open(manifest_path, "r") as manifest_file: for line in manifest_file: key, value = line.strip().split(" ", 1) if "mayhem" in key.lower() and "cli" in key.lower(): return value - - # If the manifest file is not set or the mayhem CLI is not found in the manifest, - # fall back to the original path - return mayhem_cli_short_path + + raise FileNotFoundError("Could not find mayhem CLI in runfiles manifest") mayhem_cli = resolve_runfile_mayhem_cli_path("{mayhem_cli}") run_args = {run_args} wait_args = {wait_args} +if "{package_basename}": + if runfiles_enabled: + package_dir = "{target_path}" + else: + package_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "{package_basename}") + + package_index = run_args.index("run") + run_args.insert(package_index + 1, package_dir) + run_args.insert(package_index + 2, "-f") + run_args.insert(package_index + 3, os.path.join(package_dir, "Mayhemfile")) + # Mayhem run run_result = subprocess.run([mayhem_cli] + run_args, capture_output=True, text=True) print("Run created: " + str(run_result.stdout)) @@ -301,6 +321,9 @@ if wait_args: sys.exit(show_result.returncode) """.format( mayhem_cli=ctx.executable._mayhem_cli.short_path, + target_name=ctx.label.name, + target_path=target_path, + package_basename=package_basename, run_args=run_args, wait_args=wait_args, )