Skip to content

Commit 56b62f3

Browse files
committed
Fix nxf_date for uutils coreutils (Ubuntu 26.04+)
uutils coreutils ignores the %3N field-width modifier on `date` and strips leading zeros from %N, so `date +%s%3N` returns a variable-length string (11-19 digits) that no length branch in nxf_date matches. The function falls through to `exit 1`, which is swallowed by the `local var=$(...)` wrapper at the call site, leaving the caller with the literal error string. The next arithmetic expression then triggers "bash: line 178: Unexpected: unbound variable" under set -u and kills the task. Replace the length-dispatch implementation with a version that calls `date +%s` and `date +%N` separately, re-pads %N to 9 digits to recover any zeros uutils stripped, and computes milliseconds from the first three nanosecond digits. Falls back to second precision when %N is non-numeric (BSD/macOS), preserving existing behaviour on those platforms. Verified against both `ubuntu:latest` (uutils coreutils 0.8.0) and `ubuntu:24.04` (GNU coreutils 9.4): both produce 13-digit values with plausible non-negative wall-time deltas. Fixes #7114 Signed-off-by: Rob Syme <rob.syme@gmail.com>
1 parent f540396 commit 56b62f3

3 files changed

Lines changed: 26 additions & 21 deletions

File tree

modules/nextflow/src/main/resources/nextflow/executor/command-run.txt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ nxf_sleep() {
3030

3131
nxf_date() {
3232
## should return the current timestamp in milliseconds
33-
## note1: some linux silently ignores the `%3N` option and returns the ts in seconds (len==10)
34-
## note2: old date tool ignores the `%3N` option and append that string to the timestamp in seconds
35-
## note3: mac date tools ignores the `%3N` option and the string `3N` is appended to the timestamp in seconds
36-
local ts=$(date +%s%3N);
37-
if [[ ${#ts} == 10 ]]; then echo ${ts}000
38-
elif [[ $ts == *%3N ]]; then echo ${ts/\%3N/000}
39-
elif [[ $ts == *3N ]]; then echo ${ts/3N/000}
40-
elif [[ ${#ts} == 13 ]]; then echo $ts
41-
else echo "Unexpected timestamp value: $ts"; exit 1
33+
## handles GNU coreutils (9-digit %N), uutils coreutils (zero-stripped %N),
34+
## and BSD/macOS date (literal "N" from %N, falling back to second precision)
35+
local s n
36+
s=$(date +%s)
37+
n=$(date +%N)
38+
if [[ $n =~ ^[0-9]+$ ]]; then
39+
n=$(printf '%09d' "$((10#$n))")
40+
echo "$((s * 1000 + 10#${n:0:3}))"
41+
else
42+
echo "$((s * 1000))"
4243
fi
4344
}
4445

modules/nextflow/src/test/resources/nextflow/executor/test-bash-wrapper-with-trace.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,14 @@ nxf_sleep() {
207207
}
208208

209209
nxf_date() {
210-
local ts=$(date +%s%3N);
211-
if [[ ${#ts} == 10 ]]; then echo ${ts}000
212-
elif [[ $ts == *%3N ]]; then echo ${ts/\%3N/000}
213-
elif [[ $ts == *3N ]]; then echo ${ts/3N/000}
214-
elif [[ ${#ts} == 13 ]]; then echo $ts
215-
else echo "Unexpected timestamp value: $ts"; exit 1
210+
local s n
211+
s=$(date +%s)
212+
n=$(date +%N)
213+
if [[ $n =~ ^[0-9]+$ ]]; then
214+
n=$(printf '%09d' "$((10#$n))")
215+
echo "$((s * 1000 + 10#${n:0:3}))"
216+
else
217+
echo "$((s * 1000))"
216218
fi
217219
}
218220

modules/nextflow/src/test/resources/nextflow/executor/test-bash-wrapper.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ nxf_sleep() {
1515
}
1616

1717
nxf_date() {
18-
local ts=$(date +%s%3N);
19-
if [[ ${#ts} == 10 ]]; then echo ${ts}000
20-
elif [[ $ts == *%3N ]]; then echo ${ts/\%3N/000}
21-
elif [[ $ts == *3N ]]; then echo ${ts/3N/000}
22-
elif [[ ${#ts} == 13 ]]; then echo $ts
23-
else echo "Unexpected timestamp value: $ts"; exit 1
18+
local s n
19+
s=$(date +%s)
20+
n=$(date +%N)
21+
if [[ $n =~ ^[0-9]+$ ]]; then
22+
n=$(printf '%09d' "$((10#$n))")
23+
echo "$((s * 1000 + 10#${n:0:3}))"
24+
else
25+
echo "$((s * 1000))"
2426
fi
2527
}
2628

0 commit comments

Comments
 (0)