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

cmd/go,x/tools/gopls: confusing behavior of go mod when go.work is in play #60430

Open
frankli0324 opened this issue May 25, 2023 · 3 comments
Open
Labels
gopls/metadata Issues related to metadata loading in gopls gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@frankli0324
Copy link

frankli0324 commented May 25, 2023

What version of Go are you using (go version)?

$ go version
go version go1.20.4 darwin/amd64

What did you do?

with a go workspace https://github.com/frankli0324/workspace_demo (I committed the go.work file for demonstration purposes)

go.work // uses module_a and module_b
module_a
  - go.mod // that *doesn't* require module_b
  - main.go // imports module_b
module_b
  - go.mod
  - lib.go

module_a can be compiled

now remove go.work, compilation fails with:

main.go:6:2: no required module provides package github.com/frankli0324/workspace_demo/module_b; to add it:
	go get github.com/frankli0324/workspace_demo/module_b

also note that go install github.com/frankli0324/workspace_demo/module_a succeeds, and go install github.com/frankli0324/workspace_demo/module_b@remove-go-work also succeeds.
the remove-go-work branch doesn't have a go.work file and module_a doesn't require module_b

What did you expect to see?/What did you see instead?

I expect either gopls to stop complaining about missing modules in go.mod and "you must go mod tidy" or go build/install fail to build the module if go.mod is incorrectly constructed (missing require)

Conclusion

From what I observe, I believe the essence of go work is forcing submodules to build as if there's an replace $declared_module_name => ./$local_path in each of the used go.mods:

in persudo-code:

used = use_directives_from(go.work)
replacements = []
for mod in used:
    mod_name = get_module_name_from(path.join(mod, go.mod))
    replacements.append(mod, mod_name)
for mod in used:
    for m, mod_name in replacements:
        set_replacement_for(mod, mod_name => m)

However, normally if module_a doesn't require module_b, if module_b is replaced anyway, go build still fails:

main.go:6:2: module github.com/frankli0324/workspace_demo/module_b provides package github.com/frankli0324/workspace_demo/module_b and is replaced but not required; to add it:
	go get github.com/frankli0324/workspace_demo/module_b

this is where I think is inconsistent and erroneous.

I'm aware that #52962 raised the issue already, but I believe it's not a duplicate. Also the issue seems wasted since no one is getting anywhere closer to an answer.

@gopherbot gopherbot added the gopls Issues related to the Go language server, gopls. label May 25, 2023
@frankli0324
Copy link
Author

frankli0324 commented May 25, 2023

actually this also raises a question: how should versions be managed if we could build the module_a without requiring module_b? or is this "undefined behavior"? Is this some legacy from the GOPATH era?

@suzmue suzmue added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 25, 2023
@frankli0324
Copy link
Author

also related: #53502

@jamalc jamalc assigned jamalc and findleyr and unassigned jamalc Jun 1, 2023
@findleyr findleyr changed the title cmd/go|tools/gopls: confusing behavior of go mod when go.work is in play cmd/go|x/tools/gopls: confusing behavior of go mod when go.work is in play Jul 6, 2023
@findleyr findleyr added this to the gopls/v0.13.0 milestone Jul 6, 2023
@findleyr findleyr removed their assignment Jul 6, 2023
@findleyr
Copy link
Contributor

findleyr commented Jul 6, 2023

Slotted this for gopls@v0.13.0, when we are revisiting UX of modules/workspaces/etc.

@seankhliao seankhliao changed the title cmd/go|x/tools/gopls: confusing behavior of go mod when go.work is in play cmd/go,x/tools/gopls: confusing behavior of go mod when go.work is in play Jul 6, 2023
@adonovan adonovan added the gopls/metadata Issues related to metadata loading in gopls label Aug 31, 2023
@findleyr findleyr modified the milestones: gopls/v0.14.0, gopls/v0.15.0 Oct 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls/metadata Issues related to metadata loading in gopls gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants