From fd03d41daeb65c7c9e9a33773546b2c6da13d2d8 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Wed, 23 Oct 2019 13:43:09 +0200 Subject: [PATCH] Add regression test for relative imports Invocations of `nixpkgs_package` with a Nix file that refers to other files by relative path into a separate directory, e.g. `./foo/bar.nix`, or `../baz.nix` fail with `No such file or directory`. The reason is that `nixpkgs_package` will flatten the directory hierarchy of files defined in `nix_file_deps`. See the following error: ``` $ nix-shell --pure --run 'bazel test //tests:run-relative-imports' ERROR: /home/aj/tweag.io/da/bazel-projects/rules_nixpkgs/tests/BUILD.bazel:7:5: no such package '@relative-imports//': Traceback (most recent call last): File "/home/aj/tweag.io/da/bazel-projects/rules_nixpkgs/nixpkgs/nixpkgs.bzl", line 149 _execute_or_fail(repository_ctx, nix_build, failure_m...), ...) File "/home/aj/tweag.io/da/bazel-projects/rules_nixpkgs/nixpkgs/nixpkgs.bzl", line 318, in _execute_or_fail fail("\n{failure_message}\nCommand: {...)) Cannot build Nix attribute 'hello'. Command: [/nix/store/zw52rvbzhh6x447yc0hn3w7s0wmcq3pw-nix-2.2.2/bin/nix-build, "-A", "hello", "--out-link", "bazel-support/nix-out-link", "-I", "nixpkgs=/home/aj/.cache/bazel/_bazel_aj/cf7431ac3fb5fc6f4c6a5569d7b55e4a/external/nixpkgs/nixpkgs"] Return code: 1 Error output: error: getting status of '/home/aj/.cache/bazel/_bazel_aj/cf7431ac3fb5fc6f4c6a5569d7b55e4a/external/relative-imports/relative_imports/nixpkgs.nix': No such file or directory and referenced by '//tests:run-relative-imports' ERROR: Analysis of target '//tests:run-relative-imports' failed; build aborted: no such package '@relative-imports//': Traceback (most recent call last): File "/home/aj/tweag.io/da/bazel-projects/rules_nixpkgs/nixpkgs/nixpkgs.bzl", line 149 _execute_or_fail(repository_ctx, nix_build, failure_m...), ...) File "/home/aj/tweag.io/da/bazel-projects/rules_nixpkgs/nixpkgs/nixpkgs.bzl", line 318, in _execute_or_fail fail("\n{failure_message}\nCommand: {...)) Cannot build Nix attribute 'hello'. Command: [/nix/store/zw52rvbzhh6x447yc0hn3w7s0wmcq3pw-nix-2.2.2/bin/nix-build, "-A", "hello", "--out-link", "bazel-support/nix-out-link", "-I", "nixpkgs=/home/aj/.cache/bazel/_bazel_aj/cf7431ac3fb5fc6f4c6a5569d7b55e4a/external/nixpkgs/nixpkgs"] Return code: 1 Error output: error: getting status of '/home/aj/.cache/bazel/_bazel_aj/cf7431ac3fb5fc6f4c6a5569d7b55e4a/external/relative-imports/relative_imports/nixpkgs.nix': No such file or directory ``` --- WORKSPACE | 11 +++++++++++ tests/BUILD.bazel | 1 + tests/relative_imports.nix | 4 ++++ tests/relative_imports/nixpkgs.nix | 1 + 4 files changed, 17 insertions(+) create mode 100644 tests/relative_imports.nix create mode 100644 tests/relative_imports/nixpkgs.nix diff --git a/WORKSPACE b/WORKSPACE index 9cf74d716..0470bc042 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -96,4 +96,15 @@ filegroup( repository = "@nixpkgs", ) +nixpkgs_package( + name = "relative-imports", + attribute_path = "hello", + nix_file = "//tests:relative_imports.nix", + nix_file_deps = [ + "//:nixpkgs.nix", + "//tests:relative_imports/nixpkgs.nix", + ], + repository = "@nixpkgs", +) + nixpkgs_cc_configure(repository = "@remote_nixpkgs") diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index 68e4f4c58..ef6e2feae 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -19,6 +19,7 @@ package(default_testonly = 1) "nix-file-test", "nix-file-deps-test", "nixpkgs-git-repository-test", + "relative-imports", ] ] + [ # These tests use the nix package generated by ./output.nix diff --git a/tests/relative_imports.nix b/tests/relative_imports.nix new file mode 100644 index 000000000..67f4b6e55 --- /dev/null +++ b/tests/relative_imports.nix @@ -0,0 +1,4 @@ +{ system ? builtins.currentSystem +, pkgs ? import relative_imports/nixpkgs.nix { inherit system; config = {}; overlays = []; } +}: +{ inherit (pkgs) hello; } diff --git a/tests/relative_imports/nixpkgs.nix b/tests/relative_imports/nixpkgs.nix new file mode 100644 index 000000000..40391a85a --- /dev/null +++ b/tests/relative_imports/nixpkgs.nix @@ -0,0 +1 @@ +import ../../nixpkgs.nix