Skip to content

Commit f413e17

Browse files
committed
Go: Resolve substed drives on Windows
1 parent a4c8722 commit f413e17

7 files changed

Lines changed: 110 additions & 4 deletions

File tree

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ go_sdk.download(version = "1.26.5")
280280

281281
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
282282
go_deps.from_file(go_mod = "//go/extractor:go.mod")
283-
use_repo(go_deps, "com_github_stretchr_testify", "org_golang_x_mod", "org_golang_x_tools")
283+
use_repo(go_deps, "com_github_stretchr_testify", "org_golang_x_mod", "org_golang_x_sys", "org_golang_x_tools")
284284

285285
ripunzip_archive = use_repo_rule("//misc/ripunzip:ripunzip.bzl", "ripunzip_archive")
286286

go/extractor/extractor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ func normalizedPath(ast *ast.File, fset *token.FileSet) string {
770770
if err != nil {
771771
return file
772772
}
773-
return path
773+
return util.ResolvePath(path)
774774
}
775775

776776
// extractFile extracts AST information for the given file

go/extractor/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ require (
1313
golang.org/x/tools v0.48.0
1414
)
1515

16-
require github.com/stretchr/testify v1.11.1
16+
require (
17+
github.com/stretchr/testify v1.11.1
18+
golang.org/x/sys v0.47.0
19+
)
1720

1821
require (
1922
github.com/davecgh/go-spew v1.1.1 // indirect

go/extractor/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ golang.org/x/mod v0.38.0 h1:MECBjubtXD7yj4HrhIUcywNaGeNVUdfVnxmPajOk4yk=
1010
golang.org/x/mod v0.38.0/go.mod h1:V6Xz0pq8TQ3dGqVQ1FVHuelZpAL0uNhSkk9ogYP3c40=
1111
golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
1212
golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
13+
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
14+
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
1315
golang.org/x/tools v0.48.0 h1:3+hClM1aLL5mjMKm5ovokw9epgRXPuu2tILgismM6RE=
1416
golang.org/x/tools v0.48.0/go.mod h1:08xX0orndb/F7jJxGDicx061tyd5pcMto75YMAXr6lk=
1517
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

go/extractor/util/BUILD.bazel

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/extractor/util/subst_other.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//go:build !windows
2+
3+
package util
4+
5+
// ResolvePath is a no-op on non-Windows platforms.
6+
func ResolvePath(path string) string { return path }

go/extractor/util/subst_windows.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//go:build windows
2+
3+
package util
4+
5+
import (
6+
"os"
7+
"path/filepath"
8+
"syscall"
9+
"unsafe"
10+
11+
"golang.org/x/sys/windows"
12+
)
13+
14+
var (
15+
dll *syscall.DLL
16+
procResolve *syscall.Proc
17+
procFree *syscall.Proc
18+
available bool
19+
)
20+
21+
func init() {
22+
dist := os.Getenv("CODEQL_DIST")
23+
if dist == "" {
24+
return
25+
}
26+
dllPath := filepath.Join(dist, "tools", "win64", "canonicalize.dll")
27+
d, err := syscall.LoadDLL(dllPath)
28+
if err != nil {
29+
return
30+
}
31+
p, err := d.FindProc("resolve_subst")
32+
if err != nil {
33+
return
34+
}
35+
f, _ := d.FindProc("resolve_subst_free")
36+
dll = d
37+
procResolve = p
38+
procFree = f
39+
available = true
40+
}
41+
42+
// If "path" is an absolute path starting with a "subst"ed drive letter, return an
43+
// equivalent path with the drive letter replaced by its target. Otherwise return
44+
// "path" unchanged.
45+
func ResolvePath(path string) string {
46+
if len(path) < 3 {
47+
return path
48+
}
49+
if path[1] != ':' {
50+
return path
51+
}
52+
if path[2] != '\\' && path[2] != '/' {
53+
return path
54+
}
55+
c := path[0]
56+
if !((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
57+
return path
58+
}
59+
60+
resolved, ok := resolveDrive(path[:3])
61+
if !ok {
62+
return path
63+
}
64+
return resolved + path[2:]
65+
}
66+
67+
// Given a drive root like "X:\" (or "X:/"), returns the path that drive is
68+
// "subst"ed to. Returns false if the drive is not "subst"ed or an error occurred.
69+
func resolveDrive(driveRoot string) (string, bool) {
70+
if !available {
71+
return "", false
72+
}
73+
driveBytes, err := windows.ByteSliceFromString(driveRoot)
74+
if err != nil {
75+
return "", false
76+
}
77+
ret, _, _ := procResolve.Call(uintptr(unsafe.Pointer(&driveBytes[0])))
78+
if ret == 0 {
79+
return "", false
80+
}
81+
result := windows.BytePtrToString((*byte)(unsafe.Pointer(ret)))
82+
if procFree != nil {
83+
procFree.Call(ret)
84+
}
85+
return result, true
86+
}

0 commit comments

Comments
 (0)