Disable WORKSPACE in shell integration tests

Final part of https://github.com/bazelbuild/bazel/pull/23087

Working towards #23023

PiperOrigin-RevId: 661381915
Change-Id: I1fca0e24bf2f424a3e438ab6fb68e9ae815118f6
diff --git a/src/test/shell/BUILD b/src/test/shell/BUILD
index 14f941a..ec01a7c 100644
--- a/src/test/shell/BUILD
+++ b/src/test/shell/BUILD
@@ -1,5 +1,4 @@
 load("@rules_python//python:defs.bzl", "py_test")
-load("//:workspace_deps.bzl", "gen_workspace_stanza")
 
 package(default_visibility = ["//visibility:private"])
 
@@ -11,6 +10,7 @@
     "sandboxing_test_utils.sh",
     "unittest.bash",
     "shell_utils.sh",
+    "testenv.sh",
 ])
 
 filegroup(
@@ -22,23 +22,6 @@
     visibility = ["//src:__pkg__"],
 )
 
-gen_workspace_stanza(
-    name = "testenv_sh",
-    out = "testenv.sh",
-    repos = [
-        "bazel_skylib",
-        "rules_cc",
-        "rules_java",
-        "rules_license",
-        "rules_pkg",
-        "rules_proto",
-        "rules_python",
-        "rules_testing",
-    ],
-    template = "testenv.sh.tmpl",
-    visibility = ["//visibility:public"],
-)
-
 sh_library(
     name = "bashunit",
     srcs = [
diff --git a/src/test/shell/bazel/bazel_coverage_hermetic_py_test.sh b/src/test/shell/bazel/bazel_coverage_hermetic_py_test.sh
index 59f2359..426e7dc 100755
--- a/src/test/shell/bazel/bazel_coverage_hermetic_py_test.sh
+++ b/src/test/shell/bazel/bazel_coverage_hermetic_py_test.sh
@@ -21,11 +21,9 @@
 source "${CURRENT_DIR}/../integration_test_setup.sh" \
   || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
 
-disable_bzlmod
-
 # Fetch hermetic python and register toolchain.
 function set_up() {
-    cat >>WORKSPACE <<EOF
+    cat >>MODULE.bazel <<EOF
 register_toolchains(
     "//:python_toolchain",
 )
diff --git a/src/test/shell/bazel/cc_import_starlark_test.sh b/src/test/shell/bazel/cc_import_starlark_test.sh
index ce5dd39..b19a627 100755
--- a/src/test/shell/bazel/cc_import_starlark_test.sh
+++ b/src/test/shell/bazel/cc_import_starlark_test.sh
@@ -42,8 +42,6 @@
 
   cd "$workspace_dir"
 
-  setup_skylib_support
-
   # TODO(gnish): Re-enable tests once bazel picks up changes.
   # bazel test --experimental_starlark_cc_import tools/build_defs/cc/tests:cc_import_tests
 }
diff --git a/src/test/shell/integration/bazel_command_log_test.sh b/src/test/shell/integration/bazel_command_log_test.sh
index e6a64a6..cd61a58 100755
--- a/src/test/shell/integration/bazel_command_log_test.sh
+++ b/src/test/shell/integration/bazel_command_log_test.sh
@@ -21,9 +21,6 @@
 
 log="$(bazel --batch info command_log)"
 
-# Less network flakiness without bzlmod
-disable_bzlmod
-
 function tear_down() {
   # Clean up after ourselves.
   bazel --nobatch shutdown
diff --git a/src/test/shell/integration/client_test.sh b/src/test/shell/integration/client_test.sh
index 69f1d35..e153042 100755
--- a/src/test/shell/integration/client_test.sh
+++ b/src/test/shell/integration/client_test.sh
@@ -19,9 +19,6 @@
 source "${CURRENT_DIR}/../integration_test_setup.sh" \
   || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
 
-# Less network flakiness without bzlmod
-disable_bzlmod
-
 function strip_lines_from_bazel_cc() {
   # sed can't redirect back to its input file (it'll only generate an empty
   # file). In newer versions of gnu sed there is a -i option to edit in place.
diff --git a/src/test/shell/integration/dump_test.sh b/src/test/shell/integration/dump_test.sh
index 05b36ca..6c372d1 100755
--- a/src/test/shell/integration/dump_test.sh
+++ b/src/test/shell/integration/dump_test.sh
@@ -111,8 +111,13 @@
   expect_log '"PACKAGE:b":'
 
   bazel dump --memory=full,count >& $TEST_log || fail "dump failed"
-  # //a, //b, //external and EXTERNAL_PACKAGE
-  expect_log_n '"com.google.devtools.build.lib.skyframe.PackageValue": 1' 4
+  if [[ $PRODUCT_NAME == "bazel" ]]; then
+    # WORSKAPCE disabled: //a, //b
+    expect_log_n '"com.google.devtools.build.lib.skyframe.PackageValue": 1' 2
+  else
+    # WORKSPACE enabled: //a, //b, //external and EXTERNAL_PACKAGE
+    expect_log_n '"com.google.devtools.build.lib.skyframe.PackageValue": 1' 4
+  fi
 }
 
 function test_memory_needle() {
diff --git a/src/test/shell/integration/loading_phase_test.sh b/src/test/shell/integration/loading_phase_test.sh
index 8576f3b..7298c1a 100755
--- a/src/test/shell/integration/loading_phase_test.sh
+++ b/src/test/shell/integration/loading_phase_test.sh
@@ -467,11 +467,15 @@
   local -r pkg="${FUNCNAME[0]}"
   mkdir "$pkg" || fail "Could not mkdir $pkg"
   echo "filegroup(name = '$pkg')" > "$pkg/BUILD"
-  add_bazel_skylib "MODULE.bazel"
   # Ensure bazel-<pkg> is created.
   bazel build --symlink_prefix="foo_prefix-" "//$pkg" || fail "build failed"
   [[ -d "foo_prefix-bin" ]] || fail "bazel-bin was not created"
 
+  if [[ $PRODUCT_NAME == "bazel" ]]; then
+    # Remove tools dir set up by copy_tools_directory from testenv.sh
+    rm -rf tools
+  fi
+
   # Assert that "//..." does not expand to //foo_prefix-*
   bazel query //... >& "$TEST_log"
   expect_log_once "//$pkg:$pkg"
diff --git a/src/test/shell/integration/run_test.sh b/src/test/shell/integration/run_test.sh
index b5bc67d..5f768be 100755
--- a/src/test/shell/integration/run_test.sh
+++ b/src/test/shell/integration/run_test.sh
@@ -63,6 +63,7 @@
 #### HELPER FUNCTIONS ##################################################
 
 function write_py_files() {
+  add_rules_python "MODULE.bazel"
   mkdir -p py || fail "mkdir py failed"
 
   cat > py/BUILD <<'EOF'
diff --git a/src/test/shell/testenv.sh.tmpl b/src/test/shell/testenv.sh
similarity index 88%
rename from src/test/shell/testenv.sh.tmpl
rename to src/test/shell/testenv.sh
index f18d74c..fb260df 100755
--- a/src/test/shell/testenv.sh.tmpl
+++ b/src/test/shell/testenv.sh
@@ -90,7 +90,6 @@
 
 # WORKSPACE file
 workspace_file="${BAZEL_RUNFILES}/WORKSPACE"
-distdir_bzl_file="${BAZEL_RUNFILES}/distdir.bzl"
 
 # Where to register toolchains
 TOOLCHAIN_REGISTRAION_FILE="MODULE.bazel"
@@ -306,6 +305,9 @@
 # Enable Bzlmod in all shell integration tests
 common --enable_bzlmod
 
+# Disable WORKSPACE in all shell integration tests
+common --noenable_workspace
+
 # Verify compatibility before the flip (https://github.com/bazelbuild/bazel/issues/12821)
 common --nolegacy_external_runfiles
 
@@ -537,119 +539,6 @@
   IOS_SDK_VERSION=$(xcrun --sdk iphoneos --show-sdk-version)
 }
 
-function setup_skylib_support() {
-  mkdir -p rules/private
-  touch rules/private/BUILD
-  cat >> WORKSPACE << EOF
-load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
-
-{bazel_skylib}
-
-load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
-bazel_skylib_workspace()
-EOF
-}
-
-function add_rules_cc_to_workspace() {
-  cat >> "$1"<<EOF
-load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
-
-{rules_cc}
-EOF
-}
-
-function add_rules_java_to_workspace() {
-  cat >> "$1"<<EOF
-load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
-
-{rules_java}
-EOF
-}
-
-function add_rules_license_to_workspace() {
-  cat >> "$1"<<EOF
-load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
-
-{rules_license}
-EOF
-}
-
-function add_rules_pkg_to_workspace() {
-  cat >> "$1"<<EOF
-load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
-
-{rules_pkg}
-EOF
-}
-
-function add_rules_proto_to_workspace() {
-  cat >> "$1"<<EOF
-load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
-
-{rules_proto}
-EOF
-}
-
-function add_rules_python_to_workspace() {
-  cat >> "$1"<<EOF
-load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
-
-{rules_python}
-EOF
-}
-
-function add_rules_testing_to_workspace() {
-  mkdir lib
-  touch lib/BUILD
-  cat >> "$1"<<EOF
-load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
-
-{rules_testing}
-EOF
-}
-
-function create_workspace_with_default_repos() {
-  write_workspace_file "${1:-WORKSPACE}" "${2:-main}"
-  workspace_file=${1:-WORKSPACE}
-  write_workspace_file "${workspace_file}" "${2:-main}"
-  write_default_lockfile "$(dirname ${workspace_file})/MODULE.bazel.lock"
-  echo "$1"
-}
-
-# Write the default WORKSPACE file, wiping out any custom WORKSPACE setup.
-function write_workspace_file() {
-  cat > "$1" << EOF
-workspace(name = "$2")
-EOF
-  add_rules_cc_to_workspace "WORKSPACE"
-  add_rules_java_to_workspace "WORKSPACE"
-  add_rules_license_to_workspace "WORKSPACE"
-  add_rules_pkg_to_workspace "WORKSPACE"
-  add_rules_proto_to_workspace "WORKSPACE"
-  add_rules_python_to_workspace "WORKSPACE"
-
-  maybe_setup_python_windows_workspace
-}
-
-# If the current platform is Windows, registers our custom Windows Python
-# toolchain. Otherwise does nothing.
-#
-# Since this modifies the WORKSPACE file, it must be called between test cases.
-function maybe_setup_python_windows_workspace() {
-  if [[ ! $PLATFORM =~ msys ]]; then
-    return
-  fi
-
-  # --extra_toolchains has left-to-right precedence semantics, but the bazelrc
-  # is processed before the command line. This means that any matching
-  # toolchains added to the bazelrc will always take precedence over toolchains
-  # set up by test cases. Instead, we add the toolchain to WORKSPACE so that it
-  # has lower priority than whatever is passed on the command line.
-  cat >> WORKSPACE << EOF
-register_toolchains("//tools/python/windows:py_toolchain")
-EOF
-}
-
 # Extract the module version used in the default lock file.
 function get_version_from_default_lock_file() {
   lockfile=$(rlocation io_bazel/src/test/tools/bzlmod/MODULE.bazel.lock)
@@ -717,13 +606,6 @@
   echo $module_dot_bazel
 }
 
-# Set up a lockfile to avoid accessing BCR for tests with a clean workspace.
-function write_default_lockfile() {
-  module_lockfile=${1:-MODULE.bazel.lock}
-  touch "$(dirname ${module_lockfile})/MODULE.bazel"
-  cp -f $(rlocation io_bazel/src/test/tools/bzlmod/MODULE.bazel.lock) ${module_lockfile}
-}
-
 workspaces=()
 # Set-up a new, clean workspace with only the tools installed.
 function create_new_workspace() {
@@ -736,8 +618,6 @@
 
   copy_tools_directory
 
-  write_workspace_file "WORKSPACE" "$WORKSPACE_NAME"
-
   # Suppress the echo from setup_module_dot_bazel
   setup_module_dot_bazel > /dev/null
 
@@ -775,7 +655,6 @@
         try_with_timeout rm -fr "$i"
       fi
     done
-    write_workspace_file "WORKSPACE" "$WORKSPACE_NAME"
     # Suppress the echo from setup_module_dot_bazel
     setup_module_dot_bazel > /dev/null
   fi