d2ir: allow absolute imports

This commit is contained in:
Alexander Wang 2024-09-23 07:15:27 -06:00
parent 0444389db9
commit 5df045ca78
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
3 changed files with 4 additions and 16 deletions

View file

@ -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) 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) - 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) - 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 🧹 #### Improvements 🧹

View file

@ -17,18 +17,14 @@ func (c *compiler) pushImportStack(imp *d2ast.Import) (string, bool) {
return "", false return "", false
} }
if len(c.importStack) > 0 { if len(c.importStack) > 0 {
if path.IsAbs(impPath) {
c.errorf(imp, "import paths must be relative")
return "", false
}
if path.Ext(impPath) != ".d2" { if path.Ext(impPath) != ".d2" {
impPath += ".d2" impPath += ".d2"
} }
// Imports are always relative to the importing file. if !path.IsAbs(impPath) {
impPath = path.Join(path.Dir(c.importStack[len(c.importStack)-1]), impPath) impPath = path.Join(path.Dir(c.importStack[len(c.importStack)-1]), impPath)
} }
}
for i, p := range c.importStack { for i, p := range c.importStack {
if impPath == p { if impPath == p {

View file

@ -252,15 +252,6 @@ label: meow`,
assert.ErrorString(t, err, `index.d2:1:1: failed to import "../x.d2": open ../x.d2: invalid argument`) 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", name: "parse",
run: func(t testing.TB) { run: func(t testing.TB) {