Skip to content

Commit

Permalink
Still generate a WORKSPACE file in repo rules if --enable_workspace i…
Browse files Browse the repository at this point in the history
…s 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
  • Loading branch information
Wyverald authored and copybara-github committed Dec 13, 2023
1 parent 75fffbf commit c6a5ebb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/shell/bazel/starlark_repository_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2583,4 +2583,26 @@ EOF
assert_contains '"Accept": "application/text"' "$headers"
}

function test_repo_boundary_files() {
create_new_workspace
cat > MODULE.bazel <<EOF
r = use_repo_rule("//:r.bzl", "r")
r(name = "r")
EOF
touch BUILD
cat > r.bzl <<EOF
def _r(rctx):
rctx.file("BUILD", "filegroup(name='r', srcs=glob(['*']))")
r = repository_rule(_r)
EOF

bazel query --noenable_workspace --output=build @r > 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"

0 comments on commit c6a5ebb

Please sign in to comment.