ATTENTION: Please answer these questions BEFORE submitting your issue. Thanks!
What did you do?
When I run "gopls" from within a Go program (using the exec package) against a file in a temporary directory in macOS the imports statement is not updated if I give the full pathname but is if I give a relative pathname.
Note:
this only happens on a Mac, not on Linux
It only happens if I run gopls from within a Go program, running it from the command line works
it only happens if I run it for a file within a newly created temp dir as generated by calling os.MkdirTemp("", ...), a temp dir in my home directory doesn't have this problem (but one in "/tmp" has the same problem)
only the imports subcommand has a problem, "gopls format -w " successfully updates the file
other import statement generators work - "goimports -w " successfully updates the file
The following program demonstrates the issue:
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
const (
filename = "ggg.go"
progname = "ggg"
tmpDirPattern = "ggg-*"
fileTextStr = `package main
func main() {
fmt.Println("Hello")
}
`
)
var fileText = []byte(fileTextStr)
// execCmd ...
func execCmd(prog string, args ...string) {
name := prog + " " + strings.Join(args, " ")
fmt.Println(name)
cmd := exec.Command(prog, args...)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
err := cmd.Run()
reportErr(name, err)
}
// reportErr reports the error if not nil and exits
func reportErr(name string, err error) {
if err == nil {
return
}
fmt.Println(name+": Error: ", err)
os.Exit(1)
}
// cmp compares the contents of the file with the supplied original text. If
// they differ it will report the difference
func cmp(origText []byte, fileName string, showFileDiffs bool) {
text, err := os.ReadFile(fileName)
reportErr("reading file: "+fileName, err)
differs := len(origText) != len(text)
if !differs {
for i, ch := range origText {
if text[i] != ch {
differs = true
break
}
}
}
if !differs {
fmt.Println("File is unchanged")
} else {
fmt.Println("File has changed")
if showFileDiffs {
fmt.Println("Before")
fmt.Println(string(origText))
fmt.Println("After")
fmt.Println(string(text))
}
}
}
func main() {
var desc,
tmpDir string
var goplsAct = "imports"
var useRelName,
useGoImports,
showFileDiffs bool
flag.BoolVar(&useRelName, "rel", false, "use the relative pathname")
flag.BoolVar(&showFileDiffs, "show", false, "show the differences")
flag.BoolVar(&useGoImports, "use-goimp", false, "use goimports not gopls")
flag.StringVar(&tmpDir, "tmp", "", "use this as base temp dir")
flag.StringVar(&goplsAct, "act", "imports", "the gopls action")
flag.Parse()
desc = `MkdirTemp("` + tmpDir + `", "` + tmpDirPattern + `")`
fmt.Println(desc)
dir, err := os.MkdirTemp(tmpDir, tmpDirPattern)
reportErr(desc, err)
desc = "Chdir " + dir
fmt.Println(desc)
err = os.Chdir(dir)
reportErr(desc, err)
execCmd("go", "mod", "init", progname)
desc = "WriteFile " + filename
fmt.Println(desc)
err = os.WriteFile(filename, fileText, 0666)
reportErr(desc, err)
filePath := filepath.Join(dir, filename)
if useRelName {
filePath = filename
}
if useGoImports {
execCmd("goimports", "-w", filePath)
} else {
execCmd("gopls", goplsAct, "-w", filePath)
}
cmp(fileText, filePath, showFileDiffs)
execCmd("go", "mod", "tidy")
execCmd("go", "build")
execPath := filepath.Join(dir, progname)
execCmd(execPath)
}
What did you expect to see?
I expected the Go program to have the imports statement filled in
What did you see instead?
The file was unchanged
Build info
the OS version as given by uname -a is
Darwin Nicholass-MBP.broadband 21.6.0 Darwin Kernel Version 21.6.0: Mon Dec 19 20:44:01 PST 2022; root:xnu-8020.240.18~2/RELEASE_X86_64 x86_64
The text was updated successfully, but these errors were encountered:
gopherbot
added
Tools
This label describes issues relating to any tools in the x/tools repository.
gopls
Issues related to the Go language server, gopls.
labels
Mar 6, 2023
ATTENTION: Please answer these questions BEFORE submitting your issue. Thanks!
What did you do?
When I run "gopls" from within a Go program (using the exec package) against a file in a temporary directory in macOS the imports statement is not updated if I give the full pathname but is if I give a relative pathname.
Note:
The following program demonstrates the issue:
What did you expect to see?
I expected the Go program to have the imports statement filled in
What did you see instead?
The file was unchanged
Build info
the OS version as given by
uname -a
isThe text was updated successfully, but these errors were encountered: