Skip to content
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

Separate core tests and test with bzlmod enabled #319

Merged
merged 38 commits into from
Feb 10, 2023
Merged

Conversation

aherrmann
Copy link
Member

@aherrmann aherrmann commented Feb 8, 2023

Related to #183.
This PR builds and tests rules_nixpkgs_core with bzlmod enabled.

Following the motivation laid out in tweag/rules_sh#41, this separates the tests out into a dedicated testing module, testing/core. The naming is chosen such that further testing modules can be added for other modules, e.g. testing/cc.

The CI workflow is extended to run both, the old tests in WORKSPACE mode, and to test core and testing/core with bzlmod enabled.

The location expansion unit tests remain within core. The reason is that these are just unit-tests and don't require extra dependencies or module extensions. And bazel test //... (as run by the CI script) fails if there are no test targets, see bazelbuild/bazel#7291 (comment).

The core module contains no more tests after this PR. bazel test needs to resolve a Java runtime, meaning core would either have to depend on rules_nixpkgs_java (problematic due to tweag/rules_sh#41) or pick a local or bindist Java runtime (problematic for hermeticity and on NixOS). Unfortunately, bazel test //... fails when there are no test targets, so the CI script is adapted to run bazel build in core instead.

The testing module testing/core configures a Java runtime using rules_nixpkgs_java. This uses the WORKSPACE.bzlmod fall-back in when bzlmod is enabled as rules_nixpkgs_java is not bzlmod compatible, yet.

This does not yet add a module extension to @rules_nixpkgs_core. Instead, testing/core uses an ad-hoc non_module_deps module extension to invoke the repository rules provided by @rules_nixpkgs_core. Creating actual module extensions will be done in a separate PR.

Most of the functionality worked without change with bzlmod enabled. However, the location expansion feature for the nixopts attribute broke due to bzlmod's use of module remapping and workspace name mangling. This is addressed by passing the labels to nix_file_deps in stringly form along their actual labels in a dictionary. This mapping is then used to resolve the stringly labels in the location expansion expressions to the actual file paths. This somewhat related to bazelbuild/bazel#17260. However, the new native.package_relative_label would probably not address this issue, as the labels in the location expressions need to be resolved to paths within the nixpkgs_package repository rule implementation, not a macro.

The goal is to move the tests in rules_nixpkgs_core into this testing
module for BCR compatibility. The tests in rules_nixpkgs_core incur
extra dependencies that should not be part of rules_nixpkgs_core. In
principle, such dependencies can be marked as dev-dependencies.
However, since rules_nixpkgs only consists of repository rules (or in
the future module extensions) those are easier to test in a dedicated
module.
Requires a workaround for bzlmod module name mangling and remapping.
For now this requires manual name mangling.
These require some workarounds for module mapping and equality not
working on labels pointing to unknown external workspaces.
The labels as expected by the user are passed in as strings in a mapping
to the actual labels which will be exposed with mangled names in the
repository rule implementation. The mapping allows to map the user
defined location variables to the corresponding paths without exposing
mangled labels to the user.
Move the docstring to nixpkgs_package and reference it from the cc site.
Skylib offers diff_test for this exact purpose.
It uses `diff` (or `fc.exe` on Windows) under the hood.
That test case had been forgotten in the test-target.
These have been moved to testing/core
The file is needed by docs and propagated as a dependency to the other
toolchains through docs/dependencies_2.bzl.
Setting these flags in `.bazelrc` incurred a required for a Java
toolchain. This, in turn, would require a dependency on
rules_nixpkgs_java from rules_nixpkgs_core to set up a Java toolchain.
I'd like to avoid that kind of circular dependency.
The core module no longer contains any test targets, and unfortunately
bazel test fails when there are no test targets defined.
Any Bazel workspace that has tests seems to depend on a JDK toolchain
due to
```
ERROR: .../external/remote_coverage_tools/BUILD:10:12: While resolving toolchains for target @remote_coverage_tools//:Main: No matching toolchains found for types @bazel_tools//tools/jdk:runtime_toolchain_type.
To debug, rerun with --toolchain_resolution_debug='@bazel_tools//tools/jdk:runtime_toolchain_type'
If platforms or toolchains are a new concept for you, we'd encourage reading https://bazel.build/concepts/platforms-intro.
```
Bazel needs a Java runtime to execute tests.
@aherrmann
Copy link
Member Author

CI is finally green, so this is ready for review.

Copy link
Member

@benradf benradf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave this a try locally and it all looks good. Great to see the bzlmod-ularization of rules_nixpkgs progressing!

"nixpkgs_package",
)

def nixpkgs_repositories(*, bzlmod):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see two invocations of this macro (one with bzlmod = True and one with bzlmod = False), but unless there's some magic going on it doesn't look like the parameter is actually used at the moment. Is it intended to be in the future?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, it was needed at one point during development, but is no longer necessary here. I've removed it.

)

### end dependencies for tests
rules_nixpkgs_dependencies(toolchains = [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to ask why pass toolchains = [] explicitly instead of relying on the default of None, but then I saw None means load all toolchains.

@aherrmann aherrmann added the merge-queue merge on green CI label Feb 10, 2023
@aherrmann
Copy link
Member Author

@Mergifyio refresh

@mergify
Copy link
Contributor

mergify bot commented Feb 10, 2023

refresh

✅ Pull request refreshed

@aherrmann
Copy link
Member Author

The renaming of the CI steps to capture workspace and bzlmod required an update to the Mergify config as well. Merging manually because of that.

@aherrmann aherrmann merged commit 04a2b58 into master Feb 10, 2023
@aherrmann aherrmann deleted the bzlmod-core branch February 10, 2023 15:36
@mergify mergify bot removed the merge-queue merge on green CI label Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants