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

via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 17 08:05:16 PDT 2026


Author: David Young
Date: 2026-07-17T08:05:11-07:00
New Revision: f1b5b061832610e78d5be7313f0ab3e5e55d790f

URL: https://github.com/llvm/llvm-project/commit/f1b5b061832610e78d5be7313f0ab3e5e55d790f
DIFF: https://github.com/llvm/llvm-project/commit/f1b5b061832610e78d5be7313f0ab3e5e55d790f.diff

LOG: Modify lit config to allow specify extra env vars to pass through to lit invocation (#208239)

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

Added: 
    llvm/utils/lit/tests/Inputs/pass-env/lit.cfg
    llvm/utils/lit/tests/Inputs/pass-env/test.txt
    llvm/utils/lit/tests/pass-env.py

Modified: 
    llvm/docs/CommandGuide/lit.rst
    llvm/utils/lit/lit/LitConfig.py
    llvm/utils/lit/lit/TestingConfig.py
    llvm/utils/lit/lit/cl_arguments.py
    llvm/utils/lit/lit/main.py

Removed: 
    


################################################################################
diff  --git a/llvm/docs/CommandGuide/lit.rst b/llvm/docs/CommandGuide/lit.rst
index 26d0bd4b28bb1..b52a09eb088a1 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 c65dd253e7995..1bc466014f93d 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 956ec07aef216..4b3b45cf8fa2e 100644
--- a/llvm/utils/lit/lit/cl_arguments.py
+++ b/llvm/utils/lit/lit/cl_arguments.py
@@ -280,6 +280,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 e4844e261ddda..05f2992e566ec 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


        


More information about the llvm-commits mailing list