From 19b11046353a1924fdc404f8a653b0860f4afc0c Mon Sep 17 00:00:00 2001 From: Googler Date: Thu, 8 Aug 2024 03:37:03 -0700 Subject: [PATCH] Make sure starlark_* tests work with --enable_workspace=false A part of https://github.com/bazelbuild/bazel/pull/23087 Working towards: https://github.com/bazelbuild/bazel/issues/23023 PiperOrigin-RevId: 660767730 Change-Id: I6ba96928d6aaa3e6bc93a4ce442aa1b04992c5c0 --- .../bazel/starlark_git_repository_test.sh | 184 +++++++------ .../shell/bazel/starlark_prefetching_test.sh | 50 ++-- .../shell/bazel/starlark_repository_test.sh | 252 +++++++++--------- 3 files changed, 238 insertions(+), 248 deletions(-) diff --git a/src/test/shell/bazel/starlark_git_repository_test.sh b/src/test/shell/bazel/starlark_git_repository_test.sh index 46222d5ffdaffd..910b6411252a7c 100755 --- a/src/test/shell/bazel/starlark_git_repository_test.sh +++ b/src/test/shell/bazel/starlark_git_repository_test.sh @@ -68,7 +68,6 @@ fi # # Unpacks the test Git repositories in the test temporary directory. function set_up() { - bazel clean --expunge local repos_dir=$TEST_TMPDIR/repos if [ -e "$repos_dir" ]; then rm -rf $repos_dir @@ -85,6 +84,8 @@ function set_up() { tar zxf refetch-repo.tar.gz tar zxf strip-prefix-repo.tar.gz + setup_module_dot_bazel + # Fix environment variables for a hermetic use of git. export GIT_CONFIG_NOSYSTEM=1 export GIT_CONFIG_NOGLOBAL=1 @@ -92,6 +93,11 @@ function set_up() { export XDG_CONFIG_HOME= } +# Shutdown Bazel so that we can safely delete files on Windows +function tear_down() { + bazel shutdown +} + function get_pluto_repo() { echo "$TEST_TMPDIR/repos/pluto" } @@ -102,7 +108,7 @@ function get_pluto_repo() { # following files: # # pluto/ -# WORKSPACE +# MODULE.bazel # BUILD # info # @@ -111,14 +117,14 @@ function get_pluto_repo() { # # pluto/ # pluto/ -# WORKSPACE +# MODULE.bazel # BUILD # info # # In each case, set up workspace with the following files: # # $WORKSPACE_DIR/ -# WORKSPACE +# MODULE.bazel # planets/ # BUILD # planet_info.sh @@ -133,9 +139,8 @@ function do_git_repository_test() { [ $# -eq 2 ] && strip_prefix="strip_prefix=\"$2\"," [ $# -eq 3 ] && shallow_since="shallow_since=\"$3\"," # Create a workspace that clones the repository at the first commit. - cd $WORKSPACE_DIR - cat >> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel < planets/planet_info.sh <> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel <> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel < planets/planet_info.sh <> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel < planets/planet_info.sh <> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel < planets/planet_info.sh <> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel <& $TEST_log || fail "Build failed" expect_log "Cloning" - assert_contains "GIT 1" bazel-genfiles/external/g/go + assert_contains "GIT 1" bazel-genfiles/external/+_repo_rules+g/go # Without changing anything, restart the server, which should not cause the checkout to be re-cloned. bazel --batch build @g//:g >& $TEST_log || fail "Build failed" expect_not_log "Cloning" - assert_contains "GIT 1" bazel-genfiles/external/g/go + assert_contains "GIT 1" bazel-genfiles/external/+_repo_rules+g/go # Change the commit id, which should cause the checkout to be re-cloned. - rm WORKSPACE - cat >> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel <& $TEST_log || fail "Build failed" expect_log "Cloning" - assert_contains "GIT 2" bazel-genfiles/external/g/go + assert_contains "GIT 2" bazel-genfiles/external/+_repo_rules+g/go - # Change the WORKSPACE but not the commit id, which should not cause the checkout to be re-cloned. - rm WORKSPACE - cat >> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel <& $TEST_log || fail "Build failed" expect_not_log "Cloning" - assert_contains "GIT 2" bazel-genfiles/external/g/go + assert_contains "GIT 2" bazel-genfiles/external/+_repo_rules+g/go } function test_git_repository_not_refetched_on_server_restart_strip_prefix() { local repo_dir=$TEST_TMPDIR/repos/refetch # Change the strip_prefix which should cause a new checkout - cat >> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel <& $TEST_log || fail "Build failed" expect_log "Cloning" - assert_contains "GIT 2" bazel-genfiles/external/g/gdir/go + assert_contains "GIT 2" bazel-genfiles/external/+_repo_rules+g/gdir/go - rm WORKSPACE - cat >> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel <& $TEST_log || fail "Build failed" expect_log "Cloning" - assert_contains "GIT 2" bazel-genfiles/external/g/go + assert_contains "GIT 2" bazel-genfiles/external/+_repo_rules+g/go } function test_git_repository_refetched_when_commit_changes() { local repo_dir=$TEST_TMPDIR/repos/refetch - cd $WORKSPACE_DIR - cat >> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel <& $TEST_log || fail "Build failed" expect_log "Cloning" - assert_contains "GIT 1" bazel-genfiles/external/g/go + assert_contains "GIT 1" bazel-genfiles/external/+_repo_rules+g/go # Change the commit id, which should cause the checkout to be re-cloned. - rm WORKSPACE - cat >> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel <& $TEST_log || fail "Build failed" expect_log "Cloning" - assert_contains "GIT 2" bazel-genfiles/external/g/go + assert_contains "GIT 2" bazel-genfiles/external/+_repo_rules+g/go } function test_git_repository_and_nofetch() { local repo_dir=$TEST_TMPDIR/repos/refetch - cd $WORKSPACE_DIR - cat >> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel <& $TEST_log && fail "Build succeeded" expect_log "fetching repositories is disabled" bazel build @g//:g >& $TEST_log || fail "Build failed" - assert_contains "GIT 1" bazel-genfiles/external/g/go + assert_contains "GIT 1" bazel-genfiles/external/+_repo_rules+g/go - rm WORKSPACE - cat >> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel <& $TEST_log || fail "Build failed" - expect_log "External repository 'g' is not up-to-date" - assert_contains "GIT 1" bazel-genfiles/external/g/go + expect_log "External repository '+_repo_rules+g' is not up-to-date" + assert_contains "GIT 1" bazel-genfiles/external/+_repo_rules+g/go bazel build @g//:g >& $TEST_log || fail "Build failed" - assert_contains "GIT 2" bazel-genfiles/external/g/go + assert_contains "GIT 2" bazel-genfiles/external/+_repo_rules+g/go - rm WORKSPACE - cat >> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel <& $TEST_log || fail "Build failed" - expect_log "External repository 'g' is not up-to-date" + expect_log "External repository '+_repo_rules+g' is not up-to-date" bazel build @g//:g >& $TEST_log || fail "Build failed" - assert_contains "GIT 2" bazel-genfiles/external/g/go + assert_contains "GIT 2" bazel-genfiles/external/+_repo_rules+g/go } # Helper function for setting up the workspace as follows # # $WORKSPACE_DIR/ -# WORKSPACE +# MODULE.bazel # planets/ # planet_info.sh # BUILD function setup_error_test() { - cd $WORKSPACE_DIR mkdir -p planets cat > planets/planet_info.sh < planets/BUILD <> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel <> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel <> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel <> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel <> $(create_workspace_with_default_repos WORKSPACE) <> MODULE.bazel < BUILD <& $TEST_log || fail "Expect bazel info to succeed." + bazel build //:all >& $TEST_log || fail "Expect bazel build to succeed." } run_suite "Starlark git_repository tests" diff --git a/src/test/shell/bazel/starlark_prefetching_test.sh b/src/test/shell/bazel/starlark_prefetching_test.sh index dfceb7f261a4ae..b7f41d23af7d22 100755 --- a/src/test/shell/bazel/starlark_prefetching_test.sh +++ b/src/test/shell/bazel/starlark_prefetching_test.sh @@ -35,7 +35,6 @@ test_label_arg() { def _impl(ctx): # Access the build file late ctx.execute(["/bin/sh", "-c", "date +%s >> ${WRKDIR}/log"]) - ctx.file("WORKSPACE", "workspace(name = \"%s\")\n" % ctx.name) ctx.symlink(ctx.attr.build_file, "BUILD") myrule=repository_rule(implementation=_impl, @@ -50,11 +49,10 @@ genrule( cmd = "echo foo > $@", ) EOF - cat > WORKSPACE <<'EOF' -load("//:rule.bzl", "myrule") + cat > $(setup_module_dot_bazel) <<'EOF' +myrule = use_repo_rule("//:rule.bzl", "myrule") myrule(name="ext", build_file="//:ext.BUILD") EOF - write_default_lockfile "MODULE.bazel.lock" bazel build @ext//:foo || fail "expected success" [ `cat "${WRKDIR}/log" | wc -l` -eq 1 ] \ || fail "did not find precisely one invocation of the action" @@ -70,7 +68,7 @@ test_unused_invalid_label_arg() { touch BUILD cat > rule.bzl <<'EOF' def _impl(ctx): - ctx.file("WORKSPACE", "workspace(name = \"%s\")\n" % ctx.name) + ctx.file("REPO.bazel") ctx.file("BUILD", "genrule(name=\"foo\", outs=[\"foo.txt\"], cmd = \"echo foo > $@\")") @@ -79,11 +77,10 @@ myrule=repository_rule(implementation=_impl, "unused" : attr.label(), }) EOF - cat > WORKSPACE <<'EOF' -load("//:rule.bzl", "myrule") + cat > $(setup_module_dot_bazel) <<'EOF' +myrule = use_repo_rule("//:rule.bzl", "myrule") myrule(name="ext", unused="//does/not/exist:file") EOF - write_default_lockfile "MODULE.bazel.lock" bazel build @ext//:foo || fail "expected success" } @@ -100,7 +97,7 @@ test_label_list_arg() { cat > rule.bzl <> ${WRKDIR}/log"]) - ctx.file("WORKSPACE", "workspace(name = \"%s\")\n" % ctx.name) + ctx.file("REPO.bazel") ctx.file("BUILD", """ genrule( name="foo", @@ -117,11 +114,10 @@ myrule=repository_rule(implementation=_impl, "data" : attr.label_list(), }) EOF - cat > WORKSPACE <<'EOF' -load("//:rule.bzl", "myrule") + cat > $(setup_module_dot_bazel) <<'EOF' +myrule = use_repo_rule("//:rule.bzl", "myrule") myrule(name="ext", data = ["//:a.txt", "//:b.txt"]) EOF - write_default_lockfile "MODULE.bazel.lock" echo Hello > a.txt echo World > b.txt bazel build @ext//:foo || fail "expected success" @@ -140,7 +136,7 @@ test_unused_invalid_label_list_arg() { touch BUILD cat > rule.bzl <<'EOF' def _impl(ctx): - ctx.file("WORKSPACE", "workspace(name = \"%s\")\n" % ctx.name) + ctx.file("REPO.bazel") ctx.file("BUILD", "genrule(name=\"foo\", outs=[\"foo.txt\"], cmd = \"echo foo > $@\")") @@ -149,12 +145,11 @@ myrule=repository_rule(implementation=_impl, "unused_list" : attr.label_list(), }) EOF - cat > WORKSPACE <<'EOF' -load("//:rule.bzl", "myrule") + cat > $(setup_module_dot_bazel) <<'EOF' +myrule = use_repo_rule("//:rule.bzl", "myrule") myrule(name="ext", unused_list=["//does/not/exist:file1", "//does/not/exists:file2"]) EOF - write_default_lockfile "MODULE.bazel.lock" bazel build @ext//:foo || fail "expected success" } @@ -172,7 +167,7 @@ test_label_keyed_string_dict_arg() { cat > rule.bzl <> ${WRKDIR}/log"]) - ctx.file("WORKSPACE", "workspace(name = \"%s\")\n" % ctx.name) + ctx.file("REPO.bazel") ctx.file("BUILD", """ genrule( name = "foo", @@ -192,11 +187,10 @@ myrule = repository_rule( }, ) EOF - cat > WORKSPACE <<'EOF' -load("//:rule.bzl", "myrule") + cat > $(setup_module_dot_bazel) <<'EOF' +myrule = use_repo_rule("//:rule.bzl", "myrule") myrule(name="ext", data = {"//:a.txt": "a", "//:b.txt": "b"}) EOF - write_default_lockfile "MODULE.bazel.lock" echo Hello > a.txt echo World > b.txt bazel build @ext//:foo || fail "expected success" @@ -215,7 +209,7 @@ test_unused_invalid_label_keyed_string_dict_arg() { touch BUILD cat > rule.bzl <<'EOF' def _impl(ctx): - ctx.file("WORKSPACE", "workspace(name = \"%s\")\n" % ctx.name) + ctx.file("REPO.bazel") ctx.file("BUILD", "genrule(name=\"foo\", outs=[\"foo.txt\"], cmd = \"echo foo > $@\")") @@ -224,12 +218,11 @@ myrule=repository_rule(implementation=_impl, "unused_dict" : attr.label_keyed_string_dict(), }) EOF - cat > WORKSPACE <<'EOF' -load("//:rule.bzl", "myrule") + cat > $(setup_module_dot_bazel) <<'EOF' +myrule = use_repo_rule("//:rule.bzl", "myrule") myrule(name="ext", unused_dict={"//does/not/exist:file1": "file1", "//does/not/exists:file2": "file2"}) EOF - write_default_lockfile "MODULE.bazel.lock" bazel build @ext//:foo || fail "expected success" } @@ -237,12 +230,12 @@ EOF function test_files_tracked_with_non_existing_files() { cat > rules.bzl <<'EOF' def _repo_impl(ctx): - ctx.symlink(ctx.path(Label("@//:WORKSPACE")).dirname, "link") + ctx.symlink(ctx.path(Label("@//:MODULE.bazel")).dirname, "link") print("b.txt: " + ctx.read("link/b.txt")) print("c.txt: " + ctx.read("link/c.txt")) ctx.file("BUILD") - ctx.file("WORKSPACE") + ctx.file("REPO.bazel") repo = repository_rule( _repo_impl, @@ -256,11 +249,10 @@ repo = repository_rule( ) EOF - cat > WORKSPACE <<'EOF' -load(":rules.bzl", "repo") + cat > $(setup_module_dot_bazel) <<'EOF' +repo = use_repo_rule("//:rules.bzl", "repo") repo(name = "ext") EOF - write_default_lockfile "MODULE.bazel.lock" touch BUILD # a.txt is intentionally not created diff --git a/src/test/shell/bazel/starlark_repository_test.sh b/src/test/shell/bazel/starlark_repository_test.sh index 409506fa54ec71..e11e457b30a104 100755 --- a/src/test/shell/bazel/starlark_repository_test.sh +++ b/src/test/shell/bazel/starlark_repository_test.sh @@ -69,7 +69,7 @@ source "$(rlocation "io_bazel/src/test/shell/bazel/remote_helpers.sh")" \ mock_rules_java_to_avoid_downloading # Make sure no repository cache is used in this test -add_to_bazelrc "build --repository_cache=" +add_to_bazelrc "common --repository_cache=" # Basic test. function test_macro_local_repository() { @@ -87,10 +87,9 @@ genrule( EOF cd ${WORKSPACE_DIR} - cat >> $(create_workspace_with_default_repos WORKSPACE) < $(setup_module_dot_bazel) < zoo/BUILD <<'EOF' @@ -138,9 +139,11 @@ EOF cd ${WORKSPACE_DIR} cat >test.bzl <& $TEST_log || fail "Failed to build" expect_log "blah" @@ -155,7 +158,7 @@ EOF function test_load_from_symlink_to_outside_of_workspace() { OTHER=$TEST_TMPDIR/other - cat >> $(create_workspace_with_default_repos WORKSPACE)< WORKSPACE <> $(create_workspace_with_default_repos WORKSPACE) <> WORKSPACE <& $TEST_log \ + bazel build --enable_workspace @endangered//carnivore:mongoose >& $TEST_log \ || fail "Failed to build" expect_log "bleh" } @@ -236,7 +239,7 @@ macro() EOF cd ${WORKSPACE_DIR} - cat >> $(create_workspace_with_default_repos WORKSPACE) <> WORKSPACE <& $TEST_log || fail "Failed to build" + bazel build --enable_workspace //:foo >& $TEST_log || fail "Failed to build" expect_log "bleh" expect_log "Tra-la!" } @@ -271,7 +274,7 @@ EOF cat >WORKSPACE cd ${WORKSPACE_DIR} - cat >> $(create_workspace_with_default_repos WORKSPACE) < WORKSPACE <& $TEST_log || exitCode=$? + bazel build --enable_workspace @foo//:data.txt >& $TEST_log || exitCode=$? [ $exitCode != 0 ] || fail "building @foo//:data.txt succeed while expected failure" expect_not_log "PACKAGE" @@ -297,7 +300,7 @@ EOF # Test build //... bazel clean --expunge - bazel build //... >& $TEST_log || exitCode=$? + bazel build --enable_workspace //... >& $TEST_log || exitCode=$? [ $exitCode != 0 ] || fail "building //... succeed while expected failure" expect_not_log "PACKAGE" @@ -305,7 +308,7 @@ EOF # Retest with query //... bazel clean --expunge - bazel query //... >& $TEST_log || exitCode=$? + bazel query --enable_workspace //... >& $TEST_log || exitCode=$? [ $exitCode != 0 ] || fail "querying //... succeed while expected failure" expect_not_log "PACKAGE" @@ -315,17 +318,14 @@ EOF function test_starlark_local_repository() { create_new_workspace repo2=$new_workspace_dir - # Remove the WORKSPACE file in the symlinked repo, so our Starlark rule has to - # create one. - rm $repo2/WORKSPACE cat > BUILD <<'EOF' genrule(name='bar', cmd='echo foo | tee $@', outs=['bar.txt']) EOF cd ${WORKSPACE_DIR} - cat >> $(create_workspace_with_default_repos WORKSPACE) < $(setup_module_dot_bazel) <& $TEST_log || fail "Failed to build" expect_log "foo" - cat bazel-bin/external/foo/bar.txt >$TEST_log + cat bazel-bin/external/+_repo_rules+foo/bar.txt >$TEST_log expect_log "foo" } @@ -356,8 +356,8 @@ function setup_starlark_repository() { echo "filegroup(name='bar', srcs=['bar.txt'])" > BUILD cd "${WORKSPACE_DIR}" - cat >> $(create_workspace_with_default_repos WORKSPACE) < $(setup_module_dot_bazel) < MODULE.bazel < $(setup_module_dot_bazel) < WORKSPACE <<'EOF' -load("//:my_repository_rule.bzl", "my_repository_rule") + cat > $(setup_module_dot_bazel) <<'EOF' +my_repository_rule = use_repo_rule("//:my_repository_rule.bzl", "my_repository_rule") my_repository_rule( name = "my_repository_rule", @@ -1076,10 +1076,10 @@ EOF bazel run @foo//:bar >& $TEST_log || fail "Execution of @foo//:bar failed" output_base=$(bazel info output_base) - test -x "${output_base}/external/foo/test.sh" || fail "test.sh is not executable" - test -x "${output_base}/external/foo/test2.sh" || fail "test2.sh is not executable" - test ! -x "${output_base}/external/foo/BUILD" || fail "BUILD is executable" - test ! -x "${output_base}/external/foo/test2" || fail "test2 is executable" + test -x "${output_base}/external/+_repo_rules+foo/test.sh" || fail "test.sh is not executable" + test -x "${output_base}/external/+_repo_rules+foo/test2.sh" || fail "test2.sh is not executable" + test ! -x "${output_base}/external/+_repo_rules+foo/BUILD" || fail "BUILD is executable" + test ! -x "${output_base}/external/+_repo_rules+foo/test2" || fail "test2 is executable" } function test_starlark_repository_download() { @@ -1115,15 +1115,15 @@ EOF output_base="$(bazel info output_base)" # Test download - test -e "${output_base}/external/foo/download_with_sha256.txt" \ + test -e "${output_base}/external/+_repo_rules+foo/download_with_sha256.txt" \ || fail "download_with_sha256.txt is not downloaded" - test -e "${output_base}/external/foo/download_executable_file.sh" \ + test -e "${output_base}/external/+_repo_rules+foo/download_executable_file.sh" \ || fail "download_executable_file.sh is not downloaded" # Test download - diff "${output_base}/external/foo/download_with_sha256.txt" \ + diff "${output_base}/external/+_repo_rules+foo/download_with_sha256.txt" \ "${download_with_sha256}" >/dev/null \ || fail "download_with_sha256.txt is not downloaded successfully" - diff "${output_base}/external/foo/download_executable_file.sh" \ + diff "${output_base}/external/+_repo_rules+foo/download_executable_file.sh" \ "${download_executable_file}" >/dev/null \ || fail "download_executable_file.sh is not downloaded successfully" @@ -1133,9 +1133,9 @@ EOF fi # Test executable - test ! -x "${output_base}/external/foo/download_with_sha256.txt" \ + test ! -x "${output_base}/external/+_repo_rules+foo/download_with_sha256.txt" \ || fail "download_with_sha256.txt is executable" - test -x "${output_base}/external/foo/download_executable_file.sh" \ + test -x "${output_base}/external/+_repo_rules+foo/download_executable_file.sh" \ || fail "download_executable_file.sh is not executable" } @@ -1204,13 +1204,13 @@ EOF >& $TEST_log && shutdown_server || fail "Execution of @foo//:all failed" output_base="$(bazel info output_base)" - grep "no_sha_return $not_provided_sha256" $output_base/external/foo/returned_shas.txt \ + grep "no_sha_return $not_provided_sha256" $output_base/external/+_repo_rules+foo/returned_shas.txt \ || fail "expected calculated sha256 $not_provided_sha256" - grep "with_sha_return $provided_sha256" $output_base/external/foo/returned_shas.txt \ + grep "with_sha_return $provided_sha256" $output_base/external/+_repo_rules+foo/returned_shas.txt \ || fail "expected provided sha256 $provided_sha256" - grep "compressed_with_sha_return $compressed_provided_sha256" $output_base/external/foo/returned_shas.txt \ + grep "compressed_with_sha_return $compressed_provided_sha256" $output_base/external/+_repo_rules+foo/returned_shas.txt \ || fail "expected provided sha256 $compressed_provided_sha256" - grep "compressed_no_sha_return $compressed_not_provided_sha256" $output_base/external/foo/returned_shas.txt \ + grep "compressed_no_sha_return $compressed_not_provided_sha256" $output_base/external/+_repo_rules+foo/returned_shas.txt \ || fail "expected compressed calculated sha256 $compressed_not_provided_sha256" } @@ -1233,8 +1233,8 @@ function test_starlark_repository_download_args() { cat > bar.txt echo "filegroup(name='bar', srcs=['bar.txt'])" > BUILD - cat >> $(create_workspace_with_default_repos WORKSPACE) < $(setup_module_dot_bazel) </dev/null \ || fail "download_and_extract1.tar.gz was not extracted successfully" - diff "${output_base}/external/foo/some/path/server_dir/download_and_extract1.txt" \ + diff "${output_base}/external/+_repo_rules+foo/some/path/server_dir/download_and_extract1.txt" \ "${file_prefix}1.txt" >/dev/null \ || fail "download_and_extract1.tar.gz was not extracted successfully in some/path" - diff "${output_base}/external/foo/server_dir/download_and_extract2.txt" \ + diff "${output_base}/external/+_repo_rules+foo/server_dir/download_and_extract2.txt" \ "${file_prefix}2.txt" >/dev/null \ || fail "download_and_extract2.zip was not extracted successfully" - diff "${output_base}/external/foo/server_dir/download_and_extract3.txt" \ + diff "${output_base}/external/+_repo_rules+foo/server_dir/download_and_extract3.txt" \ "${file_prefix}3.txt" >/dev/null \ || fail "download_and_extract3.zip was not extracted successfully" - diff "${output_base}/external/foo/other/path/server_dir/download_and_extract3.txt" \ + diff "${output_base}/external/+_repo_rules+foo/other/path/server_dir/download_and_extract3.txt" \ "${file_prefix}3.txt" >/dev/null \ || fail "download_and_extract3.tar.gz was not extracted successfully" } @@ -1362,7 +1362,7 @@ genrule( EOF cd ${WORKSPACE_DIR} - cat >> $(create_workspace_with_default_repos WORKSPACE) <> WORKSPACE <& $TEST_log || fail "Failed to build" + bazel build --enable_workspace @test//:test >& $TEST_log || fail "Failed to build" expect_log ": ${version}." } @@ -1394,7 +1394,6 @@ EOF # Test native.existing_rule(s), regression test for #1277 function test_existing_rule() { create_new_workspace - setup_skylib_support repo2=$new_workspace_dir cat > BUILD @@ -1421,7 +1420,10 @@ def macro(): test("non_existing") EOF - bazel query //... >& $TEST_log || fail "Failed to build" + # Remove tools directory set up by copy_tools_directory in testenv.sh + rm -rf tools + + bazel query --enable_workspace //... >& $TEST_log || fail "Failed to build" expect_log "existing = True,True" expect_log "non_existing = False,False" } @@ -1466,21 +1468,21 @@ EOF echo "Just to get the path" > reference.txt echo "initial" > reference.txt.shadow - bazel build //:source //:configure + bazel build --enable_workspace //:source //:configure grep 'initial' `bazel info bazel-bin`/source.txt \ || fail '//:source not generated properly' grep 'initial' `bazel info bazel-bin`/configure.txt \ || fail '//:configure not generated properly' echo "new value" > reference.txt.shadow - bazel sync --configure --experimental_repository_resolved_file=resolved.bzl \ + bazel sync --enable_workspace --configure --experimental_repository_resolved_file=resolved.bzl \ 2>&1 || fail "Expected sync --configure to succeed" grep -q 'name.*configure' resolved.bzl \ || fail "Expected 'configure' to be synced" grep -q 'name.*source' resolved.bzl \ && fail "Expected 'source' not to be synced" || : - bazel build //:source //:configure + bazel build --enable_workspace //:source //:configure grep -q 'initial' `bazel info bazel-bin`/source.txt \ || fail '//:source did not keep its old value' grep -q 'new value' `bazel info bazel-bin`/configure.txt \ @@ -1489,8 +1491,8 @@ EOF function test_timeout_tunable() { - cat >> $(create_workspace_with_default_repos WORKSPACE) <<'EOF' -load("//:repo.bzl", "with_timeout") + cat >> $(setup_module_dot_bazel) <<'EOF' +with_timeout = use_repo_rule("//:repo.bzl", "with_timeout") with_timeout(name="maytimeout") EOF @@ -1505,9 +1507,9 @@ def _impl(ctx): with_timeout = repository_rule(attrs = {}, implementation = _impl) EOF - bazel sync && fail "expected timeout" || : + bazel fetch --repo=@maytimeout && fail "expected timeout" || : - bazel sync --experimental_scale_timeouts=100 \ + bazel fetch --repo=@maytimeout --experimental_scale_timeouts=100 \ || fail "expected success now the timeout is scaled" bazel build @maytimeout//... \ @@ -1540,25 +1542,25 @@ EOF # Normal sync should hit both repositories echo; echo bazel sync; echo - bazel sync + bazel sync --enable_workspace assert_equals 1 $(cat "${TEST_TMPDIR}/executionFOO") assert_equals 1 $(cat "${TEST_TMPDIR}/executionBAR") # Only foo echo; echo bazel sync --only foo; echo - bazel sync --only foo + bazel sync --enable_workspace --only foo assert_equals 2 $(cat "${TEST_TMPDIR}/executionFOO") assert_equals 1 $(cat "${TEST_TMPDIR}/executionBAR") # Only bar echo; echo bazel sync --only bar; echo - bazel sync --only bar + bazel sync --enable_workspace --only bar assert_equals 2 $(cat "${TEST_TMPDIR}/executionFOO") assert_equals 2 $(cat "${TEST_TMPDIR}/executionBAR") # Only bar echo; echo bazel sync --only bar; echo - bazel sync --only bar + bazel sync --enable_workspace --only bar assert_equals 2 $(cat "${TEST_TMPDIR}/executionFOO") assert_equals 3 $(cat "${TEST_TMPDIR}/executionBAR") } @@ -1596,7 +1598,7 @@ def root_cause(): ) EOF - cat >> $(create_workspace_with_default_repos WORKSPACE) <<'EOF' + cat > WORKSPACE <<'EOF' load("//:root.bzl", "root_cause") load("//:repo.bzl", "trivial_repo") @@ -1617,7 +1619,7 @@ trivial_repo( ) EOF - bazel build //:it > "${TEST_log}" 2>&1 \ + bazel build --enable_workspace //:it > "${TEST_log}" 2>&1 \ && fail "Expected failure" || : # Extract the first error message printed @@ -1653,7 +1655,7 @@ repo = repository_rule(implementation=_impl, attrs = {}) def root_cause(): repo(name = "this_is_the_root_cause") EOF - bazel build //:it > "${TEST_log}" 2>&1 \ + bazel build --enable_workspace //:it > "${TEST_log}" 2>&1 \ && fail "Expected failure" || : # Extract the first error message printed (see previous awk command). @@ -1670,7 +1672,7 @@ EOF } function test_circular_load_error_message() { - cat >> $(create_workspace_with_default_repos WORKSPACE) <<'EOF' + cat > WORKSPACE <<'EOF' load("//:a.bzl", "total") EOF touch BUILD @@ -1689,7 +1691,7 @@ b = 20 difference = b - a EOF - bazel build //... > "${TEST_log}" 2>&1 && fail "Expected failure" || : + bazel build --enable_workspace //... > "${TEST_log}" 2>&1 && fail "Expected failure" || : expect_not_log "[iI]nternal [eE]rror" expect_not_log "IllegalStateException" @@ -1698,7 +1700,7 @@ EOF } function test_ciruclar_load_error_with_path_message() { - cat >> $(create_workspace_with_default_repos WORKSPACE) <<'EOF' + cat > WORKSPACE <<'EOF' load("//:x.bzl", "x") EOF touch BUILD @@ -1726,7 +1728,7 @@ b = 20 difference = b - a EOF - bazel build //... > "${TEST_log}" 2>&1 && fail "Expected failure" || : + bazel build --enable_workspace //... > "${TEST_log}" 2>&1 && fail "Expected failure" || : expect_not_log "[iI]nternal [eE]rror" expect_not_log "IllegalStateException" @@ -1786,8 +1788,8 @@ maybe_with_auth = repository_rule( ) EOF - cat >> $(create_workspace_with_default_repos WORKSPACE) < $(setup_module_dot_bazel) <> $(create_workspace_with_default_repos WORKSPACE) < $(setup_module_dot_bazel) <> $(create_workspace_with_default_repos WORKSPACE) < $(setup_module_dot_bazel) < WORKSPACE < MODULE.bazel < WORKSPACE < $(setup_module_dot_bazel) < WORKSPACE < $(setup_module_dot_bazel) < WORKSPACE < $(setup_module_dot_bazel) < WORKSPACE < $(setup_module_dot_bazel) < WORKSPACE < $(setup_module_dot_bazel) < WORKSPACE < $(setup_module_dot_bazel) < "${TEST_log}" 2>&1 \ + bazel build --repository_disable_download --repository_cache= //:it > "${TEST_log}" 2>&1 \ && fail "Expected failure" || : expect_log "Failed to download repository @.*: download is disabled" } @@ -2360,7 +2362,7 @@ genrule( EOF # for some reason --repository_disable_download fails with bzlmod trying to download @platforms. - bazel build --distdir="." --repository_disable_download --noenable_bzlmod //:it || fail "Failed to build" + bazel build --distdir="." --repository_disable_download --repository_cache= --enable_workspace --noenable_bzlmod //:it || fail "Failed to build" } function test_disable_download_should_allow_local_repository() { @@ -2387,7 +2389,7 @@ genrule( ) EOF # for some reason --repository_disable_download fails with bzlmod trying to download @platforms. - bazel build --repository_disable_download --noenable_bzlmod //:it || fail "Failed to build" + bazel build --repository_disable_download --enable_workspace --noenable_bzlmod //:it || fail "Failed to build" } function test_no_restarts_fetching_with_worker_thread() { @@ -2425,15 +2427,15 @@ EOF } function test_duplicate_value_in_environ() { - cat >> WORKSPACE < MODULE.bazel < def.bzl <<'EOF' def _impl(repository_ctx): - repository_ctx.file("WORKSPACE") + repository_ctx.file("REPO.bazel") repository_ctx.file("BUILD", """filegroup(name="bar",srcs=[])""") repo = repository_rule( @@ -2623,7 +2625,7 @@ EOF function test_repo_boundary_files() { create_new_workspace - cat > MODULE.bazel < $(setup_module_dot_bazel) < MODULE.bazel < $(setup_module_dot_bazel) < MODULE.bazel < $(setup_module_dot_bazel) < MODULE.bazel < $(setup_module_dot_bazel) < MODULE.bazel < $(setup_module_dot_bazel) < ${outside_dir}/data.txt create_new_workspace - cat > MODULE.bazel < $(setup_module_dot_bazel) < ${outside_dir}/data.txt create_new_workspace - cat > MODULE.bazel < $(setup_module_dot_bazel) < MODULE.bazel < $(setup_module_dot_bazel) < MODULE.bazel < $(setup_module_dot_bazel) < ${outside_dir}/data.txt create_new_workspace - cat > MODULE.bazel < $(setup_module_dot_bazel) < MODULE.bazel < $(setup_module_dot_bazel) < MODULE.bazel < $(setup_module_dot_bazel) < MODULE.bazel < $(setup_module_dot_bazel) < MODULE.bazel < $(setup_module_dot_bazel) <& $TEST_log || fail "expected bazel to succeed" + bazel build --enable_workspace @r >& $TEST_log || fail "expected bazel to succeed" } # Regression test for https://github.com/bazelbuild/bazel/issues/21823. @@ -3245,7 +3247,7 @@ function test_keep_going_weird_deadlock() { return fi create_new_workspace - cat > MODULE.bazel < $(setup_module_dot_bazel) < WORKSPACE <<'EOF' -load("//:my_repository_rule.bzl", "my_repository_rule") + cat > $(setup_module_dot_bazel) <<'EOF' +my_repository_rule = use_repo_rule("//:my_repository_rule.bzl", "my_repository_rule") my_repository_rule( name = "my_first_repo", @@ -3304,10 +3306,10 @@ filegroup( ], ) EOF - bazel build --noenable_bzlmod //:foo >& $TEST_log || fail "expected bazel to succeed" - expect_log "main repo: //:foo @//:foo" - expect_log "my_first_repo: @my_first_repo//:foo @my_first_repo//:foo" - expect_log "my_second_repo: @my_first_repo//:foo @my_first_repo//:foo" + bazel build //:foo >& $TEST_log || fail "expected bazel to succeed" + expect_log "main repo: //:foo @@//:foo" + expect_log "my_first_repo: @my_first_repo//:foo @@+_repo_rules+my_first_repo//:foo" + expect_log "my_second_repo: @my_first_repo//:foo @@+_repo_rules+my_first_repo//:foo" } run_suite "local repository tests"