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 testing module and bzlmod tests for toolchains/posix #323

Merged
merged 10 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
build:bzlmod --enable_bzlmod

build --host_platform=@rules_nixpkgs_core//platforms:host

# Using toolchain resolution can lead to spurious dependencies on
# `@local_config_cc//:builtin_include_directory_paths`. This needs to be
# resolved before `--incompatible_enable_cc_toolchain_resolution` can be
# recommended for `nixpkgs_cc_configure_hermetic`.
# build --incompatible_enable_cc_toolchain_resolution

build --java_runtime_version=nixpkgs_java_11
build --java_language_version=11
build --tool_java_runtime_version=nixpkgs_java_11
build --tool_java_language_version=11

# The following lines provide a workaround for a test failure in on the Java
# toolchain on MacOS 11, documented here: https://github.com/tweag/rules_nixpkgs/issues/299.
# They should be removed as soon as that issue has been resolved.
build --enable_platform_specific_config
build:macos --experimental_strict_java_deps=warn

# User Configuration
# ------------------
try-import %workspace%/.bazelrc.local
import %workspace%/.bazelrc.common
import %workspace%/.bazelrc.cc
import %workspace%/.bazelrc.java
Comment on lines +1 to +3
Copy link
Contributor

@benradf benradf Feb 15, 2023

Choose a reason for hiding this comment

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

This is much cleaner.

8 changes: 8 additions & 0 deletions .bazelrc.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
build --crosstool_top=@nixpkgs_config_cc//:toolchain
# Using toolchain resolution can lead to spurious dependencies on
# `@local_config_cc//:builtin_include_directory_paths`. This needs to be
# resolved before `--incompatible_enable_cc_toolchain_resolution` can be
# recommended for `nixpkgs_cc_configure_hermetic`.
# build --incompatible_enable_cc_toolchain_resolution

# vim: ft=conf
10 changes: 10 additions & 0 deletions .bazelrc.common
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
build --host_platform=@rules_nixpkgs_core//platforms:host

build:bzlmod --enable_bzlmod
build:bzlmod --registry=https://bcr.bazel.build

# User Configuration
# ------------------
try-import %workspace%/.bazelrc.local

# vim: ft=conf
18 changes: 18 additions & 0 deletions .bazelrc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Bazel requires a Java runtime to run tests, so any module that contains tests
# will need Java configuration. However, not all modules should have to depend
# on rules_nixpkgs_java to provide a Java runtime, in particular the core
# module should be free of this dependency. Hence, the Java configuration
# exists in a separate configuration file.

build --java_runtime_version=nixpkgs_java_11
build --java_language_version=11
build --tool_java_runtime_version=nixpkgs_java_11
build --tool_java_language_version=11

# The following lines provide a workaround for a test failure in on the Java
# toolchain on MacOS 11, documented here: https://github.com/tweag/rules_nixpkgs/issues/299.
# They should be removed as soon as that issue has been resolved.
build --enable_platform_specific_config
build:macos --experimental_strict_java_deps=warn

# vim: ft=conf
3 changes: 3 additions & 0 deletions .github/build-and-test
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ if [[ ${BZLMOD_ENABLED-} = true ]]; then
declare -ra build_dirs=(
core
toolchains/java
toolchains/posix
)
declare -ra test_dirs=(
testing/core
testing/java
testing/posix
)
declare -ra extra_flags=(
--config=bzlmod
Expand All @@ -51,6 +53,7 @@ else
toolchains/nodejs
testing/core
testing/java
testing/posix
)
declare -ra extra_flags=(
)
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,7 @@ default is <code>None</code>
### nixpkgs_sh_posix_configure

<pre>
nixpkgs_sh_posix_configure(<a href="#nixpkgs_sh_posix_configure-name">name</a>, <a href="#nixpkgs_sh_posix_configure-packages">packages</a>, <a href="#nixpkgs_sh_posix_configure-exec_constraints">exec_constraints</a>, <a href="#nixpkgs_sh_posix_configure-kwargs">kwargs</a>)
nixpkgs_sh_posix_configure(<a href="#nixpkgs_sh_posix_configure-name">name</a>, <a href="#nixpkgs_sh_posix_configure-packages">packages</a>, <a href="#nixpkgs_sh_posix_configure-exec_constraints">exec_constraints</a>, <a href="#nixpkgs_sh_posix_configure-register">register</a>, <a href="#nixpkgs_sh_posix_configure-kwargs">kwargs</a>)
</pre>

Create a POSIX toolchain from nixpkgs.
Expand Down Expand Up @@ -1803,6 +1803,20 @@ default is <code>None</code>

Constraints for the execution platform.

</p>
</td>
</tr>
<tr id="nixpkgs_sh_posix_configure-register">
<td><code>register</code></td>
<td>

optional.
default is <code>True</code>

<p>

Automatically register the generated toolchain if set to True.

