p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

[p5.js 2.0+ Bug Report]: p5.strands hook lifecycle mistakes should give friendlier errors

Open bhabishnu opened this issue 1 month ago • 6 comments

Most appropriate sub-area of p5.js?

  • [ ] Accessibility
  • [ ] Color
  • [ ] Core/Environment/Rendering
  • [ ] Data
  • [ ] DOM
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [ ] Math
  • [ ] Typography
  • [ ] Utilities
  • [ ] WebGL
  • [ ] WebGPU
  • [x] p5.strands
  • [ ] Build process
  • [ ] Unit testing
  • [ ] Internationalization
  • [x] Friendly errors
  • [ ] Other (specify if possible)

p5.js version

2.3.2 / current main (5c627cd70aa90fb5ca34059c58a19b4f1cda0d3)

Web browser and version

Chrome 152.0.7977.64

Operating system

Fedora Linux

Steps to reproduce this

Case 1: missing end()

function setup() {
  createCanvas(160, 160, WEBGL);

  const myShader = buildFilterShader(() => {
    filterColor.begin();
    filterColor.set([0, 1, 0, 1]);
    // filterColor.end() is missing
  });

  filter(myShader);
}

The filter doesn't seem to apply, but there's no error explaining that the hook block wasn't closed.

Case 2: calling set() after end()

function setup() {
  createCanvas(160, 160, WEBGL);

  const myShader = buildFilterShader(() => {
    filterColor.begin();
    filterColor.end();
    filterColor.set([0, 1, 0, 1]);
  });

  filter(myShader);
}

This reaches a raw shader compilation error:

'HOOK_getColor' : Function does not return a value

For comparison, the normal lifecycle works as expected:

filterColor.begin();
filterColor.set([0, 1, 0, 1]);
filterColor.end();

It would be helpful if the first two cases were caught earlier with a friendly scope/lifecycle error instead of silently failing or reaching the shader compiler.

Reproduced on current main at 5c627cd70aa90fb5ca34059c58a19b4f1cda0d3.

bhabishnu avatar Oct 02 '26 02:10 bhabishnu

Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, please make sure to fill out the inputs in the issue forms.

For guidance on contributing, check out our contributing guidelines and other resources for contributors. 💬 If you have questions or need support, feel free to join the Processing Foundation Forum or visit our Community page. 📜 Please also review our Code of Conduct to understand our community standards. Thank You!

welcome[bot] avatar Oct 02 '26 02:10 welcome[bot]

@bhabishnu @ksen0 can i take this one?

yashgoyal0110 avatar Oct 02 '26 12:10 yashgoyal0110

hey! I’ve already been looking into this one and was planning to take it forward. just waiting on maintainer guidance before starting the fix

bhabishnu avatar Oct 02 '26 12:10 bhabishnu

thanks for the clear repros @bhabishnu! I can confirm both reproduce on current main

hook.set() doesn't check hook._active like the property getters/setters right below it do, so set() after end() (or before begin()) is silently accepted

Then finishHook() doesn't complain when nothing was set for a hook that needs a return value, which is where the Function does not return a value error comes from (even just filterColor.begin(); filterColor.end(); with no set() hits it)

and after the callback runs in modify(), nothing checks if strandsContext.activeHook is still set, so a missing end() goes unnoticed and the hook never gets registered

all three could reuse FES.userError('scope error', ...) like the existing property checks, so it should be a pretty small change. It would be good to add tests for each case too

aashu2006 avatar Oct 02 '26 13:10 aashu2006

@bhabishnu assigning this to you because you are already into it. @yashgoyal0110 thanks for offering, a review on the PR once this is up would be really helpful, thanks!

aashu2006 avatar Oct 02 '26 13:10 aashu2006

thanks! I’ll cover all three cases and add regression tests for each

bhabishnu avatar Oct 02 '26 16:10 bhabishnu