Skip to content

Commit 5e6da08

Browse files
authored
fix: convert relative symlinks in test_module_repo to absolute (#2350)
Although relative links are valid in Windows, it seems that they fail under some circumstances. For example, see bazelbuild/bazel-central-registry#5205
1 parent c248c02 commit 5e6da08

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

buildkite/bazel-central-registry/bcr_presubmit.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import json
2525
import os
2626
import pathlib
27+
import platform
2728
import re
2829
import sys
2930
import subprocess
@@ -233,13 +234,48 @@ def extract_zip_with_permissions(zip_file_path, destination_dir):
233234
if entry.external_attr > 0xffff:
234235
os.chmod(file_path, entry.external_attr >> 16)
235236

237+
def make_symlinks_absolute(output_dir):
238+
"""
239+
Walks a directory and converts all relative symlinks to absolute ones.
240+
"""
241+
for root, dirs, files in os.walk(output_dir):
242+
for name in files + dirs:
243+
path = pathlib.Path(root) / name
244+
245+
if not path.is_symlink():
246+
continue
247+
248+
target = path.readlink()
249+
250+
if target.is_absolute():
251+
continue
252+
253+
# NOTE:
254+
# On Windows, we must know if the target is a directory to recreate
255+
# the symlink correctly. Check this *before* unlinking.
256+
# path.is_dir() correctly follows the symlink to check the target.
257+
is_dir = path.is_dir()
258+
259+
# Resolve the relative target path to an absolute one
260+
absolute_target = (path.parent / target).resolve()
261+
262+
# Replace the old relative symlink with the new absolute one
263+
path.unlink()
264+
path.symlink_to(absolute_target, target_is_directory=is_dir)
265+
236266
def unpack_archive(archive_file, output_dir):
237267
# Addressing https://github.com/bazelbuild/continuous-integration/issues/1536
238268
if archive_file.endswith(".zip"):
239269
extract_zip_with_permissions(archive_file, output_dir)
240270
else:
241271
shutil.unpack_archive(archive_file, output_dir)
242272

273+
if platform.system() == "Windows":
274+
# https://github.com/bazelbuild/bazel-central-registry/pull/5205
275+
# Windows has issues with relative symlinks so let's make all of the
276+
# symlinks in the unpacked archive absolute.
277+
make_symlinks_absolute(output_dir)
278+
243279
def prepare_test_module_repo(module_name, module_version, overwrite_bazel_version=None, root=None, suppress_log=False):
244280
"""Prepare the test module repo and the presubmit yml file it should use"""
245281
suppress_log or bazelci.print_collapsed_group(":information_source: Prepare test module repo")

0 commit comments

Comments
 (0)