From c6a5ebb9af3d3e114a055c566f9aca777263a945 Mon Sep 17 00:00:00 2001 From: Googler Date: Wed, 13 Dec 2023 01:09:24 -0800 Subject: [PATCH] Still generate a WORKSPACE file in repo rules if --enable_workspace is set https://github.com/bazelbuild/bazel/commit/47e48a13563742473abdcaef46b7c4f222a86831 made it so that, after a repo rule finishes, we no longer generate an empty WORKSPACE file if one is not found at the repo root. (We generate a REPO.bazel file instead, which also marks the boundary of the repo just fine.) But some users actually expect a WORKSPACE file specifically, so this CL restores the behavior of creating an empty WORKSPACE file if --enable_workspace is set. Note that neither WORKSPACE nor REPO.bazel is created if the repo rule logic creates any repo boundary file itself. Fixes https://github.com/bazelbuild/bazel/issues/20498. PiperOrigin-RevId: 590492936 Change-Id: I43bfc7bf79b3749f1fb02ce31be789d92c36a2c0 --- .../starlark/StarlarkRepositoryFunction.java | 5 +++++ .../shell/bazel/starlark_repository_test.sh | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryFunction.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryFunction.java index 452d1efe634231..74f52360613611 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryFunction.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryFunction.java @@ -32,6 +32,7 @@ import com.google.devtools.build.lib.packages.BazelStarlarkContext; import com.google.devtools.build.lib.packages.Rule; import com.google.devtools.build.lib.packages.SymbolGenerator; +import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions; import com.google.devtools.build.lib.pkgcache.PathPackageLocator; import com.google.devtools.build.lib.profiler.Profiler; import com.google.devtools.build.lib.profiler.ProfilerTask; @@ -355,6 +356,10 @@ private RepositoryDirectoryValue.Builder fetchInternal( if (!WorkspaceFileHelper.isValidRepoRoot(outputDirectory)) { try { FileSystemUtils.createEmptyFile(outputDirectory.getRelative(LabelConstants.REPO_FILE_NAME)); + if (starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_WORKSPACE)) { + FileSystemUtils.createEmptyFile( + outputDirectory.getRelative(LabelConstants.WORKSPACE_FILE_NAME)); + } } catch (IOException e) { throw new RepositoryFunctionException(e, Transience.TRANSIENT); } diff --git a/src/test/shell/bazel/starlark_repository_test.sh b/src/test/shell/bazel/starlark_repository_test.sh index d44bf29840bf5d..d5008c47cd5dd4 100755 --- a/src/test/shell/bazel/starlark_repository_test.sh +++ b/src/test/shell/bazel/starlark_repository_test.sh @@ -2583,4 +2583,26 @@ EOF assert_contains '"Accept": "application/text"' "$headers" } +function test_repo_boundary_files() { + create_new_workspace + cat > MODULE.bazel < r.bzl < output || fail "expected bazel to succeed" + assert_contains 'REPO.bazel' output + assert_not_contains 'WORKSPACE' output + + bazel query --enable_workspace --output=build @r > output || fail "expected bazel to succeed" + assert_contains 'REPO.bazel' output + assert_contains 'WORKSPACE' output +} + run_suite "local repository tests"