swaggo / gin-swagger

gin middleware to automatically generate RESTful API documentation with Swagger 2.0.
MIT License
3.8k stars 271 forks source link

swag cannot generate docs when main go is located in directory #73

Open kotyara85 opened 5 years ago

kotyara85 commented 5 years ago

My project structure: ./ ./cmd/main/main.go ./internal/xxxx ./pkg/xxxx

When I run swag init under root directory of the project it complains on absence of main.go. When I run swag init under cmd/main/main.go it doesn't include ./internal or ./pkg directories

artolevi commented 5 years ago

You can target swag cli to files that you need. For example:

swag init -g ./cmd/main/main.go -o ./docs

it will execute swag on your main.go file and put docs it root dir. For more information you could check swag repo https://github.com/swaggo/swag

And what do you mean by "doesn't include ./internal ..."

game1991 commented 4 years ago

very helpful~~

Mentioum commented 3 years ago

Yeah this still doesnt work for me as there is still the error when using the above parameters:

warning: failed to get package name in dir: ./, error: execute go list command, exit status 1, stdout:, stderr:can't load package: package .: no Go files in /home/xxxxxxxx
Mentioum commented 3 years ago

So I worked around it by doing the following:

My file tree looks something like this.

./
./cmd/server/main.go
./pkg/rest/
./pkg/otherservice
./pkg/anotherservice

I run the following command in the project root directory:

swag init -g ../../cmd/server/main.go -o ./pkg/docs -d ./pkg/rest

So there are few things to note here which I thought were really weird.

1.) As you can see the -g parameter uses the -d parameter as its starting directory, whereas -o does not and instead used the current working directory as its starting directory. This is weird and inconsistent. I feel they should either both be relative to the -d parameter directory or both be relative to the cwd. 2.) I can get what I want as all the functions i need are in my rest package. Perhaps it would be good if you could allow for some directory walking instead of assuming everything will be in one directory when loading definitions, or at least allow a way to include several directories when looking for definitions? (Ideally you'd do what go-swagger does and walk your dependency tree from main.go) EDIT: dependency tree walking can be done using --parseDependency

If you feel these should be separate issue please let me know and I will make them.

Mentioum commented 3 years ago

Interestingly there is a way to do what go-swagger does - aka dependency hunting. Using the same structure as above I can run the following command instead:

swag init -d ./cmd/server -o ./pkg/docs --parseDependency

Takes a while (slower than go-swagger for whatever reason) but at least it generates everything I'd like.

I really think the base readme should try to make the options for swag init clearer. Also the issue with -d and the relative paths still stands as in my previous comment.

cedricve commented 3 years ago

I have my gin code running in a subfolder of my Go project, it doesn't recognise it as well. Anyone an idea?

 swag init
fuddin95 commented 11 months ago

I am having the same issue. My repository structure is same as @kotyara85 and the moment I move my controllers out of main.go file to pkg directory the auto generated swagger docs fails to pickup the route. Did anyone find a solution?

crazyoptimist commented 9 months ago
--dir value, -d value                      Directories you want to parse,comma separated and general-info file must be in the first one (default: "./")

Above is from the output of swag init --help.

My project structure is as follows:

├── LICENSE
├── Makefile
├── README.md
├── cmd
│   └── server
│       └── main.go
├── deployments
│   ├── docker-compose.yml
│   └── prod.Dockerfile
├── docs
│   ├── docs.go
│   ├── swagger.json
│   └── swagger.yaml
├── go.mod
├── go.sum
├── internal
│   ├── router
│   │   ├── registry.go
│   │   └── router.go
... ...
├── pkg
... ...
└── testdata

And I met the same error with swag init.

Using swag init -d ./cmd/server,./ worked perfectly fine.

irahulranjan commented 3 months ago

I tried everything mentioned in the above comments and still doesn't work for me.

olauro commented 3 months ago

This worked for me

swag init -d ./cmd/api/ --pdl 3

/cmd/api/ is where my main.go file is located

--pdl flag is describe in swag init -h:

--parseDependencyLevel value, --pdl value Parse go files inside dependency folder, 0 disabled, 1 only parse models, 2 only parse operations, 3 parse all (default: 0)

basic the -pdl will try to find all your dependencies by the main.go file

dosedaf commented 3 months ago

cmd/sireng/main.go internal/user/handler internal/tracker/handler

i still can't get the api documentation handlers

i have done swag init -g ../cmd/sireng/main.go -d ./internal -o ./docs but it only parses the main.go

RichieMuga commented 2 months ago

This is what worked for me, This is my folder structure

├── cmd
│   └── server
│       └── main.go
├── config
├── controllers
│   └── ping_controller.go
├── database
│   ├── db.go
│   └── migrations
├── docs
│   ├── docs.go
│   ├── swagger.json
│   └── swagger.yaml
├── dummy.go
├── go.mod
├── gorm.db
├── go.sum
├── internals
│   ├── handlers
│   ├── middleware
│   ├── repositories
│   └── services
├── models
│   └── user.go
├── pkg
│   └── logger
│       └── logger.go
├── README.md
├── routes
│   └── routes.go
└── utils

I created a dummy.go and inserted the followning

package main

// This file is intentionally left empty to satisfy swag's requirement
// for Go files in the root directory.

and then I ran the following command:

swag init -g cmd/server/main.go -d ./ --exclude ./models ./cmd ./internals ./config ./database ./routes ./pkg

Let me know if it works for you with a thumbs up.