Skip to content

Blocks without asset.php file cannot be registered #40447

@kmonahan

Description

@kmonahan

Description

Blocks without an .asset.php file, such as the Hello, World block from the Building a Basic Block tutorial, result in a PHP notice during the registration process.

<?php
$script_asset_path = wp_normalize_path(
  realpath(
    dirname( $metadata['file'] ) . '/' .
    substr_replace( $script_path, '.asset.php', - strlen( '.js' ) )
  )
);

results in $script_asset_path being set to an empty string when no .asset.php file exists, triggering the _doing_it_wrong error.

If this is the intended behavior, the tutorial steps should probably be updated to include a step to manually create that file when the build tools aren't being used.

Step-by-step reproduction instructions

  1. Follow the instructions at https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/writing-your-first-block-type/ to create a Hello World block, using the Plain JS options with no build process.
  2. Observe that the block is not registered. Instead the error "Notice: register_block_script_handle was called incorrectly. The asset file for the "editorScript" defined in "gutenberg-examples/example-01-basic" block definition is missing." is displayed.
  3. Add an empty block.asset.php file to the plugin directory.
  4. Observe that the error goes away and the block can then be added in the editor.

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions