Skip to content

fix: the first compile works, and *.php scanners find PHP# classes - #160

Merged
heyJordanParker merged 6 commits into
masterfrom
fix/sharp-discovery
Oct 17, 2026
Merged

heyJordanParker merged 6 commits into
masterfrom
fix/sharp-discovery

Conversation

@heyJordanParker

Copy link
Copy Markdown
Owner

Two fixes found by validating PHP# on a real Laravel app. Design note: docs/architecture/slices/sharp-discovery.md.

The first compile works

On a fresh clone, every PHP process on the engine that loaded vendor/autoload.php died with Module.sharp isn't compiled. Run vendor/bin/mago compile.: vendor/bin/mago compile itself, a mago extension host, and the @php artisan package:discover a stock Laravel app runs from composer install. The plugin's autoload.php read the standard library's compiled module as it loaded, since 4567fd6.

  • Sharp\Internal\readModule now names the library's module, and the container reads it at its first module lookup, before any other module, as it already reads every app module. A request that resolves no service never needs it compiled. The name stays readModule, because sharp/bin/spec, which is guarded, writes it into every example's run script.

PHP code that lists *.php files finds PHP# classes

Spatie's route attributes, Laravel's console kernel and Laravel's event discovery list a folder's *.php files and name each class by its path, so they never saw a .sharp class.

  • mago compile writes a <Name>.php beside each of the project's .sharp files that declares a type. It loads the .sharp file through the engine, and on plain PHP it throws the "needs the PHP# engine" error. It is written before the compile, so every compiled file stays current.
  • The next compile rewrites a changed file and deletes the file of a removed source. A <Name>.php someone wrote beside a .sharp file is a compile error that names both.
  • A package in vendor/ and a file in an autoload-dev folder get none. PHPUnit loads a test class only from the file it lists (TestSuiteLoader compares getFileName()), so a test class keeps one file, and phpunit.xml lists suffix="Test.sharp", as on master.
  • In a git repository, each checkout keeps its files in its own block of the file git rev-parse --git-path info/exclude names, so they never show in git status.
  • Spec section 27 states the rules and the editor settings that hide the files. Decision 81 records the choice and the rejected options.

Tests

  • Zend/tests/sharp/library_autoload_uncompiled.phpt: autoload.php loads before anything is compiled, then the container builds a service through the library's Environment. It failed on master with the CompileError.
  • Zend/tests/sharp/discovery.phpt: a directory iterator and glob('*.php') find and load the PHP# class and read its attribute. It failed with the files switched off.
  • sharp/mago/tests/compile.rs, 8 rows: written, kept current, deleted with its source, refused beside a hand-written file, none in vendor/, none in autoload-dev, passes lint, every compiled file current (a class that shadows a default included), and the exclude block in a clone and in a worktree.
  • sharp/bin/test --phpunit passes: PHPUnit runs the PHP# test class in one process and with process isolation.

Not in this PR

  • Composer's class map, getClassMap(), never lists a PHP# class, so discovery that reads it instead of folders misses them.
  • A .php link to a .sharp file prints the PHP# source.
  • A package's PHP# classes get no .php file.
  • An extension host that runs the project's own PHP# code fails until a first compile without it.
  • Renaming readModule, with the next pull request that changes sharp/bin/spec.

…nding PHP# classes

Problem: on a fresh tree, vendor/autoload.php reads the standard
library's compiled Module.sharp as it loads, so every PHP process on the
engine dies before the first compile, vendor/bin/mago compile and an
extension host included. And PHP code that lists *.php files to find
classes never sees a .sharp class, because its compiled unit is binary
and lives in .sharp/.

Solution: the note records what master does, observed in neutral
fixtures, and the approved design. The container reads the library's
module before the first module or binding it reads, and readModule
becomes addModule. mago compile writes a <Name>.php beside each project
.sharp file that declares a type, which loads the .sharp file through
the engine.

docs/architecture/slices/
└── sharp-discovery.md      new: where compiled output lives, the
                            Solutions, the first-compile options and
                            their cost, the evidence
…rst compile works

Problem: since 4567fd6, the plugin's autoload.php read the standard
library's compiled Module.sharp as it loaded. On a fresh clone nothing
is compiled, so every PHP process on the engine that loads
vendor/autoload.php died with "Module.sharp isn't compiled. Run
vendor/bin/mago compile.": vendor/bin/mago compile itself, a mago
extension host, and a Laravel app's artisan in composer install.

Solution: Sharp\Internal\readModule names the library's module, and the
container reads it at its first module lookup, before any other module,
as it already reads every app module. A request that resolves no
service never needs it compiled, and a missing compile is refused where
PHP# code runs.

ext/sharp/
└── sharp.c                                 the container_library global; readModule
                                            names the file; sharp_container_read_folders
                                            and sharp_container_binding read it first
Zend/tests/sharp/
├── library_autoload.inc                    uncompiled: requires autoload.php before
│                                           mago compiles, then builds a service
│                                           through the library's Environment
└── library_autoload_uncompiled.phpt        new
docs/architecture/
└── modules.md                              the Engine list says when it is read
Problem: route-attribute discovery, Laravel's console kernel and its
event discovery list a folder's *.php files and name each class by its
path. A PHP# class has only its .sharp source and a binary unit in
.sharp/, so none of them found it, and a user had to patch each
library.

