rust-lang/rust · error · InvalidCheck

Tried to use the previous path in the first command

Error message

Tried to use the previous path in the first command

What it means

Raised as InvalidCheck by CachedFiles.resolve_path() when a command uses '-' (meaning 'reuse the previously resolved path') but no previous path has been recorded yet (self.last_path is None). '-' is a shorthand for repeating the file argument of the preceding directive; it is illegal on the very first directive.

Source

Thrown at src/etc/htmldocck.py:241

        raise InvalidCheck(
            "Non-absolute XPath is not supported due to implementation issues"
        )


class CachedFiles(object):
    def __init__(self, root):
        self.root = root
        self.files = {}
        self.trees = {}
        self.last_path = None

    def resolve_path(self, path):
        if path != "-":
            path = os.path.normpath(path)
            self.last_path = path
            return path
        elif self.last_path is None:
            raise InvalidCheck("Tried to use the previous path in the first command")
        else:
            return self.last_path

    def get_absolute_path(self, path):
        if "*" in path:
            paths = list(Path(self.root).glob(path))
            if len(paths) != 1:
                raise FailedCheck("glob path does not resolve to one file")
            return str(paths[0])
        return os.path.join(self.root, path)

    def get_file(self, path):
        path = self.resolve_path(path)
        if path in self.files:
            return self.files[path]

        abspath = self.get_absolute_path(path)
        if not (os.path.exists(abspath) and os.path.isfile(abspath)):

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. Make the first directive reference an explicit file path instead of '-'.
  2. Move a directive that establishes a concrete path to the top, so '-' has something to refer back to.

Example fix

// before (first directive in the .rs file)
//@ has: - 'expected text'

// after
//@ has: path/to/output.html 'expected text'
//@ has: - 'more text'   // now legal, refers to output.html
Defensive patterns

Strategy: validation

Validate before calling

def first_directive_has_concrete_path(commands):
    if commands and commands[0].args and commands[0].args[0] == "-":
        return False
    return True

if not first_directive_has_concrete_path(commands):
    raise SystemExit("first htmldocck directive must not use '-' as its path")

Type guard

null

Try / catch

from srcetc_htmldocck import InvalidCheck
try:
    cache.resolve_path(path)
except InvalidCheck as e:
    if "previous path in the first command" in str(e):
        logging.error("add a concrete file path before using '-' in directives")
    raise

Prevention

When it happens

Trigger: The first //@ directive in a test template uses '-' as its path argument, e.g. `//@ has: - 'some text'` as the opening line. resolve_path('-') is called before any prior resolve_path set last_path.

Common situations: Reordering directives so a '-' one ends up first; copy-pasting a block that relied on an earlier file reference into a new test where it becomes the first directive; deleting the leading file-establishing directive.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/36f15bf9cc47008c. Report an issue: GitHub.