[llvm] Modify lit config to allow specify extra env vars to pass through to lit invocation (PR #208239)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 8 08:20:30 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-testing-tools

Author: David Young (youngd007)

<details>
<summary>Changes</summary>

lit's pass-through environment allow-list is hard-coded in TestingConfig.py. There are cases where a  particular test environment needs an extra variable passed through (e.g. credentials or tooling paths required to fetch dependencies during a test), but today the only way to add one is to fork this list downstream.

This adds a --pass-env NAME option (repeatable, and also honored via LIT_OPTS) that copies the named variable from lit's environment into the test environment, in addition to the built-in allow-list. Since the caller chooses exactly which variables to forward, passing through a wrong or sensitive variable is user error rather than a default. The default is empty, so existing behavior — only the hard-coded list is passed through — is unchanged.

test creation help via Claude

---
Full diff: https://github.com/llvm/llvm-project/pull/208239.diff


8 Files Affected:

- (modified) llvm/docs/CommandGuide/lit.rst (+6) 
- (modified) llvm/utils/lit/lit/LitConfig.py (+4) 
- (modified) llvm/utils/lit/lit/TestingConfig.py (+4) 
- (modified) llvm/utils/lit/lit/cl_arguments.py (+10) 
- (modified) llvm/utils/lit/lit/main.py (+1) 
- (added) llvm/utils/lit/tests/Inputs/pass-env/lit.cfg (+7) 
- (added) llvm/utils/lit/tests/Inputs/pass-env/test.txt (+1) 
- (added) llvm/utils/lit/tests/pass-env.py (+26) 


``````````diff
diff --git a/llvm/docs/CommandGuide/lit.rst b/llvm/docs/CommandGuide/lit.rst
index 8b58011f8ec37..4519981dec653 100644
--- a/llvm/docs/CommandGuide/lit.rst
+++ b/llvm/docs/CommandGuide/lit.rst
@@ -166,6 +166,12 @@ EXECUTION OPTIONS
 
  Specify an additional ``PATH`` to use when searching for executables in tests.
 
+.. option:: --pass-env NAME
+
+ Pass the environment variable ``NAME`` through to the test environment, in
+ addition to the built-in allow-list of variables that are always passed
+ through. May be specified multiple times to pass through several variables.
+
 .. option:: --vg
 
  Run individual tests under valgrind (using the memcheck tool).  The
diff --git a/llvm/utils/lit/lit/LitConfig.py b/llvm/utils/lit/lit/LitConfig.py
index 331320cf8ebc8..1517ae1fc65ce 100644
--- a/llvm/utils/lit/lit/LitConfig.py
+++ b/llvm/utils/lit/lit/LitConfig.py
@@ -35,6 +35,7 @@ def __init__(
         order,
         params,
         config_prefix=None,
+        pass_env=[],
         maxIndividualTestTime=None,
         maxRetriesPerTest=None,
         parallelism_groups={},
@@ -55,6 +56,9 @@ def __init__(
         self.isWindows = bool(isWindows)
         self.order = order
         self.params = dict(params)
+        # Extra environment variables to pass through to the test environment,
+        # in addition to the built-in allow-list (see TestingConfig).
+        self.pass_env = list(pass_env)
         self.bashPath = None
 
         # Configuration files to look for when discovering test suites.
diff --git a/llvm/utils/lit/lit/TestingConfig.py b/llvm/utils/lit/lit/TestingConfig.py
index 1508b1d71caf2..ae3cf24462f03 100644
--- a/llvm/utils/lit/lit/TestingConfig.py
+++ b/llvm/utils/lit/lit/TestingConfig.py
@@ -69,6 +69,10 @@ def fromdefaults(litConfig):
             "HOME",
         ]
 
+        # Pass through any additional environment variables requested via the
+        # --pass-env command line option.
+        pass_vars += litConfig.pass_env
+
         if sys.platform.startswith("aix"):
             pass_vars += ["LIBPATH"]
         elif sys.platform == "win32":
diff --git a/llvm/utils/lit/lit/cl_arguments.py b/llvm/utils/lit/lit/cl_arguments.py
index 6225ac57abfd0..8dfdc6ddd4c81 100644
--- a/llvm/utils/lit/lit/cl_arguments.py
+++ b/llvm/utils/lit/lit/cl_arguments.py
@@ -276,6 +276,16 @@ def parse_args():
         default=[],
         type=os.path.abspath,
     )
+    execution_group.add_argument(
+        "--pass-env",
+        dest="pass_env",
+        metavar="NAME",
+        help="Pass the named environment variable through to the test"
+        " environment (in addition to the built-in allow-list). May be"
+        " specified multiple times.",
+        action="append",
+        default=[],
+    )
     execution_group.add_argument(
         "--vg", dest="useValgrind", help="Run tests under valgrind", action="store_true"
     )
diff --git a/llvm/utils/lit/lit/main.py b/llvm/utils/lit/lit/main.py
index a3bd153040a69..df344d8a1c446 100755
--- a/llvm/utils/lit/lit/main.py
+++ b/llvm/utils/lit/lit/main.py
@@ -40,6 +40,7 @@ def main(builtin_params={}):
         order=opts.order,
         params=params,
         config_prefix=opts.configPrefix,
+        pass_env=opts.pass_env,
         per_test_coverage=opts.per_test_coverage,
         gtest_sharding=opts.gtest_sharding,
         maxRetriesPerTest=opts.maxRetriesPerTest,
diff --git a/llvm/utils/lit/tests/Inputs/pass-env/lit.cfg b/llvm/utils/lit/tests/Inputs/pass-env/lit.cfg
new file mode 100644
index 0000000000000..efe5a2572dd68
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/pass-env/lit.cfg
@@ -0,0 +1,7 @@
+import lit.formats
+
+config.name = "pass-env"
+config.suffixes = [".txt"]
+config.test_format = lit.formats.ShTest()
+config.test_source_root = None
+config.test_exec_root = None
diff --git a/llvm/utils/lit/tests/Inputs/pass-env/test.txt b/llvm/utils/lit/tests/Inputs/pass-env/test.txt
new file mode 100644
index 0000000000000..dc5cdbad09afc
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/pass-env/test.txt
@@ -0,0 +1 @@
+# RUN: env
diff --git a/llvm/utils/lit/tests/pass-env.py b/llvm/utils/lit/tests/pass-env.py
new file mode 100644
index 0000000000000..e893dfd970faf
--- /dev/null
+++ b/llvm/utils/lit/tests/pass-env.py
@@ -0,0 +1,26 @@
+# Check that --pass-env passes the named environment variable through to the
+# test environment.
+#
+# RUN: env LIT_TEST_VAR=passed_value \
+# RUN:   %{lit} -a %{inputs}/pass-env --pass-env=LIT_TEST_VAR \
+# RUN:   | FileCheck -check-prefix=PASSED %s
+
+# Check that --pass-env can be repeated and also works via LIT_OPTS.
+#
+# RUN: env LIT_TEST_VAR=passed_value LIT_TEST_VAR2=passed_value2 LIT_OPTS=--pass-env=LIT_TEST_VAR2 \
+# RUN:   %{lit} -a %{inputs}/pass-env --pass-env=LIT_TEST_VAR \
+# RUN:   | FileCheck -check-prefix=BOTH %s
+
+# Check that without --pass-env, a non-allow-listed variable is not passed
+# through to the test environment.
+#
+# RUN: env LIT_TEST_VAR=passed_value \
+# RUN:   %{lit} -a %{inputs}/pass-env \
+# RUN:   | FileCheck -check-prefix=NOTPASSED %s
+
+# PASSED: LIT_TEST_VAR=passed_value
+
+# BOTH-DAG: LIT_TEST_VAR=passed_value
+# BOTH-DAG: LIT_TEST_VAR2=passed_value2
+
+# NOTPASSED-NOT: LIT_TEST_VAR=passed_value

``````````

</details>


https://github.com/llvm/llvm-project/pull/208239


More information about the llvm-commits mailing list