</p>
</td>
</tr>
Expand Down
8 changes: 1 addition & 7 deletions core/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
build:bzlmod --enable_bzlmod

build --host_platform=@rules_nixpkgs_core//platforms:host

# User Configuration
# ------------------
try-import %workspace%/.bazelrc.local
import %workspace%/../.bazelrc.common
1 change: 0 additions & 1 deletion testing/core/.bazelrc

This file was deleted.

2 changes: 2 additions & 0 deletions testing/core/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import %workspace%/../../.bazelrc.common
import %workspace%/../../.bazelrc.java
1 change: 0 additions & 1 deletion testing/java/.bazelrc

This file was deleted.

2 changes: 2 additions & 0 deletions testing/java/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import %workspace%/../../.bazelrc.common
import %workspace%/../../.bazelrc.java
2 changes: 2 additions & 0 deletions testing/posix/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import %workspace%/../../.bazelrc.common
import %workspace%/../../.bazelrc.java
Empty file added testing/posix/BUILD.bazel
Empty file.
39 changes: 39 additions & 0 deletions testing/posix/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module(name = "rules_nixpkgs_posix_testing")

bazel_dep(name = "rules_nixpkgs_posix")
local_path_override(
module_name = "rules_nixpkgs_posix",
path = "../../toolchains/posix",
)

bazel_dep(name = "rules_nixpkgs_core")
local_path_override(
module_name = "rules_nixpkgs_core",
path = "../../core",
)

bazel_dep(name = "rules_nixpkgs_java")
local_path_override(
module_name = "rules_nixpkgs_java",
path = "../../toolchains/java",
)

bazel_dep(name = "bazel_skylib", version = "1.0.3")
bazel_dep(name = "rules_sh", version = "0.3.0")

# TODO[AH] Remove these transitive dependencies once nixpkgs_java_configure has
# become a module extension in rules_nixpkgs_java.
bazel_dep(name = "platforms", version = "0.0.4")
bazel_dep(name = "rules_java", version = "4.0.0")

java_toolchains = use_extension("@rules_java//java:extensions.bzl", "toolchains")
use_repo(java_toolchains, "remote_java_tools")

non_module_deps = use_extension("//:non_module_deps.bzl", "non_module_deps")
use_repo(non_module_deps, "nixpkgs")
use_repo(non_module_deps, "nixpkgs_sh_posix_config")
use_repo(non_module_deps, "nixpkgs_sh_posix_config_toolchain")
register_toolchains("@nixpkgs_sh_posix_config_toolchain//:nixpkgs_sh_posix_toolchain")
# Bazel requries a Java runtime to run tests.
use_repo(non_module_deps, "nixpkgs_java_runtime_toolchain")
register_toolchains("@nixpkgs_java_runtime_toolchain//:all")
49 changes: 49 additions & 0 deletions testing/posix/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
workspace(name = "rules_nixpkgs_posix_testing")

local_repository(
name = "rules_nixpkgs_posix",
path = "../../toolchains/posix",
)

local_repository(
name = "rules_nixpkgs_core",
path = "../../core",
)

local_repository(
name = "rules_nixpkgs_java",
path = "../../toolchains/java",
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "bazel_skylib",
sha256 = "f24ab666394232f834f74d19e2ff142b0af17466ea0c69a3f4c276ee75f6efce",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.4.0/bazel-skylib-1.4.0.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.4.0/bazel-skylib-1.4.0.tar.gz",
],
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

http_archive(
name = "rules_sh",
sha256 = "d668bb32f112ead69c58bde2cae62f6b8acefe759a8c95a2d80ff6a85af5ac5e",
strip_prefix = "rules_sh-0.3.0",
urls = ["https://github.com/tweag/rules_sh/archive/v0.3.0.tar.gz"],
)

load("@rules_sh//sh:repositories.bzl", "rules_sh_dependencies")

rules_sh_dependencies()

load(
"//tests:nixpkgs_repositories.bzl",
"nixpkgs_repositories",
)

nixpkgs_repositories(bzlmod = False)
Empty file.
1 change: 1 addition & 0 deletions testing/posix/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions testing/posix/nixpkgs.nix
11 changes: 11 additions & 0 deletions testing/posix/non_module_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load(
"//tests:nixpkgs_repositories.bzl",
"nixpkgs_repositories",
)

def _non_module_deps_impl(ctx):
nixpkgs_repositories(bzlmod = True)

non_module_deps = module_extension(
implementation = _non_module_deps_impl,
)
Comment on lines +6 to +11
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is implied by your TODO comment in testing/posix/MODULE.bazel, but is the idea to later remove this non_module_deps extension and directly call the various *_configure macros that nixpkgs_repositories currently invokes once they've been modularised?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, exactly, that's the plan once #183 is implemented (or as part of it).

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load(":posix-test.bzl", "posix_test_suite")

package(default_testonly = 1)

