From 5f0d7da660e0dfeea8bcca7387d6fa43347099db Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Mon, 4 Feb 2019 15:37:11 -0500 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20use=20relativize=20paths=20in?= =?UTF-8?q?=20internal=20modules?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes it possible to use cabal_paths with local projects. Fixes #75. --- third_party/cabal2bazel/bzl/cabal_paths.bzl | 30 ++++++++++++++------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/third_party/cabal2bazel/bzl/cabal_paths.bzl b/third_party/cabal2bazel/bzl/cabal_paths.bzl index e41d62a..f3f5bd4 100644 --- a/third_party/cabal2bazel/bzl/cabal_paths.bzl +++ b/third_party/cabal2bazel/bzl/cabal_paths.bzl @@ -31,19 +31,31 @@ def _impl_path_module_gen(ctx): base_dir = ctx.label.package + ( ("/" + ctx.attr.data_dir) if ctx.attr.data_dir else "") - ctx.template_action( + # If workspace_root is not empty, we have an external repo. + # We need to relativize the paths. + if ctx.label.workspace_root != "": + ctx.template_action( template=ctx.file._template, output=paths_file, substitutions={ - "%{module}": ctx.attr.module, - "%{base_dir}": paths.join( - # TODO: this probably won't work for packages not in external - # repositories. See: - # https://github.com/bazelbuild/bazel/wiki/Updating-the-runfiles-tree-structure - "..", paths.relativize(ctx.label.workspace_root, "external"), base_dir), - "%{version}": str(ctx.attr.version), + "%{module}": ctx.attr.module, + "%{base_dir}": paths.join( + # https://github.com/bazelbuild/bazel/wiki/Updating-the-runfiles-tree-structure + "..", paths.relativize(ctx.label.workspace_root, "external"), base_dir), + "%{version}": str(ctx.attr.version), }, - ) + ) + else: # Otherwise this is a local directory, and we can use relative paths. + ctx.template_action( + template=ctx.file._template, + output=paths_file, + substitutions={ + "%{module}": ctx.attr.module, + "%{base_dir}": base_dir, + "%{version}": str(ctx.attr.version), + }, + ) + return struct(files=depset([paths_file]))