Skip to content

Execution Order of Tags Not Guaranteed if Using src #1804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JeffersGlass opened this issue Oct 18, 2023 · 6 comments · Fixed by pyscript/polyscript#64
Closed

Execution Order of Tags Not Guaranteed if Using src #1804

JeffersGlass opened this issue Oct 18, 2023 · 6 comments · Fixed by pyscript/polyscript#64
Labels
type: bug Something isn't working

Comments

@JeffersGlass
Copy link
Contributor

Try this in the current build (1):

# foo.py
x = 1
<!-- index.html -->
<script type="py" src="foo.py">
</script>
<script type="py">
    print(x)
</script>

This breaks, with NameError: name 'x' is not defined.

I would have expected the scripts to run in DOM order per-type, regardless of whether they used src or not,

Things that do Work

(2) Using inline before src (WORKS)

# bar.py
print(x)
<script type="py">
    x = 1
</script>
<script type="py" src="bar.py">
</script>

(3) Neither uses src (WORKS)

<script type="py">
    x = 1
</script>
<script type="py">
    print(x)
</script>

(4) Both use src (WORKS)

# one.py
x = 1
# two.py
print(x)
<script type="py" src="one.py"></script>
<script type="py" src="two.py"></script>

Using MicroPython (5)

All four of the samples above (the broken one and the ones which work) show the same behavior using script type='mpy' instead of py.

PolyScript (6)

Doing the following in pure Polyscript works fine, so I think it's an issue in PyScript specifically?

# foo.py
x = 1
<script type="pyodide" src="foo.py"></script>
<script type="pyodide">
    print(x)
</script>
@JeffersGlass JeffersGlass added the type: bug Something isn't working label Oct 18, 2023
@WebReflection
Copy link
Contributor

it's probably a matter of queue when it comes to custom types ... polyscript handles non-custom types slightly differently so that this might still be a polyscript issue after all, as PyScript just uses its registry to define the type.

Multiple scripts of same type are still a not super common use case though, so let's see if we want to prioritize this before the coming RC.

/cc @fpliger @ntoll

@JeffersGlass
Copy link
Contributor Author

Investigating a little more, I think this is PyScript specific. It seems the inline tags are always executed before any src tags, but that's not the case in Polyscript.


In PyScript, I can mix up the following tags in any order:

<script type="py" src="one.py"></script>         <!-- print('src one') -->
<script type="py">print('Inline one')</script>
<script type="py" src="two.py"></script>         <!-- print('src two') -->
<script type="py">print('Inline two')</script>
<script type="py" src="three.py"></script>       <!-- print('src three') -->
<script type="py">print('Inline three')</script>

And I always get:

Inline one
Inline two
Inline three
src one
src two
src three

But in PolyScript, they are executed in overall page order, regardless of whether they're inline or use src:

<script type="pyodide" src="one.py"></script>        <!-- print('src one') -->
<script type="pyodide">print('inline one')</script>
<script type="pyodide" src="two.py"></script>        <!-- print('src two') -->
<script type="pyodide">print('inline two')</script>
<script type="pyodide" src="three.py"></script>      <!-- print('src three') -->
<script type="pyodide">print('inline three')</script>

prints:

src one
inline one
src two
inline two
src two
inline three

@WebReflection
Copy link
Contributor

I can solve this in PyScript but it's a polyscript custom type issue ... polyscript queue per interpreter all executing things:
https://github.com/pyscript/polyscript/blob/main/esm/script-handler.js#L160-L162

there's no such mechanism for custom types (IIRC) and I think if non custom type works like that, so should custom types too.

I'll try to fix this today, thanks for the investigation though!

@WebReflection
Copy link
Contributor

@JeffersGlass this has been fixed and tested in polyscript so it will get in next time we update PyScript too ... which arguably could be today as well but ... you know, don't release on Friday and stuff 😄

@WebReflection
Copy link
Contributor

plot twist ... it might be today: #1805

@WebReflection
Copy link
Contributor

it's in and published to npm ... you should be able to grab either latest from npm or from our CDN and verify your expectations now are met.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants