Skip to content

Manifest-aware analysis: find unknown functions, classes and constants before the front end finishes #489

Closed

Description

@blacktrs

Problem

Compiling a large Composer application (Laravel 13: 4,652 files, 24k functions) takes ~15 min in the front end before the first diagnostic about a missing symbol. Today the missing symbols surface in three late, uneven ways:

  • an undefined function shows up only as a trap list at the very end of the build (MANTICORE_DUMP_TRAPS);
  • an undefined class (Class "Illuminate\Support\Arr" not found) and an undefined constant (Undefined constant "LOG_USER") show up only at run time, after a ~23 min build;
  • manticore analyze <dirs> is fast but does not read the manifest or the Composer autoload map, so every vendor symbol is reported unknown (221 false errors on Laravel's config/, routes/, app/).

Each missing symbol therefore costs a full build cycle to discover.

Idea

An analysis mode that reads the manifest (src, entry, composer) and resolves the same file list as build, then parses only (parsing 4,652 files takes ~5 s) and checks name resolution without lowering anything:

  • build a declaration table: classes, interfaces, traits, enums, functions, const and define() constants from the project and vendor sources, plus the prelude and the stdlib .sig;
  • walk the AST and report every unresolved name: function calls, new, static access, instanceof, type hints, extends/implements/use, bare constants;
  • print a de-duplicated "name → first use site" list; non-zero exit when anything is unresolved.

Likely home: manticore analyze --manifest (the analyzer in src/Analyze/ already supports --only undefined.), reusing the build's file resolution so both agree.

Out of scope / known limits

  • dynamic names ($class::create(), call_user_func('f'), constant($n));
  • conditional declarations (if (!function_exists('x')) { function x() {} }) — treat as declared;
  • type-level failures found by lowering (wrong representations, arity errors in generated dispatchers) — those still need a build or a mini-probe.

Acceptance

  • On examples/laravel-app the check finishes in well under a minute and lists LOG_USER-class gaps (constants, functions, classes) before any build.
  • No false positives for symbols the build itself resolves.
  • Documented in docs/ next to the analyzer.

Related

A second, smaller helper would complement it: a script that builds a throw-away project from a handful of real vendor files so a crash can be reproduced in seconds instead of a full-app build (this found three Laravel crashes today).

Activity

  1. added
    enhancement
    on Oct 26, 2026
  2. blacktrs commented on Nov 4, 2026

    @blacktrs
    ContributorAuthor

    A second data point from compiling symfony/demo (Symfony + Doctrine + Twig, ~4,500 classes), and a cheaper first step than the full analyzer.

    Constants are the slowest gap to find

    One unresolved constant cost a full build cycle each time, and they come one after another:

    Constant Where it was read Found by
    LOG_PID (also LOG_USER, LOG_*) default parameter of Monolog's SyslogHandler::__construct run time, after a ~18 min build
    CURLE_COULDNT_RESOLVE_HOST and the other CURLE_* a private static array default in Monolog's Handler\Curl\Util run time
    a global const FOO named in a static default any class run time (separate lowering-order bug, fixed)

    The build already prints undefined-function traps (161): … at the end, so the function half of this issue is partly solved for the final report. Constants and classes get nothing: they are lowered to throwErrorExpr('Undefined constant "X"') (LowerFromAst::lowerIdentifier) and the first sign is the process dying at boot.

    Cheap addition, before the manifest-aware analyzer

    lowerIdentifier already knows when a bare constant does not resolve (the branch that returns throwErrorExpr). Record each name and its first use site there and print it next to the function traps:

    build: undefined-constant traps (3): LOG_PID (vendor/monolog/.../SyslogHandler.php:39), …
    

    The same for new X / static access / instanceof on a class that is in no table. That needs no new pass, costs nothing on a clean build, and turns "boots, then dies" into a list at the end of the build that is already running. It would have shown LOG_PID, the LOG_* family and the CURLE_* set in the first build instead of three runs.

    The full analyze --manifest mode in this issue is still the right end state, because it removes the 15–20 min wait entirely; this is the step that makes the wait pay off once.

    Related finding for the acceptance list

    A constant can be defined and still unresolved: the curl prelude declares every CURLE_*, but class-level lowering ran before the define() / const tables were filled, so the name did not resolve. The analyzer should resolve names the way the build does, in the build's order, or it will report false negatives here.

  3. blacktrs commented on Nov 4, 2026

    @blacktrs
    ContributorAuthor

    Extension of the idea above: report not only what is unknown but what is actually reachable.

    Why

    Today's trap list is a flat set of every unresolved name in the whole program. On symfony/demo it has 161 functions (apcu_*, sqlsrv_*, pg_*, db2_*, sodium_*, Amp\Dns\resolve, …). Almost all of them sit behind a feature check or in an adapter the application never constructs. The two or three that matter (here: the missing LOG_* constants, which sit in a default parameter evaluated at boot) are indistinguishable from the other 160, so someone has to triage the list by hand, and the dangerous entries look like noise.

    Proposal

    Rank each unresolved name by reachability from the program's entry points, in three tiers:

    1. Reachable on the boot/request path: reached from the entry file, a static initialiser, a default parameter, a constructor of a class the application instantiates, or a registered handler. These fail for real.
    2. Reachable only through a guard or a dynamic dispatch: function_exists() / extension_loaded() / class_exists() guards, $class::, new $name, method-name dispatch. Possible but not certain; print with the guard.
    3. Unreachable: only mentioned in code no entry point can reach (an adapter for a driver nobody configures). Collapsed into a count.

    How it could stay cheap

    • The build already has the pieces: a call graph (InferTypes::callGraph), dead-function stripping, and closed-world class/method tables. Reachability can be an over-approximation, since the aim is to remove noise and not to prove absence.
    • Dynamic calls resolve conservatively by method name over the class table (what the dispatch helpers already do), so tier 2 is large but honest.
    • Constants and default-parameter expressions are evaluated at their declaring site, so for them "reachable" means "the declaring function / class is reachable", which is the same graph.
    • Explicit limit: no value tracking. A guard is reported, not evaluated, unless foldGuard already folds it.

    Output

    unresolved, reachable (2):
      constant LOG_PID   SyslogHandler::__construct default   <- Kernel::boot -> MonologBundle -> …
      function apcu_fetch  ApcuAdapter::doFetch                <- guarded by extension_loaded('apcu')   [tier 2]
    unresolved, unreachable: 158 (--all to list)
    

    The chain after <- is the shortest path from an entry point, which also answers "why is this class in my binary" and feeds the IR-size work (the same graph decides what dead-stripping may drop before the front end instead of after).

    This is clearly the harder analysis of the two, so it should come after the plain unknown-symbol list, but the output format should leave room for the tier now.

  4. blacktrs commented on Nov 4, 2026

    @blacktrs
    ContributorAuthor

    One more category the same pre-build analysis could catch: files a bundle loads at boot with a run-time computed path.

    The failure

    Symfony bundles register their services with PhpFileLoader:

    $loader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
    $loader->load('dbal.php');

    PhpFileLoader::load() ends in include $path; where $path comes from FileLocator::locate(). A compiled binary can only run files that are part of the build, and a path computed at run time has no constant prefix for the compiler to follow. The file is silently not there: nothing fails at build time, nothing fails at the load() call, and the first symptom is a service missing much later:

    Missing service seen at boot The file that defines it
    doctrine.dbal.connection_factory.dsn_parser doctrine-bundle/config/dbal.php
    stimulus.asset_mapper.controllers_map_generator stimulus-bundle/config/services.php

    Each one cost a full build (15–18 min) to discover, and there were two so far on a bundle list of eleven.

    Today's workaround is to list every such directory by hand in the manifest's src array (Laravel does this for vendor/laravel/framework/config). Which directories are needed is not discoverable from the project.

    What the analysis can do

    The directory is almost always a literal in the bundle's own code: new FileLocator(<constant expression>) next to a new PhpFileLoader(...) / YamlFileLoader / XmlFileLoader, with the file names as literal ->load('x.php') arguments. That is checkable without evaluating anything:

    • find new FileLocator(<expr>) where <expr> folds to a constant (__DIR__ . '/../../config') and the locator feeds a PhpFileLoader;
    • resolve the directory and check that its *.php files are in the build set (manifest src, or a composer autoload root that includes them);
    • report the ones that are not, with the exact manifest line to add:
    unresolved at run time: PhpFileLoader will include files that are not in the build
      vendor/doctrine/doctrine-bundle/config      <- DoctrineExtension.php:501  (load 'dbal.php', 'orm.php')
      vendor/symfony/stimulus-bundle/config       <- StimulusExtension.php:38   (load 'services.php')
      add to "src": ["vendor/doctrine/doctrine-bundle/config", "vendor/symfony/stimulus-bundle/config"]
    

    It combines with the reachability tiers: a bundle is only relevant when the kernel registers it (registerBundles()), so dev-only bundles (web-profiler, debug, doctrine-fixtures here) land in the "unreachable" count instead of the list.

    Longer term

    Better than reporting it is not needing the manual line: when composer: true is set, a FileLocator literal under vendor/ that feeds a loader could add that directory to the build set by itself, the way a constant-prefix require already does. The report is the safe first step; the auto-include is the follow-up.

  5. blacktrs commented on Nov 14, 2026

    @blacktrs
    ContributorAuthor

    Done by #634 (build --check / compile --check). It runs the whole front end, collects every error, and stops before type inference. Reachable undeclared names are reported as errors with their call path, and dead ones as warnings.

    Checked on main 40e0e5e with a three-line program:

    src/main.php:3: error[undefined.constant]: constant LOG_USERX is not declared (reachable: main <- __main)
    src/main.php:4: error[undefined.function]: function nope_fn() is not declared (reachable: main <- __main)
    src/main.php:5: error[undefined.class]: class Missing\Klass is not declared (reachable: main <- __main)
    front end: 3 error(s), 0 warning(s)
    

    The acceptance criteria are met:

    A parse-only analyze --manifest would be faster, but nothing needs it now.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions