You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode中mix phx.server自动重载报错,终端运行正常求助

Phoenix项目VSCode调试时自动重载报错的解决方法

问题现象

终端执行mix phx.server时,修改模板文件(比如index.html.heex)触发的自动重载功能完全正常;但通过VSCode的launch.json配置启动项目后,触发自动重载时网页端出现编译错误,提示HelloWeb.__using__/1未定义或私有,但存在同名同元数的宏。


相关配置与输出

launch.json配置

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "mix_task",
            "name": "mix (Default task)",
            "request": "launch",
            "task": "phx.server",
            "projectDir": "${workspaceRoot}"
        },
        {
            "type": "mix_task",
            "name": "mix test",
            "request": "launch",
            "task": "test",
            "taskArgs": [
                "--trace"
            ],
            "startApps": true,
            "projectDir": "${workspaceRoot}",
            "requireFiles": [
                "test/**/test_helper.exs",
                "test/**/*_test.exs"
            ]
        }
    ]
}

VSCode调试控制台输出

Started ElixirLS debugger v0.11.0
Elixir version: "1.13.4 (compiled with Erlang/OTP 24)"
Erlang version: "24"
ElixirLS compiled with Elixir 1.11.4 and erlang 22
[info] Running HelloWeb.Endpoint with cowboy 2.9.0 at 127.0.0.1:4000 (http)
[info] Access HelloWeb.Endpoint at http://localhost:4000
[watch] build finished, watching for changes...
[debug] Live reload: lib/hello_web/templates/hello/index.html.heex

网页端错误信息

Compiling 1 file (.ex)
== Compilation error in file lib/hello_web/views/hello_view.ex ==
** (UndefinedFunctionError) function HelloWeb.__using__/1 is undefined or private. However there is a macro with the same name and arity. Be sure to require HelloWeb if you intend to invoke this macro
    (hello 0.1.0) HelloWeb.__using__(:view)
    lib/hello_web/views/hello_view.ex:2: (module)
    (elixir 1.13.4) lib/kernel/parallel_compiler.ex:346: anonymous fn/5 in Kernel.ParallelCompiler.spawn_workers/7

终端正常运行输出

macbook:hello yuchen$ mix phx.server
[info] Running HelloWeb.Endpoint with cowboy 2.9.0 at 127.0.0.1:4000 (http)
[info] Access HelloWeb.Endpoint at http://localhost:4000
[watch] build finished, watching for changes...
[info] GET /hello
[debug] Processing with HelloWeb.HelloController.index/2
  Parameters: %{}
  Pipelines: [:browser]
[info] Sent 200 in 81ms
[debug] Live reload: lib/hello_web/templates/hello/index.html.heex
Compiling 1 file (.ex)
[info] GET /hello
[debug] Processing with HelloWeb.HelloController.index/2
  Parameters: %{}
  Pipelines: [:browser]
[info] Sent 200 in 4ms

测试用的模板修改内容

修改lib/hello_web/templates/hello/index.html.heex(删除一个字符):

<section class="phx-hero">  <h2>Hello World, from Phoenix cheny!</h2></section>

解决方案

这个问题根源是VSCode的ElixirLS调试器在自动重载编译时,未正确加载宏依赖,可通过以下步骤解决:

  1. 明确指定调试环境
    在launch.json的phx.server配置项中添加env字段,强制使用开发环境(和终端运行环境保持一致):
{
    "type": "mix_task",
    "name": "mix (Default task)",
    "request": "launch",
    "task": "phx.server",
    "projectDir": "${workspaceRoot}",
    "env": {"MIX_ENV": "dev"}
}
  1. 确保视图文件宏引用正确
    检查lib/hello_web/views/hello_view.ex,如果问题依旧,可在文件顶部显式添加require HelloWeb:
defmodule HelloWeb.HelloView do
  require HelloWeb
  use HelloWeb, :view
end
  1. 重启调试环境
    修改配置后,重启VSCode的ElixirLS插件(或直接重启VSCode),重新启动调试会话后测试自动重载功能即可。

原理:终端运行mix phx.server默认使用dev环境,而VSCode调试器若未明确指定环境,会导致编译时宏加载逻辑异常,指定MIX_ENV=dev后,编译环境与终端一致,就能正确处理宏的引用。

内容的提问来源于stack exchange,提问作者Chen Yu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 14:48:16