This commit is contained in:
Alexander Wang 2025-04-03 19:52:41 -06:00
parent 9ea0101dbd
commit 1acd6ef34a
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
3 changed files with 41 additions and 104 deletions

View file

@ -1732,6 +1732,10 @@ environment: {
} }
`, `,
}, },
assertions: func(t *testing.T, g *d2graph.Graph) {
assert.Equal(t, 1, len(g.Objects))
assert.Equal(t, "k", g.Objects[0].AbsID())
},
}, },
{ {
name: "import-connections", name: "import-connections",

View file

@ -111,6 +111,14 @@ func (c *compiler) __import(imp *d2ast.Import) (*Map, bool) {
c.compileMap(ir, ast, ast) c.compileMap(ir, ast, ast)
// We attempt to resolve variables in the imported file scope first
// But ignore errors, in case the variable is meant to be resolved at the
// importer
savedErrors := make([]d2ast.Error, len(c.err.Errors))
copy(savedErrors, c.err.Errors)
c.compileSubstitutions(ir, nil)
c.err.Errors = savedErrors
c.seenImports[impPath] = struct{}{} c.seenImports[impPath] = struct{}{}
return ir, true return ir, true

View file

@ -3,66 +3,37 @@
"name": "", "name": "",
"isFolderOnly": false, "isFolderOnly": false,
"ast": { "ast": {
"range": "d2/testdata/d2compiler/TestCompile/import-nested-var.d2,0:0:0-3:0:42", "range": "d2/testdata/d2compiler/TestCompile/import-nested-var.d2,0:0:0-1:0:23",
"nodes": [ "nodes": [
{ {
"map_key": { "import": {
"range": "d2/testdata/d2compiler/TestCompile/import-nested-var.d2,0:0:0-2:1:41", "range": "d2/testdata/d2compiler/TestCompile/import-nested-var.d2,0:0:0-0:22:22",
"key": { "spread": true,
"range": "d2/testdata/d2compiler/TestCompile/import-nested-var.d2,0:0:0-0:11:11", "pre": "",
"path": [ "path": [
{ {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/import-nested-var.d2,0:0:0-0:11:11", "range": "d2/testdata/d2compiler/TestCompile/import-nested-var.d2,0:4:4-0:10:10",
"value": [ "value": [
{ {
"string": "development", "string": "models",
"raw_string": "development" "raw_string": "models"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile/import-nested-var.d2,0:13:13-2:1:41",
"nodes": [
{
"import": {
"range": "d2/testdata/d2compiler/TestCompile/import-nested-var.d2,1:2:17-1:24:39",
"spread": true,
"pre": "",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/import-nested-var.d2,1:6:21-1:12:27",
"value": [
{
"string": "models",
"raw_string": "models"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/import-nested-var.d2,1:13:28-1:24:39",
"value": [
{
"string": "environment",
"raw_string": "environment"
}
]
}
}
]
} }
} ]
] }
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/import-nested-var.d2,0:11:11-0:22:22",
"value": [
{
"string": "environment",
"raw_string": "environment"
}
]
}
} }
} ]
} }
} }
] ]
@ -93,63 +64,17 @@
}, },
"edges": null, "edges": null,
"objects": [ "objects": [
{
"id": "development",
"id_val": "development",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile/import-nested-var.d2,0:0:0-0:11:11",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/import-nested-var.d2,0:0:0-0:11:11",
"value": [
{
"string": "development",
"raw_string": "development"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "development"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"iconStyle": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
{ {
"id": "k", "id": "k",
"id_val": "k", "id_val": "k",
"references": [ "references": [
{ {
"key": { "key": {
"range": "d2/testdata/d2compiler/TestCompile/models.d2,8:2:56-8:3:57", "range": "d2/testdata/d2compiler/TestCompile/models.d2,3:4:20-3:5:21",
"path": [ "path": [
{ {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/models.d2,8:2:56-8:3:57", "range": "d2/testdata/d2compiler/TestCompile/models.d2,3:4:20-3:5:21",
"value": [ "value": [
{ {
"string": "k", "string": "k",