Don't overwrite user-set Starlark flags with PROJECT.scl Starlark flags.

(when enforcement is "warn" or "compatible").

We had a bug in that when PROJECT.scl output calls `starlarkOptionsParser.parseGivenArg` it [overwrites](https://github.com/bazelbuild/bazel/blob/0dd18a5e8adbc0d27c3b4da88fae59d5ab09c3fe/src/main/java/com/google/devtools/build/lib/runtime/StarlarkOptionsParser.java#L266) whatever Starlark flags were already parsed.

Also added a new Java integration test to be able to emulate more of the build process without having to do a full end-to-end shell test.

PiperOrigin-RevId: 808336133
Change-Id: I1e83b7140e28e1337a5dfb98b2603fb10da07310
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java b/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java
index 8f672ee..e9af571 100644
--- a/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java
@@ -173,6 +173,7 @@
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Stream;
 import javax.annotation.Nullable;
 
 /**
@@ -349,8 +350,11 @@
                 .build();
         Preconditions.checkState(
             starlarkOptionsParser.parseGivenArgs(
-                projectEvaluationResult.buildOptions().stream()
-                    .filter(o -> STARLARK_SKIPPED_PREFIXES.stream().anyMatch(o::startsWith))
+                Stream.concat(
+                        projectEvaluationResult.buildOptions().stream()
+                            .filter(
+                                o -> STARLARK_SKIPPED_PREFIXES.stream().anyMatch(o::startsWith)),
+                        optionsParser.getSkippedArgs().stream())
                     .collect(toImmutableList())));
 
         env.getEventBus()
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/config/BUILD b/src/test/java/com/google/devtools/build/lib/analysis/config/BUILD
index f3c1551..079781e 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/config/BUILD
+++ b/src/test/java/com/google/devtools/build/lib/analysis/config/BUILD
@@ -194,6 +194,20 @@
 )
 
 java_test(
+    name = "FlagSetIntegrationTest",
+    srcs = ["FlagSetIntegrationTest.java"],
+    deps = [
+        "//src/main/java/com/google/devtools/build/lib/analysis/config:invalid_configuration_exception",
+        "//src/main/java/com/google/devtools/build/lib/cmdline",
+        "//src/test/java/com/google/devtools/build/lib/buildtool/util",
+        "//third_party:guava",
+        "//third_party:junit4",
+        "//third_party:truth",
+        "@maven//:com_google_testparameterinjector_test_parameter_injector",
+    ],
+)
+
+java_test(
     name = "FragmentRegistryTest",
     srcs = ["FragmentRegistryTest.java"],
     deps = [
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/config/FlagSetIntegrationTest.java b/src/test/java/com/google/devtools/build/lib/analysis/config/FlagSetIntegrationTest.java
new file mode 100644
index 0000000..faf8e62
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/analysis/config/FlagSetIntegrationTest.java
@@ -0,0 +1,249 @@
+// Copyright 2025 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.google.devtools.build.lib.analysis.config;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.junit.Assert.assertThrows;
+
+import com.google.common.collect.ImmutableList;
+import com.google.devtools.build.lib.buildtool.util.BuildIntegrationTestCase;
+import com.google.devtools.build.lib.cmdline.Label;
+import com.google.testing.junit.testparameterinjector.TestParameter;
+import com.google.testing.junit.testparameterinjector.TestParameterInjector;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Integration tests for building with {@code --scl_config} (flag sets). See {@link Project} and
+ * {@link CoreOptions#sclConfig} for details.
+ *
+ * <p>Use this for tests that cover building targets with {@code --scl-config}.
+ *
+ * <p>If you just want to test {@link com.google.devtools.build.lib.skyframe.config.FlagSetFunction}
+ * (i.e. parsing --scl_config independent of how builds use it), use {@link
+ * com.google.devtools.build.lib.skyframe.config.FlagSetFunctionTest}.
+ *
+ * <p>If you need full end-to-end testing, use {@code flagset_tests.sh}.
+ */
+@RunWith(TestParameterInjector.class)
+public class FlagSetIntegrationTest extends BuildIntegrationTestCase {
+  @Before
+  public void setup() throws Exception {
+    writeProjectSclDefinition("test/project_proto.scl", /* alsoWriteBuildFile= */ true);
+  }
+
+  /**
+   * Given "//foo:myflag" and "default_value", creates the BUILD and .bzl files to realize a
+   * string_flag with that label and default value.
+   */
+  private void createStringFlag(String labelName, String defaultValue) throws Exception {
+    String flagDir = labelName.substring(2, labelName.indexOf(":"));
+    String flagName = labelName.substring(labelName.indexOf(":") + 1);
+    write(
+        flagDir + "/build_settings.bzl",
+"""
+string_flag = rule(implementation = lambda ctx: [], build_setting = config.string(flag = True))
+""");
+    write(
+        flagDir + "/BUILD",
+        """
+        load(":build_settings.bzl", "string_flag")
+        string_flag(
+            name = "%s",
+            build_setting_default = "%s",
+        )
+        """
+            .formatted(flagName, defaultValue));
+  }
+
+  /** Given ""//test:s", creates the BUILD and .bzl files for a trivial target with that label. */
+  private void createSimpleTarget(String labelName) throws Exception {
+    String targetDir = labelName.substring(2, labelName.indexOf(":"));
+    String targetName = labelName.substring(labelName.indexOf(":") + 1);
+    write(
+        targetDir + "/defs.bzl",
+"""
+simple_rule = rule(
+ implementation = lambda ctx: [],
+ attrs = {}
+ )
+""");
+    write(
+        targetDir + "/BUILD",
+"""
+load(":defs.bzl", "simple_rule")
+simple_rule(name = "%s")
+"""
+            .formatted(targetName));
+  }
+
+  @Test
+  public void noSclConfigSetAndNoDefaultConfig(@TestParameter boolean enforceProjectConfigs)
+      throws Exception {
+    createSimpleTarget("//test:s");
+    write(
+        "test/PROJECT.scl",
+"""
+load(
+  "//test:project_proto.scl",
+  "buildable_unit_pb2",
+  "project_pb2",
+)
+project = project_pb2.Project.create(
+    enforcement_policy = "warn",
+    buildable_units = [
+      buildable_unit_pb2.BuildableUnit.create(
+          name = "test_config",
+          flags = ["--//test:myflag=test_config_value"],
+          is_default = False,
+      )
+  ],
+)
+""");
+
+    addOptions("--enforce_project_configs=" + (enforceProjectConfigs ? "1" : "0"));
+    if (!enforceProjectConfigs) {
+      // There's no default project config but that doesn't matter when project enforcement is
+      // disabled: the entire PROJECT.scl is ignored.
+      assertThat(buildTarget("//test:s")).isNotNull();
+    } else {
+      // With project enforcement enabled, no default config means user must set --scl_config.
+      InvalidConfigurationException expectedError =
+          assertThrows(InvalidConfigurationException.class, () -> buildTarget("//test:s"));
+      assertThat(expectedError)
+          .hasMessageThat()
+          .contains("This project's builds must set --scl_config");
+    }
+  }
+
+  @Test
+  public void warnModeAddsBothUserAndProjectStarlarkFlags() throws Exception {
+    createStringFlag("//test1:project_flag", /* defaultValue= */ "default");
+    createStringFlag("//test2:user_flag", /* defaultValue= */ "default");
+    createSimpleTarget("//test:s");
+    write(
+        "test/PROJECT.scl",
+"""
+load(
+  "//test:project_proto.scl",
+  "buildable_unit_pb2",
+  "project_pb2",
+)
+project = project_pb2.Project.create(
+    enforcement_policy = "warn",
+    buildable_units = [
+      buildable_unit_pb2.BuildableUnit.create(
+          name = "test_config",
+          flags = ["--//test1:project_flag=set_by_project"],
+          is_default = True,
+      )
+  ],
+)
+""");
+
+    addOptions("--enforce_project_configs=1", "--//test2:user_flag=set_by_user");
+    var result = buildTarget("//test:s");
+
+    assertThat(result).isNotNull();
+    assertThat(result.getBuildConfiguration().getOptions().getStarlarkOptions())
+        .containsExactly(
+            Label.parseCanonicalUnchecked("//test1:project_flag"),
+            "set_by_project",
+            Label.parseCanonicalUnchecked("//test2:user_flag"),
+            "set_by_user");
+  }
+
+  @Test
+  public void warnMode_userFlagTakesPrecedenceOverProjectFlag() throws Exception {
+    createStringFlag("//test1:flag", /* defaultValue= */ "default");
+    createSimpleTarget("//test:s");
+    write(
+        "test/PROJECT.scl",
+"""
+load(
+  "//test:project_proto.scl",
+  "buildable_unit_pb2",
+  "project_pb2",
+)
+project = project_pb2.Project.create(
+    enforcement_policy = "warn",
+    buildable_units = [
+      buildable_unit_pb2.BuildableUnit.create(
+          name = "test_config",
+          flags = ["--//test1:flag=set_by_project"],
+          is_default = True,
+      )
+  ],
+)
+""");
+
+    addOptions("--enforce_project_configs=1", "--//test1:flag=set_by_user");
+    var result = buildTarget("//test:s");
+
+    assertThat(result).isNotNull();
+    assertThat(result.getBuildConfiguration().getOptions().getStarlarkOptions())
+        .containsExactly(Label.parseCanonicalUnchecked("//test1:flag"), "set_by_user");
+  }
+
+  @Test
+  public void warnMode_allowMultipleFlagShowsUserSettingsLast() throws Exception {
+    write(
+        "test1//build_settings.bzl",
+"""
+repeatable_string_flag = rule(
+    implementation = lambda ctx: [],
+    build_setting = config.string_list(flag = True, repeatable = True),
+)
+""");
+    write(
+        "test1/BUILD",
+        """
+        load(":build_settings.bzl", "repeatable_string_flag")
+        repeatable_string_flag(
+            name = "flag",
+            build_setting_default = [],
+        )
+        """);
+    createSimpleTarget("//test:s");
+    write(
+        "test/PROJECT.scl",
+"""
+load(
+  "//test:project_proto.scl",
+  "buildable_unit_pb2",
+  "project_pb2",
+)
+project = project_pb2.Project.create(
+    enforcement_policy = "warn",
+    buildable_units = [
+      buildable_unit_pb2.BuildableUnit.create(
+          name = "test_config",
+          flags = ["--//test1:flag=set_by_project"],
+          is_default = True,
+      )
+  ],
+)
+""");
+
+    addOptions("--enforce_project_configs=1", "--//test1:flag=set_by_user");
+    var result = buildTarget("//test:s");
+
+    assertThat(result).isNotNull();
+    assertThat(result.getBuildConfiguration().getOptions().getStarlarkOptions())
+        .containsExactly(
+            Label.parseCanonicalUnchecked("//test1:flag"),
+            ImmutableList.of("set_by_project", "set_by_user"));
+  }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/buildtool/util/BlazeRuntimeWrapper.java b/src/test/java/com/google/devtools/build/lib/buildtool/util/BlazeRuntimeWrapper.java
index 915484a..40c5309 100644
--- a/src/test/java/com/google/devtools/build/lib/buildtool/util/BlazeRuntimeWrapper.java
+++ b/src/test/java/com/google/devtools/build/lib/buildtool/util/BlazeRuntimeWrapper.java
@@ -364,6 +364,7 @@
     if (ignoreUserOptions) {
       optionserParserBuilder.ignoreUserOptions();
     }
+    optionserParserBuilder.skipStarlarkOptionPrefixes();
     return optionserParserBuilder.build();
   }