Skip to content

fix: repl: Always select a file when calling workspace.set() #1297

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

Merged
merged 2 commits into from
May 22, 2025

Conversation

cmcaine
Copy link
Contributor

@cmcaine cmcaine commented Apr 12, 2025

This might fix #1287, #868, etc.

Copy link

vercel bot commented Apr 12, 2025

@cmcaine is attempting to deploy a commit to the Svelte Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

vercel bot commented Apr 30, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
svelte-dev ✅ Ready (Inspect) Visit Preview May 22, 2025 3:15am

@cmcaine
Copy link
Contributor Author

cmcaine commented May 1, 2025

The way to test the bug is to go to the tutorial, try selecting some files and then click forward and back and see what happens.

Navigating between most of the tutorials now seems to work correctly, but going here, selecting Canvas.svelte and then clicking the next arrow shows me a JSON file for some reason.

@zqianem
Copy link
Contributor

zqianem commented May 18, 2025

going here, selecting Canvas.svelte and then clicking the next arrow shows me a JSON file for some reason.

The strange behavior here is avoided by #1352 (fixes the error when navigating), but this fix is still the correct one for #868 and #1164 (fixes the error when clicking "solve").

Comment on lines +428 to 433
const matching_file = selected && files.find((file) => is_file(file) && file.name === selected);
if (matching_file) {
this.#select(matching_file as File);
} else {
this.#select(first);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If we trust the types (selected can't be undefined because this.#current is never nullish), this can be further simplified to:

Suggested change
const matching_file = selected && files.find((file) => is_file(file) && file.name === selected);
if (matching_file) {
this.#select(matching_file as File);
} else {
this.#select(first);
}
const file = files.find((file) => is_file(file) && file.name === selected) as File;
this.#select(file ?? first);

and the ? in this method's default argument can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we trust that this.#current is never nullish? It is initialised like this: #current = $state.raw() as File (which is obviously a lie) and set() is called in the constructor before any obvious updates to this.#current.

I think maybe the type of this.#current should be updated to File | undefined.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes, you're right, I completely missed the initialization and was only looking at assignments to #current.

Copy link
Member

@eltigerchino eltigerchino left a comment

Choose a reason for hiding this comment

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

Thank you so much!

@eltigerchino eltigerchino merged commit c60f4c3 into sveltejs:main May 22, 2025
4 checks passed
@cmcaine
Copy link
Contributor Author

cmcaine commented May 22, 2025

Thanks for reviewing and accepting this PR!

cmcaine added a commit to cmcaine/svelte.dev that referenced this pull request May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants