Skip to content

Commit

Permalink
Fix windows path handling (#202)
Browse files Browse the repository at this point in the history
* fs.FS always uses "/" as the separator

* Account for Windows volumes

---------

Co-authored-by: Pieter Lazzaro <pieter.lazzaro@pureharvest.com.au>
  • Loading branch information
pieter-lazzaro and Pieter Lazzaro committed Jun 5, 2023
1 parent 664a6c1 commit d31c49f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
14 changes: 7 additions & 7 deletions schema/ridl/ridl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"io"
"io/fs"
"path/filepath"
"path"
"strconv"

"github.com/webrpc/webrpc/schema"
Expand Down Expand Up @@ -51,15 +51,15 @@ func (p *Parser) Parse() (*schema.WebRPCSchema, error) {
return s, nil
}

func (p *Parser) importRIDLFile(path string) (*schema.WebRPCSchema, error) {
func (p *Parser) importRIDLFile(filename string) (*schema.WebRPCSchema, error) {
for node := p; node != nil; node = node.parent {
if _, imported := node.imports[path]; imported {
return nil, fmt.Errorf("circular import %q in file %q", filepath.Base(path), p.path)
if _, imported := node.imports[filename]; imported {
return nil, fmt.Errorf("circular import %q in file %q", path.Base(filename), p.path)
}
node.imports[path] = struct{}{}
node.imports[filename] = struct{}{}
}

m := NewParser(p.fsys, path)
m := NewParser(p.fsys, filename)
m.parent = p
return m.Parse()
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func (p *Parser) parse() (*schema.WebRPCSchema, error) {

// imports
for _, line := range q.root.Imports() {
importPath := filepath.Join(filepath.Dir(p.path), line.Path().String())
importPath := path.Join(path.Dir(p.path), line.Path().String())

imported, err := p.importRIDLFile(importPath)
if err != nil {
Expand Down
14 changes: 12 additions & 2 deletions webrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,19 @@ func ParseSchemaFile(path string) (*schema.WebRPCSchema, error) {
case ".ridl":
// Use root FS to allow RIDL file imports from parent directories,
// ie. import ../../common.ridl.
rootFS := os.DirFS("/")

r := ridl.NewParser(rootFS, absolutePath[1:])
root := "/"

// Support Windows paths. Currently only supports paths on the same volume.
if volume := filepath.VolumeName(absolutePath); volume != "" {
root = volume + "/"
}

path := filepath.ToSlash(absolutePath[len(root):])

rootFS := os.DirFS(root)

r := ridl.NewParser(rootFS, path)
return r.Parse()

default:
Expand Down

0 comments on commit d31c49f

Please sign in to comment.