@@ -32,18 +32,25 @@
3232 # https://nixos.org/nix/manual/#builtin-filterSource
3333 #
3434 # subDir: Descend into a subdirectory in a way that will compose.
35− # It will be ase if `src = src + "/${subDir}` and filters
35+ # It will be as if `src = src + "/${subDir}` and filters
3636 # already applied to `src` will be respected.
3737 #
3838 # name: Optional name to use as part of the store path.
39− # This defaults `src.name` or otherwise `baseNameOf src`.
40− # We recommend setting `name` whenever `src` is syntactically `./.`.
41− # Otherwise, you depend on `./.`'s name in the parent directory,
42− # which can cause inconsistent names, defeating caching.
39+ # If you do not provide a `name` it wil be derived
40+ # from the `subDir`. You should provide `name` or
41+ # `subDir`. If you do not a warning will be displayed
42+ # and the name used will be `source`.
4343 #
44− cleanSourceWith = { filter ? _path: _type: true, src, subDir ? "", name ? null }:
44+ # caller: Name of the function used in warning message.
45+ # Functions that are implemented using `cleanSourceWith`
46+ # (and forward a `name` argument) can use this to make
47+ # the message to the use more meaningful.
48+ #
49+ cleanSourceWith = { filter ? _path: _type: true, src, subDir ? "", name ? null
50+ , caller ? "cleanSourceWith" }:
4551 let
4652 subDir' = if subDir == "" then "" else "/" + subDir;
53+ subDirName = __replaceStrings ["/"] ["-"] subDir;
4754 # In case this is mixed with older versions of cleanSourceWith
4855 isFiltered = src ? _isLibCleanSourceWith;
4956 isFilteredEx = src ? _isLibCleanSourceWithEx;
@@ -62,10 +69,32 @@
6269 && (filter path type && parentFilter path type));
6370 name' = if name != null
6471 then name
65− else (if isFiltered && src ? name
66− then src.name
67− else baseNameOf src)
68− + (lib.optionalString (origSubDir != "") ("--" + baseNameOf origSubDir));
72+ else
73+ if subDirName != ""
74+ then if src ? name
75+ then src.name + "-" + subDirName
76+ else "source-" + subDirName
77+ else if src ? name
78+ then src.name
79+ else
80+ # No name was provided and one could not be constructed from
81+ # the `subDirName`.
82+
83+ # We used to use `baseNameOf src` as a default here.
84+ # This was cute, but it lead to cache misses. For instance if
85+ # `x = cleanSourceWith { src = ./.; }` then `baseName src`
86+ # will be different when `src` resolves to "/nix/store/X"
87+ # than when it is in a local directory. If people use
88+ # git worktrees they also may wind up with different
89+ # values for `name`. Anything that depends on `x.name` will
90+ # propagate the issue.
91+
92+ # Encourage adding a suitable `name` with:
93+ # * A warning message.
94+ # * A default name that gives a hint as to why there is no name.
95+ __trace (
96+ "WARNING: `${caller}` called on ${toString src} without a `name`. "
97+ + "Consider adding `name = \"${baseNameOf src};\"`") "source";
6998 in {
7099 inherit origSrc origSubDir origSrcSubDir;
71100 filter = filter';