rules_android_ndk icon indicating copy to clipboard operation
rules_android_ndk copied to clipboard

Error in symlink: Cannot write outside of the repository directory

Open hbf17517 opened this issue 11 months ago • 1 comments

I am using bazel 8.1.1 My MODULE.bazel contains the following:

bazel_dep(name = "rules_android_ndk", version = "0.1.3")
android_ndk_repository_extension = use_extension("@rules_android_ndk//:extension.bzl", "android_ndk_repository_extension")
use_repo(android_ndk_repository_extension, "androidndk")  

When I build I get the following error:

INFO: Repository rules_android_ndk++android_ndk_repository_extension+androidndk instantiated at:
  <builtin>: in <toplevel>
Repository rule android_ndk_repository defined at:
  C:/users/rob/_bazel_rob/q6nhqnyq/external/rules_android_ndk+/rules.bzl:127:41: in <toplevel>
ERROR: C:/users/rob/_bazel_rob/q6nhqnyq/external/rules_android_ndk+/rules.bzl:116:24: An error occurred during the fetch of repository 'rules_android_ndk++android_ndk_repository_extension+androidndk':
   Traceback (most recent call last):
        File "C:/users/rob/_bazel_rob/q6nhqnyq/external/rules_android_ndk+/rules.bzl", line 49, column 21, in _android_ndk_repository_impl
                _create_symlinks(ctx, ndk_path, clang_directory, sysroot_directory)
        File "C:/users/rob/_bazel_rob/q6nhqnyq/external/rules_android_ndk+/rules.bzl", line 116, column 24, in _create_symlinks
                ctx.symlink(p, repo_relative_path)
Error in symlink: Cannot write outside of the repository directory for path C:/Users/rob/AppData/Local/Android/Sdk/ndk/28.0.13004108/toolchains/llvm/prebuilt/windows-x86_64/AndroidVersion.txt
ERROR: Analysis of target '//third_party/percetto:percetto_lib' failed; build aborted: Cannot write outside of the repository directory for path C:/Users/rob/AppData/Local/Android/Sdk/ndk/28.0.13004108/toolchains/llvm/prebuilt/windows-x86_64/AndroidVersion.txt

I discovered that p and repo_relative_path are of the same value: C:/Users/rob/AppData/Local/Android/Sdk/ndk/28.0.13004108/toolchains/llvm/prebuilt/windows-x86_64/AndroidVersion.txt It looks like string.replace at https://github.com/bazelbuild/rules_android_ndk/blob/main/rules.bzl#L112 doesn't work!

    for p in ctx.path(ndk_path + clang_directory).readdir():
        repo_relative_path = str(p).replace(ndk_path, "")
+        print("p=",p,"repo_relative_path=",repo_relative_path)
        # Skip sysroot directory, since it gets its own BUILD file
        if repo_relative_path != sysroot_directory:
            ctx.symlink(p, repo_relative_path)

Any idea what's happening? Thank you

hbf17517 avatar Mar 14 '25 12:03 hbf17517

The solution for me was the following:

diff --git a/rules.bzl b/rules.bzl
index 9f76a87..66149da 100644
--- a/rules.bzl
+++ b/rules.bzl
@@ -104,6 +104,7 @@ def _android_ndk_repository_impl(ctx):
 # Manually create a partial symlink tree of the NDK to avoid creating BUILD
 # files in the real NDK directory.
 def _create_symlinks(ctx, ndk_path, clang_directory, sysroot_directory):
+    ndk_path = str(ndk_path).replace("\\", "/")
     # Path needs to end in "/" for replace() below to work
     if not ndk_path.endswith("/"):
         ndk_path = ndk_path + "/"

hbf17517 avatar Mar 14 '25 22:03 hbf17517