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

Fix apiviewgo download paths (#8856) #8883

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/go/cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,14 @@ func mustEscape(modPath string) string {
// downloadDir creates a directory to store downloaded module zips and source.
// Callers are responsible for removing the directory when they're done with it.
func downloadDir() (string, error) {
root, err := os.UserHomeDir()
root := os.TempDir()
err := os.MkdirAll(root, 0700)
if err != nil {
root = os.TempDir()
return "", fmt.Errorf("failed to create root directory %q for downloads: %w", root, err)
}
d, err := os.MkdirTemp(root, "apiviewgo")
if err != nil {
err = fmt.Errorf("failed to create download directory: %w", err)
err = fmt.Errorf("failed to create download directory %q: %w", d, err)
}
return d, err
}
2 changes: 1 addition & 1 deletion src/go/cmd/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func NewModule(dir string) (*Module, error) {
return filepath.SkipDir
}
}
p, err := NewPkg(path, m.ModFile.Module.Mod.Path)
p, err := NewPkg(path, m.ModFile.Module.Mod.Path, dir)
if err == nil {
m.Packages[baseImportPath+p.Name()] = p
} else if !errors.Is(err, ErrNoPackages) {
Expand Down
15 changes: 7 additions & 8 deletions src/go/cmd/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ type Pkg struct {
}

// NewPkg loads the package in the specified directory.
// It's required there is only one package in the directory.
func NewPkg(dir, modulePath string) (*Pkg, error) {
//
// - dir is the directory containing the package
// - modulePath is the import path of the module containing the package
// - moduleRoot is the root directory of the module on disk i.e., the directory containing its go.mod
func NewPkg(dir, modulePath, moduleRoot string) (*Pkg, error) {
pk := &Pkg{
modulePath: modulePath,
c: newContent(),
Expand All @@ -61,12 +64,8 @@ func NewPkg(dir, modulePath string) (*Pkg, error) {
}
modulePathWithoutVersion := strings.TrimSuffix(versionReg.ReplaceAllString(modulePath, "/"), "/")
moduleName := filepath.Base(modulePathWithoutVersion)
if _, after, found := strings.Cut(dir, moduleName); found {
pk.relName = moduleName
if after != "" && after[0] != '@' {
pk.relName += after
}
pk.relName = strings.ReplaceAll(pk.relName, "\\", "/")
if _, after, found := strings.Cut(dir, moduleRoot); found {
pk.relName = strings.ReplaceAll(moduleName+after, "\\", "/")
} else {
return nil, errors.New(dir + " isn't part of module " + moduleName)
}
Expand Down
48 changes: 48 additions & 0 deletions src/go/cmd/pkg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package cmd

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestName(t *testing.T) {
for _, test := range []struct {
modulePath, moduleRoot, pkgPath, want string
}{
{
modulePath: "test_package_name",
moduleRoot: "testdata/test_package_name/[email protected]",
want: "test_package_name",
},
{
modulePath: "test_package_name",
moduleRoot: "testdata/test_package_name/[email protected]",
pkgPath: "subpackage",
want: "test_package_name/subpackage",
},
{
modulePath: "test_subpackage",
moduleRoot: "testdata/test_subpackage",
want: "test_subpackage",
},
{
modulePath: "test_subpackage",
moduleRoot: "testdata/test_subpackage",
pkgPath: "subpackage",
want: "test_subpackage/subpackage",
},
} {
t.Run("", func(t *testing.T) {
d, err := filepath.Abs(test.moduleRoot)
require.NoError(t, err)
p, err := NewPkg(filepath.Join(d, test.pkgPath), test.modulePath, d)
require.NoError(t, err)
require.Equal(t, test.want, p.Name())
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module test_package_name

go 1.18
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package subpackage

type S string
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package test_package_name

type S string