Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kotyara85 opened this issue Aug 8, 2019 · 15 comments
Open

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

kotyara85 opened this issue Aug 8, 2019 · 15 comments

Comments

@kotyara85
Copy link

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

@0xk175un3
Copy link

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
Copy link

very helpful~~

@Mentioum
Copy link

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
Copy link

Mentioum commented Nov 23, 2020

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
Copy link

Mentioum commented Nov 23, 2020

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
Copy link

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
Copy link
Contributor

fuddin95 commented Dec 9, 2023

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?

@fuddin95 fuddin95 mentioned this issue Dec 10, 2023
@crazyoptimist
Copy link

crazyoptimist commented Jan 27, 2024

--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
Copy link

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

@olauro
Copy link

olauro commented Jul 29, 2024

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
Copy link

dosedaf commented Aug 3, 2024

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
Copy link

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.

@KonstantinSavin
Copy link

Tried all of the above - didn't help.

@RichieMuga
Copy link

We could try and debug. What are the errors you are getting?

@rishikesh-parspec
Copy link

rishikesh-parspec commented Feb 28, 2025

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

suppose your project name is CRUD and there are multiple directory named- cmd, internal, pkg

go to the projects directory-
cd CRUD

And then run the command

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

This command traverse all the directory and create a documentation for all files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests