Skip to content

Issue #867: Keep csh scripts running when a module load fails - #868

Closed
underwoo wants to merge 1 commit into
TACC:mainfrom
underwoo:IS867-csh-last-error
Closed

underwoo wants to merge 1 commit into
TACC:mainfrom
underwoo:IS867-csh-last-error

Conversation

@underwoo

Copy link
Copy Markdown

Fixes #867.

Problem

Since #820 (module last-error), a failed load emits __LMOD_LAST_ERROR holding the multi-line error message. Csh.expandVar() escapes blanks and special characters but not newlines, so the csh output is a setenv split across several lines. csh's eval `...` turns those newlines into word breaks, so setenv gets too many arguments:

setenv: Too many arguments.

In a csh/tcsh script that error is fatal, so the script stops at the failed module load. Before #820 only false was emitted, which just set $status. Bash quotes newlines correctly and was unaffected. Lmod 9.4.3, 9.4.4 and 9.4.5 are affected.

Fix

  • shells/Csh.lua: for __LMOD_LAST_ERROR, store newlines as \n and backslashes as \\, so it is a single valid setenv. Any other value that contains a newline has it collapsed to an escaped blank rather than breaking setenv.
  • src/cmdfuncs.lua: under csh, LastError() decodes those escapes, so module last-error still prints the full multi-line message.

csh cannot carry a newline through eval `...` at all (tried backtick printf, a literal \n, and a newline stored in a variable), so encoding and decoding is needed to keep the message. Bash output is unchanged.

Test

New rt/csh_error: a tcsh script loads a missing module and prints $status, runs last-error, then loads a real module and prints a completion line.

  • Without the fix the script stops at step 1 with setenv: Too many arguments., so the test fails.
  • With the fix it passes; last-error shows the full multi-line message.

The existing rt tests compare the text Lmod emits but don't check that the shell accepts it, and csh_swap never takes an error path, which is how this got through.

Local test run (macOS arm64, Lua 5.5.1, luaposix 37.0)

tm .: 158 passed, 11 diffs. The 11 (avail_termwidth, configDir, help, lars, load, modulerc, saverestore, sd_tacc, sitePkg, tacc, termWidthFallback) diff the same way on unmodified main, with byte-identical generated output, so they come from this machine's setup (TCL interpreter detection, the shasum path, no lua-term, PATH), not from this change.

Also verified by patching an installed 9.4.3: tcsh and csh scripts continue after a failed load with status=1, and module last-error prints the full message.

🤖 Generated with Claude Code

Since TACC#820 a failed load emits a multi-line __LMOD_LAST_ERROR.  For csh
the newlines were left unescaped, so csh's eval `...` split them into
extra words and setenv failed with "Too many arguments", which aborts a
csh script.  Bash was unaffected.

Csh.expandVar() now stores __LMOD_LAST_ERROR with newlines as "\n" (and
"\" as "\\"), and LastError() decodes them under csh so "module
last-error" still prints the full message.  Any other value containing
newlines has them collapsed to blanks rather than breaking setenv.

Add rt/csh_error: a tcsh script loads a missing module, runs
last-error, then loads a real module; it fails if the script aborts.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@rtmclay

rtmclay commented Oct 31, 2026

Copy link
Copy Markdown
Member

Thanks for the patch. You are correct that the issue was with __LMOD_LAST_ERROR and your solution works. However I have decided to use a base64 encoding of __LMOD_LAST_ERROR and decode it when printing the error out when module last_error is run. You can test the new version on branch IS867-lastErr

@rtmclay rtmclay closed this Oct 31, 2026

@baudilio baudilio left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix in PR #868 has been successfully tested. The issue causing anomalous termination of tcsh processes has been verified as resolved.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lmod handles errors differently in t/csh and in ba/sh shells.

3 participants