-
Notifications
You must be signed in to change notification settings - Fork 9
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
remove external executable dependency 'goimports' #11
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very interesting change. Thanks a lot for the information. I didn't know that goimports
is a direct wrapper of the golang.org/x/tools/imports
.
- Will the imports package also fix the code formatting?
Could you also clean up a recent change to the command check of goimports
, see https://github.com/golang-design/ssaplayground/pull/7/files for more details.
This change arises me question regarding getting SSA information more directly, such as:
- Is it possible to directly use https://golang.org/x/tools/go/ssa for generating ssa.html?
Sure. Looking at following example: package main
import (
"fmt"
"golang.org/x/tools/imports"
)
func main() {
doimports()
}
// source code we want to do autoimport
const src string = `
package main
func main() {
ctx := context.Background()
println(ctx)
r := gin.New()
println(r)
}
`
func doimports() {
out, err := imports.Process("", []byte(src), &imports.Options{})
if err != nil {
fmt.Println("imports failed,", err)
return
}
fmt.Printf("%s\n", out)
} it will give the output:
Currently infeasible in my opinion. the generated ssa.html is the human-friendly visualization In addition, there are other reasons for not doing it :
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like there is a behavior change:
If I use the following snippet:
func main() {
fmt.Println(time.Second)
}
Your change will result in:
cannot run autoimports for your code, err:
1:1: expected 'package', found 'func'
whereas the live server returns ssa.html properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for another awesome fix!
Sorry for not testing fully. New commit has fixed this, which keeps sync with goimports options
Here are test results of the 6 cases above.
|
Currently in-use
goimports
command shares the same internal implementation with golang.org/x/tools/imports.But calling an external executable may be unstable and a bit inefficient, so I propose using corresponding library to replace it,
which also solves the problem of building failure in docker as
go get -u golang.org/x/tools/cmd/goimports
needs GOPROXYto be set if in China mainland.
// Chinese version
英文不太好见谅,大意就是goimports和golang.org/x/tools/imports这个库底层都是调用golang.org/x/tools/internal/imports的
Process函数,因此不会有表现上的不同。此外依赖goimports的话需要在Readme里申明一下prerequisite,并且在docker构建时
如果在大陆网络条件下会go get失败,加入GOPROXY的话也要对国外用户区分,综合以上说法我觉得替换成库实现更简便高效。