direnv/direnv · error

line %d: %w

Error message

line %d: %w

What it means

While parsing DIRENV_WATCHES, each line's first field must be an integer mtime parsed by strconv.Atoi. If it is not numeric (or overflows int), the parse error is wrapped with the offending line number via `line %d: %w`, preserving the underlying strconv error (e.g. `strconv.Atoi: parsing "abc": invalid syntax`).

Source

Thrown at internal/cmd/cmd_watch_list.go:59

		if err != nil {
			return err
		}
	}

	// Read `mtime path` lines from stdin
	reader := bufio.NewReader(os.Stdin)

	i := 1
	for {
		line, err := reader.ReadString('\n')
		if err == nil {
			elems := strings.SplitN(line, " ", 2)
			if len(elems) != 2 {
				return fmt.Errorf("line %d: expected to contain two elements", i)
			}
			mtime, err := strconv.Atoi(elems[0])
			if err != nil {
				return fmt.Errorf("line %d: %w", i, err)
			}
			path := elems[1][:len(elems[1])-1]

			// add to watches
			err = watches.NewTime(path, int64(mtime), true)
			if err != nil {
				return err
			}
		} else if errors.Is(err, io.EOF) {
			break
		} else {
			return fmt.Errorf("line %d: %w", i, err)
		}
		i++
	}

	e := make(ShellExport)
	e.Add(DIRENV_WATCHES, watches.Marshal())

View on GitHub (pinned to b00e451f54)

Solutions

  1. Clear and rebuild: `unset DIRENV_WATCHES`, then `direnv reload` and re-run the watch commands.
  2. Inspect the variable and fix the offending line so it is '<integer-mtime> <path>'.
  3. Re-create watches with the official commands (`direnv watch <file>`) rather than hand-crafting the value.
  4. Read the wrapped strconv message in the error to identify the exact bad token on the reported line.

Example fix

// before (wrong field order)
export DIRENV_WATCHES="/some/path 1600000000"
// after
export DIRENV_WATCHES="1600000000 /some/path"
Defensive patterns

Strategy: validation

Validate before calling

# check the first field of every DIRENV_WATCHES line is numeric
while IFS=' ' read -r mtime rest; do
  [ -n "$rest" ] || continue
  case "$mtime" in ''|*[!0-9]*) echo "bad mtime: $mtime" >&2; exit 1 ;; esac
done <<< "${DIRENV_WATCHES:-}"

Try / catch

out, err := exec.Command("direnv", "watch-list", shell).CombinedOutput()
if err != nil && strings.Contains(string(out), "invalid syntax") {
    os.Unsetenv("DIRENV_WATCHES")
    return fmt.Errorf("DIRENV_WATCHES had non-numeric mtime; cleared: %s", out)
}

Prevention

When it happens

Trigger: A DIRENV_WATCHES line whose first space-delimited field is not an integer — e.g. 'abc /some/path', a path placed before the mtime, or a float/negative-garbage mtime in a hand-edited or corrupted variable.

Common situations: Manually constructing the watch string with fields in the wrong order; scripts appending watches with unformatted timestamps; corruption from shell quoting stripping digits; copy-paste edits of the env var.

Related errors


AI-assisted analysis of direnv/direnv@b00e451f54 (2026-09-05). Data as JSON: /api/errors/9d318b0736c4ee82. Report an issue: GitHub.