-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
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
lib.fileset.difference
: init
#259065
lib.fileset.difference
: init
#259065
Conversation
bee5e1d
to
b93c133
Compare
b93c133
to
4f1dd7b
Compare
lib/fileset/default.nix
Outdated
difference ./. ./tests | ||
|
||
let | ||
# A list of Nix files |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# A list of Nix files | |
# A set of Nix-related files |
nix/
may contain non-.nix
files, which could be considered non-Nix.
lib/fileset/internal.nix
Outdated
fileset2._internalBaseComponents | ||
); | ||
|
||
# The filesetTree's need to have the same base path in order to be able to compute the difference between them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# The filesetTree's need to have the same base path in order to be able to compute the difference between them. | |
# The filesetTree's need to have the same base path in order to be able to compute the difference between them. | |
# Technically an unrelated root in the second, negative argument could be considered not to match any files in | |
# the first argument, but users would be clueless as to why their filter doesn't work. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm not sure what you mean here. If the root of the second argument is unrelated (e.g. difference /foo /bar
), then no files inside /foo
match /bar
, meaning nothing gets subtracted, so the result is /foo
. And that should be expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of entirely unrelated trees, such as when one is an absolute path, but the other is a fetchGit
lazy tree.
Rejecting difference /foo /bar
seems excessive, I agree.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, it will error for different filesystem roots, there's a comment on this in the comments for _difference
:
# The filesets must already be coerced and validated to be in the same filesystem root.
The public difference
function passes its arguments through _coerceMany
, which checks against this, before calling the internal one.
lib/fileset/internal.nix
Outdated
# Compute the set difference between two file sets. | ||
# The filesets must already be coerced and validated to be in the same filesystem root. | ||
# Type: Fileset -> Fileset -> Fileset | ||
_difference = fileset1: fileset2: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_difference = fileset1: fileset2: | |
_difference = positive: negative: |
_difference = fileset1: fileset2: | |
_difference = baseSet: negatingSet: |
_difference = fileset1: fileset2: | |
_difference = fileset: subtrahend: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I'll also apply this to the user-facing function. I like positive/negative
the most
lib/fileset/internal.nix
Outdated
# The common prefix is the same as the first path, so the second path is equal or longer. | ||
# We need to shorten the second arguments tree to the same base path as the first one |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not clear to me why.
What is tree2
for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as the tree of the negative file set, but with the same base path as the positive file set, such that they are comparable and can be operated on.
E.g. if you have difference /foo /foo/bar
, the common path would be /foo
, meaning the first branch applies.
It then takes the negative file set, (base: /foo/bar, tree: "directory")
in this case, and shortens the base to /foo
instead, such that you get (base: /foo, tree: { bar = "directory"; })
. tree2
here only contains the tree part though, so { bar = "directory"; }
.
Now we have two trees, "directory"
from the positive file set, and { bar = "directory"; }
from the negative file set, that have the same base /foo
, so they can be operated on, and it matches the base path that the result should have.
I'll put something like this in the code docs, and I'll improve the variable name, negativeTreeWithPositiveBasePath
or so (maybe shorter :P)
lib/fileset/internal.nix
Outdated
# The result is always the left hand side, because nothing gets removed | ||
lhs | ||
else | ||
# Otherwise we always have two attribute sets which we can recurse into |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Otherwise we always have two attribute sets which we can recurse into | |
# Otherwise we always have two attribute sets to recurse into |
[c]=1 | ||
) | ||
checkFileset 'difference ./. ./a/x' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about removing the parent? e.g.
difference ./a ./.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the test could be more systematic, by framing it as a matrix of possible arguments. Perhaps not an actual matrix in terms of code, but laid out in a way that you can see that all combinations are tested.
That way you would naturally test difference ./a ./.
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did that!
2d4b2e4
to
cb9a53d
Compare
@roberth Should be good now! |
lib/fileset/default.nix
Outdated
{ | ||
context = "first argument"; | ||
value = positive; | ||
} | ||
{ | ||
context = "second argument"; | ||
value = negative; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{ | |
context = "first argument"; | |
value = positive; | |
} | |
{ | |
context = "second argument"; | |
value = negative; | |
} | |
{ | |
context = "first argument (positive set)"; | |
value = positive; | |
} | |
{ | |
context = "second argument (negative set)"; | |
value = negative; | |
} |
Not 100% sure, but might be nice.
lib/fileset/internal.nix
Outdated
# because under the base path of `/foo/bar`, everything from the negative file set is included | ||
# For `difference /foo /bar`, `negativeTreePositiveBase = null` | ||
# because under the base path of `/foo`, nothing from the negative file set is included | ||
negativeTreePositiveBase = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
negativeTreePositiveBase = | |
negativeTreeWithPositiveBase = |
cb9a53d
to
50df7f9
Compare
Done now, unless you do it before me, I'll merge once CI passes :) |
Description of changes
This is another split off from the WIP file set combinators PR. This PR adds one function:
lib.fileset.difference
: Subtracts the second file set from the first.This work is sponsored by Antithesis ✨
Things done