# Test nixpkgs_sh_posix_configure() checking that Unix commands are in Nix store.
Expand All @@ -13,3 +15,5 @@ sh_test(
],
toolchains = ["@rules_sh//sh/posix:make_variables"],
)

posix_test_suite(name = "posix-test")
26 changes: 26 additions & 0 deletions testing/posix/tests/nixpkgs_repositories.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_local_repository")
load("@rules_nixpkgs_java//:java.bzl", "nixpkgs_java_configure")
load("@rules_nixpkgs_posix//:posix.bzl", "nixpkgs_sh_posix_configure")

def nixpkgs_repositories(*, bzlmod):
nixpkgs_local_repository(
name = "nixpkgs",
nix_file = "//:nixpkgs.nix",
nix_file_deps = ["//:flake.lock"],
)

nixpkgs_java_configure(
name = "nixpkgs_java_runtime",
attribute_path = "jdk11.home",
repository = "@nixpkgs",
toolchain = True,
register = not bzlmod,
toolchain_name = "nixpkgs_java",
toolchain_version = "11",
)

nixpkgs_sh_posix_configure(
name = "nixpkgs_sh_posix_config",
repository = "@nixpkgs",
register = not bzlmod,
)
38 changes: 38 additions & 0 deletions testing/posix/tests/posix-test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load("@nixpkgs_sh_posix_config//:nixpkgs_sh_posix.bzl", "discovered")

ResolvedPosixToolchainInfo = provider()

def _posix_toolchain_impl(ctx):
posix = ctx.toolchains["@rules_sh//sh/posix:toolchain_type"]
return [
ResolvedPosixToolchainInfo(commands = posix.commands)
]

_posix_toolchain = rule(
_posix_toolchain_impl,
toolchains = ["@rules_sh//sh/posix:toolchain_type"],
)

def _posix_runtime_test_impl(ctx):
env = unittest.begin(ctx)

commands = ctx.attr.toolchain[ResolvedPosixToolchainInfo].commands
for (command, path) in commands.items():
asserts.equals(env, path, discovered.get(command), "expected path to {} to match".format(command))

return unittest.end(env)

_posix_runtime_test = unittest.make(
_posix_runtime_test_impl,
attrs = {
"toolchain": attr.label(),
},
)

def posix_test_suite(name):
_posix_toolchain(name = "posix-toolchain")
unittest.suite(
name,
lambda name: _posix_runtime_test(name = name, toolchain = ":posix-toolchain")
)
26 changes: 3 additions & 23 deletions toolchains/cc/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
build --host_platform=@rules_nixpkgs_core//platforms:host

build --crosstool_top=@nixpkgs_config_cc//:toolchain
# Using toolchain resolution can lead to spurious dependencies on
# `@local_config_cc//:builtin_include_directory_paths`. This needs to be
# resolved before `--incompatible_enable_cc_toolchain_resolution` can be
# recommended for `nixpkgs_cc_configure_hermetic`.
# build --incompatible_enable_cc_toolchain_resolution

build --java_runtime_version=nixpkgs_java_11
build --java_language_version=11
build --tool_java_runtime_version=nixpkgs_java_11
build --tool_java_language_version=11

# The following lines provide a workaround for a test failure in on the Java
# toolchain on MacOS 11, documented here: https://github.com/tweag/rules_nixpkgs/issues/299.
# They should be removed as soon as that issue has been resolved.
build --enable_platform_specific_config
build:macos --experimental_strict_java_deps=warn

# User Configuration
# ------------------
try-import %workspace%/.bazelrc.local
import %workspace%/../../.bazelrc.common
import %workspace%/../../.bazelrc.cc
import %workspace%/../../.bazelrc.java
26 changes: 3 additions & 23 deletions toolchains/go/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
build --host_platform=@rules_nixpkgs_core//platforms:host

build --crosstool_top=@nixpkgs_config_cc//:toolchain
# Using toolchain resolution can lead to spurious dependencies on
# `@local_config_cc//:builtin_include_directory_paths`. This needs to be
# resolved before `--incompatible_enable_cc_toolchain_resolution` can be
# recommended for `nixpkgs_cc_configure_hermetic`.
# build --incompatible_enable_cc_toolchain_resolution

build --java_runtime_version=nixpkgs_java_11
build --java_language_version=11
build --tool_java_runtime_version=nixpkgs_java_11
build --tool_java_language_version=11

# The following lines provide a workaround for a test failure in on the Java
# toolchain on MacOS 11, documented here: https://github.com/tweag/rules_nixpkgs/issues/299.
# They should be removed as soon as that issue has been resolved.
build --enable_platform_specific_config
build:macos --experimental_strict_java_deps=warn

# User Configuration
# ------------------
try-import %workspace%/.bazelrc.local
import %workspace%/../../.bazelrc.common
import %workspace%/../../.bazelrc.cc
import %workspace%/../../.bazelrc.java
Loading