Skip to content

Commit 70b8a70

Browse files
authored
fix(gnodev): fix the use of filepath instead of path on windows (#3737)
fix #3722 Using `filepath` to work with the `path` creates some obvious issues on Windows, primarily because the `Clean` function replaces `/` with `\\`. This PR updates the usage of `filepath` to `path` where necessary. At some point, adapting path-specific tests on Windows would be very helpful. --- @Milosevic02, can you confirm that this fix resolves most of your issues with gnodev? **EDIT:** It seems that the fix is effective: #3722 (comment) --------- Signed-off-by: gfanton <[email protected]>
1 parent e9e04ea commit 70b8a70

File tree

18 files changed

+101
-68
lines changed

18 files changed

+101
-68
lines changed

contribs/gnodev/cmd/gnobro/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"net/url"
1212
"os"
1313
"os/signal"
14-
"path/filepath"
14+
gopath "path"
1515
"strings"
1616
"time"
1717

@@ -239,7 +239,6 @@ func runLocal(ctx context.Context, gnocl *gnoclient.Client, cfg *broCfg, bcfg br
239239
)
240240

241241
var errgs errgroup.Group
242-
243242
if cfg.dev {
244243
devpoint, err := getDevEndpoint(cfg)
245244
if err != nil {
@@ -453,7 +452,7 @@ func ValidatePathCommandMiddleware(pathPrefix string) wish.Middleware {
453452
return
454453
case 1: // check for valid path
455454
path := cmd[0]
456-
if strings.HasPrefix(path, pathPrefix) && filepath.Clean(path) == path {
455+
if strings.HasPrefix(path, pathPrefix) && gopath.Clean(path) == path {
457456
s.Context().SetValue("path", path)
458457
next(s)
459458
return

contribs/gnodev/cmd/gnodev/command_staging.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"flag"
6+
"path"
67
"path/filepath"
78

89
"github.com/gnolang/gno/contribs/gnodev/pkg/packages"
@@ -28,7 +29,7 @@ var defaultStagingOptions = AppConfig{
2829
interactive: false,
2930
unsafeAPI: false,
3031
lazyLoader: false,
31-
paths: filepath.Join(DefaultDomain, "/**"), // Load every package under the main domain},
32+
paths: path.Join(DefaultDomain, "/**"), // Load every package under the main domain},
3233

3334
// As we have no reason to configure this yet, set this to random port
3435
// to avoid potential conflict with other app

contribs/gnodev/cmd/gnodev/setup_loader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"log/slog"
6+
gopath "path"
67
"path/filepath"
78
"regexp"
89
"strings"
@@ -103,5 +104,5 @@ func guessPath(cfg *AppConfig, dir string) (path string) {
103104
}
104105

105106
rname := reInvalidChar.ReplaceAllString(filepath.Base(dir), "-")
106-
return filepath.Join(cfg.chainDomain, "/r/dev/", rname)
107+
return gopath.Join(cfg.chainDomain, "/r/dev/", rname)
107108
}

contribs/gnodev/pkg/browser/model.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"log/slog"
9-
"path/filepath"
9+
gopath "path"
1010
"regexp"
1111
"strings"
1212

@@ -576,5 +576,5 @@ func (m *model) getCurrentPath() string {
576576
return m.urlPrefix
577577
}
578578

579-
return filepath.Join(m.urlPrefix, path)
579+
return gopath.Join(m.urlPrefix, path)
580580
}

contribs/gnodev/pkg/browser/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package browser
22

33
import (
4-
"path/filepath"
4+
gopath "path"
55
"strings"
66

77
"github.com/gnolang/gno/gno.land/pkg/gnoweb"
@@ -27,7 +27,7 @@ func cleanupRealmPath(prefix, realm string) string {
2727
// trim any slash
2828
path = strings.TrimPrefix(path, "/")
2929
// clean up path
30-
path = filepath.Clean(path)
30+
path = gopath.Clean(path)
3131

3232
return path
3333
}

contribs/gnodev/pkg/dev/query_path.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dev
33
import (
44
"fmt"
55
"net/url"
6-
"path/filepath"
6+
"path"
77

88
"github.com/gnolang/gno/contribs/gnodev/pkg/address"
99
"github.com/gnolang/gno/tm2/pkg/crypto"
@@ -24,7 +24,7 @@ func ResolveQueryPath(bk *address.Book, query string) (QueryPath, error) {
2424
return qpath, fmt.Errorf("malformed path/query: %w", err)
2525
}
2626

27-
qpath.Path = filepath.Clean(upath.Path)
27+
qpath.Path = path.Clean(upath.Path)
2828

2929
// Check for creator option
3030
creator := upath.Query().Get("creator")

contribs/gnodev/pkg/events/events.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ type PackagesUpdate struct {
4141
}
4242

4343
type PackageUpdate struct {
44-
Package string `json:"package"`
45-
Files []string `json:"files"`
44+
PackageDir string `json:"package"`
45+
Files []string `json:"files"`
4646
}
4747

4848
func (PackagesUpdate) Type() Type { return EvtPackagesUpdate }

contribs/gnodev/pkg/packages/loader_glob.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"go/token"
66
"io/fs"
77
"os"
8+
"path"
89
"path/filepath"
910
"strings"
1011
)
@@ -33,7 +34,7 @@ func (l GlobLoader) MatchPaths(globs ...string) ([]string, error) {
3334

3435
mpaths := []string{}
3536
for _, input := range globs {
36-
cleanInput := filepath.Clean(input)
37+
cleanInput := path.Clean(input)
3738
gpath, err := Parse(cleanInput)
3839
if err != nil {
3940
return nil, fmt.Errorf("invalid glob path %q: %w", input, err)
@@ -45,18 +46,20 @@ func (l GlobLoader) MatchPaths(globs ...string) ([]string, error) {
4546
continue
4647
}
4748

48-
// root := filepath.Join(l.Root, base)
4949
root := l.Root
5050
err = filepath.WalkDir(root, func(dirpath string, d fs.DirEntry, err error) error {
5151
if err != nil {
5252
return err
5353
}
5454

55-
relPath, relErr := filepath.Rel(root, dirpath)
56-
if relErr != nil {
57-
return relErr
55+
path, err := filepath.Rel(root, dirpath)
56+
if err != nil {
57+
return err
5858
}
5959

60+
// normalize filepath to path
61+
path = NormalizeFilepathToPath(path)
62+
6063
if !d.IsDir() {
6164
return nil
6265
}
@@ -65,8 +68,8 @@ func (l GlobLoader) MatchPaths(globs ...string) ([]string, error) {
6568
return fs.SkipDir
6669
}
6770

68-
if gpath.Match(relPath) {
69-
mpaths = append(mpaths, relPath)
71+
if gpath.Match(path) {
72+
mpaths = append(mpaths, path)
7073
}
7174

7275
return nil

contribs/gnodev/pkg/packages/resolver_local.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package packages
33
import (
44
"fmt"
55
"go/token"
6+
"path"
67
"path/filepath"
78
"strings"
89
)
@@ -20,7 +21,7 @@ func NewLocalResolver(path, dir string) *LocalResolver {
2021
}
2122

2223
func (r *LocalResolver) Name() string {
23-
return fmt.Sprintf("local<%s>", filepath.Base(r.Dir))
24+
return fmt.Sprintf("local<%s>", path.Base(r.Dir))
2425
}
2526

2627
func (r LocalResolver) IsValid() bool {

contribs/gnodev/pkg/packages/resolver_remote.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
"go/parser"
88
"go/token"
9-
"path/filepath"
9+
gopath "path"
1010
"strings"
1111

1212
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
@@ -56,7 +56,7 @@ func (res *remoteResolver) Resolve(fset *token.FileSet, path string) (*Package,
5656
files := bytes.Split(qres.Response.Data, []byte{'\n'})
5757
for _, filename := range files {
5858
fname := string(filename)
59-
fpath := filepath.Join(path, fname)
59+
fpath := gopath.Join(path, fname)
6060
qres, err := res.RPCClient.ABCIQuery(qpath, []byte(fpath))
6161
if err != nil {
6262
return nil, fmt.Errorf("unable to query path")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package packages
5+
6+
// NormalizeFilepathToPath normalize path an unix like path
7+
func NormalizeFilepathToPath(path string) string {
8+
return path
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build windows
2+
// +build windows
3+
4+
package packages
5+
6+
import "strings"
7+
8+
// NormalizeFilepathToPath normalize path an unix like path
9+
func NormalizeFilepathToPath(path string) string {
10+
return strings.ReplaceAll(path, "\\", "/")
11+
}

contribs/gnodev/pkg/proxy/path_interceptor.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"log/slog"
1111
"net"
1212
"net/http"
13-
"path/filepath"
13+
gopath "path"
1414
"strings"
1515
"sync"
1616

@@ -220,7 +220,11 @@ func (upaths uniqPaths) list() []string {
220220
return paths
221221
}
222222

223-
func (upaths uniqPaths) add(path string) { upaths[path] = struct{}{} }
223+
// Add a path to
224+
func (upaths uniqPaths) add(path string) {
225+
path = cleanupPath(path)
226+
upaths[path] = struct{}{}
227+
}
224228

225229
// handleRequest parses and processes the RPC request body.
226230
func (proxy *PathInterceptor) handleRequest(body []byte) error {
@@ -312,13 +316,6 @@ func handleQuery(path string, data []byte, upaths uniqPaths) error {
312316

313317
case "vm/qrender", "vm/qfile", "vm/qfuncs", "vm/qeval":
314318
path, _, _ := strings.Cut(string(data), ":") // Cut arguments out
315-
path = filepath.Clean(path)
316-
317-
// If path is a file, grab the directory instead
318-
if ext := filepath.Ext(path); ext != "" {
319-
path = filepath.Dir(path)
320-
}
321-
322319
upaths.add(path)
323320
return nil
324321

@@ -328,3 +325,13 @@ func handleQuery(path string, data []byte, upaths uniqPaths) error {
328325

329326
// XXX: handle more cases
330327
}
328+
329+
func cleanupPath(path string) string {
330+
path = gopath.Clean(path)
331+
// If path is a file, grab the directory instead
332+
if ext := gopath.Ext(path); ext != "" {
333+
path = gopath.Dir(path)
334+
}
335+
336+
return path
337+
}

contribs/gnodev/pkg/proxy/path_interceptor_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package proxy_test
33
import (
44
"net"
55
"net/http"
6+
"path"
67
"path/filepath"
78
"testing"
89

@@ -86,7 +87,7 @@ func TestProxy(t *testing.T) {
8687
cli, err := client.NewHTTPClient(interceptor.TargetAddress())
8788
require.NoError(t, err)
8889

89-
res, err := cli.ABCIQuery("vm/qfile", []byte(filepath.Join(targetPath, "foo.gno")))
90+
res, err := cli.ABCIQuery("vm/qfile", []byte(path.Join(targetPath, "foo.gno")))
9091
require.NoError(t, err)
9192
assert.Nil(t, res.Response.Error)
9293

0 commit comments

Comments
 (0)