Skip to content

Commit

Permalink
Allow discard previous overrides for --override_repository and --over…
Browse files Browse the repository at this point in the history
…ride_module

`--override_module=foo=` will discard previous overrides specified by this option.

Closes #22751.

PiperOrigin-RevId: 644253498
Change-Id: If723756433545bfc875d7f877d596f2e3f041464
  • Loading branch information
meteorcloudy authored and Copybara-Service committed Jun 18, 2024
1 parent fc570e1 commit 82e3135
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,10 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
// We use a LinkedHashMap to preserve the iteration order.
Map<RepositoryName, PathFragment> overrideMap = new LinkedHashMap<>();
for (RepositoryOverride override : repoOptions.repositoryOverrides) {
if (override.path().isEmpty()) {
overrideMap.remove(override.repositoryName());
continue;
}
String repoPath = getAbsolutePath(override.path(), env);
overrideMap.put(override.repositoryName(), PathFragment.create(repoPath));
}
Expand All @@ -477,6 +481,10 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
if (repoOptions.moduleOverrides != null) {
Map<String, ModuleOverride> moduleOverrideMap = new LinkedHashMap<>();
for (RepositoryOptions.ModuleOverride override : repoOptions.moduleOverrides) {
if (override.path().isEmpty()) {
moduleOverrideMap.remove(override.moduleName());
continue;
}
String modulePath = getAbsolutePath(override.path(), env);
moduleOverrideMap.put(override.moduleName(), LocalPathOverride.create(modulePath));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ public class RepositoryOptions extends OptionsBase {
+ " given path is an absolute path, it will be used as it is. If the given path is a"
+ " relative path, it is relative to the current working directory. If the given path"
+ " starts with '%workspace%, it is relative to the workspace root, which is the"
+ " output of `bazel info workspace`")
+ " output of `bazel info workspace`. If the given path is empty, then remove any"
+ " previous overrides.")
public List<RepositoryOverride> repositoryOverrides;

@Option(
Expand All @@ -186,7 +187,8 @@ public class RepositoryOptions extends OptionsBase {
+ " path is an absolute path, it will be used as it is. If the given path is a"
+ " relative path, it is relative to the current working directory. If the given path"
+ " starts with '%workspace%, it is relative to the workspace root, which is the"
+ " output of `bazel info workspace`")
+ " output of `bazel info workspace`. If the given path is empty, then remove any"
+ " previous overrides.")
public List<ModuleOverride> moduleOverrides;

@Option(
Expand Down
21 changes: 21 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_overrides_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,27 @@ def testCmdRelativeModuleOverride(self):
'Target @@ss~//:choose_me up-to-date (nothing to build)', stderr
)

# Test delete previous overrides
_, _, stderr = self.RunBazel(
[
'build',
'--announce_rc',
'@ss//:all',
'--override_module',
'ss=../../bb',
'--override_module',
'ss=',
'--enable_bzlmod',
],
cwd=self.Path('aa/cc'),
allow_failure=True,
)
self.assertIn(
'ERROR: Error computing the main repository mapping: module not found'
' in registries: ss@1.0',
stderr,
)

def testCmdWorkspaceRelativeModuleOverride(self):
self.ScratchFile(
'MODULE.bazel',
Expand Down

0 comments on commit 82e3135

Please sign in to comment.