@@ -377,15 +377,14 @@ local_runtime_repo(
377377local_runtime_toolchains_repo(
378378 name = " local_toolchains" ,
379379 runtimes = [" local_python3" ],
380+ # TIP: The `target_settings` arg can be used to activate them based on
381+ # command line flags; see docs below.
380382)
381383
382384# Step 3: Register the toolchains
383385register_toolchains(" @local_toolchains//:all" , dev_dependency = True )
384386```
385387
386- Note that ` register_toolchains ` will insert the local toolchain earlier in the
387- toolchain ordering, so it will take precedence over other registered toolchains.
388-
389388:::{important}
390389Be sure to set ` dev_dependency = True ` . Using a local toolchain only makes sense
391390for the root module.
@@ -397,6 +396,49 @@ downstream modules.
397396
398397Multiple runtimes and/or toolchains can be defined, which allows for multiple
399398Python versions and/or platforms to be configured in a single ` MODULE.bazel ` .
399+ Note that ` register_toolchains ` will insert the local toolchain earlier in the
400+ toolchain ordering, so it will take precedence over other registered toolchains.
401+
402+ This behavior can be mitigated by specifying the
403+ {obj}` local_runtime_toolchains_repo.target_settings ` arg. This allows attaching
404+ arbitrary {obj}` config_setting() ` conditions for activating the toolchain.
405+
406+ ```
407+ # File: MODULE.bazel
408+ bazel_dep(name = "bazel_skylib", version = "1.7.1")
409+
410+ local_runtime_toolchains_repo(
411+ name = "local_toolchains",
412+ runtimes = ["local_python3"],
413+ target_settings = {
414+ "local_python3": ["@//:is_py_local_set"]
415+ }
416+ )
417+
418+ # Step 2: Create toolchains for the runtimes
419+ local_runtime_toolchains_repo(
420+ name = "local_toolchains",
421+ runtimes = ["local_python3"],
422+ # TIP: The `target_settings` arg can be used to activate them based on
423+ # command line flags; see docs below.
424+ )
425+
426+ # File: BUILD.bazel
427+ load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
428+
429+ config_setting(
430+ name = "is_py_local_set",
431+ flag_values = {":py": "local"},
432+ )
433+
434+ string_flag(
435+ name = "py",
436+ build_setting_default = "",
437+ )
438+
439+ ```
440+
441+
400442
401443## Runtime environment toolchain
402444
0 commit comments