Skip to content

Conversation

boomanaiden154
Copy link
Contributor

env -i is needed for some lit tests. The feature requires a minimal amount of work to support and there is no easy way to rewrite the tests that require it.

At least two tests that need this:

  1. clang/test/Driver/env.c
  2. lldb/test/Shell/Host/TestCustomShell.test

env -i is needed for some lit tests. The feature requires a minimal
amount of work to support and there is no easy way to rewrite the tests
that require it.

At least two tests that need this:
1. clang/test/Driver/env.c
2. lldb/test/Shell/Host/TestCustomShell.test
@llvmbot
Copy link
Member

llvmbot commented Sep 4, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-testing-tools

Author: Aiden Grossman (boomanaiden154)

Changes

env -i is needed for some lit tests. The feature requires a minimal amount of work to support and there is no easy way to rewrite the tests that require it.

At least two tests that need this:

  1. clang/test/Driver/env.c
  2. lldb/test/Shell/Host/TestCustomShell.test

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

3 Files Affected:

  • (modified) llvm/utils/lit/lit/TestRunner.py (+10-1)
  • (added) llvm/utils/lit/tests/Inputs/shtest-env-positive/env-i.txt (+23)
  • (modified) llvm/utils/lit/tests/shtest-env-positive.py (+10-4)
diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py
index 36c19c1c86c75..69ca80008e2f9 100644
--- a/llvm/utils/lit/lit/TestRunner.py
+++ b/llvm/utils/lit/lit/TestRunner.py
@@ -326,6 +326,10 @@ def updateEnv(env, args):
         if arg == "-u":
             unset_next_env_var = True
             continue
+        # Support for the -i flag which clears the environment
+        if arg == "-i":
+            env.env = {}
+            continue
         if unset_next_env_var:
             unset_next_env_var = False
             if arg in env.env:
@@ -890,7 +894,12 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
             if os.path.isfile(exe_in_cwd):
                 executable = exe_in_cwd
         if not executable:
-            executable = lit.util.which(args[0], cmd_shenv.env["PATH"])
+            # Use the path from cmd_shenv by default, but if the environment variable
+            # is unset (like if the user is using env -i), use the standard path.
+            path = (
+                cmd_shenv.env["PATH"] if "PATH" in cmd_shenv.env else shenv.env["PATH"]
+            )
+            executable = lit.util.which(args[0], shenv.env["PATH"])
         if not executable:
             raise InternalShellError(j, "%r: command not found" % args[0])
 
diff --git a/llvm/utils/lit/tests/Inputs/shtest-env-positive/env-i.txt b/llvm/utils/lit/tests/Inputs/shtest-env-positive/env-i.txt
new file mode 100644
index 0000000000000..2a66db1d48d05
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/shtest-env-positive/env-i.txt
@@ -0,0 +1,23 @@
+## Tests env command for clearing the environment
+
+## Check and make sure preset environment variable were set in lit.cfg.
+#
+# RUN: env | FileCheck --check-prefix=CHECK-ENV-PRESET %s
+## Check clearing the entire environment.
+#
+# RUN: env -i | FileCheck --check-prefix=CHECK-ENV-CLEAR-1 %s
+#
+## Check setting a variable in a clear environment.
+#
+# RUN: env -i BAZ=3 | FileCheck --check-prefix=CHECK-ENV-ONE-1 %s
+#
+
+# CHECK-ENV-PRESET: BAR = 2
+# CHECK-ENV-PRESET: FOO = 1
+
+# CHECK-ENV-CLEAR-NOT: BAR
+# CHECK-ENV-CLEAR-NOT: FOO
+
+# CHECK-ENV-ONE-NOT: BAR
+# CHECK-ENV-ONE: BAZ = 3
+# CHECK-ENV-ONE-NOT: FOO
diff --git a/llvm/utils/lit/tests/shtest-env-positive.py b/llvm/utils/lit/tests/shtest-env-positive.py
index 863fbda8c5b6d..4f07b69ecc7d3 100644
--- a/llvm/utils/lit/tests/shtest-env-positive.py
+++ b/llvm/utils/lit/tests/shtest-env-positive.py
@@ -1,13 +1,13 @@
 ## Test the env command (passing tests).
 
 # RUN: %{lit} -a -v %{inputs}/shtest-env-positive \
