structurizr / dsl

Structurizr DSL
https://docs.structurizr.com/dsl
Apache License 2.0
1.41k stars 265 forks source link

Cannot create refs when using !include #145

Closed linusnorton closed 2 years ago

linusnorton commented 2 years ago

If I define a container in one file:

workspace {
    model {
        bSystem = softwareSystem "B" {
            bContainer = container "B"
        }
    }
}

and reference it in another

!include b.dsl

workspace {
    model {
        aSystem = softwareSystem "A" {
            aContainer = container "A"
        }

        aContainer -> bContainer "Uses"
        bContainer -> aContainer "Uses"
    }
    views {
        container aSystem "ContainerView" {
            include *
            autoLayout
        }
        systemContext aSystem "SystemView" {
            include *
            autoLayout
        }
    }
}

I get an error: Invalid DSL: The element named B does not exist in the model associated with this view at line 18: include *

I would like to have separate systems in their own file and then be able to use them in downstream systems. Is that possible?

simonbrowndotje commented 2 years ago

!include simply inlines the referenced file, so you essentially have two workspace definitions in a single file, which is causing the problem you’re seeing. See https://github.com/structurizr/dsl/blob/master/docs/language-reference.md#includes for more.

linusnorton commented 2 years ago

I see, modifying it to:

b.dsl:

bSystem = softwareSystem "B" {
    bContainer = container "B"
}

and a.dsl:

workspace {

    model {
        !include b.dsl

        aSystem = softwareSystem "A" {
            aContainer = container "A"
        }

        aContainer -> bContainer "Uses"
        bContainer -> aContainer "Uses"
    }
    views {
        container aSystem "ContainerView" {
            include *
            autoLayout
        }
        systemContext aSystem "SystemView" {
            include *
            autoLayout
        }
    }
}

Seems to do the trick.

I would say it might be nice to include an example in the docs because it wasn't immediately clear that !include could be used in any part of the file.

`

simonbrowndotje commented 2 years ago

Newer versions of Structurizr Lite will actually throw an error if you try to use the workspace keyword multiple times, so if you're using Lite, you should be able to update. This same feature will be rolled out in the next version of the CLI later this month if you're using that instead.