diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000..13566b81b0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000..3ce358829c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000..e95caad3e8 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/rules_go.iml b/.idea/rules_go.iml new file mode 100644 index 0000000000..5e764c4f0b --- /dev/null +++ b/.idea/rules_go.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000..94a25f7f4c --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.ijwb/.bazelproject b/.ijwb/.bazelproject new file mode 100644 index 0000000000..7ec5102063 --- /dev/null +++ b/.ijwb/.bazelproject @@ -0,0 +1,19 @@ +directories: + # Add the directories you want added as source here + # By default, we've added your entire workspace ('.') + . + +# Automatically includes all relevant targets under the 'directories' above +derive_targets_from_directories: true + +targets: + # If source code isn't resolving, add additional targets that compile it here + # //go/private:all + # //go:all + +additional_languages: + # Uncomment any additional languages you want supported + # dart + # javascript + # python + # typescript diff --git a/.ijwb/.blaze/modules/.project-data-dir.iml b/.ijwb/.blaze/modules/.project-data-dir.iml new file mode 100644 index 0000000000..a296edf1c5 --- /dev/null +++ b/.ijwb/.blaze/modules/.project-data-dir.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.ijwb/.blaze/modules/.workspace.iml b/.ijwb/.blaze/modules/.workspace.iml new file mode 100644 index 0000000000..4b3ccb58cc --- /dev/null +++ b/.ijwb/.blaze/modules/.workspace.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.ijwb/.idea/.gitignore b/.ijwb/.idea/.gitignore new file mode 100644 index 0000000000..13566b81b0 --- /dev/null +++ b/.ijwb/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.ijwb/.idea/.name b/.ijwb/.idea/.name new file mode 100644 index 0000000000..78dda5ba02 --- /dev/null +++ b/.ijwb/.idea/.name @@ -0,0 +1 @@ +rules_go \ No newline at end of file diff --git a/.ijwb/.idea/misc.xml b/.ijwb/.idea/misc.xml new file mode 100644 index 0000000000..3ce358829c --- /dev/null +++ b/.ijwb/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.ijwb/.idea/modules.xml b/.ijwb/.idea/modules.xml new file mode 100644 index 0000000000..6e46a2e042 --- /dev/null +++ b/.ijwb/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.ijwb/.idea/runConfigurations.xml b/.ijwb/.idea/runConfigurations.xml new file mode 100644 index 0000000000..26bfd419a2 --- /dev/null +++ b/.ijwb/.idea/runConfigurations.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.ijwb/.idea/vcs.xml b/.ijwb/.idea/vcs.xml new file mode 100644 index 0000000000..6c0b863585 --- /dev/null +++ b/.ijwb/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/go/tools/builders/env.go b/go/tools/builders/env.go index 694754c90b..5e3afd5b06 100644 --- a/go/tools/builders/env.go +++ b/go/tools/builders/env.go @@ -44,9 +44,6 @@ var ( // See ./README.rst for more information about handling arguments and // environment variables. type env struct { - // wd is the path to the working directory - wd string - // sdk is the path to the Go SDK, which contains tools for the host // platform. This may be different than GOROOT. sdk string @@ -74,7 +71,6 @@ func envFlags(flags *flag.FlagSet) *env { flags.StringVar(&env.installSuffix, "installsuffix", "", "Standard library under GOROOT/pkg") flags.BoolVar(&env.verbose, "v", false, "Whether subprocess command lines should be printed") flags.BoolVar(&env.shouldPreserveWorkDir, "work", false, "if true, the temporary work directory will be preserved") - flags.StringVar(&env.wd, "wd", ".", "working director default to dot") return env } @@ -125,9 +121,6 @@ func (e *env) goTool(tool string, args ...string) []string { // and additional arguments. func (e *env) goCmd(cmd string, args ...string) []string { exe := filepath.Join(e.sdk, "bin", "go") - if len(e.wd) > 0 { - exe = filepath.Join(e.wd, exe) - } if runtime.GOOS == "windows" { exe += ".exe" diff --git a/go/tools/builders/stdliblist.go b/go/tools/builders/stdliblist.go index 571a2a4b81..eedf74d143 100644 --- a/go/tools/builders/stdliblist.go +++ b/go/tools/builders/stdliblist.go @@ -15,138 +15,142 @@ package main import ( - "bytes" - "encoding/json" - "flag" - "fmt" - "go/build" - "os" - "path/filepath" - "strings" + "bytes" + "encoding/json" + "flag" + "fmt" + "go/build" + "os" + "path/filepath" + "strings" ) // Copy and pasted from golang.org/x/tools/go/packages type flatPackagesError struct { - Pos string // "file:line:col" or "file:line" or "" or "-" - Msg string - Kind flatPackagesErrorKind + Pos string // "file:line:col" or "file:line" or "" or "-" + Msg string + Kind flatPackagesErrorKind } type flatPackagesErrorKind int const ( - UnknownError flatPackagesErrorKind = iota - ListError - ParseError - TypeError + UnknownError flatPackagesErrorKind = iota + ListError + ParseError + TypeError ) func (err flatPackagesError) Error() string { - pos := err.Pos - if pos == "" { - pos = "-" // like token.Position{}.String() - } - return pos + ": " + err.Msg + pos := err.Pos + if pos == "" { + pos = "-" // like token.Position{}.String() + } + return pos + ": " + err.Msg } // flatPackage is the JSON form of Package // It drops all the type and syntax fields, and transforms the Imports type flatPackage struct { - ID string - Name string `json:",omitempty"` - PkgPath string `json:",omitempty"` - Standard bool `json:",omitempty"` - Errors []flatPackagesError `json:",omitempty"` - GoFiles []string `json:",omitempty"` - CompiledGoFiles []string `json:",omitempty"` - OtherFiles []string `json:",omitempty"` - ExportFile string `json:",omitempty"` - Imports map[string]string `json:",omitempty"` + ID string + Name string `json:",omitempty"` + PkgPath string `json:",omitempty"` + Standard bool `json:",omitempty"` + Errors []flatPackagesError `json:",omitempty"` + GoFiles []string `json:",omitempty"` + CompiledGoFiles []string `json:",omitempty"` + OtherFiles []string `json:",omitempty"` + ExportFile string `json:",omitempty"` + Imports map[string]string `json:",omitempty"` } type goListPackage struct { - Dir string // directory containing package sources - ImportPath string // import path of package in dir - Name string // package name - Target string // install path - Goroot bool // is this package in the Go root? - Standard bool // is this package part of the standard Go library? - Root string // Go root or Go path dir containing this package - Export string // file containing export data (when using -export) - // Source files - GoFiles []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles) - CgoFiles []string // .go source files that import "C" - CompiledGoFiles []string // .go files presented to compiler (when using -compiled) - IgnoredGoFiles []string // .go source files ignored due to build constraints - IgnoredOtherFiles []string // non-.go source files ignored due to build constraints - CFiles []string // .c source files - CXXFiles []string // .cc, .cxx and .cpp source files - MFiles []string // .m source files - HFiles []string // .h, .hh, .hpp and .hxx source files - FFiles []string // .f, .F, .for and .f90 Fortran source files - SFiles []string // .s source files - SwigFiles []string // .swig files - SwigCXXFiles []string // .swigcxx files - SysoFiles []string // .syso object files to add to archive - TestGoFiles []string // _test.go files in package - XTestGoFiles []string // _test.go files outside package - // Embedded files - EmbedPatterns []string // //go:embed patterns - EmbedFiles []string // files matched by EmbedPatterns - TestEmbedPatterns []string // //go:embed patterns in TestGoFiles - TestEmbedFiles []string // files matched by TestEmbedPatterns - XTestEmbedPatterns []string // //go:embed patterns in XTestGoFiles - XTestEmbedFiles []string // files matched by XTestEmbedPatterns - // Dependency information - Imports []string // import paths used by this package - ImportMap map[string]string // map from source import to ImportPath (identity entries omitted) - // Error information - Incomplete bool // this package or a dependency has an error - Error *flatPackagesError // error loading package - DepsErrors []*flatPackagesError // errors loading dependencies + Dir string // directory containing package sources + ImportPath string // import path of package in dir + Name string // package name + Target string // install path + Goroot bool // is this package in the Go root? + Standard bool // is this package part of the standard Go library? + Root string // Go root or Go path dir containing this package + Export string // file containing export data (when using -export) + // Source files + GoFiles []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles) + CgoFiles []string // .go source files that import "C" + CompiledGoFiles []string // .go files presented to compiler (when using -compiled) + IgnoredGoFiles []string // .go source files ignored due to build constraints + IgnoredOtherFiles []string // non-.go source files ignored due to build constraints + CFiles []string // .c source files + CXXFiles []string // .cc, .cxx and .cpp source files + MFiles []string // .m source files + HFiles []string // .h, .hh, .hpp and .hxx source files + FFiles []string // .f, .F, .for and .f90 Fortran source files + SFiles []string // .s source files + SwigFiles []string // .swig files + SwigCXXFiles []string // .swigcxx files + SysoFiles []string // .syso object files to add to archive + TestGoFiles []string // _test.go files in package + XTestGoFiles []string // _test.go files outside package + // Embedded files + EmbedPatterns []string // //go:embed patterns + EmbedFiles []string // files matched by EmbedPatterns + TestEmbedPatterns []string // //go:embed patterns in TestGoFiles + TestEmbedFiles []string // files matched by TestEmbedPatterns + XTestEmbedPatterns []string // //go:embed patterns in XTestGoFiles + XTestEmbedFiles []string // files matched by XTestEmbedPatterns + // Dependency information + Imports []string // import paths used by this package + ImportMap map[string]string // map from source import to ImportPath (identity entries omitted) + // Error information + Incomplete bool // this package or a dependency has an error + Error *flatPackagesError // error loading package + DepsErrors []*flatPackagesError // errors loading dependencies } func stdlibPackageID(importPath string) string { - return "@io_bazel_rules_go//stdlib:" + importPath + return "@io_bazel_rules_go//stdlib:" + importPath } // labelledPath replace the cloneBase with output base label func labelledPath(cloneBase, p string) string { - dir, _ := filepath.Rel(cloneBase, p) - return filepath.Join("__BAZEL_OUTPUT_BASE__", dir) + dir, _ := filepath.Rel(cloneBase, p) + println("rel:" + dir) + return filepath.Join("__BAZEL_OUTPUT_BASE__", dir) } // absoluteSourcesPaths replace cloneBase of the absolution // paths with the label for all source files in a package func absoluteSourcesPaths(cloneBase, pkgDir string, srcs []string) []string { - ret := make([]string, 0, len(srcs)) - pkgDir = labelledPath(cloneBase, pkgDir) - for _, src := range srcs { - ret = append(ret, filepath.Join(pkgDir, src)) - } - return ret + ret := make([]string, 0, len(srcs)) + pkgDir = labelledPath(cloneBase, pkgDir) + println("labelled:" + pkgDir) + for _, src := range srcs { + ret = append(ret, filepath.Join(pkgDir, src)) + } + return ret } func flatPackageForStd(cloneBase string, pkg *goListPackage) *flatPackage { - // Don't use generated files from the stdlib - goFiles := absoluteSourcesPaths(cloneBase, pkg.Dir, pkg.GoFiles) - - newPkg := &flatPackage{ - ID: stdlibPackageID(pkg.ImportPath), - Name: pkg.Name, - PkgPath: pkg.ImportPath, - ExportFile: labelledPath(cloneBase, pkg.Target), - Imports: map[string]string{}, - Standard: pkg.Standard, - GoFiles: goFiles, - CompiledGoFiles: goFiles, - } - for _, imp := range pkg.Imports { - newPkg.Imports[imp] = stdlibPackageID(imp) - } - // We don't support CGo for now - delete(newPkg.Imports, "C") - return newPkg + // Don't use generated files from the stdlib + println("pkg.Dir:" + pkg.Dir) + println("cloneBase:" + cloneBase) + goFiles := absoluteSourcesPaths(cloneBase, pkg.Dir, pkg.GoFiles) + + newPkg := &flatPackage{ + ID: stdlibPackageID(pkg.ImportPath), + Name: pkg.Name, + PkgPath: pkg.ImportPath, + ExportFile: labelledPath(cloneBase, pkg.Target), + Imports: map[string]string{}, + Standard: pkg.Standard, + GoFiles: goFiles, + CompiledGoFiles: goFiles, + } + for _, imp := range pkg.Imports { + newPkg.Imports[imp] = stdlibPackageID(imp) + } + // We don't support CGo for now + delete(newPkg.Imports, "C") + return newPkg } // In Go 1.18, the standard library started using go:embed directives. @@ -165,88 +169,92 @@ func flatPackageForStd(cloneBase string, pkg *goListPackage) *flatPackage { // that file paths in the generated JSON are still valid. // // cloneGoRoot returns the new GOROOT we should run under. -func cloneGoRoot(execRoot, relativeGoroot, cloneBase string) (newGoRoot string, err error) { - goroot := filepath.Join(execRoot, relativeGoroot) - newGoRoot = filepath.Join(cloneBase, relativeGoroot) - if err := os.MkdirAll(newGoRoot, 01755); err != nil { - return "", err - } - - if err := replicate(goroot, newGoRoot, replicatePaths("src", "pkg/tool", "pkg/include")); err != nil { - return "", err - } - - return newGoRoot, nil +func cloneGoRoot(goRoot, relativeGoroot, cloneBase string) (newGoRoot string, err error) { + newGoRoot = filepath.Join(cloneBase, relativeGoroot) + if err := os.MkdirAll(newGoRoot, 01755); err != nil { + return "", err + } + + if err := replicate(goRoot, newGoRoot, replicatePaths("src", "pkg/tool", "pkg/include")); err != nil { + return "", err + } + + return newGoRoot, nil } // stdliblist runs `go list -json` on the standard library and saves it to a file. func stdliblist(args []string) error { - // process the args - flags := flag.NewFlagSet("stdliblist", flag.ExitOnError) - goenv := envFlags(flags) - out := flags.String("out", "", "Path to output go list json") - if err := flags.Parse(args); err != nil { - return err - } - if err := goenv.checkFlags(); err != nil { - return err - } - - cloneBase, cleanup, err := goenv.workDir() - if err != nil { - return err - } - defer func() { cleanup() }() - - cloneGoRoot, err := cloneGoRoot(goenv.wd, goenv.sdk, cloneBase) - if err != nil { - return fmt.Errorf("failed to clone new go root %v", err) - } - - // Ensure paths are absolute. - absPaths := []string{} - for _, path := range filepath.SplitList(os.Getenv("PATH")) { - absPaths = append(absPaths, abs(path)) - } - os.Setenv("PATH", strings.Join(absPaths, string(os.PathListSeparator))) - os.Setenv("GOROOT", cloneGoRoot) - // Make sure we have an absolute path to the C compiler. - // TODO(#1357): also take absolute paths of includes and other paths in flags. - os.Setenv("CC", abs(os.Getenv("CC"))) - - cachePath := abs(*out + ".gocache") - defer os.RemoveAll(cachePath) - os.Setenv("GOCACHE", cachePath) - os.Setenv("GOMODCACHE", cachePath) - os.Setenv("GOPATH", cachePath) - - listArgs := goenv.goCmd("list") - if len(build.Default.BuildTags) > 0 { - listArgs = append(listArgs, "-tags", strings.Join(build.Default.BuildTags, ",")) - } - listArgs = append(listArgs, "-json", "builtin", "std", "runtime/cgo") - - jsonFile, err := os.Create(*out) - if err != nil { - return err - } - defer jsonFile.Close() - - jsonData := &bytes.Buffer{} - if err := goenv.runCommandToFile(jsonData, listArgs); err != nil { - return err - } - encoder := json.NewEncoder(jsonFile) - decoder := json.NewDecoder(jsonData) - for decoder.More() { - var pkg *goListPackage - if err := decoder.Decode(&pkg); err != nil { - return err - } - if err := encoder.Encode(flatPackageForStd(cloneBase, pkg)); err != nil { - return err - } - } - - return nil + // process the args + flags := flag.NewFlagSet("stdliblist", flag.ExitOnError) + goenv := envFlags(flags) + out := flags.String("out", "", "Path to output go list json") + if err := flags.Parse(args); err != nil { + return err + } + if err := goenv.checkFlags(); err != nil { + return err + } + + goRoot, err := filepath.Abs(goenv.sdk) + if err != nil { + return err + } + cloneBase, cleanup, err := goenv.workDir() + if err != nil { + return err + } + defer func() { cleanup() }() + + cloneGoRoot, err := cloneGoRoot(goRoot, goenv.sdk, cloneBase) + if err != nil { + return fmt.Errorf("failed to clone new go root %v", err) + } + + // Ensure paths are absolute. + absPaths := []string{} + for _, path := range filepath.SplitList(os.Getenv("PATH")) { + absPaths = append(absPaths, abs(path)) + } + os.Setenv("PATH", strings.Join(absPaths, string(os.PathListSeparator))) + os.Setenv("GOROOT", cloneGoRoot) + // Make sure we have an absolute path to the C compiler. + // TODO(#1357): also take absolute paths of includes and other paths in flags. + os.Setenv("CC", abs(os.Getenv("CC"))) + + cachePath := abs(*out + ".gocache") + defer os.RemoveAll(cachePath) + os.Setenv("GOCACHE", cachePath) + os.Setenv("GOMODCACHE", cachePath) + os.Setenv("GOPATH", cachePath) + + listArgs := goenv.goCmd("list") + println("listargs:" + strings.Join(listArgs, ",")) + if len(build.Default.BuildTags) > 0 { + listArgs = append(listArgs, "-tags", strings.Join(build.Default.BuildTags, ",")) + } + listArgs = append(listArgs, "-json", "builtin", "std", "runtime/cgo") + + jsonFile, err := os.Create(*out) + if err != nil { + return err + } + defer jsonFile.Close() + + jsonData := &bytes.Buffer{} + if err := goenv.runCommandToFile(jsonData, listArgs); err != nil { + return err + } + encoder := json.NewEncoder(jsonFile) + decoder := json.NewDecoder(jsonData) + for decoder.More() { + var pkg *goListPackage + if err := decoder.Decode(&pkg); err != nil { + return err + } + if err := encoder.Encode(flatPackageForStd(cloneBase, pkg)); err != nil { + return err + } + } + + return nil } diff --git a/go/tools/builders/stdliblist_test.go b/go/tools/builders/stdliblist_test.go index bcd713be27..ec86f68164 100644 --- a/go/tools/builders/stdliblist_test.go +++ b/go/tools/builders/stdliblist_test.go @@ -26,10 +26,10 @@ func Test_stdliblist(t *testing.T) { if err != nil { t.Error("failed to find runfiles path") } + runFilesPath = filepath.Dir(filepath.Clean(runFilesPath)) test_args := []string{ fmt.Sprintf("-out=%s", outJSON), - fmt.Sprintf("-sdk=%s", "go_sdk"), - fmt.Sprintf("-wd=%s", filepath.Dir(filepath.Clean(runFilesPath))), + fmt.Sprintf("-sdk=%s", filepath.Join(runFilesPath, "go_sdk")), } err = stdliblist(test_args)