OCaml:运行 `dune init exec` 后的沙丘构建错误

OCaml: dune build error after running `dune init exec`

我是 OCaml 的新手,我按照 tutorial to set up a developpement environment. I then tried the helloworld 示例使用 dune 和 OCaml,但遇到了以下错误:

$ dune init exe helloworld
Success: initialized executable component named helloworld
$ dune build
Error: I cannot find the root of the current workspace/project.
If you would like to create a new dune project, you can type:

    dune init project NAME

Otherwise, please make sure to run dune inside an existing project or
workspace. For more information about how dune identifies the root of the
current workspace/project, please refer to
https://dune.readthedocs.io/en/stable/usage.html#finding-the-root

目录结构:

.
├── _build
│   └── log
├── dune
└── helloworld.ml

沙丘:3.0.2
ocaml: 4.11.1

您可以通过在项目的根目录中添加一个 dune-project 文件来解决此问题,该文件包含以下行:

(lang dune 3.0)

但是,根据文档,dune init exec 不适用于创建项目:它仅为现有项目 中的可执行组件创建样板文件(参见 dune init --helpdune cli manual 了解详情)。

要初始化项目,您应该运行

dune init proj helloworld

这还将生成一个模板 dune-project 文件以及推荐的项目文件结构。


注意: 在 dune 3.0 之前 dune build 会为您创建文件,如果它不存在的话:

❯ dune init exe helloworld
Success: initialized executable component named helloworld
❯ ls
_build  dune  helloworld.ml
❯ dune build
Info: Creating file dune-project with this contents:
| (lang dune 2.9)
❯ ls
_build  dune  dune-project  helloworld.ml

但是,自 dune 3.0 起,dune-project 在项目中找不到时不会自动添加。