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

use go list to obtain the standard library's package mapping #11910

Closed
tdyas opened this issue Apr 14, 2021 · 0 comments · Fixed by #12368
Closed

use go list to obtain the standard library's package mapping #11910

tdyas opened this issue Apr 14, 2021 · 0 comments · Fixed by #12368
Labels
backend: Go Go backend-related issues

Comments

@tdyas
Copy link
Contributor

tdyas commented Apr 14, 2021

The experiment Go backend currently manually scans the standard library to find packages. The go list command already provides this information in a standard way without having to deal with filesystem specifics.

The grok_goroot rule should invoke go list -json std (via a GoToolRunRequest once #11909 is implemented) and then use the output to construct a mapping of import path to package file.

The JSON output looks similar to:

{
        "Dir": "/Users/XXX/homebrew/Cellar/go/1.16.3/libexec/src/archive/tar",
        "ImportPath": "archive/tar",
        "Name": "tar",
        "Doc": "Package tar implements access to tar archives.",
        "Target": "/Users/tdyas/.toolchain/homebrew/Cellar/go/1.16.3/libexec/pkg/darwin_amd64/archive/tar.a",
        "Root": "/Users/tdyas/.toolchain/homebrew/Cellar/go/1.16.3/libexec",
        "Match": [
                "std"
        ],
        "Goroot": true,
        "Standard": true,
        "GoFiles": [
                "common.go",
                "format.go",
                "reader.go",
                "stat_actime2.go",
                "stat_unix.go",
                "strconv.go",
                "writer.go"
        ],
        "IgnoredGoFiles": [
                "stat_actime1.go"
        ],
        "Imports": [...],
        "Deps": [...],
        "TestGoFiles": [...],
        "TestImports": [...],
        "XTestGoFiles": [...],
        "XTestImports": [...],
}

The ImportPath and Target attributes should then be used to construct the mapping of import path to package file.

@tdyas tdyas added the backend: Go Go backend-related issues label Apr 14, 2021
tdyas pushed a commit that referenced this issue Jul 17, 2021
## Problem

As described in #11910, the current version of the experimental Go plugin parses the directory structure of the Go distribution to determine the imports provided by the standard library. `go list` already provides this information in JSON form when asked. This was also how the v1 plugin obtained the same information. 

## Solution

Use `go list` to obtain import paths for the standard library. This PR introduces the base class `ResolvedImportPaths` which will be inherited by all types that represent import path mappings. The first use of this base class is for the `ResolvedImportPathsForGoLangDistribution` dataclass which represents analysis of the Go standard library.

## Testing

Added integration test for the import analysis and the first-party build integration test continues to pass.

Closes #11910.

[ci skip-rust]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend: Go Go backend-related issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant