Skip to content

Commit 15656bb

Browse files
RaitoBezariuskolloch
authored andcommitted
fix(template): run tests in /build/source instead /build
Previously, the source tree was located inline in `/build` during tests, this was a mistake because the crates more than often are built in `/build/source` as per the `sourceRoot` system. This can cause issues with test binaries hardcoding `/build/source/...` as their choice for doing things, causing them to be confused in the test phase which is relocated without rewriting the paths inside test binaries. We fix that by relocating ourselves in the right hierarchy. This is a "simple" fix in the sense that more edge cases could exist but they are hard to reason about because they would be crates using custom `sourceRoot`, i.e. having `crate.sourceRoot` set and then it becomes a bit hard to reproduce the hierarchy, you need to analyze whether the path is absolute or relative, If it's relative, you can just reuse it and reproduce that specific hierarchy. If it's absolute, you need to cut the "absolute" meaningless part, e.g. `$NIX_BUILD_TOP/` and proceed like it's a relative path IMHO.
1 parent 7010242 commit 15656bb

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

crate2nix/Cargo.nix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,6 +2889,16 @@ rec {
28892889
# recreate a file hierarchy as when running tests with cargo
28902890
28912891
# the source for test data
2892+
# It's necessary to locate the source in $NIX_BUILD_TOP/source/
2893+
# instead of $NIX_BUILD_TOP/
2894+
# because we compiled those test binaries in the former and not the latter.
2895+
# So all paths will expect source tree to be there and not in the build top directly.
2896+
# For example: $NIX_BUILD_TOP := /build in general, if you ask yourself.
2897+
# TODO(raitobezarius): I believe there could be more edge cases if `crate.sourceRoot`
2898+
# do exist but it's very hard to reason about them, so let's wait until the first bug report.
2899+
mkdir -p source/
2900+
cd source/
2901+
28922902
${pkgs.buildPackages.xorg.lndir}/bin/lndir ${crate.src}
28932903
28942904
# build outputs

crate2nix/templates/nix/crate2nix/default.nix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ rec {
135135
# recreate a file hierarchy as when running tests with cargo
136136
137137
# the source for test data
138+
# It's necessary to locate the source in $NIX_BUILD_TOP/source/
139+
# instead of $NIX_BUILD_TOP/
140+
# because we compiled those test binaries in the former and not the latter.
141+
# So all paths will expect source tree to be there and not in the build top directly.
142+
# For example: $NIX_BUILD_TOP := /build in general, if you ask yourself.
143+
# TODO(raitobezarius): I believe there could be more edge cases if `crate.sourceRoot`
144+
# do exist but it's very hard to reason about them, so let's wait until the first bug report.
145+
mkdir -p source/
146+
cd source/
147+
138148
${pkgs.buildPackages.xorg.lndir}/bin/lndir ${crate.src}
139149
140150
# build outputs

sample_projects/bin_with_git_submodule_dep/Cargo.nix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,16 @@ rec {
14831483
# recreate a file hierarchy as when running tests with cargo
14841484
14851485
# the source for test data
1486+
# It's necessary to locate the source in $NIX_BUILD_TOP/source/
1487+
# instead of $NIX_BUILD_TOP/
1488+
# because we compiled those test binaries in the former and not the latter.
1489+
# So all paths will expect source tree to be there and not in the build top directly.
1490+
# For example: $NIX_BUILD_TOP := /build in general, if you ask yourself.
1491+
# TODO(raitobezarius): I believe there could be more edge cases if `crate.sourceRoot`
1492+
# do exist but it's very hard to reason about them, so let's wait until the first bug report.
1493+
mkdir -p source/
1494+
cd source/
1495+
14861496
${pkgs.buildPackages.xorg.lndir}/bin/lndir ${crate.src}
14871497
14881498
# build outputs

sample_projects/codegen/Cargo.nix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,16 @@ rec {
658658
# recreate a file hierarchy as when running tests with cargo
659659
660660
# the source for test data
661+
# It's necessary to locate the source in $NIX_BUILD_TOP/source/
662+
# instead of $NIX_BUILD_TOP/
663+
# because we compiled those test binaries in the former and not the latter.
664+
# So all paths will expect source tree to be there and not in the build top directly.
665+
# For example: $NIX_BUILD_TOP := /build in general, if you ask yourself.
666+
# TODO(raitobezarius): I believe there could be more edge cases if `crate.sourceRoot`
667+
# do exist but it's very hard to reason about them, so let's wait until the first bug report.
668+
mkdir -p source/
669+
cd source/
670+
661671
${pkgs.buildPackages.xorg.lndir}/bin/lndir ${crate.src}
662672
663673
# build outputs

sample_projects/sub_dir_crates/Cargo.nix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,16 @@ rec {
267267
# recreate a file hierarchy as when running tests with cargo
268268
269269
# the source for test data
270+
# It's necessary to locate the source in $NIX_BUILD_TOP/source/
271+
# instead of $NIX_BUILD_TOP/
272+
# because we compiled those test binaries in the former and not the latter.
273+
# So all paths will expect source tree to be there and not in the build top directly.
274+
# For example: $NIX_BUILD_TOP := /build in general, if you ask yourself.
275+
# TODO(raitobezarius): I believe there could be more edge cases if `crate.sourceRoot`
276+
# do exist but it's very hard to reason about them, so let's wait until the first bug report.
277+
mkdir -p source/
278+
cd source/
279+
270280
${pkgs.buildPackages.xorg.lndir}/bin/lndir ${crate.src}
271281
272282
# build outputs

0 commit comments

Comments
 (0)