Skip to content

Commit 80c59de

Browse files
juliexxiakatre
authored andcommitted
fix main repo starlark options parsing - now flags passed on the command line as --@main_workspace//flag and --//flag will both parse to --//flag. Before this CL, the former maintained its workspace prefix and we would get different entries for these two formats.
work towards bazelbuild#11128 PiperOrigin-RevId: 350575360
1 parent 19491a9 commit 80c59de

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/main/java/com/google/devtools/build/lib/runtime/StarlarkOptionsParser.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public void parse(ExtendedEventHandler eventHandler) throws OptionsParsingExcept
109109
ImmutableMap.Builder<String, Object> parsedOptions = new ImmutableMap.Builder<>();
110110
for (Map.Entry<String, Pair<String, Target>> option : unparsedOptions.entrySet()) {
111111
String loadedFlag = option.getKey();
112+
// String loadedFlag =
113+
// Label.parseAbsoluteUnchecked(option.getKey()).getDefaultCanonicalForm();
112114
String unparsedValue = option.getValue().first;
113115
Target buildSettingTarget = option.getValue().second;
114116
BuildSetting buildSetting =
@@ -155,7 +157,11 @@ private void parseArg(
155157
if (value != null) {
156158
// --flag=value or -flag=value form
157159
Target buildSettingTarget = loadBuildSetting(name, nativeOptionsParser, eventHandler);
158-
unparsedOptions.put(name, new Pair<>(value, buildSettingTarget));
160+
// Use the unambiguous canonical form to ensure we don't have
161+
// duplicate options getting into the starlark options map.
162+
unparsedOptions.put(
163+
buildSettingTarget.getLabel().getDefaultCanonicalForm(),
164+
new Pair<>(value, buildSettingTarget));
159165
} else {
160166
boolean booleanValue = true;
161167
// check --noflag form

src/test/java/com/google/devtools/build/lib/starlark/StarlarkOptionsParsingTest.java

+27-5
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,40 @@ public void testFlagEqualsValueForm() throws Exception {
4545
assertThat(result.getResidue()).isEmpty();
4646
}
4747

48-
// test --@workspace//flag=value
48+
// test --@main_workspace//flag=value parses out to //flag=value
49+
// test --@other_workspace//flag=value parses out to @other_workspace//flag=value
4950
@Test
5051
public void testFlagNameWithWorkspace() throws Exception {
5152
writeBasicIntFlag();
52-
rewriteWorkspace("workspace(name = 'starlark_options_test')");
53+
scratch.file("test/repo2/WORKSPACE");
54+
scratch.file(
55+
"test/repo2/defs.bzl",
56+
"def _impl(ctx):",
57+
" pass",
58+
"my_flag = rule(",
59+
" implementation = _impl,",
60+
" build_setting = config.int(flag = True),",
61+
")");
62+
scratch.file(
63+
"test/repo2/BUILD",
64+
"load(':defs.bzl', 'my_flag')",
65+
"my_flag(name = 'flag2', build_setting_default=2)");
66+
67+
rewriteWorkspace(
68+
"workspace(name = 'starlark_options_test')",
69+
"local_repository(",
70+
" name = 'repo2',",
71+
" path = 'test/repo2',",
72+
")");
5373

5474
OptionsParsingResult result =
55-
parseStarlarkOptions("--@starlark_options_test//test:my_int_setting=666");
75+
parseStarlarkOptions(
76+
"--@starlark_options_test//test:my_int_setting=666 --@repo2//:flag2=222");
5677

57-
assertThat(result.getStarlarkOptions()).hasSize(1);
58-
assertThat(result.getStarlarkOptions().get("@starlark_options_test//test:my_int_setting"))
78+
assertThat(result.getStarlarkOptions()).hasSize(2);
79+
assertThat(result.getStarlarkOptions().get("//test:my_int_setting"))
5980
.isEqualTo(StarlarkInt.of(666));
81+
assertThat(result.getStarlarkOptions().get("@repo2//:flag2")).isEqualTo(StarlarkInt.of(222));
6082
assertThat(result.getResidue()).isEmpty();
6183
}
6284

0 commit comments

Comments
 (0)