-
Notifications
You must be signed in to change notification settings - Fork 84
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
Add optional location expansion for the nixopts attribute #132
Conversation
Hey there! Walid from Dpulls here. |
@aherrmann why not do this always? i.e. not gate the location expansion by a flag? |
The motivation was to not make this a breaking change. Prior uses of |
I'd go for making this a breaking change. We're otherwise stuck with a more complicated API that breaks precedent with the way location expansion in other rules work (where i've never seen this be gated by a flag). We could introduce Alternatively, maybe it's possible to steal less syntax? Allow Nit: I'd put the |
Fair enough, I'll remove the gating.
That's what the initial implementation did, however based on feedback I moved it to be consistent with Bazel's behavior. |
Addressing review comment #132 (comment)
Addressing review comment #132 (comment)
I've removed the gating and factored out the location expansion into its own module. |
Addressing review comment #132 (comment)
Addressing review comment #132 (comment)
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 feature makes sense to me, but the tests and implementation are a bit involved and rather hard to understand (I couldn’t fit everything into my head during review without spending undue amounts of time on it). More separation into smaller tests/subroutines would help.
nixpkgs/nixpkgs.bzl
Outdated
expr_args.extend(repository_ctx.attr.nixopts) | ||
if repository_ctx.attr.expand_location: | ||
expr_args.extend([ | ||
_expand_location(repository_ctx, opt, nix_file_deps, "nixopts") |
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.
use attr keys for easier reading
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.
Done
tests/BUILD.bazel
Outdated
# Test nixopts location expansion | ||
sh_test( | ||
name = "location-expansion-test", | ||
srcs = ["location_expansion.sh"], | ||
args = [ | ||
"$(POSIX_DIFF)", | ||
"$(rootpath //:nixpkgs.json)", | ||
"$(rootpath //:nixpkgs.nix)", | ||
"$(rootpath //tests:relative_imports/nixpkgs.nix)", | ||
"$(rootpath @nixpkgs_location_expansion_test//:out/nixpkgs.json)", | ||
"$(rootpath @nixpkgs_location_expansion_test//:out/nixpkgs.nix)", | ||
"$(rootpath @nixpkgs_location_expansion_test//:out/relative_imports.nix)", | ||
], | ||
data = [ | ||
"//:nixpkgs.json", | ||
"//:nixpkgs.nix", | ||
"//tests:relative_imports/nixpkgs.nix", | ||
"@nixpkgs_location_expansion_test//:out/nixpkgs.json", | ||
"@nixpkgs_location_expansion_test//:out/nixpkgs.nix", | ||
"@nixpkgs_location_expansion_test//:out/relative_imports.nix", | ||
], | ||
toolchains = ["@rules_sh//sh/posix:make_variables"], | ||
) |
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.
This test is extremely convoluted, it has so many moving parts that it’s hard to understand what it does or why. Needs either a more detailed description of what is done and why, or be split up into more tests that each do a lot less.
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.
For example, the location expansion stuff is just a string replacement, and the algo could be tested as a unit-test entirely; which would mean the integration test needs to test less.
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.
With the factored out pure parsing and label resolution functions it is now possible to define unit tests for these, which I've done. I've simplified this integration test to only test that the mapping to paths works correctly for a file in the main repository and for one in an external repository.
nixpkgs/nixpkgs.bzl
Outdated
# Step through occurrences of `$`. This is bounded by the length of the string. | ||
for _ in range(len(string)): |
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.
This function is a bit too unwieldy for my taste.
I would have said we should split it up into generators, but skylark does not have them. However, the location strings should be short enough for it not to matter.
So e.g. have one function which returns all “$
to end” strings for $ that are not escaped, one function tries to parse the location syntax, and then one function which replaces the location syntax with its actual content.
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've factored out the parsing and label resolution into separate pure functions. With this change it becomes possible to test them in unit tests. I've also changed the parsing logic a bit to make it clearer.
Addressing review comment #132 (comment)
Addressing review comment #132 (comment) Implement parsing of location expansion commands and label resolution as pure functions that do not depend on the repository context. This allows to test these functions in unit tests.
Addressing review comment #132 (comment)
It now only tests the correct mapping of paths in the local workspace and in an external workspace. The parsing of more complicated location strings is tested in the unit tests. Addressing review comment #132 (comment)
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.
This looks great now! I’m confident that it does what we want and is well-tested.
Addressing review comment #132 (comment)
Addressing review comment #132 (comment)
Addressing review comment #132 (comment)
Addressing review comment #132 (comment) Implement parsing of location expansion commands and label resolution as pure functions that do not depend on the repository context. This allows to test these functions in unit tests.
Addressing review comment #132 (comment)
It now only tests the correct mapping of paths in the local workspace and in an external workspace. The parsing of more complicated location strings is tested in the unit tests. Addressing review comment #132 (comment)
This factors out the addition of location expansion for the
nixopts
attribute from #128.This adds an additional attributeThis performs location expansion on the value of theexpand_location
tonixpkgs_package
. When set toTrue
location expansion will be performed on the value of thenixopts
attribute.nixopts
attribute tonixpkgs_packge
. I.e. any occurrence of$(location LABEL)
will be expanded to the path to the file referenced byLABEL
. This feature is motivated by #128 where we need to predict the file path of a dependency from within a macro invoking repository rules.This also adds a test-case for location expansion.