From dc1536e8fbf26319f8f959581580f8eaa464b69e Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Fri, 9 Jun 2023 19:31:57 -0700 Subject: [PATCH 1/2] d2format: path.Clean imports --- d2format/format.go | 6 ++++++ d2format/format_test.go | 8 ++++++++ d2parser/parse.go | 4 +--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/d2format/format.go b/d2format/format.go index 84df2e945a..b915c26380 100644 --- a/d2format/format.go +++ b/d2format/format.go @@ -216,6 +216,12 @@ func (p *printer) _import(i *d2ast.Import) { p.sb.WriteString(pre) p.sb.WriteRune('/') } + if len(i.Path) > 0 { + i2 := *i + i2.Path = append([]*d2ast.StringBox{}, i.Path...) + i2.Path[0] = d2ast.RawStringBox(path.Clean(i.Path[0].Unbox().ScalarString()), true) + i = &i2 + } p.path(i.Path) } diff --git a/d2format/format_test.go b/d2format/format_test.go index 63e185dcf1..3c957129dc 100644 --- a/d2format/format_test.go +++ b/d2format/format_test.go @@ -649,6 +649,14 @@ x: @./file x: @../file `, exp: `x: @../file +`, + }, + { + name: "import/4", + in: ` +x: @"x/../file" +`, + exp: `x: @file `, }, } diff --git a/d2parser/parse.go b/d2parser/parse.go index 7970f1fa62..fa64531a31 100644 --- a/d2parser/parse.go +++ b/d2parser/parse.go @@ -1742,9 +1742,7 @@ func (p *parser) parseImport(spread bool) *d2ast.Import { if k.Path[0].UnquotedString != nil && len(k.Path) > 1 && k.Path[1].UnquotedString != nil && k.Path[1].Unbox().ScalarString() == "d2" { k.Path = append(k.Path[:1], k.Path[2:]...) } - if k != nil { - imp.Path = k.Path - } + imp.Path = k.Path return imp } From 1f891a3c9e197fa2ebda6c49e391f5e57ec9fafe Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Fri, 9 Jun 2023 19:45:09 -0700 Subject: [PATCH 2/2] d2ir: Fix for windows Closes #1388 --- d2compiler/compile.go | 4 ---- d2ir/import.go | 25 +++++++------------------ 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 0825166d64..8c06fd4b32 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -6,7 +6,6 @@ import ( "io" "io/fs" "net/url" - "os" "strconv" "strings" @@ -32,9 +31,6 @@ func Compile(p string, r io.RuneReader, opts *CompileOptions) (*d2graph.Graph, e if opts == nil { opts = &CompileOptions{} } - if opts.FS == nil { - opts.FS = os.DirFS("/") - } ast, err := d2parser.Parse(p, r, &d2parser.ParseOptions{ UTF16: opts.UTF16, diff --git a/d2ir/import.go b/d2ir/import.go index 2a58a0622b..aaad0d035c 100644 --- a/d2ir/import.go +++ b/d2ir/import.go @@ -2,6 +2,7 @@ package d2ir import ( "bufio" + "io/fs" "os" "path" "strings" @@ -85,25 +86,13 @@ func (c *compiler) __import(imp *d2ast.Import) (*Map, bool) { return ir, true } - p := path.Clean(impPath) - if path.IsAbs(p) { - // Path cannot be absolute. DirFS does not accept absolute paths. We strip off the leading - // slash to make it relative to the root. - p = p[1:] - } else if c.fs == os.DirFS("/") { - wd, err := os.Getwd() - if err != nil { - c.errorf(imp, "failed to import %q: %v", impPath, err) - return nil, false - } - p = path.Join(wd, p) - // See above explanation. - if path.IsAbs(p) { - p = p[1:] - } + var f fs.File + var err error + if c.fs == nil { + f, err = os.Open(impPath) + } else { + f, err = c.fs.Open(impPath) } - - f, err := c.fs.Open(p) if err != nil { c.errorf(imp, "failed to import %q: %v", impPath, err) return nil, false