Skip to content

Commit

Permalink
Add regression test for relative imports
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
aherrmann committed Oct 23, 2019
1 parent f4e2ec9 commit fd03d41
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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")
1 change: 1 addition & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/relative_imports.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{ system ? builtins.currentSystem
, pkgs ? import relative_imports/nixpkgs.nix { inherit system; config = {}; overlays = []; }
}:
{ inherit (pkgs) hello; }
1 change: 1 addition & 0 deletions tests/relative_imports/nixpkgs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import ../../nixpkgs.nix

0 comments on commit fd03d41

Please sign in to comment.