Solution: mago compile writes a <Name>.php beside each of the project's
.sharp files that declares a type, before it compiles, so every
compiled file stays current. The file loads the .sharp file through the
engine, and on plain PHP it throws the "needs the PHP# engine" error.
The next compile rewrites a changed file and deletes the file of a
removed source, and a hand-written <Name>.php beside a .sharp file is a
compile error. A package in vendor/ and an autoload-dev folder get
none, so a test class keeps one file for PHPUnit, which loads a class
only from the file it lists. In a git repository each checkout keeps
its files in its own block of the info/exclude file git rev-parse
--git-path names.

sharp/mago/
├── src/commands/compile.rs        writes, refreshes and deletes the
│                                  files, refuses one someone wrote
├── src/utils/git.rs               exclude: the checkout's block
└── tests/compile.rs               written, kept current, deleted with
                                   its source, refused beside a
                                   hand-written file, none in vendor/
                                   or autoload-dev, passes lint, every
                                   unit current, the block in a clone
                                   and a worktree
Zend/tests/sharp/
└── discovery.phpt                 new: a directory iterator and glob
                                   find and load the PHP# class
docs/
├── spec.md                        section 27: the rule, autoload-dev
│                                  and vendor/, phpunit.xml, editors
├── decisions/081-discovery.md     new
├── architecture/modules.md        phpunit.xml lists Test.sharp
└── architecture/slices/
    └── sharp-discovery.md         what was built, found not fixed
sharp/
└── README.md                      the Composer section names the files
Problem: a PHP file whose source was deleted stopped every scanner with
"Failed opening required". A post-autoload-dump callback in Composer's
own process found no binding of Sharp\Environment, because Composer's
loader includes the PHP file and the plugin never named the library's
module. A project whose analyzer loads only .sharp files kept every
orphan. Parallel spec runs filled the shared .git/info/exclude with 210
blocks and torn lines for folders git already ignores. The file failed
mago fmt --check. The refusal named no class and missed a file of
another case. Written files were 0600, which a PHP-FPM pool user
cannot read. composer run-script post-autoload-dump crashed on the
missing optimize flag. mago analyze knew no Sharp\Container in plain
PHP. No test ran an extension host on an uncompiled project.

Solution:
- The PHP file returns before its require_once once its source is
  gone, in mago fmt's layout.
- autoload.php names the library's module whenever the engine runs.
- Orphans are found on disk: the folders of the sources and of the
  last compile's units, by the file's first line.
- The exclude block lists only the files no .gitignore ignores, drops
  the block of a folder that is gone or ignored, and is replaced by
  rename. sharp/.gitignore ignores the hosts fixture's files.
- The refusal reads "app/Order.php and app/Order.sharp both name
  App.Order", finds the file whatever its case, and the summary prints
  at ERROR when the compile failed.
- write_by_rename gives a new file's mode.
- Plugin::dumpClassMap leaves the class map as it is without flags.
- The prelude declares Sharp\Container and Sharp\ContainerError.
- sharp/phpunit runs an extension host that loads vendor/autoload.php,
  and sharp/bin/test --phpunit compiles it from nothing compiled and
  runs composer run-script.

sharp/mago/
├── src/commands/compile.rs       guard, layout, orphans on disk,
│                                 refusal, summary level
├── src/utils/mod.rs              write_by_rename, moved, new mode
├── src/utils/git.rs              exclude: .gitignore, stale blocks,
│                                 rename
├── crates/prelude/assets/extensions/sharp.php   Container
├── crates/analyzer/tests/sharp.rs                plain PHP row
└── tests/compile.rs              fmt, orphans with sharp only, case,
                                  exact name, mode, ignored files,
                                  stale blocks
sharp/composer/
├── autoload.php                  readModule whenever the engine runs
└── src/Plugin.php                run-script
sharp/phpunit/
├── extension-host.php            new
├── mago.toml                     starts it
└── composer.json, composer.lock  revolt/event-loop
sharp/bin/test                    --phpunit from nothing compiled
sharp/.gitignore                  the hosts fixture's PHP files
Zend/tests/sharp/
├── discovery.phpt                a deleted source's file
└── library_autoload_composer_container.phpt   new
docs/
├── spec.md                       section 27: guard, case, exclude,
│                                 the full VS Code setting
├── decisions/029-typed-compilation.md   a deploy's compile
├── decisions/081-discovery.md    the same
├── architecture/modules.md       when the module is named
└── architecture/slices/sharp-discovery.md   the review, found 5, 6
Problem: the prelude now declares Sharp\Container and
Sharp\ContainerError, and the names crate's binder test checks its
generated list of the class-likes declared in Sharp against the
standard library and the prelude, so cargo test failed on every
platform.

Solution: regenerate the list with the regen-sharp-classes script, so
PHP# code names the two engine classes by default, as it names
Sharp\Position and Sharp\Listening.

sharp/mago/crates/names/src/
└── sharp_classes.rs    regenerated: Container, ContainerError
@heyJordanParker
heyJordanParker merged commit 8459f79 into master Oct 17, 2026
22 checks passed
@heyJordanParker
heyJordanParker deleted the fix/sharp-discovery branch November 3, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant