Skip to content

Commit 7f2e1f4

Browse files
authored
Merge pull request #2113 from alixander/abs-imp
d2ir: allow absolute imports
2 parents 0444389 + b070ab6 commit 7f2e1f4

3 files changed

Lines changed: 5 additions & 16 deletions

File tree

ci/release/changelogs/next.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
scope (e.g. to a sibling board at the scope its imported to) [#2075](https://github.com/terrastruct/d2/pull/2075)
66
- Autoformat: Reserved keywords are formatted to be lowercase [#2098](https://github.com/terrastruct/d2/pull/2098)
77
- Misc: characters in the unicode range for Latin-1 and geometric shapes are measured more accurately [#2100](https://github.com/terrastruct/d2/pull/2100)
8+
- Imports: can now import from absolute file paths [#2113](https://github.com/terrastruct/d2/pull/2113)
89

910
#### Improvements 🧹
1011

d2ir/import.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"io/fs"
55
"os"
66
"path"
7+
"path/filepath"
78
"strings"
89

910
"oss.terrastruct.com/d2/d2ast"
@@ -17,17 +18,13 @@ func (c *compiler) pushImportStack(imp *d2ast.Import) (string, bool) {
1718
return "", false
1819
}
1920
if len(c.importStack) > 0 {
20-
if path.IsAbs(impPath) {
21-
c.errorf(imp, "import paths must be relative")
22-
return "", false
23-
}
24-
2521
if path.Ext(impPath) != ".d2" {
2622
impPath += ".d2"
2723
}
2824

29-
// Imports are always relative to the importing file.
30-
impPath = path.Join(path.Dir(c.importStack[len(c.importStack)-1]), impPath)
25+
if !filepath.IsAbs(impPath) {
26+
impPath = path.Join(path.Dir(c.importStack[len(c.importStack)-1]), impPath)
27+
}
3128
}
3229

3330
for i, p := range c.importStack {

d2ir/import_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,6 @@ label: meow`,
252252
assert.ErrorString(t, err, `index.d2:1:1: failed to import "../x.d2": open ../x.d2: invalid argument`)
253253
},
254254
},
255-
{
256-
name: "absolute",
257-
run: func(t testing.TB) {
258-
_, err := compileFS(t, "index.d2", map[string]string{
259-
"index.d2": "...@/x.d2",
260-
})
261-
assert.ErrorString(t, err, `index.d2:1:1: import paths must be relative`)
262-
},
263-
},
264255
{
265256
name: "parse",
266257
run: func(t testing.TB) {

0 commit comments

Comments
 (0)