www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - DUB "Error: only one `main` allowed."

reply tastyminerals <tastyminerals gmail.com> writes:
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_du
_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?
Aug 11 2021
next sibling parent tastyminerals <tastyminerals gmail.com> writes:
On Wednesday, 11 August 2021 at 09:31:46 UTC, 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_du
_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?
On a side note, can somebody advise a less buggy build tool for D? Does meson work any better?
Aug 11 2021
prev sibling next sibling parent reply tastyminerals <tastyminerals gmail.com> writes:
On Wednesday, 11 August 2021 at 09:31:46 UTC, 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_du
_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?
Hahaha, I fixed it by renaming the `my_script.d` to `app.d`. Oh boy.
Aug 11 2021
parent Mike Parker <aldacron gmail.com> writes:
On Wednesday, 11 August 2021 at 09:38:13 UTC, tastyminerals wrote:

 Hahaha, I fixed it by renaming the `my_script.d` to `app.d`. Oh 
 boy.
What you want is the `mainSourceFile` entry. From the dub documentation, [under "Build Settings"][1]:
 Determines the file that contains the main() function. This 
 setting can be used by dub to exclude this file in situations 
 where a different main function is defined (e.g. for "dub 
 test") - this setting does not support platform suffixes
`app.d` has special significance to dub in that it automatically triggers the executable target type if it's present. I assume dub also considers it the main source file by default if you have one. [1]: https://dub.pm/package-format-json.html#build-settings
Aug 11 2021
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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_du
_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
Aug 11 2021
parent tastyminerals <tastyminerals gmail.com> writes:
On Wednesday, 11 August 2021 at 11:44:42 UTC, Steven 
Schveighoffer wrote:
 On 8/11/21 5:31 AM, tastyminerals wrote:
 [...]
`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
I see. Thank you for a detailed answer! I just with this was somehow reflected in the DUB docs...
Aug 11 2021