Skip to content

Do not throw when an ESM script has attribute data but no schema - #9204

Merged
mvaligursky merged 1 commit into
mainfrom
mv-script-attributes-no-schema
Aug 21, 2026
Merged

Do not throw when an ESM script has attribute data but no schema#9204
mvaligursky merged 1 commit into
mainfrom
mv-script-attributes-no-schema

Conversation

@mvaligursky

Copy link
Copy Markdown
Contributor

Description

ScriptComponent#initializeAttributes warns that a schema is required for the data to be instantiated, then falls straight through and dereferences it anyway:

const schema = this.system.app.scripts?.getSchema(name);
if (!schema) {
    Debug.warnOnce(`No schema exists for the script '${name}'. ...`);
}

assignAttributesToScript(this.system.app, schema.attributes, data, script);

So creating a Script class with attribute data but no registered schema throws TypeError: Cannot read properties of undefined (reading 'attributes') out of ScriptComponent#create, rather than just warning:

class MyScript extends Script {
    static scriptName = 'myScript';
}
entity.script.create(MyScript, { attributes: { speed: 42 } });   // throws

Return after the warning, which is what the message already says happens. Note the Debug call is stripped from release builds while the dereference is not, so the throw is there in both.

Found while writing tests for #9203, unrelated to the clone path.

Testing

One test in test/framework/components/script/component.test.mjs, fails on main: creating a Script with attribute data and no schema warns, does not throw, and leaves the script instance usable with no attributes assigned.

Checklist

  • I have read the contributing guidelines
  • My code follows the project's coding standards
  • This PR focuses on a single change

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Aug 20, 2026

Copy link
Copy Markdown

Build size report

This PR changes the size of the minified bundles.

Bundle Minified Gzip Brotli
playcanvas.min.js 2372.2 KB (+0.0 KB, +0.00%) 609.6 KB (+0.0 KB, +0.00%) 473.4 KB (−0.0 KB, −0.01%)
playcanvas.min.mjs 2369.6 KB (+0.0 KB, +0.00%) 608.5 KB (+0.0 KB, +0.00%) 472.7 KB (−0.1 KB, −0.03%)

@mvaligursky mvaligursky left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated PR review by Codex (GPT-5).

No actionable issues found.

The early return matches the existing warning contract and is required outside debug builds as well, where the Debug call is stripped but the schema dereference would otherwise remain. It leaves the script instance usable, avoids fabricating an attribute layout without a schema, and preserves the stored raw data should initialization be retried after a schema is supplied.

Local verification on the exact head: the full focused ScriptComponent suite passed (89 tests), ESLint passed for both changed files, and git diff --check passed. All current GitHub checks are green.

`initializeAttributes` warns that a schema is required for the data to be
instantiated, then falls straight through to `schema.attributes` and throws
`TypeError: Cannot read properties of undefined (reading 'attributes')`. So
creating a Script class with attribute data but no registered schema throws
out of `ScriptComponent#create` instead of just warning. Return after the
warning, which is what the message already says happens.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@mvaligursky
mvaligursky force-pushed the mv-script-attributes-no-schema branch from b230b0c to 27e7884 Compare August 21, 2026 10:13
@mvaligursky
mvaligursky merged commit 0594cf9 into main Aug 21, 2026
10 checks passed
@mvaligursky
mvaligursky deleted the mv-script-attributes-no-schema branch August 21, 2026 10:16
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