d2cli: Pass path for imports correctly
This commit is contained in:
parent
ad21f1fbea
commit
111759f6bd
4 changed files with 26 additions and 4 deletions
|
|
@ -322,6 +322,7 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, rende
|
|||
Ruler: ruler,
|
||||
ThemeID: renderOpts.ThemeID,
|
||||
FontFamily: fontFamily,
|
||||
InputPath: inputPath,
|
||||
}
|
||||
if renderOpts.Sketch {
|
||||
opts.FontFamily = go2.Pointer(d2fonts.HandDrawn)
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ type CompileOptions struct {
|
|||
FS fs.FS
|
||||
}
|
||||
|
||||
func Compile(path string, r io.RuneReader, opts *CompileOptions) (*d2graph.Graph, error) {
|
||||
func Compile(p string, r io.RuneReader, opts *CompileOptions) (*d2graph.Graph, error) {
|
||||
if opts == nil {
|
||||
opts = &CompileOptions{}
|
||||
}
|
||||
|
|
@ -36,7 +36,7 @@ func Compile(path string, r io.RuneReader, opts *CompileOptions) (*d2graph.Graph
|
|||
opts.FS = os.DirFS("/")
|
||||
}
|
||||
|
||||
ast, err := d2parser.Parse(path, r, &d2parser.ParseOptions{
|
||||
ast, err := d2parser.Parse(p, r, &d2parser.ParseOptions{
|
||||
UTF16: opts.UTF16,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package d2ir
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
|
|
@ -82,7 +83,25 @@ func (c *compiler) __import(imp *d2ast.Import) (*Map, bool) {
|
|||
return ir, true
|
||||
}
|
||||
|
||||
f, err := c.fs.Open(impPath)
|
||||
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:]
|
||||
}
|
||||
}
|
||||
|
||||
f, err := c.fs.Open(p)
|
||||
if err != nil {
|
||||
c.errorf(imp, "failed to import %q: %v", impPath, err)
|
||||
return nil, false
|
||||
|
|
|
|||
|
|
@ -31,6 +31,8 @@ type CompileOptions struct {
|
|||
// - pre-measured (web setting)
|
||||
// TODO maybe some will want to configure code font too, but that's much lower priority
|
||||
FontFamily *d2fonts.FontFamily
|
||||
|
||||
InputPath string
|
||||
}
|
||||
|
||||
func Compile(ctx context.Context, input string, opts *CompileOptions) (*d2target.Diagram, *d2graph.Graph, error) {
|
||||
|
|
@ -38,7 +40,7 @@ func Compile(ctx context.Context, input string, opts *CompileOptions) (*d2target
|
|||
opts = &CompileOptions{}
|
||||
}
|
||||
|
||||
g, err := d2compiler.Compile("", strings.NewReader(input), &d2compiler.CompileOptions{
|
||||
g, err := d2compiler.Compile(opts.InputPath, strings.NewReader(input), &d2compiler.CompileOptions{
|
||||
UTF16: opts.UTF16,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Reference in a new issue