Skip to content

Commit

Permalink
Merge pull request terrastruct#2113 from alixander/abs-imp
Browse files Browse the repository at this point in the history
d2ir: allow absolute imports
  • Loading branch information
alixander authored Sep 27, 2024
2 parents 0444389 + b070ab6 commit 7f2e1f4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 16 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 🧹

Expand Down
11 changes: 4 additions & 7 deletions d2ir/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/fs"
"os"
"path"
"path/filepath"
"strings"

"oss.terrastruct.com/d2/d2ast"
Expand All @@ -17,17 +18,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 !filepath.IsAbs(impPath) {
impPath = path.Join(path.Dir(c.importStack[len(c.importStack)-1]), impPath)
}
}

for i, p := range c.importStack {
Expand Down
9 changes: 0 additions & 9 deletions d2ir/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7f2e1f4

Please sign in to comment.