-# RUN: | FileCheck -match-full-lines %s
+# RUN:   | FileCheck -match-full-lines %s
 #
 # END.
 
 ## Test the env command's successful executions.
 
-# CHECK: -- Testing: 9 tests{{.*}}
+# CHECK: -- Testing: 10 tests{{.*}}
 
 # CHECK: PASS: shtest-env :: env-args-last-is-assign.txt ({{[^)]*}})
 # CHECK: env FOO=1
@@ -39,6 +39,12 @@
 # CHECK-NOT: # error:
 # CHECK: --
 
+# CHECK: PASS: shtest-env :: env-i.txt ({{[^)]*}})
+# CHECK: env -i | {{.*}}
+# CHECK: # executed command: env -i
+# CHECK-NOT: # error:
+# CHECK: --
+
 # CHECK: PASS: shtest-env :: env-no-subcommand.txt ({{[^)]*}})
 # CHECK: env | {{.*}}
 # CHECK: # executed command: env
@@ -65,6 +71,6 @@
 # CHECK-NOT: # error:
 # CHECK: --
 
-# CHECK: Total Discovered Tests: 9
-# CHECK: Passed: 9 {{\([0-9]*\.[0-9]*%\)}}
+# CHECK: Total Discovered Tests: 10
+# CHECK: Passed: 10 {{\([0-9]*\.[0-9]*%\)}}
 # CHECK-NOT: {{.}}

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Sep 4, 2025
@boomanaiden154 boomanaiden154 merged commit 57782ef into llvm:main Sep 4, 2025
11 checks passed
@boomanaiden154 boomanaiden154 deleted the env-i-lit-internal-shell branch September 4, 2025 19:27
@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 4, 2025

LLVM Buildbot has detected a new failure on builder llvm-clang-x86_64-sie-win running on sie-win-worker while building clang,llvm at step 4 "clean-build-dir".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/46/builds/22797

Here is the relevant piece of the build log for the reference
Step 4 (clean-build-dir) failure: Delete failed. (failure)
Step 7 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'Clang :: Driver/env.c' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 2
env -i LC_ALL=C LD_LIBRARY_PATH="$LD_LIBRARY_PATH" CLANG_NO_DEFAULT_CONFIG=1    z:\b\llvm-clang-x86_64-sie-win\build\bin\clang.exe Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Driver\env.c -### -o Z:\b\llvm-clang-x86_64-sie-win\build\tools\clang\test\Driver\Output\env.c.tmp.o --target=i386-unknown-linux      --sysroot=Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Driver/Inputs/basic_linux_tree      --rtlib=platform --unwindlib=platform -no-pie      2>&1 | z:\b\llvm-clang-x86_64-sie-win\build\bin\filecheck.exe --check-prefix=CHECK-LD-32 Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Driver\env.c
# executed command: env -i LC_ALL=C 'LD_LIBRARY_PATH=$LD_LIBRARY_PATH' CLANG_NO_DEFAULT_CONFIG=1 'z:\b\llvm-clang-x86_64-sie-win\build\bin\clang.exe' 'Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Driver\env.c' '-###' -o 'Z:\b\llvm-clang-x86_64-sie-win\build\tools\clang\test\Driver\Output\env.c.tmp.o' --target=i386-unknown-linux '--sysroot=Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Driver/Inputs/basic_linux_tree' --rtlib=platform --unwindlib=platform -no-pie
# note: command had no output on stdout or stderr
# error: command failed with exit status: 1
# executed command: 'z:\b\llvm-clang-x86_64-sie-win\build\bin\filecheck.exe' --check-prefix=CHECK-LD-32 'Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Driver\env.c'
# note: command had no output on stdout or stderr

--

********************


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category llvm-lit testing-tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants