diff --git a/test/BUILD b/test/BUILD index f399903..c60fb82 100644 --- a/test/BUILD +++ b/test/BUILD @@ -34,3 +34,24 @@ haskell_doctest( name = "doctest-test", deps = [":DoctestExample"], ) + +load("//:third_party/cabal2bazel/bzl/cabal_paths.bzl", "cabal_paths") + +cabal_paths( + name = "foo-paths", + package = "foo", + version = [ + 1, + 2, + ], +) + +haskell_test( + name = "PathsTest", + srcs = ["PathsTest.hs"], + deps = [ + ":foo-paths", + hazel_library("base"), + hazel_library("filepath"), + ], +) diff --git a/test/PathsTest.hs b/test/PathsTest.hs new file mode 100644 index 0000000..29cc1d4 --- /dev/null +++ b/test/PathsTest.hs @@ -0,0 +1,23 @@ +-- Copyright 2018 Google LLC +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- https://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. + +module Main where + +import qualified Paths_foo + +import Data.Version (versionBranch) +import Control.Exception (assert) + +main = do + assert (versionBranch Paths_foo.version == [1, 2]) $ return () diff --git a/third_party/cabal2bazel/bzl/cabal_paths.bzl b/third_party/cabal2bazel/bzl/cabal_paths.bzl index ce99516..874b227 100644 --- a/third_party/cabal2bazel/bzl/cabal_paths.bzl +++ b/third_party/cabal2bazel/bzl/cabal_paths.bzl @@ -34,19 +34,31 @@ def _impl_path_module_gen(ctx): 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])) @@ -96,7 +108,7 @@ def cabal_paths(name=None, package=None, data_dir='',data=[], version=[], **kwar data: The data files that this package depends on to run. version: The version number of this package (list of ints) """ - module_name = "Paths_" + package + module_name = "Paths_" + package.replace("-", "_") paths_file = module_name + ".hs" _path_module_gen( name = paths_file,