From 5df045ca789ef95d6231eed84667f52225ad899d Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 23 Sep 2024 07:15:27 -0600 Subject: [PATCH 1/2] d2ir: allow absolute imports --- ci/release/changelogs/next.md | 1 + d2ir/import.go | 10 +++------- d2ir/import_test.go | 9 --------- 3 files changed, 4 insertions(+), 16 deletions(-) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index c1d4e710b..460e4b017 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -5,6 +5,7 @@ scope (e.g. to a sibling board at the scope its imported to) [#2075](https://github.com/terrastruct/d2/pull/2075) - Autoformat: Reserved keywords are formatted to be lowercase [#2098](https://github.com/terrastruct/d2/pull/2098) - Misc: characters in the unicode range for Latin-1 and geometric shapes are measured more accurately [#2100](https://github.com/terrastruct/d2/pull/2100) +- Imports: can now import from absolute file paths [#2113](https://github.com/terrastruct/d2/pull/2113) #### Improvements 🧹 diff --git a/d2ir/import.go b/d2ir/import.go index c415cf138..2cde21ec8 100644 --- a/d2ir/import.go +++ b/d2ir/import.go @@ -17,17 +17,13 @@ func (c *compiler) pushImportStack(imp *d2ast.Import) (string, bool) { return "", false } if len(c.importStack) > 0 { - if path.IsAbs(impPath) { - c.errorf(imp, "import paths must be relative") - return "", false - } - if path.Ext(impPath) != ".d2" { impPath += ".d2" } - // Imports are always relative to the importing file. - impPath = path.Join(path.Dir(c.importStack[len(c.importStack)-1]), impPath) + if !path.IsAbs(impPath) { + impPath = path.Join(path.Dir(c.importStack[len(c.importStack)-1]), impPath) + } } for i, p := range c.importStack { diff --git a/d2ir/import_test.go b/d2ir/import_test.go index 10de29e2d..e9557b0ab 100644 --- a/d2ir/import_test.go +++ b/d2ir/import_test.go @@ -252,15 +252,6 @@ label: meow`, assert.ErrorString(t, err, `index.d2:1:1: failed to import "../x.d2": open ../x.d2: invalid argument`) }, }, - { - name: "absolute", - run: func(t testing.TB) { - _, err := compileFS(t, "index.d2", map[string]string{ - "index.d2": "...@/x.d2", - }) - assert.ErrorString(t, err, `index.d2:1:1: import paths must be relative`) - }, - }, { name: "parse", run: func(t testing.TB) { From b070ab66968b3e59f60fee3f9f583a2b3502edb5 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 23 Sep 2024 07:20:52 -0600 Subject: [PATCH 2/2] account for non-unix filepaths --- d2ir/import.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/d2ir/import.go b/d2ir/import.go index 2cde21ec8..6f593c5c7 100644 --- a/d2ir/import.go +++ b/d2ir/import.go @@ -4,6 +4,7 @@ import ( "io/fs" "os" "path" + "path/filepath" "strings" "oss.terrastruct.com/d2/d2ast" @@ -21,7 +22,7 @@ func (c *compiler) pushImportStack(imp *d2ast.Import) (string, bool) { impPath += ".d2" } - if !path.IsAbs(impPath) { + if !filepath.IsAbs(impPath) { impPath = path.Join(path.Dir(c.importStack[len(c.importStack)-1]), impPath) } }