Skip to content

Commit c6a5ebb

Browse files
Wyveraldcopybara-github
authored andcommitted
Still generate a WORKSPACE file in repo rules if --enable_workspace is set
47e48a1 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 #20498. PiperOrigin-RevId: 590492936 Change-Id: I43bfc7bf79b3749f1fb02ce31be789d92c36a2c0
1 parent 75fffbf commit c6a5ebb

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryFunction.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.devtools.build.lib.packages.BazelStarlarkContext;
3333
import com.google.devtools.build.lib.packages.Rule;
3434
import com.google.devtools.build.lib.packages.SymbolGenerator;
35+
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
3536
import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
3637
import com.google.devtools.build.lib.profiler.Profiler;
3738
import com.google.devtools.build.lib.profiler.ProfilerTask;
@@ -355,6 +356,10 @@ private RepositoryDirectoryValue.Builder fetchInternal(
355356
if (!WorkspaceFileHelper.isValidRepoRoot(outputDirectory)) {
356357
try {
357358
FileSystemUtils.createEmptyFile(outputDirectory.getRelative(LabelConstants.REPO_FILE_NAME));
359+
if (starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_WORKSPACE)) {
360+
FileSystemUtils.createEmptyFile(
361+
outputDirectory.getRelative(LabelConstants.WORKSPACE_FILE_NAME));
362+
}
358363
} catch (IOException e) {
359364
throw new RepositoryFunctionException(e, Transience.TRANSIENT);
360365
}

src/test/shell/bazel/starlark_repository_test.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2583,4 +2583,26 @@ EOF
25832583
assert_contains '"Accept": "application/text"' "$headers"
25842584
}
25852585

2586+
function test_repo_boundary_files() {
2587+
create_new_workspace
2588+
cat > MODULE.bazel <<EOF
2589+
r = use_repo_rule("//:r.bzl", "r")
2590+
r(name = "r")
2591+
EOF
2592+
touch BUILD
2593+
cat > r.bzl <<EOF
2594+
def _r(rctx):
2595+
rctx.file("BUILD", "filegroup(name='r', srcs=glob(['*']))")
2596+
r = repository_rule(_r)
2597+
EOF
2598+
2599+
bazel query --noenable_workspace --output=build @r > output || fail "expected bazel to succeed"
2600+
assert_contains 'REPO.bazel' output
2601+
assert_not_contains 'WORKSPACE' output
2602+
2603+
bazel query --enable_workspace --output=build @r > output || fail "expected bazel to succeed"
2604+
assert_contains 'REPO.bazel' output
2605+
assert_contains 'WORKSPACE' output
2606+
}
2607+
25862608
run_suite "local repository tests"

0 commit comments

Comments
 (0)