···252252 - (+) That can change depending on which files are included, so if it's used for `fileFilter`
253253 it would change the `subpath`/`components` value depending on which files are included.
254254- (+) If necessary, this restriction can be relaxed later, the opposite wouldn't be possible
255255-256256-## To update in the future
257257-258258-Here's a list of places in the library that need to be updated in the future:
259259-- If/Once a function exists that can optionally include a path depending on whether it exists, the error message for the path not existing in `_coerce` should mention the new function
+31
lib/fileset/default.nix
···1111 Basics:
1212 - [Implicit coercion from paths to file sets](#sec-fileset-path-coercion)
13131414+ - [`lib.fileset.maybeMissing`](#function-library-lib.fileset.maybeMissing):
1515+1616+ Create a file set from a path that may be missing.
1717+1418 - [`lib.fileset.trace`](#function-library-lib.fileset.trace)/[`lib.fileset.traceVal`](#function-library-lib.fileset.trace):
15191620 Pretty-print file sets for debugging.
···105109 _difference
106110 _mirrorStorePath
107111 _fetchGitSubmodulesMinver
112112+ _emptyWithoutBase
108113 ;
109114110115 inherit (builtins)
···147152 ;
148153149154in {
155155+156156+ /*
157157+ Create a file set from a path that may or may not exist:
158158+ - If the path does exist, the path is [coerced to a file set](#sec-fileset-path-coercion).
159159+ - If the path does not exist, a file set containing no files is returned.
160160+161161+ Type:
162162+ maybeMissing :: Path -> FileSet
163163+164164+ Example:
165165+ # All files in the current directory, but excluding main.o if it exists
166166+ difference ./. (maybeMissing ./main.o)
167167+ */
168168+ maybeMissing =
169169+ path:
170170+ if ! isPath path then
171171+ if isStringLike path then
172172+ throw ''
173173+ lib.fileset.maybeMissing: Argument ("${toString path}") is a string-like value, but it should be a path instead.''
174174+ else
175175+ throw ''
176176+ lib.fileset.maybeMissing: Argument is of type ${typeOf path}, but it should be a path instead.''
177177+ else if ! pathExists path then
178178+ _emptyWithoutBase
179179+ else
180180+ _singleton path;
150181151182 /*
152183 Incrementally evaluate and trace a file set in a pretty way.
+2-1
lib/fileset/internal.nix
···181181 ${context} is of type ${typeOf value}, but it should be a file set or a path instead.''
182182 else if ! pathExists value then
183183 throw ''
184184- ${context} (${toString value}) is a path that does not exist.''
184184+ ${context} (${toString value}) is a path that does not exist.
185185+ To create a file set from a path that may not exist, use `lib.fileset.maybeMissing`.''
185186 else
186187 _singleton value;
187188
+36-1
lib/fileset/tests.sh
···413413\s*Note that this only works for sources created from paths.'
414414415415# Path coercion errors for non-existent paths
416416-expectFailure 'toSource { root = ./.; fileset = ./a; }' 'lib.fileset.toSource: `fileset` \('"$work"'/a\) is a path that does not exist.'
416416+expectFailure 'toSource { root = ./.; fileset = ./a; }' 'lib.fileset.toSource: `fileset` \('"$work"'/a\) is a path that does not exist.
417417+\s*To create a file set from a path that may not exist, use `lib.fileset.maybeMissing`.'
417418418419# File sets cannot be evaluated directly
419420expectFailure 'union ./. ./.' 'lib.fileset: Directly evaluating a file set is not supported.
···14491450checkGitTracked
1450145114511452rm -rf -- *
14531453+14541454+## lib.fileset.maybeMissing
14551455+14561456+# Argument must be a path
14571457+expectFailure 'maybeMissing "someString"' 'lib.fileset.maybeMissing: Argument \("someString"\) is a string-like value, but it should be a path instead.'
14581458+expectFailure 'maybeMissing null' 'lib.fileset.maybeMissing: Argument is of type null, but it should be a path instead.'
14591459+14601460+tree=(
14611461+)
14621462+checkFileset 'maybeMissing ./a'
14631463+checkFileset 'maybeMissing ./b'
14641464+checkFileset 'maybeMissing ./b/c'
14651465+14661466+# Works on single files
14671467+tree=(
14681468+ [a]=1
14691469+ [b/c]=0
14701470+ [b/d]=0
14711471+)
14721472+checkFileset 'maybeMissing ./a'
14731473+tree=(
14741474+ [a]=0
14751475+ [b/c]=1
14761476+ [b/d]=0
14771477+)
14781478+checkFileset 'maybeMissing ./b/c'
14791479+14801480+# Works on directories
14811481+tree=(
14821482+ [a]=0
14831483+ [b/c]=1
14841484+ [b/d]=1
14851485+)
14861486+checkFileset 'maybeMissing ./b'
1452148714531488# TODO: Once we have combinators and a property testing library, derive property tests from https://en.wikipedia.org/wiki/Algebra_of_sets
14541489