| 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/ |
| 6 | |
| 7 | import ( |
| 8 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/" |
| 9 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 10 | |
| 11 | "golang.org/x/tools/internal/gocommandscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/" |
| 12 | ) |
| 13 | |
| 14 | // determineRootDirs returns a mapping from absolute directories that could |
| 15 | // contain code to their corresponding import path prefixes. |
| 16 | func (state *golistStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#) determineRootDirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#determineRootDirs().() (map[string]string, error) { |
| 17 | env, err := state.getEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#getEnv().() |
| 18 | if err != nil { |
| 19 | return nil, err |
| 20 | } |
| 21 | if env["GOMOD"] != "" { |
| 22 | state.rootsOncescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#rootsOnce..Doscip-go gomod github.com/golang/go/src go1.25.0 sync/Once#Do().(func() { |
| 23 | state.rootDirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#rootDirs., state.rootDirsErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#rootDirsError. = state.determineRootDirsModulesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#determineRootDirsModules().() |
| 24 | }) |
| 25 | } else { |
| 26 | state.rootsOncescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#rootsOnce..Doscip-go gomod github.com/golang/go/src go1.25.0 sync/Once#Do().(func() { |
| 27 | state.rootDirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#rootDirs., state.rootDirsErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#rootDirsError. = state.determineRootDirsGOPATHscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#determineRootDirsGOPATH().() |
| 28 | }) |
| 29 | } |
| 30 | return state.rootDirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#rootDirs., state.rootDirsErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#rootDirsError. |
| 31 | } |
| 32 | |
| 33 | func (state *golistStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#) determineRootDirsModulesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#determineRootDirsModules().() (map[string]string, error) { |
| 34 | // List all of the modules--the first will be the directory for the main |
| 35 | // module. Any replaced modules will also need to be treated as roots. |
| 36 | // Editing files in the module cache isn't a great idea, so we don't |
| 37 | // plan to ever support that. |
| 38 | out, err := state.invokeGoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#invokeGo().("list", "-m", "-json", "all") |
| 39 | if err != nil { |
| 40 | // 'go list all' will fail if we're outside of a module and |
| 41 | // GO111MODULE=on. Try falling back without 'all'. |
| 42 | var innerErr error |
| 43 | out, innerErr = state.invokeGoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#invokeGo().("list", "-m", "-json") |
| 44 | if innerErr != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | } |
| 48 | roots := map[string]string{} |
| 49 | modules := map[string]string{} |
| 50 | var i int |
| 51 | for dec := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.NewDecoderscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/NewDecoder().(out); dec.Morescip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Decoder#More().(); { |
| 52 | mod := new(gocommandscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/.ModuleJSONscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#) |
| 53 | if err := dec.Decodescip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Decoder#Decode().(mod); err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | if mod.Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Dir. != "" && mod.Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Path. != "" { |
| 57 | // This is a valid module; add it to the map. |
| 58 | absDir, err := state.cfgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#cfg..absscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#abs().(mod.Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Dir.) |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | modules[absDir] = mod.Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Path. |
| 63 | // The first result is the main module. |
| 64 | if i == 0 || mod.Replacescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Replace. != nil && mod.Replacescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Replace..Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Path. != "" { |
| 65 | roots[absDir] = mod.Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Path. |
| 66 | } |
| 67 | } |
| 68 | i++ |
| 69 | } |
| 70 | return roots, nil |
| 71 | } |
| 72 | |
| 73 | func (state *golistStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#) determineRootDirsGOPATHscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#determineRootDirsGOPATH().() (map[string]string, error) { |
| 74 | m := map[string]string{} |
| 75 | for _, dir := range filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.SplitListscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/SplitList().(state.mustGetEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/golistState#mustGetEnv().()["GOPATH"]) { |
| 76 | absDir, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Abs().(dir) |
| 77 | if err != nil { |
| 78 | return nil, err |
| 79 | } |
| 80 | m[filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(absDir, "src")] = "" |
| 81 | } |
| 82 | return m, nil |
| 83 | } |