bazelbuild/bazel · error · Undefined

name '%s' is not defined

Error message

name '%s' is not defined

What it means

The resolver's undefined-name error: a free identifier matched no global binding, no predeclared binding, and no universal (Starlark builtin) name. Module.resolve collects all in-scope names as candidates so the resolver can suggest 'did you mean' alternatives. Thrown as Undefined, typically reported as a load/analysis-time error before execution.

Source

Thrown at src/main/java/net/starlark/java/eval/Module.java:294

    }

    // universal?
    if (Starlark.UNIVERSE.containsKey(name)) {
      return Resolver.Scope.UNIVERSAL;
    }
    if (resolveTypeSyntax && Starlark.UNIVERSE_EXTRA_TYPE_CONSTRUCTORS.containsKey(name)) {
      return Resolver.Scope.UNIVERSAL;
    }

    // undefined
    Set<String> candidates = new HashSet<>();
    candidates.addAll(globalIndex.keySet());
    candidates.addAll(predeclared.keySet());
    candidates.addAll(Starlark.UNIVERSE.keySet());
    if (resolveTypeSyntax) {
      candidates.addAll(Starlark.UNIVERSE_EXTRA_TYPE_CONSTRUCTORS.keySet());
    }
    throw new Undefined(String.format("name '%s' is not defined", name), candidates);
  }

  @Override
  @Nullable
  public TypeConstructor getTypeConstructor(String name) throws Undefined {
    Resolver.Scope scope = resolve(name, /* resolveTypeSyntax= */ true);
    Object value;
    switch (scope) {
      case GLOBAL -> value = getGlobal(name);
      case PREDECLARED -> value = getPredeclared(name);
      case UNIVERSAL -> {
        value = Starlark.UNIVERSE.get(name);
        if (value == null) {
          value = Starlark.UNIVERSE_EXTRA_TYPE_CONSTRUCTORS.get(name);
        }
      }
      default -> throw new AssertionError(String.format("Unexpected scope: %s", scope));
    }

View on GitHub (pinned to e6e199d060)

Solutions

  1. Check the exact spelling and case (Starlark builtins are lowercase: len, str, range; constants are None/True/False).
  2. Add the missing load("//pkg:file.bzl", "symbol") statement.
  3. Use the error's candidate list / 'did you mean' suggestion to find the intended name.
  4. Ensure the defining statement actually executes before the use (top-level defs and assignments run in order).

Example fix

# before
x = flase

# after
x = False
Defensive patterns

Strategy: validation

Validate before calling

# ensure the symbol exists before use when availability is uncertain
if not hasattr(module_ns, "new_api"):
    fail("new_api unavailable; update the providing .bzl and its load()")

Prevention

When it happens

Trigger: Typos (flase for False), referencing a name defined only inside an if/function that never ran, forgetting load() for a .bzl symbol, using a Python name that Starlark does not provide (e.g. self at module level, or filter/map as builtins depending on semantics).

Common situations: Renaming a symbol in one place but not its uses; deleting a rule/function still referenced elsewhere; missing load() statement after splitting a .bzl file; assuming Python builtins (like isinstance in old dialects, or dict) exist in the host application's predeclared environment.

Related errors


AI-assisted analysis of bazelbuild/bazel@e6e199d060 (2026-08-14). Data as JSON: /api/errors/c134613cc55cf03d. Report an issue: GitHub.