DUB "Error: only one `main` allowed."

Steven Schveighoffer schveiguy at gmail.com
Wed Aug 11 11:44:42 UTC 2021


On 8/11/21 5:31 AM, tastyminerals wrote:
> I would like to trigger tests in a simple dub project.
> 
> ```
> source/my_script.d
> dub.json
> ```
> 
> Here is a dub config:
> ```json
> {
>      "targetPath": "build",
>      "targetType": "executable",
>      "sourcePaths": ["source"],
>      "name": "my_script",
>      "buildTypes": {
>          "release": {
>              "buildOptions": [
>                  "releaseMode",
>                  "inline",
>                  "optimize"
>              ]
>          },
>          "tests": {
>              "buildOptions": [
>                  "unittests"
>              ]
>          }
>      }
> }
> 
> ```
> The project builds but when I attempt to run `dub test`, I get
> 
> ```
> .dub/code/my_script-test-application-unittest-posix.osx.darwin-aarch64.arm_hardfloat-ldc_v1.26.0-8A5B544D5AC6B47B68DE875ACB4BA60E_dub_test_root.d(9,12): 
> Error: only one `main` allowed. Previously found `main` at 
> source/my_script.d(131,6)
> ```
> 
> How can one run tests with dub?
> 

`dub -b unittest` should work (you don't need the extra build type stuff)

dub test does something funky -- it removes the *whole module* where 
your main function is (if you identify it, or if it's `app.d`) and then 
builds its own main module. Why does it do this? Legacy reasons, the 
runtime used to run main after running unittests, which dub didn't want 
to do. It also is useful on a library where there is no main function.

However, dub with a build type of `unittest` just enables the unittest 
switch, and builds all your stuff as normal.

-Steve


More information about the Digitalmars-d-learn mailing list