Mike,

Let's use official Go sources.

go/src/cmd/tools/tools.go:

$ go version
go version devel go1.24-0f003f9d15 Thu Aug 29 09:15:06 2024 +0000 
linux/amd64
$ 

$ cat tools.go
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build tools

package tools

// Arrange to vendor the bisect command for use
// by the internal/godebug package test.
import _ "golang.org/x/tools/cmd/bisect"
$ 

Indent the tools.go import statement by a tab to force a reformat.

$ go fmt 
tools.go
$ 

Attempt to build package tools.

$ go build
package cmd/tools: build constraints exclude all Go files in 
/home/peter/go/src/cmd/tools
$ go build -tags=tools 
tools.go:11:8: import "golang.org/x/tools/cmd/bisect" is a program, not an 
importable package
$ 

Looks like a go build generated error.

A related Go issue.

https://github.com/golang/go/issues/48429

peter

On Thursday, August 29, 2024 at 9:54:43 PM UTC-4 Mike Schinkel wrote:

> Hi all,
>
> I think I already know the answer, but hoping there is a way I am missing 
> to get gofmt to stop generating the "import in a program, not an importable 
> package" error when parsing a `tools.go` file with the `tools` build tag.
>
> If you are not familiar, there is an approach several people have written 
> about to store references to your project's required Go-based CLI tools in 
> a `tools.go` file:
>
> https://play-with-go.dev/tools-as-dependencies_go119_en/
> https://www.jvt.me/posts/2022/06/15/go-tools-dependency-management/
>
> I am using it because oapi-codegen recommends it for maintaining enabling 
> others who may clone your repo to easily get the proper tools then need 
> installed:
>
> https://github.com/oapi-codegen/oapi-codegen/?tab=readme-ov-file#install
>
> And grpc-gateway evidently recommends it too:
>
> https://github.com/grpc-ecosystem/grpc-gateway#installation 
>
> When I run gofmt — which GoLand does prior to a commit — it generates that 
> as an error. People have said just to ignore it:
>
> https://github.com/grpc-ecosystem/grpc-gateway/issues/3515
>
> https://stackoverflow.com/questions/77664121/import-is-a-program-not-an-importable-package
>
> However — call me anal —I really want to ensure no errors are generated 
> unless they are errors I really need to fix. Otherwise I will likely get 
> complacent and accidentally commit a real error.
>
> Is there any way to get gofmt to ignore code based on build tags, e.g. 
> `tools` in this case?
>
> -Mike

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/13fa8263-0ba9-4703-93e5-644dadfccbd7n%40googlegroups.com.

Reply via email to