Fix load function to prevent errors when re-evaluated ElixirLS#138
Fix load function to prevent errors when re-evaluated ElixirLS#138FelonEkonom merged 2 commits intomembraneframework:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes Bundlex.Project.load/1 so it no longer crashes when re-evaluated in the same VM session (e.g., ElixirLS-triggered recompiles) by handling Code.require_file/1 returning nil on subsequent calls.
Changes:
- Add a fallback to
Code.compile_file/1whenCode.require_file/1returnsnil, preventingKeyword.keys(nil)crashes.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| modules = | ||
| (Code.require_file(bundlex_file_path) || Code.compile_file(bundlex_file_path)) | ||
| |> Keyword.keys() |
There was a problem hiding this comment.
Code.compile_file/1 will recompile and re-evaluate bundlex.exs every time load/1 is called after the file has been required once (since Code.require_file/1 will keep returning nil). That can add noticeable overhead and may emit redefining module ... warnings / re-run any side effects in bundlex.exs. Consider first attempting to recover the project module(s) from already-loaded modules whose module_info(:compile)[:source] matches bundlex_file_path, and only falling back to Code.compile_file/1 when none are currently loaded (e.g., after purge/reload).
| modules = | ||
| (Code.require_file(bundlex_file_path) || Code.compile_file(bundlex_file_path)) | ||
| |> Keyword.keys() |
There was a problem hiding this comment.
This change fixes a regression scenario (calling Bundlex.Project.get/1/load/1 more than once in the same VM), but it isn’t covered by tests. Please add an ExUnit test that loads a project once, clears Bundlex.Project.Store (or restarts the agent), and then loads again in the same test process to ensure it doesn’t crash when Code.require_file/1 returns nil.
Attempt to resolve a re-evaluation error in
Bundlex.Project.load/1where the function will fail if called more than once in the same session.Code.require_file/1returns{module, bytecode}tuples on the first call, but returnsnilon subsequent calls. When ElixirLS triggers a recompile in the same session,Code.require_file/1returnsnil.Not sure I am approaching fixing this problem appropriately, it feels quite hack-ish as-is. Please let me know what you think.