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

document "reserved standard library" module paths #135

Closed
twmb opened this issue Jul 21, 2021 · 7 comments
Closed

document "reserved standard library" module paths #135

twmb opened this issue Jul 21, 2021 · 7 comments

Comments

@twmb
Copy link

twmb commented Jul 21, 2021

If I have the following directory structure:

go.mod
main.go
pkg/thing/thing.go

with the following file contents:

go.mod:

module foo

go 1.16

main.go:

package main

import (
	"fmt"

	"foo/pkg/thing"
)

func main() {
	fmt.Println("hello")
	thing.Print()
}

pkg/thing/thing.go:

package thing

func Print() {
	println("foo")
}

Then gofumpt will change main.go's import block such that the foo/pkg/thing import is tied to the stdlib fmt import.

I think this is only possible to detect with the current gofumpt implementation if gofumpt reads the go.mod file to know what module it is in. If import == module || strings.HasPrefix(import, module + "/"), then the import is non-standard. I do not see a way to have this same logic if just relying on the ast. Another potential option is to just have a list of stdlib imports, but this obviously is not future proof for when the stdlib adds new modules (a user would have to update gofumpt, which also would then have the obligation of staying up to date with Go releases).

@mvdan
Copy link
Owner

mvdan commented Jul 24, 2021

You seem to understand what's going on here :) Have you seen #117 (comment)? I might add that to a FAQ since this keeps coming up.

@mvdan
Copy link
Owner

mvdan commented Jul 24, 2021

I think this is only possible to detect with the current gofumpt implementation if gofumpt reads the go.mod file to know what module it is in.

That's planned for #38, so that would affect grouping imports too. Still, though, you shouldn't be using paths which are "reserved for std" as per the explanation above.

@mvdan
Copy link
Owner

mvdan commented Sep 2, 2021

friendly ping :) did you see my reply above?

@twmb
Copy link
Author

twmb commented Sep 2, 2021

Yep -- I updated the module in question to be a github based name, not a single word. given #38 and your golang issue, I'm thinking the only thing to do here would be to update the FAQ.

@mvdan
Copy link
Owner

mvdan commented Sep 3, 2021

Thank you - agreed.

@mvdan mvdan removed the needs info label Sep 3, 2021
@mvdan mvdan changed the title standard library import group is too aggressive document "reserved standard library" module paths Sep 3, 2021
@twmb twmb closed this as completed Sep 3, 2021
@mvdan
Copy link
Owner

mvdan commented Sep 5, 2021

I'll leave this open as a doc reminder for myself, if that's OK :)

@mvdan mvdan reopened this Sep 5, 2021
@mvdan mvdan closed this as completed in 1727efe Sep 8, 2021
@mvdan
Copy link
Owner

mvdan commented Feb 5, 2022

FYI, see #187 (comment); I'm currently thinking we could do better for everyone by looking at the main module's path.

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

2 participants