diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index f3c0d2a77..2a4f40c5a 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -3,3 +3,5 @@ #### Improvements 🧹 #### Bugfixes ⛑️ + +- Accept absolute paths again on the CLI. [#979](https://github.com/terrastruct/d2/pull/979) diff --git a/d2cli/main.go b/d2cli/main.go index 838b086a4..34533043c 100644 --- a/d2cli/main.go +++ b/d2cli/main.go @@ -154,14 +154,14 @@ func Run(ctx context.Context, ms *xmain.State) (err error) { } } if inputPath != "-" { - inputPath = filepath.Join(ms.PWD, inputPath) + inputPath = ms.AbsPath(inputPath) d, err := os.Stat(inputPath) if err == nil && d.IsDir() { inputPath = filepath.Join(inputPath, "index.d2") } } if outputPath != "-" { - outputPath = filepath.Join(ms.PWD, outputPath) + outputPath = ms.AbsPath(outputPath) } match := d2themescatalog.Find(*themeFlag) @@ -308,7 +308,7 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketc if filepath.Ext(outputPath) == ".pdf" { dur := time.Since(start) - ms.Log.Success.Printf("successfully compiled %s to %s in %s", inputPath, outputPath, dur) + ms.Log.Success.Printf("successfully compiled %s to %s in %s", ms.HumanPath(inputPath), ms.HumanPath(outputPath), dur) } return svg, true, nil @@ -383,7 +383,7 @@ func render(ctx context.Context, ms *xmain.State, compileDur time.Duration, plug return svg, err } dur := compileDur + time.Since(start) - ms.Log.Success.Printf("successfully compiled %s to %s in %s", inputPath, boardOutputPath, dur) + ms.Log.Success.Printf("successfully compiled %s to %s in %s", ms.HumanPath(inputPath), ms.HumanPath(boardOutputPath), dur) return svg, nil } diff --git a/d2cli/watch.go b/d2cli/watch.go index ee5662630..ca2280dc0 100644 --- a/d2cli/watch.go +++ b/d2cli/watch.go @@ -48,6 +48,7 @@ type watcherOpts struct { port string inputPath string outputPath string + pwd string bundle bool forceAppendix bool pw png.Playwright @@ -216,7 +217,7 @@ func (w *watcher) watchLoop(ctx context.Context) error { if err != nil { return err } - w.ms.Log.Info.Printf("compiling %v...", w.inputPath) + w.ms.Log.Info.Printf("compiling %v...", w.ms.HumanPath(w.inputPath)) w.requestCompile() eatBurstTimer := time.NewTimer(0) @@ -270,7 +271,7 @@ func (w *watcher) watchLoop(ctx context.Context) error { // misleading error. eatBurstTimer.Reset(time.Millisecond * 16) case <-eatBurstTimer.C: - w.ms.Log.Info.Printf("detected change in %v: recompiling...", w.inputPath) + w.ms.Log.Info.Printf("detected change in %v: recompiling...", w.ms.HumanPath(w.inputPath)) w.requestCompile() case err, ok := <-w.fw.Errors: if !ok { @@ -300,7 +301,7 @@ func (w *watcher) ensureAddWatch(ctx context.Context) (time.Time, error) { return mt, nil } if interval >= time.Second { - w.ms.Log.Error.Printf("failed to watch inputPath %q: %v (retrying in %v)", w.inputPath, err, interval) + w.ms.Log.Error.Printf("failed to watch inputPath %q: %v (retrying in %v)", w.ms.HumanPath(w.inputPath), err, interval) } tc.Reset(interval) diff --git a/e2etests-cli/main_test.go b/e2etests-cli/main_test.go index 0b666d8f0..05595d4d4 100644 --- a/e2etests-cli/main_test.go +++ b/e2etests-cli/main_test.go @@ -161,6 +161,16 @@ layers: { assert.Testdata(t, ".svg", stdout.Bytes()) }, }, + { + name: "abspath", + run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) { + writeFile(t, dir, "hello-world.d2", `x -> y`) + err := runTestMain(t, ctx, dir, env, filepath.Join(dir, "hello-world.d2")) + assert.Success(t, err) + svg := readFile(t, dir, "hello-world.svg") + assert.Testdata(t, ".svg", svg) + }, + }, } ctx := context.Background() diff --git a/e2etests-cli/testdata/TestCLI_E2E/abspath.exp.svg b/e2etests-cli/testdata/TestCLI_E2E/abspath.exp.svg new file mode 100644 index 000000000..7ef10b540 --- /dev/null +++ b/e2etests-cli/testdata/TestCLI_E2E/abspath.exp.svg @@ -0,0 +1,23 @@ +xy + + + diff --git a/go.mod b/go.mod index 587e097fa..653155887 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 gonum.org/v1/plot v0.12.0 nhooyr.io/websocket v1.8.7 - oss.terrastruct.com/util-go v0.0.0-20230303204624-090ad7923dcb + oss.terrastruct.com/util-go v0.0.0-20230305220024-ae08a694f221 ) require ( diff --git a/go.sum b/go.sum index acbac3377..666761c94 100644 --- a/go.sum +++ b/go.sum @@ -283,6 +283,6 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= -oss.terrastruct.com/util-go v0.0.0-20230303204624-090ad7923dcb h1:Zgb7Wf7mTpXmIiYv0bIJ8IULcdJVIxiRihnUvDP9YMQ= -oss.terrastruct.com/util-go v0.0.0-20230303204624-090ad7923dcb/go.mod h1:eMWv0sOtD9T2RUl90DLWfuShZCYp4NrsqNpI8eqO6U4= +oss.terrastruct.com/util-go v0.0.0-20230305220024-ae08a694f221 h1:LbO79j1CUB/mkmPSzLrA/EL4TRK0rjfAq+CejQapHyw= +oss.terrastruct.com/util-go v0.0.0-20230305220024-ae08a694f221/go.mod h1:eMWv0sOtD9T2RUl90DLWfuShZCYp4NrsqNpI8eqO6U4= rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4=