Skip to content

Commit bd0fc39

Browse files
Fix apiviewgo download paths (#8856) (#8883)
* apiviewgo downloads to os.TempDir instead of os.UserHomeDir
1 parent 9040deb commit bd0fc39

7 files changed

Lines changed: 69 additions & 12 deletions

File tree

src/go/cmd/download.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,14 @@ func mustEscape(modPath string) string {
139139
// downloadDir creates a directory to store downloaded module zips and source.
140140
// Callers are responsible for removing the directory when they're done with it.
141141
func downloadDir() (string, error) {
142-
root, err := os.UserHomeDir()
142+
root := os.TempDir()
143+
err := os.MkdirAll(root, 0700)
143144
if err != nil {
144-
root = os.TempDir()
145+
return "", fmt.Errorf("failed to create root directory %q for downloads: %w", root, err)
145146
}
146147
d, err := os.MkdirTemp(root, "apiviewgo")
147148
if err != nil {
148-
err = fmt.Errorf("failed to create download directory: %w", err)
149+
err = fmt.Errorf("failed to create download directory %q: %w", d, err)
149150
}
150151
return d, err
151152
}

src/go/cmd/module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func NewModule(dir string) (*Module, error) {
8989
return filepath.SkipDir
9090
}
9191
}
92-
p, err := NewPkg(path, m.ModFile.Module.Mod.Path)
92+
p, err := NewPkg(path, m.ModFile.Module.Mod.Path, dir)
9393
if err == nil {
9494
m.Packages[baseImportPath+p.Name()] = p
9595
} else if !errors.Is(err, ErrNoPackages) {

src/go/cmd/pkg.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ type Pkg struct {
5151
}
5252

5353
// NewPkg loads the package in the specified directory.
54-
// It's required there is only one package in the directory.
55-
func NewPkg(dir, modulePath string) (*Pkg, error) {
54+
//
55+
// - dir is the directory containing the package
56+
// - modulePath is the import path of the module containing the package
57+
// - moduleRoot is the root directory of the module on disk i.e., the directory containing its go.mod
58+
func NewPkg(dir, modulePath, moduleRoot string) (*Pkg, error) {
5659
pk := &Pkg{
5760
modulePath: modulePath,
5861
c: newContent(),
@@ -61,12 +64,8 @@ func NewPkg(dir, modulePath string) (*Pkg, error) {
6164
}
6265
modulePathWithoutVersion := strings.TrimSuffix(versionReg.ReplaceAllString(modulePath, "/"), "/")
6366
moduleName := filepath.Base(modulePathWithoutVersion)
64-
if _, after, found := strings.Cut(dir, moduleName); found {
65-
pk.relName = moduleName
66-
if after != "" && after[0] != '@' {
67-
pk.relName += after
68-
}
69-
pk.relName = strings.ReplaceAll(pk.relName, "\\", "/")
67+
if _, after, found := strings.Cut(dir, moduleRoot); found {
68+
pk.relName = strings.ReplaceAll(moduleName+after, "\\", "/")
7069
} else {
7170
return nil, errors.New(dir + " isn't part of module " + moduleName)
7271
}

src/go/cmd/pkg_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package cmd
5+
6+
import (
7+
"path/filepath"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestName(t *testing.T) {
14+
for _, test := range []struct {
15+
modulePath, moduleRoot, pkgPath, want string
16+
}{
17+
{
18+
modulePath: "test_package_name",
19+
moduleRoot: "testdata/test_package_name/test_package_name@v1.0.0",
20+
want: "test_package_name",
21+
},
22+
{
23+
modulePath: "test_package_name",
24+
moduleRoot: "testdata/test_package_name/test_package_name@v1.0.0",
25+
pkgPath: "subpackage",
26+
want: "test_package_name/subpackage",
27+
},
28+
{
29+
modulePath: "test_subpackage",
30+
moduleRoot: "testdata/test_subpackage",
31+
want: "test_subpackage",
32+
},
33+
{
34+
modulePath: "test_subpackage",
35+
moduleRoot: "testdata/test_subpackage",
36+
pkgPath: "subpackage",
37+
want: "test_subpackage/subpackage",
38+
},
39+
} {
40+
t.Run("", func(t *testing.T) {
41+
d, err := filepath.Abs(test.moduleRoot)
42+
require.NoError(t, err)
43+
p, err := NewPkg(filepath.Join(d, test.pkgPath), test.modulePath, d)
44+
require.NoError(t, err)
45+
require.Equal(t, test.want, p.Name())
46+
})
47+
}
48+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module test_package_name
2+
3+
go 1.18
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package subpackage
2+
3+
type S string
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package test_package_name
2+
3+
type S string

0 commit comments

Comments
 (0)