Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix symlink collision bug #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
22 changes: 19 additions & 3 deletions pkg_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def _find_binary(ctx, binary_name):
return _success(binary)

def _execute(ctx, binary, args):
result = ctx.execute([binary] + args)
result = ctx.execute(
[binary] + args,
)
if result.return_code != 0:
return _error("Failed execute {} {}".format(binary, args))
return _success(result.stdout)
Expand Down Expand Up @@ -86,15 +88,28 @@ def _ignore_opts(opts, ignore_opts):
remain += [opt]
return remain

def _symlink_no_duplicate(ctx, src, dest):
if not dest.exists:
ctx.symlink(src, dest)
return True
return False

def _symlinks(ctx, basename, srcpaths):
result = []
root = ctx.path("")
base = root.get_child(basename)
rootlen = len(str(base)) - len(basename)
for src in [ctx.path(p) for p in srcpaths]:
dest = base.get_child(src.basename)
ctx.symlink(src, dest)
result += [str(dest)[rootlen:]]
# if the directory already exists then:
# 1) attempt to modify the name with the parent directory name
# 2) if that fails don't create the symlink at all
# we can sometimes skip symlink creation because of ocassional
# duplicates in pkg_config output
if dest.exists:
Copy link

Choose a reason for hiding this comment

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

Instead of just looking up 1 level in the directory tree, what do you think about generating unique identifiers from the complete srcpath? (For example: s.replace("_", "__").replace("/", "_slash_")?) This will be more robust to collisions in file paths.

dest = base.get_child(src.basename + src.dirname.basename)
if _symlink_no_duplicate(ctx, src, dest):
result += [str(dest)[rootlen:]]
Copy link

Choose a reason for hiding this comment

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

Modifying the name of the symlink that is created like this may lead to nondeterministic include path naming (or spooky action at a distance, where include path names depend on the order in which symlinks are created). Moreover, if you look at how the return value of _symlinks is used, you should see that this changes how the strip_include option to the pkg_config rule works.

For example, consider a package with include paths /usr/include/x86_64-linux-gnu/MyPackage and /usr/include/MyPackage. It provides the include files /usr/include/x86_64-linux-gnu/MyPackage/my.h and /usr/include/MyPackage/package.h.

A user of the pkg_config Bazel rule might specify a strip_prefix value of MyPackage, so that they can just #include "my.h" and #include "package.h" directly.

Under this PR, Bazel will create symlinks named includes/MyPackage and includes/include/MyPackage or includes/x86_64-linux-gnu/MyPackage, depending on which gets created first. There is no single strip_prefix value that can correctly strip out both of these. Furthermore, the names of the symlinks that get created depends on the order in which they are created, which is undesirable for multiple reasons.

Copy link
Author

Choose a reason for hiding this comment

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

yes, that makes a lot of sense; a strip_prefix or prefix (for adding a path prefix)

return result

def _deps(ctx, pkg_config, pkg_name):
Expand Down Expand Up @@ -192,3 +207,4 @@ pkg_config = repository_rule(
local = True,
implementation = _pkg_config_impl,
)