Add optional location expansion for the nixopts attribute#132
Add optional location expansion for the nixopts attribute#132mergify[bot] merged 16 commits intomasterfrom
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)
Profpatsch
left a comment
There was a problem hiding this comment.
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.
| 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.
use attr keys for easier reading
| # 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.
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.
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.
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.
| # 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.
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.
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)
Profpatsch
left a comment
There was a problem hiding this comment.
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
nixoptsattribute from #128.This adds an additional attributeThis performs location expansion on the value of theexpand_locationtonixpkgs_package. When set toTruelocation expansion will be performed on the value of thenixoptsattribute.nixoptsattribute 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.