Repository: promptfoo/promptfoo
Issue: 'no required module provides package x: go.mod file not found' error in goProvider
Root cause: Validation Gap

**Describe the bug**
When running `promptfoo eval` in a subfolder with a main.go file which has a go.mod file in a higher level directory we get the following error:
```
main.go:17:2: no required module provides package [package name]: go.mod file not found in current directory or any parent directory; see 'go help modules'
```
Example folder structure:
--myproj
  --core
    --pkg1
    --pkg2
    --evaluation
      --main.go
      --promptfooconfig.yml
  go.mod
  go.sum
  vendor

**To Reproduce**
Steps to reproduce the behavior, including example Promptfoo configurations if applicable:

1. Run 'promptfoo eval' in the evaluation folder in the example folder structure

**Expected behavior**
Promptfoo eval should run using core/evaluation/main.go as it's provider


**System information:**

If possible, please output the results of `promptfoo debug` and paste the output here.

- Promptfoo version: 0.103.17

**Additional context**
I believe the offending line is this one https://github.com/promptfoo/promptfoo/blob/9b65868b8f4a0538b00ef6a6476506eeac4dfbeb/src/providers/golangCompletion.ts#L58

Added as part of this PR: https://github.com/promptfoo/promptfoo/pull/2888


Hi @gregnuttall, I believe this issue is fixed in [0.104.4](https://github.com/promptfoo/promptfoo/releases/tag/0.104.4). Please update to the latest version and let us know if you have any more issues. If it's still not working I am happy to jump on a call and debug with you.

~~Hi @mldangelo , I am @gregnuttall 's colleague, greg and me tested 0.104.4 today, but we see further issues when running `promptfoo eval --no-cache`, same test is working with 0.103.16. might be worth jump on a call so please let us know when is best for you. We are located in UK, but let's see what time is convenient for you.~~
```
Error: Error running Golang script: Command failed: cd /var/folders/yd/50q1qdp17fvcwwy4kvlt08p80000gn/T/golang-provider-F7UL2z/core/evaluation/rider_asking_for_more_orders && go build -o /var/folders/yd/50q1qdp17fvcwwy4kvlt08p80000gn/T/golang-provider-F7UL2z/golang_wrapper wrapper.go main.go
# command-line-arguments
./main.go:23:6: CallApi redeclared in this block
	./wrapper.go:14:5: other declaration of CallApi


Error: Error running Golang script: Command failed: cd /var/folders/yd/50q1qdp17fvcwwy4kvlt08p80000gn/T/golang-provider-F7UL2z/core/evaluation/rider_asking_for_more_orders && go build -o /var/folders/yd/50q1qdp17fvcwwy4kvlt08p80000gn/T/golang-provider-F7UL2z/golang_wrapper wrapper.go main.go
# command-line-arguments
./main.go:23:6: CallApi redeclared in this block
	./wrapper.go:14:5: other declaration of CallApi

    at GolangProvider.executeGolangScript (/Users/zeqiancao/.nvm/versions/node/v20.12.0/lib/node_modules/promptfoo/dist/src/providers/golangCompletion.js:118:23)
    at async runEval (/Users/zeqiancao/.nvm/versions/node/v20.12.0/lib/node_modules/promptfoo/dist/src/evaluator.js:180:24)
    at async processEvalStep (/Users/zeqiancao/.nvm/versions/node/v20.12.0/lib/node_modules/promptfoo/dist/src/evaluator.js:635:26)
    at async /Users/zeqiancao/.nvm/versions/node/v20.12.0/lib/node_modules/promptfoo/dist/src/evaluator.js:845:13
```

Hi @mldangelo I can confirm issue is fixed in 0.104.4. Please ignore my comment above. I looked at you new example in golang provider and just followed it and it resolved issue for me. Thank you!


Hi @mldangelo  Actually there is a problem here, I think what promptfoo does is it reads the source code from our customized provider like in this [example](https://github.com/promptfoo/promptfoo/blob/main/examples/golang-provider/main.go) and make it part of the "main" module in [wrapper.go](https://github.com/promptfoo/promptfoo/blob/main/src/golang/wrapper.go#L14), and [CallApi](https://github.com/promptfoo/promptfoo/blob/main/src/golang/wrapper.go#L14) has already been defined there.

So whether to define CallApi myself will both lead to problems:
1. If I define it myself then I would have error like [mentioned above](https://github.com/promptfoo/promptfoo/issues/3057#issuecomment-2663631113)
2. Or if I don't define a CallApi in my own implemention, it will work but compiler will say it has error(because compiler can't see its definition in wrapper.go in promptfoo)

Do you have any suggestion here? Jumping on a call would be convenient.

@mldangelo acutally same error also happens with [examples/go-provider](https://github.com/promptfoo/promptfoo/tree/main/examples/golang-provider)
I used `promptfoo init --example golang-provider` to initialize a example golang provider, and I see exactly the same error in this example:

<img width="1370" alt="Image" src="https://github.com/user-attachments/assets/1bda375a-a43f-415d-8be7-3aa3d5714175" />
This is error for "provider in root", same as what error I got above in our own project:

```
Error: Error running Golang script: Command failed: cd /var/folders/yd/50q1qdp17fvcwwy4kvlt08p80000gn/T/golang-provider-OLYsKJ && go build -o /var/folders/yd/50q1qdp17fvcwwy4kvlt08p80000gn/T/golang-provider-OLYsKJ/golang_wrapper wrapper.go main.go
go: downloading github.com/sashabaranov/go-openai v1.37.0
# command-line-arguments
./main.go:25:5: CallApi redeclared in this block
	./wrapper.go:14:5: other declaration of CallApi


Error: Error running Golang script: Command failed: cd /var/folders/yd/50q1qdp17fvcwwy4kvlt08p80000gn/T/golang-provider-OLYsKJ && go build -o /var/folders/yd/50q1qdp17fvcwwy4kvlt08p80000gn/T/golang-provider-OLYsKJ/golang_wrapper wrapper.go main.go
go: downloading github.com/sashabaranov/go-openai v1.37.0
# command-line-arguments
./main.go:25:5: CallApi redeclared in this block
	./wrapper.go:14:5: other declaration of CallApi

    at GolangProvider.executeGolangScript (/Users/zeqiancao/.nvm/versions/node/v20.12.0/lib/node_modules/promptfoo/dist/src/providers/golangCompletion.js:118:23)
    at async runEval (/Users/zeqiancao/.nvm/versions/node/v20.12.0/lib/node_modules/promptfoo/dist/src/evaluator.js:180:24)
    at async processEvalStep (/Users/zeqiancao/.nvm/versions/node/v20.12.0/lib/node_modules/promptfoo/dist/src/evaluator.js:635:26)
    at async /Users/zeqiancao/.nvm/versions/node/v20.12.0/lib/node_modules/promptfoo/dist/src/evaluator.js:845:13
```

Hi @gregnuttall and @zeqiancao, I’m sorry you’re still running into problems. I’d love to help debug with you directly. Feel free to pick a time on @MrFlounder and my calendar here: https://cal.com/team/promptfoo/michael-shuo We can walk through your setup together and get everything working together.

@zeqiancao Hey, can you try renaming the CallApi and init it this way:

```
func init() {
	client = openai.NewClient(os.Getenv("OPENAI_API_KEY"))
	CallApi = ProvideApi  // Assign our implementation to wrapper's CallApi variable
}

func ProviderApi(prompt string, options map[string]interface{}, ctx map[string]interface{}) (map[string]interface{}, error) {
...
```

This should unblock you for now. We'll just update the example and doc if this is acceptable solution for you.

Hi @MrFlounder this won't work for me, because although doing this will enable me to run promptfoo locally with new version, the compiler error "undefined: CallApi" will stop me from commiting the code and pass our CI/CD pipeline.

<img width="684" alt="Image" src="https://github.com/user-attachments/assets/c00a19ef-772a-4d94-9a91-138ee2824010" />

@zeqiancao hey my apologies, are you able to mute the error for now to unblock? I'll fix it the right way today!

@zeqiancao Hey, we just released 0.106.1[(https://github.com/promptfoo/promptfoo/releases/tag/0.106.1)], can you try and see if that fixes your blocker ok? Thank you! Should there be any concerns feel free to grab time with me. https://cal.com/guangshuo-zang-mjy19w

Hi @MrFlounder, thank you very much for looking into this! I updated prompt to 0.106.1 but I still see same error as before though

```Error: Error running Golang script: Command failed: cd /var/folders/yd/50q1qdp17fvcwwy4kvlt08p80000gn/T/golang-provider-Imw8Lt/core/evaluation/input_moderation_step && go build -o /var/folders/yd/50q1qdp17fvcwwy4kvlt08p80000gn/T/golang-provider-Imw8Lt/golang_wrapper wrapper.go main.go
# command-line-arguments
./main.go:22:6: CallApi redeclared in this block
	./wrapper.go:14:5: other declaration of CallApi
```

@zeqiancao Hey sorry I haven't follow up untill now. I made another fix attempt yesterday, but I'd like to make sure this would work for you 100% this time. Do you mind grabbing some time on my calendar to go through this together?
Here's the PR: https://github.com/promptfoo/promptfoo/pull/3414
My calendar: https://cal.com/guangshuo-zang-mjy19w

Hi @MrFlounder sorry I just saw your reply just now, and I just tested it. This time it does resolve the error and I don't have to do any workarounds. I am going to update promptfoo version to latest one(0.107.7 at this time)  for our project, and I think this issue can be closed. Thank you very much again for your help!

@zeqiancao Glad to hear!