Skip to content

Commit 44ecbc8

Browse files
authored
Improve cleaning of stack projects (#1074)
Should help with #1013
1 parent 6c9facf commit 44ecbc8

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

lib/call-stack-to-nix.nix

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,20 @@ let
2323
}) resolver fetchedResolver;
2424

2525
subDir' = src.origSubDir or "";
26+
subDir = pkgs.lib.strings.removePrefix "/" subDir';
27+
maybeCleanedSource =
28+
if haskellLib.canCleanSource src
29+
then (haskellLib.cleanSourceWith {
30+
name = if name != null then "${name}-root-cabal-files" else "source-root-cabal-files";
31+
src = src.origSrc or src;
32+
filter = path: type: (!(src ? filter) || src.filter path type) && (
33+
type == "directory" ||
34+
pkgs.lib.any (i: (pkgs.lib.hasSuffix i path)) [ stackYaml ".cabal" "package.yaml" ]); })
35+
else src.origSrc or src;
36+
2637
stackToNixArgs = builtins.concatStringsSep " " [
2738
"--full"
28-
"--stack-yaml=$SRC/${stackYaml}"
39+
"--stack-yaml=$SRC${subDir'}/${stackYaml}"
2940
(if ignorePackageYaml then "--ignore-package-yaml" else "")
3041
"-o ."
3142
];
@@ -46,18 +57,22 @@ let
4657
preferLocalBuild = false;
4758
} (''
4859
mkdir -p $out${subDir'}
60+
SRC=$(mktemp -d)
61+
cd $SRC
62+
# if maybeCleanedSource is empty, this means it's a new
63+
# project where the files haven't been added to the git
64+
# repo yet. We fail early and provide a useful error
65+
# message to prevent headaches (#290).
66+
if [ -z "$(ls -A ${maybeCleanedSource})" ]; then
67+
echo "cleaned source is empty. Did you forget to 'git add -A'?"; exit 1;
68+
fi
69+
lndir -silent "${maybeCleanedSource}/." $SRC
70+
${pkgs.lib.optionalString (subDir != "") "cd ${subDir}"}
4971
${
50-
# If no resolver was fetched use the original stack.yaml
51-
if fetchedResolver == null
52-
then ''
53-
SRC=${src}
54-
''
55-
else
72+
# If a resolver was fetched use the it instead of the original stack.yaml
73+
pkgs.lib.optionalString (fetchedResolver != null)
5674
# Replace the resolver path in the stack.yaml with the fetched version
5775
''
58-
SRC=$(mktemp -d)
59-
cd $SRC
60-
lndir -silent "${src}/." $SRC
6176
rm ${stackYaml}
6277
cp ${src}/${stackYaml} .
6378
chmod +w ${stackYaml}
@@ -71,7 +86,7 @@ let
7186
# We need to strip out any references to $src, as those won't
7287
# be accessable in restricted mode.
7388
for nixf in $(find $out -name "*.nix" -type f); do
74-
substituteInPlace $nixf --replace "$SRC" "."
89+
substituteInPlace $nixf --replace "$SRC${subDir'}" "."
7590
done
7691
7792
# move pkgs.nix to default.nix ensure we can just nix `import` the result.

lib/fetch-resolver.nix

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
, resolverSha256 ? null
66
}:
77
let
8+
srcDir = src.origSrcSubDir or src;
9+
810
# Using origSrcSubDir bypasses any cleanSourceWith so that it will work when
911
# access to the store is restricted. If origSrc was already in the store
1012
# you can pass the project in as a string.
11-
rawStackYaml = builtins.readFile ((src.origSrcSubDir or src) + ("/" + stackYaml));
13+
rawStackYaml = builtins.readFile (srcDir + "/${stackYaml}");
1214

1315
# Determine the resolver as it may point to another file we need
1416
# to look at.
@@ -28,6 +30,8 @@ let
2830
url = resolver;
2931
sha256 = resolverSha256;
3032
}
31-
else null;
33+
else if resolver != null && __pathExists (srcDir + "/${resolver}")
34+
then pkgs.copyPathToStore (srcDir + "/${resolver}")
35+
else null;
3236

3337
in { inherit resolver fetchedResolver; }

0 commit comments

Comments
 (0)