Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mkDummySrc: fix handling when src is already filtered #47

Merged
merged 2 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions checks/mkDummySrcTests/default.nix
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
{ linkFarmFromDrvs
{ lib
, linkFarmFromDrvs
, mkDummySrc
, runCommand
}:

let
cmpDummySrc = name: path:
cmpDummySrcRaw = name: input: expected:
let
dummySrc = mkDummySrc {
src = path + "/input";
src = input;
};
in
runCommand "compare-${name}" { } ''
diff -r ${path + /expected} ${dummySrc}
echo ${expected} ${dummySrc}
diff -r ${expected} ${dummySrc}
touch $out
'';

cmpDummySrc = name: path:
let
expected = path + "/expected";
input = path + "/input";

# Regression test for https://github.com/ipetkov/crane/issues/46
filteredInput = lib.cleanSourceWith {
src = input;
filter = path: type:
let baseName = builtins.baseNameOf path;
in
type == "directory" || lib.any (s: lib.hasPrefix s (builtins.baseNameOf path)) [
"Cargo"
"config"
];
};
in
[
(cmpDummySrcRaw name input expected)
(cmpDummySrcRaw "${name}-filtered" filteredInput expected)
];
in
linkFarmFromDrvs "cleanCargoToml" [
linkFarmFromDrvs "cleanCargoToml" (lib.flatten [
(cmpDummySrc "single" ./single)
(cmpDummySrc "single-alt" ./single-alt)
(cmpDummySrc "workspace" ./workspace)
]
])
16 changes: 12 additions & 4 deletions lib/mkDummySrc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,25 @@ let
# directory, we check if it happens to be an ancestor for an interesting file (i.e. is a prefix of
# an interesting file). That way we are left with the smallest possible source needed for our
# dummy derivation, and we bring any cache invalidation to a minimum. Whew!
mkBasePath = p: (toString p) + "/";
uncleanSrcBasePath = mkBasePath src;

uncleanFiles = findCargoFiles src;
# NB: if the `src` we were provided was filtered, make sure that we crawl the `origSrc`! Otherwise
# when we try to crawl the source Nix will evaluate the filter(s) fully resulting in a store path
# whose prefix won't match the paths we observe when we try to clean the source a bit further down
# (Nix optimizes multiple filters by running them all once against the original source).
# https://github.com/ipetkov/crane/issues/46
origSrc =
if src ? _isLibCleanSourceWith
then src.origSrc
else src;

uncleanSrcBasePath = (toString origSrc) + "/";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is done to forcefully evaluate the source derivation? Wouldn't this already be evaluated in the call to findCargoFiles?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a good enough intuitive understanding of how Nix treats paths and strings (and coercing between the two) to explain why this is needed... I did try taking it out and a bunch of tests failed so it's load bearing at some capacity 😅

uncleanFiles = findCargoFiles origSrc;

cargoTomlsBase = uncleanSrcBasePath;
inherit (uncleanFiles) cargoTomls;

cleanSrc =
let
adjustPaths = builtins.map (p: removePrefix uncleanSrcBasePath (toString p));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I correct in saying that this was added in 4f2b1c4 but wasn't actually used (I'm assuming it was just leftover from an iteration of that commit)?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I think I was iterating on whether to make the filter on the next line as its own variable, but it looks like it was never used (since Nix is lazy it just ignores unused input 🤷 )

allUncleanFiles = map
(p: removePrefix uncleanSrcBasePath (toString p))
# Allow the default `Cargo.lock` location to be picked up here
Expand Down