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 db616741fbdbbc..452d1efe634231 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 @@ -26,6 +26,7 @@ import com.google.devtools.build.lib.bazel.repository.downloader.DownloadManager; import com.google.devtools.build.lib.bazel.repository.starlark.RepoFetchingSkyKeyComputeState.Signal; import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.cmdline.LabelConstants; import com.google.devtools.build.lib.cmdline.RepositoryName; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.packages.BazelStarlarkContext; @@ -44,6 +45,7 @@ import com.google.devtools.build.lib.skyframe.IgnoredPackagePrefixesValue; import com.google.devtools.build.lib.skyframe.PrecomputedValue; import com.google.devtools.build.lib.util.CPU; +import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; import com.google.devtools.build.lib.vfs.SyscallCache; @@ -350,8 +352,12 @@ private RepositoryDirectoryValue.Builder fetchInternal( new IOException(rule + " must create a directory"), Transience.TRANSIENT); } - if (!WorkspaceFileHelper.doesWorkspaceFileExistUnder(outputDirectory)) { - createWorkspaceFile(outputDirectory, rule.getTargetKind(), rule.getName()); + if (!WorkspaceFileHelper.isValidRepoRoot(outputDirectory)) { + try { + FileSystemUtils.createEmptyFile(outputDirectory.getRelative(LabelConstants.REPO_FILE_NAME)); + } catch (IOException e) { + throw new RepositoryFunctionException(e, Transience.TRANSIENT); + } } return RepositoryDirectoryValue.builder().setPath(outputDirectory); diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidNdkRepositoryFunction.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidNdkRepositoryFunction.java index c7b0c960868cdb..d4e9f793ef0ce5 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidNdkRepositoryFunction.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidNdkRepositoryFunction.java @@ -33,6 +33,7 @@ import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.StlImpls; import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.StlImpls.GnuLibStdCppStlImpl; import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.StlImpls.LibCppStlImpl; +import com.google.devtools.build.lib.cmdline.LabelConstants; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.packages.Rule; import com.google.devtools.build.lib.packages.Type; @@ -270,7 +271,12 @@ public RepositoryDirectoryValue.Builder fetch( if (environ == null) { return null; } - prepareLocalRepositorySymlinkTree(rule, outputDirectory); + try { + outputDirectory.createDirectoryAndParents(); + FileSystemUtils.createEmptyFile(outputDirectory.getRelative(LabelConstants.REPO_FILE_NAME)); + } catch (IOException e) { + throw new RepositoryFunctionException(e, Transience.TRANSIENT); + } WorkspaceAttributeMapper attributes = WorkspaceAttributeMapper.of(rule); PathFragment pathFragment; String userDefinedPath = null; diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidSdkRepositoryFunction.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidSdkRepositoryFunction.java index e52dbb6303f011..04c955c2ddc965 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidSdkRepositoryFunction.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidSdkRepositoryFunction.java @@ -27,6 +27,7 @@ import com.google.devtools.build.lib.analysis.BlazeDirectories; import com.google.devtools.build.lib.analysis.RuleDefinition; import com.google.devtools.build.lib.bugreport.BugReport; +import com.google.devtools.build.lib.cmdline.LabelConstants; import com.google.devtools.build.lib.io.InconsistentFilesystemException; import com.google.devtools.build.lib.packages.Rule; import com.google.devtools.build.lib.packages.Type; @@ -37,6 +38,7 @@ import com.google.devtools.build.lib.util.ResourceFileLoader; import com.google.devtools.build.lib.vfs.Dirent; import com.google.devtools.build.lib.vfs.FileSystem; +import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; import com.google.devtools.build.lib.vfs.Root; @@ -199,7 +201,12 @@ public RepositoryDirectoryValue.Builder fetch( if (environ == null) { return null; } - prepareLocalRepositorySymlinkTree(rule, outputDirectory); + try { + outputDirectory.createDirectoryAndParents(); + FileSystemUtils.createEmptyFile(outputDirectory.getRelative(LabelConstants.REPO_FILE_NAME)); + } catch (IOException e) { + throw new RepositoryFunctionException(e, Transience.TRANSIENT); + } WorkspaceAttributeMapper attributes = WorkspaceAttributeMapper.of(rule); FileSystem fs = directories.getOutputBase().getFileSystem(); Path androidSdkPath; diff --git a/src/main/java/com/google/devtools/build/lib/packages/semantics/BuildLanguageOptions.java b/src/main/java/com/google/devtools/build/lib/packages/semantics/BuildLanguageOptions.java index 4c12bd6560a2a6..04987340966e6e 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/semantics/BuildLanguageOptions.java +++ b/src/main/java/com/google/devtools/build/lib/packages/semantics/BuildLanguageOptions.java @@ -205,6 +205,16 @@ public final class BuildLanguageOptions extends OptionsBase { + " WORKSPACE. See https://bazel.build/docs/bzlmod for more information.") public boolean enableBzlmod; + @Option( + name = "enable_workspace", + defaultValue = "true", + documentationCategory = OptionDocumentationCategory.STARLARK_SEMANTICS, + effectTags = OptionEffectTag.LOADING_AND_ANALYSIS, + help = + "If true, enables the legacy WORKSPACE system for external dependencies. See" + + " https://bazel.build/external/overview for more information.") + public boolean enableWorkspace; + @Option( name = "experimental_isolated_extension_usages", defaultValue = "false", @@ -745,6 +755,7 @@ public StarlarkSemantics toStarlarkSemantics() { EXPERIMENTAL_ENABLE_ANDROID_MIGRATION_APIS, experimentalEnableAndroidMigrationApis) .setBool(EXPERIMENTAL_ENABLE_SCL_DIALECT, experimentalEnableSclDialect) .setBool(ENABLE_BZLMOD, enableBzlmod) + .setBool(ENABLE_WORKSPACE, enableWorkspace) .setBool(EXPERIMENTAL_ISOLATED_EXTENSION_USAGES, experimentalIsolatedExtensionUsages) .setBool( INCOMPATIBLE_EXISTING_RULES_IMMUTABLE_VIEW, incompatibleExistingRulesImmutableView) @@ -852,6 +863,7 @@ public StarlarkSemantics toStarlarkSemantics() { "-experimental_enable_android_migration_apis"; public static final String EXPERIMENTAL_ENABLE_SCL_DIALECT = "-experimental_enable_scl_dialect"; public static final String ENABLE_BZLMOD = "+enable_bzlmod"; + public static final String ENABLE_WORKSPACE = "+enable_workspace"; public static final String EXPERIMENTAL_ISOLATED_EXTENSION_USAGES = "-experimental_isolated_extension_usages"; public static final String INCOMPATIBLE_EXISTING_RULES_IMMUTABLE_VIEW = diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java index 756476d263349f..5e880db394e198 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java +++ b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java @@ -149,7 +149,7 @@ public SkyValue compute(SkyKey skyKey, Environment env) } } - if (rule == null) { + if (rule == null && starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_WORKSPACE)) { // fallback to look up the repository in the WORKSPACE file. try { rule = getRepoRuleFromWorkspace(repositoryName, env); @@ -157,10 +157,13 @@ public SkyValue compute(SkyKey skyKey, Environment env) return null; } } catch (NoSuchRepositoryException e) { - return new NoRepositoryDirectoryValue( - String.format("Repository '%s' is not defined", repositoryName.getCanonicalForm())); + rule = null; } } + if (rule == null) { + return new NoRepositoryDirectoryValue( + String.format("Repository '%s' is not defined", repositoryName.getCanonicalForm())); + } RepositoryFunction handler = getHandler(rule); if (handler == null) { @@ -411,47 +414,15 @@ public static RepositoryDirectoryValue.Builder symlinkRepoRoot( Transience.PERSISTENT); } - // Check that the repository contains a WORKSPACE file. - // Note that we need to do this here since we're not creating a WORKSPACE file ourselves, but - // entrusting the entire contents of the repo root to this target directory. - FileValue workspaceFileValue = getWorkspaceFile(targetDirRootedPath, env); - if (workspaceFileValue == null) { - return null; - } - - if (!workspaceFileValue.exists()) { + // Check that the directory contains a repo boundary file. + // Note that we need to do this here since we're not creating a repo boundary file ourselves, + // but entrusting the entire contents of the repo root to this target directory. + if (!WorkspaceFileHelper.isValidRepoRoot(destination)) { throw new RepositoryFunctionException( - new IOException("No WORKSPACE file found in " + source), Transience.PERSISTENT); - } - return RepositoryDirectoryValue.builder().setPath(source); - } - - @Nullable - private static FileValue getWorkspaceFile(RootedPath directory, Environment env) - throws RepositoryFunctionException, InterruptedException { - RootedPath workspaceRootedFile; - try { - workspaceRootedFile = WorkspaceFileHelper.getWorkspaceRootedFile(directory, env); - if (workspaceRootedFile == null) { - return null; - } - } catch (IOException e) { - throw new RepositoryFunctionException( - new IOException( - "Could not determine workspace file (\"WORKSPACE.bazel\" or \"WORKSPACE\"): " - + e.getMessage()), + new IOException("No MODULE.bazel, REPO.bazel, or WORKSPACE file found in " + destination), Transience.PERSISTENT); } - SkyKey workspaceFileKey = FileValue.key(workspaceRootedFile); - FileValue value; - try { - value = (FileValue) env.getValueOrThrow(workspaceFileKey, IOException.class); - } catch (IOException e) { - throw new RepositoryFunctionException( - new IOException("Could not access " + workspaceRootedFile + ": " + e.getMessage()), - Transience.PERSISTENT); - } - return value; + return RepositoryDirectoryValue.builder().setPath(source); } // Escape a value for the marker file diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java index e830637289bb62..06288b6206805a 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java +++ b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java @@ -15,7 +15,6 @@ package com.google.devtools.build.lib.rules.repository; import static com.google.common.base.Preconditions.checkState; -import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; @@ -402,35 +401,6 @@ protected boolean isConfigure(Rule rule) { return false; } - protected Path prepareLocalRepositorySymlinkTree(Rule rule, Path repositoryDirectory) - throws RepositoryFunctionException { - try { - repositoryDirectory.createDirectoryAndParents(); - } catch (IOException e) { - throw new RepositoryFunctionException(e, Transience.TRANSIENT); - } - - // Add x/WORKSPACE. - createWorkspaceFile(repositoryDirectory, rule.getTargetKind(), rule.getName()); - return repositoryDirectory; - } - - public static void createWorkspaceFile(Path repositoryDirectory, String ruleKind, String ruleName) - throws RepositoryFunctionException { - try { - Path workspaceFile = repositoryDirectory.getRelative(LabelConstants.WORKSPACE_FILE_NAME); - FileSystemUtils.writeContent( - workspaceFile, - UTF_8, - String.format( - "# DO NOT EDIT: automatically generated WORKSPACE file for %s\n" - + "workspace(name = \"%s\")\n", - ruleKind, ruleName)); - } catch (IOException e) { - throw new RepositoryFunctionException(e, Transience.TRANSIENT); - } - } - protected static RepositoryDirectoryValue.Builder writeFile( Path repositoryDirectory, String filename, String contents) throws RepositoryFunctionException { diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceFileHelper.java b/src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceFileHelper.java index 44782eb991241a..f760d250b156ab 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceFileHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceFileHelper.java @@ -17,7 +17,6 @@ import com.google.devtools.build.lib.cmdline.LabelConstants; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; -import com.google.devtools.build.lib.vfs.Root; import com.google.devtools.build.lib.vfs.RootedPath; import com.google.devtools.build.skyframe.SkyFunction.Environment; import com.google.devtools.build.skyframe.SkyKey; @@ -27,12 +26,6 @@ /** A class to help dealing with WORKSPACE.bazel and WORKSAPCE file */ public class WorkspaceFileHelper { - public static RootedPath getWorkspaceRootedFile(Root directory, Environment env) - throws IOException, InterruptedException { - return getWorkspaceRootedFile( - RootedPath.toRootedPath(directory, PathFragment.EMPTY_FRAGMENT), env); - } - /** * Get a RootedPath of the WORKSPACE file we should use for a given directory. This function * returns a RootedPath to /WORKSPACE.bazel file if it exists and it's a regular file @@ -70,13 +63,12 @@ public static RootedPath getWorkspaceRootedFile(RootedPath directory, Environmen directory.getRootRelativePath().getRelative(LabelConstants.WORKSPACE_FILE_NAME)); } - public static boolean doesWorkspaceFileExistUnder(Path directory) { + public static boolean isValidRepoRoot(Path directory) { + // Keep in sync with //src/main/cpp/workspace_layout.h return directory.getRelative(LabelConstants.WORKSPACE_DOT_BAZEL_FILE_NAME).exists() - || directory.getRelative(LabelConstants.WORKSPACE_FILE_NAME).exists(); - } - - public static boolean matchWorkspaceFileName(String name) { - return matchWorkspaceFileName(PathFragment.create(name)); + || directory.getRelative(LabelConstants.WORKSPACE_FILE_NAME).exists() + || directory.getRelative(LabelConstants.MODULE_DOT_BAZEL_FILE_NAME).exists() + || directory.getRelative(LabelConstants.REPO_FILE_NAME).exists(); } public static boolean matchWorkspaceFileName(PathFragment name) { @@ -84,10 +76,5 @@ public static boolean matchWorkspaceFileName(PathFragment name) { || name.equals(LabelConstants.WORKSPACE_FILE_NAME); } - public static boolean endsWithWorkspaceFileName(PathFragment pathFragment) { - return pathFragment.endsWith(LabelConstants.WORKSPACE_DOT_BAZEL_FILE_NAME) - || pathFragment.endsWith(LabelConstants.WORKSPACE_FILE_NAME); - } - private WorkspaceFileHelper() {} } diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java index 3e22e47177bd41..cad3ea4721b8f7 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java @@ -332,6 +332,15 @@ private SkyValue getExternalPackage(Environment env) if (env.valuesMissing()) { return null; } + if (!starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_WORKSPACE)) { + throw PackageFunctionException.builder() + .setType(PackageFunctionException.Type.NO_SUCH_PACKAGE) + .setTransience(Transience.PERSISTENT) + .setPackageIdentifier(LabelConstants.EXTERNAL_PACKAGE_IDENTIFIER) + .setMessage("the WORKSPACE file is disabled via --noenable_workspace") + .setPackageLoadingCode(PackageLoading.Code.WORKSPACE_FILE_ERROR) + .build(); + } SkyKey workspaceKey = ExternalPackageFunction.key(); PackageValue workspace = null; diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/PackageLookupFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/PackageLookupFunction.java index fa3d4298699e21..3f3292ef071a38 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/PackageLookupFunction.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/PackageLookupFunction.java @@ -116,6 +116,7 @@ public SkyValue compute(SkyKey skyKey, Environment env) if (packageKey.equals(LabelConstants.EXTERNAL_PACKAGE_IDENTIFIER)) { return semantics.getBool(BuildLanguageOptions.EXPERIMENTAL_DISABLE_EXTERNAL_PACKAGE) + || !semantics.getBool(BuildLanguageOptions.ENABLE_WORKSPACE) ? PackageLookupValue.NO_BUILD_FILE_VALUE : computeWorkspacePackageLookupValue(env); } diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/RepositoryMappingFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/RepositoryMappingFunction.java index ee6ea36772edef..2d21acaf527e45 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/RepositoryMappingFunction.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/RepositoryMappingFunction.java @@ -57,6 +57,7 @@ public SkyValue compute(SkyKey skyKey, Environment env) } RepositoryName repositoryName = ((RepositoryMappingValue.Key) skyKey).repoName(); boolean enableBzlmod = starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_BZLMOD); + boolean enableWorkspace = starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_WORKSPACE); if (enableBzlmod) { if (StarlarkBuiltinsValue.isBuiltinsRepo(repositoryName)) { @@ -102,7 +103,8 @@ public SkyValue compute(SkyKey skyKey, Environment env) } if (repositoryName.isMain() - && ((RepositoryMappingValue.Key) skyKey).rootModuleShouldSeeWorkspaceRepos()) { + && ((RepositoryMappingValue.Key) skyKey).rootModuleShouldSeeWorkspaceRepos() + && enableWorkspace) { // The root module should be able to see repos defined in WORKSPACE. Therefore, we find all // workspace repos and add them as extra visible repos in root module's repo mappings. PackageValue externalPackageValue = @@ -154,22 +156,26 @@ public SkyValue compute(SkyKey skyKey, Environment env) } } - PackageValue externalPackageValue = - (PackageValue) env.getValue(LabelConstants.EXTERNAL_PACKAGE_IDENTIFIER); - RepositoryMappingValue rootModuleRepoMappingValue = - enableBzlmod - ? (RepositoryMappingValue) - env.getValue(RepositoryMappingValue.KEY_FOR_ROOT_MODULE_WITHOUT_WORKSPACE_REPOS) - : null; - if (env.valuesMissing()) { - return null; + if (enableWorkspace) { + PackageValue externalPackageValue = + (PackageValue) env.getValue(LabelConstants.EXTERNAL_PACKAGE_IDENTIFIER); + RepositoryMappingValue rootModuleRepoMappingValue = + enableBzlmod + ? (RepositoryMappingValue) + env.getValue(RepositoryMappingValue.KEY_FOR_ROOT_MODULE_WITHOUT_WORKSPACE_REPOS) + : null; + if (env.valuesMissing()) { + return null; + } + + RepositoryMapping rootModuleRepoMapping = + rootModuleRepoMappingValue == null + ? null + : rootModuleRepoMappingValue.getRepositoryMapping(); + return computeFromWorkspace(repositoryName, externalPackageValue, rootModuleRepoMapping); } - RepositoryMapping rootModuleRepoMapping = - rootModuleRepoMappingValue == null - ? null - : rootModuleRepoMappingValue.getRepositoryMapping(); - return computeFromWorkspace(repositoryName, externalPackageValue, rootModuleRepoMapping); + throw new RepositoryMappingFunctionException(); } /** diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/WorkspaceNameFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/WorkspaceNameFunction.java index 0145d2ed131d12..10743ce38e03e2 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/WorkspaceNameFunction.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/WorkspaceNameFunction.java @@ -59,16 +59,19 @@ public SkyValue compute(SkyKey skyKey, Environment env) // danger (i.e. going through repo mapping is idempotent). return WorkspaceNameValue.withName(ruleClassProvider.getRunfilesPrefix()); } - PackageValue externalPackageValue = - (PackageValue) env.getValue(LabelConstants.EXTERNAL_PACKAGE_IDENTIFIER); - if (externalPackageValue == null) { - return null; - } - Package externalPackage = externalPackageValue.getPackage(); - if (externalPackage.containsErrors()) { - throw new WorkspaceNameFunctionException(); + if (starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_WORKSPACE)) { + PackageValue externalPackageValue = + (PackageValue) env.getValue(LabelConstants.EXTERNAL_PACKAGE_IDENTIFIER); + if (externalPackageValue == null) { + return null; + } + Package externalPackage = externalPackageValue.getPackage(); + if (externalPackage.containsErrors()) { + throw new WorkspaceNameFunctionException(); + } + return WorkspaceNameValue.withName(externalPackage.getWorkspaceName()); } - return WorkspaceNameValue.withName(externalPackage.getWorkspaceName()); + throw new WorkspaceNameFunctionException(); } private static class WorkspaceNameFunctionException extends SkyFunctionException { diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredExecutionPlatformsFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredExecutionPlatformsFunction.java index 671f9f906d676e..9aaa1ecf5bbcae 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredExecutionPlatformsFunction.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredExecutionPlatformsFunction.java @@ -116,7 +116,8 @@ public SkyValue compute(SkyKey skyKey, Environment env) // Get the registered execution platforms from the WORKSPACE. // The WORKSPACE suffixes don't register any execution platforms, so we can register all // platforms in WORKSPACE before those in non-root Bazel modules. - ImmutableList workspaceExecutionPlatforms = getWorkspaceExecutionPlatforms(env); + ImmutableList workspaceExecutionPlatforms = + getWorkspaceExecutionPlatforms(starlarkSemantics, env); if (workspaceExecutionPlatforms == null) { return null; } @@ -161,8 +162,11 @@ public SkyValue compute(SkyKey skyKey, Environment env) */ @Nullable @VisibleForTesting - public static ImmutableList getWorkspaceExecutionPlatforms(Environment env) - throws InterruptedException { + public static ImmutableList getWorkspaceExecutionPlatforms( + StarlarkSemantics semantics, Environment env) throws InterruptedException { + if (!semantics.getBool(BuildLanguageOptions.ENABLE_WORKSPACE)) { + return ImmutableList.of(); + } PackageValue externalPackageValue = (PackageValue) env.getValue(LabelConstants.EXTERNAL_PACKAGE_IDENTIFIER); if (externalPackageValue == null) { diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredToolchainsFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredToolchainsFunction.java index c01b3e6ee712f9..50ea6cdb16dd74 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredToolchainsFunction.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredToolchainsFunction.java @@ -106,7 +106,7 @@ public SkyValue compute(SkyKey skyKey, Environment env) // Get the toolchains from the user-supplied WORKSPACE file. ImmutableList userRegisteredWorkspaceToolchains = - getWorkspaceToolchains(env, /* userRegistered= */ true); + getWorkspaceToolchains(starlarkSemantics, env, /* userRegistered= */ true); if (userRegisteredWorkspaceToolchains == null) { return null; } @@ -122,7 +122,7 @@ public SkyValue compute(SkyKey skyKey, Environment env) // Get the toolchains from the Bazel-supplied WORKSPACE suffix. ImmutableList workspaceSuffixToolchains = - getWorkspaceToolchains(env, /* userRegistered= */ false); + getWorkspaceToolchains(starlarkSemantics, env, /* userRegistered= */ false); if (workspaceSuffixToolchains == null) { return null; } @@ -160,7 +160,11 @@ public SkyValue compute(SkyKey skyKey, Environment env) @Nullable @VisibleForTesting public static ImmutableList getWorkspaceToolchains( - Environment env, boolean userRegistered) throws InterruptedException { + StarlarkSemantics semantics, Environment env, boolean userRegistered) + throws InterruptedException { + if (!semantics.getBool(BuildLanguageOptions.ENABLE_WORKSPACE)) { + return ImmutableList.of(); + } PackageValue externalPackageValue = (PackageValue) env.getValue(LabelConstants.EXTERNAL_PACKAGE_IDENTIFIER); if (externalPackageValue == null) { diff --git a/src/test/java/com/google/devtools/build/lib/packages/semantics/ConsistencyTest.java b/src/test/java/com/google/devtools/build/lib/packages/semantics/ConsistencyTest.java index 62071e046880b0..bee10a18581577 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/semantics/ConsistencyTest.java +++ b/src/test/java/com/google/devtools/build/lib/packages/semantics/ConsistencyTest.java @@ -128,6 +128,7 @@ private static BuildLanguageOptions buildRandomOptions(Random rand) throws Excep "--experimental_bzl_visibility=" + rand.nextBoolean(), "--experimental_enable_android_migration_apis=" + rand.nextBoolean(), "--enable_bzlmod=" + rand.nextBoolean(), + "--enable_workspace=" + rand.nextBoolean(), "--experimental_isolated_extension_usages=" + rand.nextBoolean(), "--experimental_google_legacy_api=" + rand.nextBoolean(), "--experimental_platforms_api=" + rand.nextBoolean(), @@ -173,6 +174,7 @@ private static StarlarkSemantics buildRandomSemantics(Random rand) { .setBool( BuildLanguageOptions.EXPERIMENTAL_ENABLE_ANDROID_MIGRATION_APIS, rand.nextBoolean()) .setBool(BuildLanguageOptions.ENABLE_BZLMOD, rand.nextBoolean()) + .setBool(BuildLanguageOptions.ENABLE_WORKSPACE, rand.nextBoolean()) .setBool(BuildLanguageOptions.EXPERIMENTAL_ISOLATED_EXTENSION_USAGES, rand.nextBoolean()) .setBool(BuildLanguageOptions.EXPERIMENTAL_GOOGLE_LEGACY_API, rand.nextBoolean()) .setBool(BuildLanguageOptions.EXPERIMENTAL_PLATFORMS_API, rand.nextBoolean()) diff --git a/src/test/java/com/google/devtools/build/lib/repository/ExternalPackageHelperTest.java b/src/test/java/com/google/devtools/build/lib/repository/ExternalPackageHelperTest.java index 289d61b157cff0..ab1f307077581c 100644 --- a/src/test/java/com/google/devtools/build/lib/repository/ExternalPackageHelperTest.java +++ b/src/test/java/com/google/devtools/build/lib/repository/ExternalPackageHelperTest.java @@ -84,6 +84,7 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Stream; import javax.annotation.Nullable; +import net.starlark.java.eval.StarlarkSemantics; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -413,12 +414,14 @@ private static final class GetRegisteredToolchainsFunction implements SkyFunctio public SkyValue compute(SkyKey skyKey, Environment env) throws SkyFunctionException, InterruptedException { ImmutableList userRegisteredToolchains = - RegisteredToolchainsFunction.getWorkspaceToolchains(env, /* userRegistered= */ true); + RegisteredToolchainsFunction.getWorkspaceToolchains( + StarlarkSemantics.DEFAULT, env, /* userRegistered= */ true); if (userRegisteredToolchains == null) { return null; } ImmutableList workspaceSuffixRegisteredToolchains = - RegisteredToolchainsFunction.getWorkspaceToolchains(env, /* userRegistered= */ false); + RegisteredToolchainsFunction.getWorkspaceToolchains( + StarlarkSemantics.DEFAULT, env, /* userRegistered= */ false); if (workspaceSuffixRegisteredToolchains == null) { return null; } @@ -451,7 +454,8 @@ private static final class GetRegisteredExecutionPlatformsFunction implements Sk public SkyValue compute(SkyKey skyKey, Environment env) throws SkyFunctionException, InterruptedException { List registeredExecutionPlatforms = - RegisteredExecutionPlatformsFunction.getWorkspaceExecutionPlatforms(env); + RegisteredExecutionPlatformsFunction.getWorkspaceExecutionPlatforms( + StarlarkSemantics.DEFAULT, env); if (registeredExecutionPlatforms == null) { return null; } diff --git a/src/test/java/com/google/devtools/build/lib/rules/repository/RepositoryFunctionTest.java b/src/test/java/com/google/devtools/build/lib/rules/repository/RepositoryFunctionTest.java index 6198b13d32f0bb..d8f56d8eaae238 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/repository/RepositoryFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/repository/RepositoryFunctionTest.java @@ -30,7 +30,6 @@ import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; import com.google.devtools.build.lib.packages.Rule; import com.google.devtools.build.lib.vfs.FileStatus; -import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; import com.google.devtools.build.lib.vfs.Root; @@ -101,18 +100,6 @@ public void testGetTargetPathAbsolute() throws Exception { .isEqualTo(PathFragment.create("/a/b/c")); } - @Test - public void testGenerateWorkspaceFile() throws Exception { - Rule rule = scratchRule("external", "abc", "local_repository(", - " name = 'abc',", - " path = '/a/b/c',", - ")"); - RepositoryFunction.createWorkspaceFile(rootDirectory, rule.getTargetKind(), rule.getName()); - String workspaceContent = new String( - FileSystemUtils.readContentAsLatin1(rootDirectory.getRelative("WORKSPACE"))); - assertThat(workspaceContent).contains("workspace(name = \"abc\")"); - } - private static void assertMarkerFileEscaping(String testCase) { String escaped = RepositoryDelegatorFunction.escape(testCase); assertThat(RepositoryDelegatorFunction.unescape(escaped)).isEqualTo(testCase); diff --git a/src/test/py/bazel/bzlmod/bazel_fetch_test.py b/src/test/py/bazel/bzlmod/bazel_fetch_test.py index d7867d6d4e99dc..506ddc2f262957 100644 --- a/src/test/py/bazel/bzlmod/bazel_fetch_test.py +++ b/src/test/py/bazel/bzlmod/bazel_fetch_test.py @@ -34,7 +34,7 @@ def setUp(self): [ # In ipv6 only network, this has to be enabled. # 'startup --host_jvm_args=-Djava.net.preferIPv6Addresses=true', - 'common --enable_bzlmod', + 'common --noenable_workspace', 'common --experimental_isolated_extension_usages', 'common --registry=' + self.main_registry.getURL(), 'common --verbose_failures', @@ -44,21 +44,16 @@ def setUp(self): 'common --lockfile_mode=update', ], ) - self.ScratchFile('WORKSPACE') - # The existence of WORKSPACE.bzlmod prevents WORKSPACE prefixes or suffixes - # from being used; this allows us to test built-in modules actually work - self.ScratchFile('WORKSPACE.bzlmod') + self.ScratchFile('MODULE.bazel') self.generatBuiltinModules() def generatBuiltinModules(self): self.ScratchFile('platforms_mock/BUILD') - self.ScratchFile('platforms_mock/WORKSPACE') self.ScratchFile( 'platforms_mock/MODULE.bazel', ['module(name="local_config_platform")'] ) self.ScratchFile('tools_mock/BUILD') - self.ScratchFile('tools_mock/WORKSPACE') self.ScratchFile('tools_mock/MODULE.bazel', ['module(name="bazel_tools")']) self.ScratchFile('tools_mock/tools/build_defs/repo/BUILD') self.CopyFile( @@ -94,7 +89,6 @@ def testFetchAll(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD")', 'repo_rule = repository_rule(implementation=_repo_rule_impl)', '', @@ -132,7 +126,6 @@ def testFetchConfig(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD")', 'repo_rule = repository_rule(implementation=_repo_rule_impl)', 'repo_rule2 = repository_rule(implementation=_repo_rule_impl, ', @@ -243,7 +236,6 @@ def testForceFetch(self): 'def _repo_rule_impl(ctx):', ' file_content = ctx.read("' + file_path + '").strip()', ' print(file_content)', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD")', 'repo_rule = repository_rule(implementation=_repo_rule_impl)', '', diff --git a/src/test/py/bazel/bzlmod/bazel_lockfile_test.py b/src/test/py/bazel/bzlmod/bazel_lockfile_test.py index 67cbacf1126ec1..65dd87d667e367 100644 --- a/src/test/py/bazel/bzlmod/bazel_lockfile_test.py +++ b/src/test/py/bazel/bzlmod/bazel_lockfile_test.py @@ -43,7 +43,7 @@ def setUp(self): [ # In ipv6 only network, this has to be enabled. # 'startup --host_jvm_args=-Djava.net.preferIPv6Addresses=true', - 'build --enable_bzlmod', + 'build --noenable_workspace', 'build --experimental_isolated_extension_usages', 'build --registry=' + self.main_registry.getURL(), # We need to have BCR here to make sure built-in modules like @@ -56,10 +56,6 @@ def setUp(self): 'build --lockfile_mode=update', ], ) - self.ScratchFile('WORKSPACE') - # The existence of WORKSPACE.bzlmod prevents WORKSPACE prefixes or suffixes - # from being used; this allows us to test built-in modules actually work - self.ScratchFile('WORKSPACE.bzlmod') # TODO(pcloudy): investigate why this is needed, MODULE.bazel.lock is not # deterministic? os.remove(self.Path('MODULE.bazel.lock')) @@ -223,7 +219,6 @@ def testLocalOverrideWithErrorMode(self): ) self.ScratchFile('BUILD', ['filegroup(name = "hello")']) self.ScratchFile('bar/MODULE.bazel', ['module(name="bar")']) - self.ScratchFile('bar/WORKSPACE', []) self.ScratchFile('bar/BUILD', ['filegroup(name = "hello from bar")']) self.RunBazel( [ @@ -271,7 +266,6 @@ def testModuleExtension(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\'lala\')")', '', 'repo_rule = repository_rule(implementation=_repo_rule_impl)', @@ -326,7 +320,6 @@ def testIsolatedAndNonIsolatedModuleExtensions(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\'lala\')")', '', 'repo_rule = repository_rule(implementation=_repo_rule_impl)', @@ -374,7 +367,6 @@ def testUpdateIsolatedModuleExtension(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\'lala\')")', '', 'repo_rule = repository_rule(implementation=_repo_rule_impl)', @@ -446,7 +438,6 @@ def testModuleExtensionsInDifferentBuilds(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\'lala\')")', 'repo_rule = repository_rule(implementation=_repo_rule_impl)', 'def _ext_a_impl(ctx):', @@ -482,7 +473,6 @@ def testUpdateModuleExtension(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\\"lala\\")")', 'repo_rule = repository_rule(implementation = _repo_rule_impl)', 'def _mod_ext_impl(ctx):', @@ -510,7 +500,6 @@ def testUpdateModuleExtension(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\\"lala\\")")', 'repo_rule = repository_rule(implementation = _repo_rule_impl)', 'def _mod_ext_impl(ctx):', @@ -541,7 +530,6 @@ def testUpdateModuleExtensionErrorMode(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\\"lala\\")")', 'repo_rule = repository_rule(implementation = _repo_rule_impl)', 'def _mod_ext_impl(ctx):', @@ -559,7 +547,6 @@ def testUpdateModuleExtensionErrorMode(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\\"lalo\\")")', 'repo_rule = repository_rule(implementation = _repo_rule_impl)', 'def _mod_ext_impl(ctx):', @@ -601,7 +588,6 @@ def testRemoveModuleExtensionsNotUsed(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\'lala\')")', 'repo_rule = repository_rule(implementation=_repo_rule_impl)', 'def _ext_impl(ctx):', @@ -640,7 +626,6 @@ def testNoAbsoluteRootModuleFilePath(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\'lala\')")', '', 'repo_rule = repository_rule(implementation=_repo_rule_impl)', @@ -700,7 +685,6 @@ def testModuleExtensionEnvVariable(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\\"lala\\")")', 'repo_rule = repository_rule(implementation = _repo_rule_impl)', 'def _module_ext_impl(ctx):', @@ -742,7 +726,6 @@ def testChangeEnvVariableInErrorMode(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\\"lala\\")")', 'repo_rule = repository_rule(implementation = _repo_rule_impl)', 'def _module_ext_impl(ctx):', @@ -786,7 +769,6 @@ def testModuleExtensionWithFile(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\'lala\')")', '', 'repo_rule = repository_rule(implementation=_repo_rule_impl)', @@ -870,7 +852,6 @@ def testExtensionEvaluationDoesNotRerunOnChangedImports(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\'lala\')")', '', 'repo_rule = repository_rule(implementation=_repo_rule_impl)', @@ -992,7 +973,6 @@ def testLockfileRecreatedAfterDeletion(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\'lala\')")', '', 'repo_rule = repository_rule(implementation=_repo_rule_impl)', @@ -1034,7 +1014,6 @@ def testExtensionOsAndArch(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\'lala\')")', 'repo_rule = repository_rule(implementation=_repo_rule_impl)', '', @@ -1069,7 +1048,6 @@ def testExtensionOsAndArch(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\'lala\')")', 'repo_rule = repository_rule(implementation=_repo_rule_impl)', '', @@ -1102,7 +1080,6 @@ def testExtensionOsAndArch(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\'lala\')")', 'repo_rule = repository_rule(implementation=_repo_rule_impl)', '', @@ -1146,7 +1123,6 @@ def testExtensionEvaluationOnlyRerunOnRelevantUsagesChanges(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "exports_files([\\"data.txt\\"])")', ' ctx.file("data.txt", ctx.attr.value)', ' print(ctx.attr.value)', @@ -1365,7 +1341,6 @@ def testExtensionEvaluationRerunsIfDepGraphOrderChanges(self): 'extension.bzl', [ 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "exports_files([\\"data.txt\\"])")', ' ctx.file("data.txt", ctx.attr.value)', ' print(ctx.attr.value)', diff --git a/src/test/py/bazel/bzlmod/bazel_module_test.py b/src/test/py/bazel/bzlmod/bazel_module_test.py index c3e916243971b6..fe80d87066d5c9 100644 --- a/src/test/py/bazel/bzlmod/bazel_module_test.py +++ b/src/test/py/bazel/bzlmod/bazel_module_test.py @@ -44,7 +44,7 @@ def setUp(self): [ # In ipv6 only network, this has to be enabled. # 'startup --host_jvm_args=-Djava.net.preferIPv6Addresses=true', - 'build --enable_bzlmod', + 'build --noenable_workspace', 'build --registry=' + self.main_registry.getURL(), # We need to have BCR here to make sure built-in modules like # bazel_tools can work. @@ -54,13 +54,12 @@ def setUp(self): 'build --java_language_version=8', 'build --tool_java_language_version=8', 'build --lockfile_mode=update', - 'build --extra_toolchains=@bazel_tools//tools/python:autodetecting_toolchain' # fmt: skip pylint: disable=line-too-long + ( # fmt: skip pylint: disable=line-too-long + 'build' + ' --extra_toolchains=@bazel_tools//tools/python:autodetecting_toolchain' + ), ], ) - self.ScratchFile('WORKSPACE') - # The existence of WORKSPACE.bzlmod prevents WORKSPACE prefixes or suffixes - # from being used; this allows us to test built-in modules actually work - self.ScratchFile('WORKSPACE.bzlmod') def writeMainProjectFiles(self): self.ScratchFile('aaa.patch', [ @@ -244,11 +243,14 @@ def testRepositoryRuleErrorInModuleExtensionFailsTheBuild(self): 'use_repo(module_ext, "foo")', ]) self.ScratchFile('pkg/BUILD.bazel') - self.ScratchFile('pkg/rules.bzl', [ - 'def _repo_rule_impl(ctx):', - ' ctx.file("WORKSPACE")', - 'repo_rule = repository_rule(implementation = _repo_rule_impl)', - ]) + self.ScratchFile( + 'pkg/rules.bzl', + [ + 'def _repo_rule_impl(ctx):', + ' pass', + 'repo_rule = repository_rule(implementation = _repo_rule_impl)', + ], + ) self.ScratchFile('pkg/extension.bzl', [ 'load(":rules.bzl", "repo_rule")', 'def _module_ext_impl(ctx):', @@ -275,10 +277,8 @@ def testDownload(self): 'use_repo(data_ext, "no_op")', ]) self.ScratchFile('BUILD') - self.ScratchFile('WORKSPACE') self.ScratchFile('ext.bzl', [ 'def _no_op_impl(ctx):', - ' ctx.file("WORKSPACE")', ' ctx.file("BUILD", "filegroup(name=\\"no_op\\")")', 'no_op = repository_rule(_no_op_impl)', 'def _data_ext_impl(ctx):', @@ -313,7 +313,6 @@ def setUpProjectWithLocalRegistryModule(self, dep_name, dep_version): ' deps = ["@%s//:lib_%s"],' % (dep_name, dep_name), ')', ]) - self.ScratchFile('WORKSPACE', []) def testLocalRepoInSourceJsonAbsoluteBasePath(self): self.main_registry.setModuleBasePath(str(self.main_registry.projects)) @@ -336,7 +335,6 @@ def testNativePackageRelativeLabel(self): 'local_path_override(module_name="bar",path="bar")', ], ) - self.ScratchFile('WORKSPACE') self.ScratchFile('BUILD') self.ScratchFile( 'defs.bzl', @@ -363,7 +361,6 @@ def testNativePackageRelativeLabel(self): 'bazel_dep(name="foo", repo_name="bleb")', ], ) - self.ScratchFile('bar/WORKSPACE') self.ScratchFile( 'bar/quux/BUILD', [ @@ -400,7 +397,7 @@ def testWorkspaceEvaluatedBzlCanSeeRootModuleMappings(self): 'test()', ], ) - self.ScratchFile('foo/WORKSPACE') + self.ScratchFile('foo/REPO.bazel') self.ScratchFile('foo/BUILD', ['filegroup(name="test")']) self.ScratchFile( 'foo/test.bzl', @@ -413,7 +410,7 @@ def testWorkspaceEvaluatedBzlCanSeeRootModuleMappings(self): ], ) - _, _, stderr = self.RunBazel(['build', '@foo//:test']) + _, _, stderr = self.RunBazel(['build', '--enable_workspace', '@foo//:test']) stderr = '\n'.join(stderr) # @bar is mapped to @@baz, which Bzlmod doesn't recognize, so we leave it be self.assertIn('1st: @@baz//:z', stderr) @@ -440,18 +437,16 @@ def testWorkspaceItselfCanSeeRootModuleMappings(self): ], ) self.ScratchFile('BUILD', ['filegroup(name="a")']) - self.ScratchFile('hello/WORKSPACE') self.ScratchFile('hello/BUILD') self.ScratchFile('hello/MODULE.bazel', ['module(name="hello")']) self.ScratchFile('hello/world.bzl', ['message="I LUV U!"']) - _, _, stderr = self.RunBazel(['build', ':a']) + _, _, stderr = self.RunBazel(['build', '--enable_workspace', ':a']) self.assertIn('I LUV U!', '\n'.join(stderr)) def testNoModuleDotBazelAndFallbackToWorkspace(self): if os.path.exists(self.Path('MODULE.bazel')): os.remove(self.Path('MODULE.bazel')) - os.remove(self.Path('WORKSPACE.bzlmod')) self.ScratchFile( 'WORKSPACE', [ @@ -461,11 +456,11 @@ def testNoModuleDotBazelAndFallbackToWorkspace(self): ], ) self.ScratchFile('BUILD', ['filegroup(name="a")']) - self.ScratchFile('hello/WORKSPACE') + self.ScratchFile('hello/REPO.bazel') self.ScratchFile('hello/BUILD') self.ScratchFile('hello/world.bzl', ['message="I LUV U!"']) - _, _, stderr = self.RunBazel(['build', ':a']) + _, _, stderr = self.RunBazel(['build', '--enable_workspace', ':a']) self.assertIn('I LUV U!', '\n'.join(stderr)) # MODULE.bazel file should be generated automatically self.assertTrue(os.path.exists(self.Path('MODULE.bazel'))) @@ -519,7 +514,6 @@ def testNativeModuleNameAndVersion(self): 'local_path_override(module_name="bar",path="bar")', ], ) - self.ScratchFile('WORKSPACE') self.ScratchFile( 'WORKSPACE.bzlmod', ['local_repository(name="quux",path="quux")'] ) @@ -534,7 +528,6 @@ def testNativeModuleNameAndVersion(self): # `report_ext` which generates a repo `report_repo`. self.main_registry.createLocalPathModule('foo', '1.0', 'foo') projects_dir.joinpath('foo').mkdir(exist_ok=True) - scratchFile(projects_dir.joinpath('foo', 'WORKSPACE')) scratchFile( projects_dir.joinpath('foo', 'BUILD'), [ @@ -566,7 +559,6 @@ def testNativeModuleNameAndVersion(self): ], ) # bar: a repo defined by a Bazel module with a non-registry override - self.ScratchFile('bar/WORKSPACE') self.ScratchFile( 'bar/MODULE.bazel', [ @@ -582,7 +574,7 @@ def testNativeModuleNameAndVersion(self): ], ) # quux: a repo defined by WORKSPACE - self.ScratchFile('quux/WORKSPACE') + self.ScratchFile('quux/REPO.bazel') self.ScratchFile( 'quux/BUILD', [ @@ -594,6 +586,7 @@ def testNativeModuleNameAndVersion(self): _, _, stderr = self.RunBazel( [ 'build', + '--enable_workspace', ':a', '@foo//:a', '@report_repo//:a', @@ -616,7 +609,6 @@ def testWorkspaceToolchainRegistrationWithPlatformsConstraint(self): self.ScratchFile( 'WORKSPACE', ['register_toolchains("//:my_toolchain_toolchain")'] ) - os.remove(self.Path('WORKSPACE.bzlmod')) self.ScratchFile( 'BUILD.bazel', @@ -682,6 +674,7 @@ def testWorkspaceToolchainRegistrationWithPlatformsConstraint(self): self.RunBazel([ 'build', + '--enable_workspace', '//:my_consumer', '--toolchain_resolution_debug=//:my_toolchain_type', ]) @@ -752,7 +745,6 @@ def testLocationNonRegistry(self): ], ) self.ScratchFile('hello/MODULE.bazel', ['wat']) - self.ScratchFile('hello/WORKSPACE.bazel') _, _, stderr = self.RunBazel(['build', '@what'], allow_failure=True) self.assertIn('ERROR: @@hello~override//:MODULE.bazel', '\n'.join(stderr)) @@ -774,7 +766,6 @@ def testLoadRulesJavaSymbolThroughBazelTools(self): " 'find_java_toolchain')" ), 'def _repo_impl(ctx):', - " ctx.file('WORKSPACE')", " ctx.file('BUILD', 'exports_files([\"data.txt\"])')", " ctx.file('data.txt', 'hi')", 'repo = repository_rule(implementation = _repo_impl)', diff --git a/src/test/py/bazel/bzlmod/bazel_overrides_test.py b/src/test/py/bazel/bzlmod/bazel_overrides_test.py index f998a043c62f40..b942700fe314a3 100644 --- a/src/test/py/bazel/bzlmod/bazel_overrides_test.py +++ b/src/test/py/bazel/bzlmod/bazel_overrides_test.py @@ -41,7 +41,7 @@ def setUp(self): [ # In ipv6 only network, this has to be enabled. # 'startup --host_jvm_args=-Djava.net.preferIPv6Addresses=true', - 'build --enable_bzlmod', + 'build --noenable_workspace', 'build --registry=' + self.main_registry.getURL(), # We need to have BCR here to make sure built-in modules like # bazel_tools can work. @@ -53,10 +53,6 @@ def setUp(self): 'build --lockfile_mode=update', ], ) - self.ScratchFile('WORKSPACE') - # The existence of WORKSPACE.bzlmod prevents WORKSPACE prefixes or suffixes - # from being used; this allows us to test built-in modules actually work - self.ScratchFile('WORKSPACE.bzlmod') def writeMainProjectFiles(self): self.ScratchFile( @@ -237,7 +233,6 @@ def testCmdAbsoluteModuleOverride(self): ], ) self.ScratchFile('BUILD') - self.ScratchFile('WORKSPACE') self.ScratchFile( 'aa/MODULE.bazel', @@ -251,7 +246,6 @@ def testCmdAbsoluteModuleOverride(self): 'filegroup(name = "never_ever")', ], ) - self.ScratchFile('aa/WORKSPACE') self.ScratchFile( 'bb/MODULE.bazel', @@ -265,7 +259,6 @@ def testCmdAbsoluteModuleOverride(self): 'filegroup(name = "choose_me")', ], ) - self.ScratchFile('bb/WORKSPACE') _, _, stderr = self.RunBazel( ['build', '@ss//:all', '--override_module', 'ss=' + self.Path('bb')] @@ -276,7 +269,6 @@ def testCmdAbsoluteModuleOverride(self): ) def testCmdRelativeModuleOverride(self): - self.ScratchFile('aa/WORKSPACE') self.ScratchFile( 'aa/MODULE.bazel', [ @@ -287,7 +279,6 @@ def testCmdRelativeModuleOverride(self): self.ScratchFile('aa/cc/BUILD') - self.ScratchFile('bb/WORKSPACE') self.ScratchFile( 'bb/MODULE.bazel', [ @@ -316,7 +307,6 @@ def testCmdRelativeModuleOverride(self): ) def testCmdWorkspaceRelativeModuleOverride(self): - self.ScratchFile('WORKSPACE') self.ScratchFile( 'MODULE.bazel', [ @@ -325,7 +315,6 @@ def testCmdWorkspaceRelativeModuleOverride(self): ) self.ScratchFile('BUILD') self.ScratchFile('aa/BUILD') - self.ScratchFile('bb/WORKSPACE') self.ScratchFile( 'bb/MODULE.bazel', [ diff --git a/src/test/py/bazel/bzlmod/bazel_repo_mapping_test.py b/src/test/py/bazel/bzlmod/bazel_repo_mapping_test.py index 909c45e34c34e5..125d9c7ed65ff4 100644 --- a/src/test/py/bazel/bzlmod/bazel_repo_mapping_test.py +++ b/src/test/py/bazel/bzlmod/bazel_repo_mapping_test.py @@ -42,7 +42,7 @@ def setUp(self): [ # In ipv6 only network, this has to be enabled. # 'startup --host_jvm_args=-Djava.net.preferIPv6Addresses=true', - 'build --enable_bzlmod', + 'build --noenable_workspace', 'build --registry=' + self.main_registry.getURL(), # We need to have BCR here to make sure built-in modules like # bazel_tools can work. @@ -52,13 +52,12 @@ def setUp(self): 'build --java_language_version=8', 'build --tool_java_language_version=8', 'build --lockfile_mode=update', - 'build --extra_toolchains=@bazel_tools//tools/python:autodetecting_toolchain' # fmt: skip pylint: disable=line-too-long + ( # fmt: skip pylint: disable=line-too-long + 'build' + ' --extra_toolchains=@bazel_tools//tools/python:autodetecting_toolchain' + ), ], ) - self.ScratchFile('WORKSPACE') - # The existence of WORKSPACE.bzlmod prevents WORKSPACE prefixes or suffixes - # from being used; this allows us to test built-in modules actually work - self.ScratchFile('WORKSPACE.bzlmod') def testRunfilesRepoMappingManifest(self): self.main_registry.setModuleBasePath('projects') @@ -67,8 +66,6 @@ def testRunfilesRepoMappingManifest(self): # Set up a "bare_rule" module that contains the "bare_test" rule which # passes runfiles along self.main_registry.createLocalPathModule('bare_rule', '1.0', 'bare_rule') - projects_dir.joinpath('bare_rule').mkdir(exist_ok=True) - scratchFile(projects_dir.joinpath('bare_rule', 'WORKSPACE')) scratchFile(projects_dir.joinpath('bare_rule', 'BUILD')) # The working directory of a test is the subdirectory of the runfiles # directory corresponding to the main repository. @@ -109,7 +106,6 @@ def testRunfilesRepoMappingManifest(self): 'bazel_dep(name="bare_rule",version="1.0")', ], ) - self.ScratchFile('WORKSPACE') self.ScratchFile('WORKSPACE.bzlmod', ['workspace(name="me_ws")']) self.ScratchFile( 'BUILD', @@ -136,8 +132,6 @@ def testRunfilesRepoMappingManifest(self): ('quux1', 'bare_test(name="quux")'), ('quux2', 'bare_test(name="quux")'), ]: - projects_dir.joinpath(dir_name).mkdir(exist_ok=True) - scratchFile(projects_dir.joinpath(dir_name, 'WORKSPACE')) scratchFile( projects_dir.joinpath(dir_name, 'BUILD'), [ @@ -155,7 +149,9 @@ def testRunfilesRepoMappingManifest(self): bazel_command = 'build' if self.IsWindows() else 'test' # Finally we get to build stuff! - self.RunBazel([bazel_command, '//:me', '--test_output=errors']) + self.RunBazel( + [bazel_command, '--enable_workspace', '//:me', '--test_output=errors'] + ) paths = ['bazel-bin/me.repo_mapping'] if not self.IsWindows(): @@ -174,7 +170,12 @@ def testRunfilesRepoMappingManifest(self): with open(self.Path('bazel-bin/me.runfiles_manifest')) as f: self.assertIn('_repo_mapping ', f.read()) - self.RunBazel([bazel_command, '@bar//:bar', '--test_output=errors']) + self.RunBazel([ + bazel_command, + '--enable_workspace', + '@bar//:bar', + '--test_output=errors', + ]) paths = ['bazel-bin/external/bar~2.0/bar.repo_mapping'] if not self.IsWindows(): @@ -197,8 +198,6 @@ def testBashRunfilesLibraryRepoMapping(self): projects_dir = self.main_registry.projects self.main_registry.createLocalPathModule('data', '1.0', 'data') - projects_dir.joinpath('data').mkdir(exist_ok=True) - scratchFile(projects_dir.joinpath('data', 'WORKSPACE')) scratchFile(projects_dir.joinpath('data', 'foo.txt'), ['hello']) scratchFile( projects_dir.joinpath('data', 'BUILD'), ['exports_files(["foo.txt"])'] @@ -207,8 +206,6 @@ def testBashRunfilesLibraryRepoMapping(self): self.main_registry.createLocalPathModule( 'test', '1.0', 'test', {'data': '1.0'} ) - projects_dir.joinpath('test').mkdir(exist_ok=True) - scratchFile(projects_dir.joinpath('test', 'WORKSPACE')) scratchFile( projects_dir.joinpath('test', 'BUILD'), [ @@ -242,7 +239,6 @@ def testBashRunfilesLibraryRepoMapping(self): os.chmod(test_script, 0o755) self.ScratchFile('MODULE.bazel', ['bazel_dep(name="test",version="1.0")']) - self.ScratchFile('WORKSPACE') # Run sandboxed on Linux and macOS. self.RunBazel( @@ -264,8 +260,6 @@ def testCppRunfilesLibraryRepoMapping(self): projects_dir = self.main_registry.projects self.main_registry.createLocalPathModule('data', '1.0', 'data') - projects_dir.joinpath('data').mkdir(exist_ok=True) - scratchFile(projects_dir.joinpath('data', 'WORKSPACE')) scratchFile(projects_dir.joinpath('data', 'foo.txt'), ['hello']) scratchFile( projects_dir.joinpath('data', 'BUILD'), ['exports_files(["foo.txt"])'] @@ -274,8 +268,6 @@ def testCppRunfilesLibraryRepoMapping(self): self.main_registry.createLocalPathModule( 'test', '1.0', 'test', {'data': '1.0'} ) - projects_dir.joinpath('test').mkdir(exist_ok=True) - scratchFile(projects_dir.joinpath('test', 'WORKSPACE')) scratchFile( projects_dir.joinpath('test', 'BUILD'), [ @@ -309,7 +301,6 @@ def testCppRunfilesLibraryRepoMapping(self): ) self.ScratchFile('MODULE.bazel', ['bazel_dep(name="test",version="1.0")']) - self.ScratchFile('WORKSPACE') # Run sandboxed on Linux and macOS. self.RunBazel(['test', '@test//:test', '--test_output=errors']) @@ -321,8 +312,6 @@ def testJavaRunfilesLibraryRepoMapping(self): projects_dir = self.main_registry.projects self.main_registry.createLocalPathModule('data', '1.0', 'data') - projects_dir.joinpath('data').mkdir(exist_ok=True) - scratchFile(projects_dir.joinpath('data', 'WORKSPACE')) scratchFile(projects_dir.joinpath('data', 'foo.txt'), ['hello']) scratchFile( projects_dir.joinpath('data', 'BUILD'), ['exports_files(["foo.txt"])'] @@ -331,8 +320,6 @@ def testJavaRunfilesLibraryRepoMapping(self): self.main_registry.createLocalPathModule( 'test', '1.0', 'test', {'data': '1.0'} ) - projects_dir.joinpath('test').mkdir(exist_ok=True) - scratchFile(projects_dir.joinpath('test', 'WORKSPACE')) scratchFile( projects_dir.joinpath('test', 'BUILD'), [ @@ -377,7 +364,6 @@ def testJavaRunfilesLibraryRepoMapping(self): ) self.ScratchFile('MODULE.bazel', ['bazel_dep(name="test",version="1.0")']) - self.ScratchFile('WORKSPACE') # Run sandboxed on Linux and macOS. self.RunBazel( diff --git a/src/test/py/bazel/bzlmod/bazel_yanked_versions_test.py b/src/test/py/bazel/bzlmod/bazel_yanked_versions_test.py index 7bb05f8cedb890..5f8c7acbc99abc 100644 --- a/src/test/py/bazel/bzlmod/bazel_yanked_versions_test.py +++ b/src/test/py/bazel/bzlmod/bazel_yanked_versions_test.py @@ -49,10 +49,6 @@ def setUp(self): 'yanked2', yanked_versions={'1.0': 'sketchy'} ) self.writeBazelrcFile() - self.ScratchFile('WORKSPACE') - # The existence of WORKSPACE.bzlmod prevents WORKSPACE prefixes or suffixes - # from being used; this allows us to test built-in modules actually work - self.ScratchFile('WORKSPACE.bzlmod') def writeBazelrcFile(self, allow_yanked_versions=True): self.ScratchFile( @@ -60,7 +56,7 @@ def writeBazelrcFile(self, allow_yanked_versions=True): [ # In ipv6 only network, this has to be enabled. # 'startup --host_jvm_args=-Djava.net.preferIPv6Addresses=true', - 'build --enable_bzlmod', + 'build --noenable_workspace', 'build --registry=' + self.main_registry.getURL(), # We need to have BCR here to make sure built-in modules like # bazel_tools can work. @@ -95,7 +91,6 @@ def testNonRegistryOverriddenModulesIgnoreYanked(self): ')', ], ) - self.ScratchFile('WORKSPACE') self.ScratchFile( 'BUILD', [ @@ -116,7 +111,6 @@ def testContainingYankedDepFails(self): 'bazel_dep(name = "yanked1", version = "1.0")', ], ) - self.ScratchFile('WORKSPACE') self.ScratchFile( 'BUILD', [ @@ -145,7 +139,6 @@ def testAllowedYankedDepsSuccessByFlag(self): 'bazel_dep(name = "ddd", version = "1.0")', ], ) - self.ScratchFile('WORKSPACE') self.ScratchFile( 'BUILD', [ @@ -173,7 +166,6 @@ def testAllowedYankedDepsByEnvVar(self): 'bazel_dep(name = "ddd", version = "1.0")', ], ) - self.ScratchFile('WORKSPACE') self.ScratchFile( 'BUILD', [ @@ -210,7 +202,6 @@ def testAllowedYankedDepsSuccessMix(self): 'bazel_dep(name = "ddd", version = "1.0")', ], ) - self.ScratchFile('WORKSPACE') self.ScratchFile( 'BUILD', [ diff --git a/src/test/py/bazel/bzlmod/bzlmod_credentials_test.py b/src/test/py/bazel/bzlmod/bzlmod_credentials_test.py index c4927ad51c7021..f22fe95716a2f9 100644 --- a/src/test/py/bazel/bzlmod/bzlmod_credentials_test.py +++ b/src/test/py/bazel/bzlmod/bzlmod_credentials_test.py @@ -38,15 +38,11 @@ def setUp(self): [ # In ipv6 only network, this has to be enabled. # 'startup --host_jvm_args=-Djava.net.preferIPv6Addresses=true', - 'common --enable_bzlmod', + 'common --noenable_workspace', # Disable yanked version check so we are not affected BCR changes. 'common --allow_yanked_versions=all', ], ) - self.ScratchFile('WORKSPACE') - # The existence of WORKSPACE.bzlmod prevents WORKSPACE prefixes or suffixes - # from being used; this allows us to test built-in modules actually work - self.ScratchFile('WORKSPACE.bzlmod') self.ScratchFile( 'MODULE.bazel', [ diff --git a/src/test/py/bazel/bzlmod/bzlmod_query_test.py b/src/test/py/bazel/bzlmod/bzlmod_query_test.py index 4c6351bac2f373..453cd02b17b9e4 100644 --- a/src/test/py/bazel/bzlmod/bzlmod_query_test.py +++ b/src/test/py/bazel/bzlmod/bzlmod_query_test.py @@ -40,7 +40,7 @@ def setUp(self): [ # In ipv6 only network, this has to be enabled. # 'startup --host_jvm_args=-Djava.net.preferIPv6Addresses=true', - 'common --experimental_enable_bzlmod', + 'common --noenable_workspace', 'common --registry=' + self.main_registry.getURL(), # We need to have BCR here to make sure built-in modules like # bazel_tools can work. @@ -49,10 +49,6 @@ def setUp(self): 'common --allow_yanked_versions=all', ], ) - self.ScratchFile('WORKSPACE') - # The existence of WORKSPACE.bzlmod prevents WORKSPACE prefixes or suffixes - # from being used; this allows us to test built-in modules actually work - self.ScratchFile('WORKSPACE.bzlmod') def testQueryModuleRepoTargetsBelow(self): self.ScratchFile('MODULE.bazel', [ diff --git a/src/test/py/bazel/bzlmod/mod_command_test.py b/src/test/py/bazel/bzlmod/mod_command_test.py index 4d77c854a1eb41..d17264ed2f7923 100644 --- a/src/test/py/bazel/bzlmod/mod_command_test.py +++ b/src/test/py/bazel/bzlmod/mod_command_test.py @@ -40,7 +40,7 @@ def setUp(self): [ # In ipv6 only network, this has to be enabled. # 'startup --host_jvm_args=-Djava.net.preferIPv6Addresses=true', - 'mod --enable_bzlmod', + 'mod --noenable_workspace', 'mod --registry=' + self.main_registry.getURL(), # We need to have BCR here to make sure built-in modules like # bazel_tools can work. @@ -51,10 +51,6 @@ def setUp(self): 'mod --charset=ascii', ], ) - self.ScratchFile('WORKSPACE') - # The existence of WORKSPACE.bzlmod prevents WORKSPACE prefixes or suffixes - # from being used; this allows us to test built-in modules actually work - self.ScratchFile('WORKSPACE.bzlmod') self.ScratchFile( 'MODULE.bazel', @@ -130,13 +126,9 @@ def setUp(self): ] self.main_registry.createLocalPathModule('ext', '1.0', 'ext') - self.projects_dir.joinpath('ext').mkdir(exist_ok=True) - scratchFile(self.projects_dir.joinpath('ext', 'WORKSPACE')) scratchFile(self.projects_dir.joinpath('ext', 'BUILD')) scratchFile(self.projects_dir.joinpath('ext', 'ext.bzl'), ext_src) self.main_registry.createLocalPathModule('ext2', '1.0', 'ext2') - self.projects_dir.joinpath('ext2').mkdir(exist_ok=True) - scratchFile(self.projects_dir.joinpath('ext2', 'WORKSPACE')) scratchFile(self.projects_dir.joinpath('ext2', 'BUILD')) scratchFile(self.projects_dir.joinpath('ext2', 'ext.bzl'), ext_src) diff --git a/src/test/py/bazel/bzlmod/test_utils.py b/src/test/py/bazel/bzlmod/test_utils.py index cfb38af14b3677..f93d41040eeb67 100644 --- a/src/test/py/bazel/bzlmod/test_utils.py +++ b/src/test/py/bazel/bzlmod/test_utils.py @@ -155,7 +155,6 @@ def calc_repo_name_str(dep): return '' return ', repo_name = "%s"' % repo_names[dep] - scratchFile(src_dir.joinpath('WORKSPACE')) scratchFile( src_dir.joinpath('MODULE.bazel'), [ @@ -330,13 +329,15 @@ def createLocalPathModule(self, name, version, path, deps=None): if deps is None: deps = {} - scratchFile( - module_dir.joinpath('MODULE.bazel'), [ - 'module(', - ' name = "%s",' % name, - ' version = "%s",' % version, - ')', - ] + ['bazel_dep(name="%s",version="%s")' % p for p in deps.items()]) + module_file_lines = [ + 'module(', + ' name = "%s",' % name, + ' version = "%s",' % version, + ')', + ] + ['bazel_dep(name="%s",version="%s")' % p for p in deps.items()] + scratchFile(module_dir.joinpath('MODULE.bazel'), module_file_lines) + self.projects.joinpath(path).mkdir(exist_ok=True) + scratchFile(self.projects.joinpath(path, 'MODULE.bazel'), module_file_lines) with module_dir.joinpath('source.json').open('w') as f: json.dump(source, f, indent=4, sort_keys=True) diff --git a/src/test/shell/bazel/local_repository_test.sh b/src/test/shell/bazel/local_repository_test.sh index aef690ecfc74f4..3ee5332a0a1792 100755 --- a/src/test/shell/bazel/local_repository_test.sh +++ b/src/test/shell/bazel/local_repository_test.sh @@ -1165,17 +1165,12 @@ local_repository( EOF bazel build @r//... &> $TEST_log && fail "Build succeeded unexpectedly" - expect_log "No WORKSPACE file found" + expect_log "No MODULE.bazel, REPO.bazel, or WORKSPACE file found" # Create the workspace and verify it now succeeds. create_workspace_with_default_repos $r/WORKSPACE bazel build @r//... &> $TEST_log || fail "Build failed unexpectedly" - expect_not_log "No WORKSPACE file found" - - # Remove again and verify it fails again. - rm -f $r/WORKSPACE - bazel build @r//... &> $TEST_log && fail "Build succeeded unexpectedly" - expect_log "No WORKSPACE file found" + expect_not_log "No MODULE.bazel, REPO.bazel, or WORKSPACE file found" } # Regression test for #1697.