Skip to content

In order placement of imported objects #2115

Description

@rutvora

If I import objects through a var, they are not imported in the order specified in the d2 file.

For example:
main.d2

vars: {
  before_elem: {
    "before_elem"
  }
  after_elem: {
    "after_elem"
  }
}


x: {
  ...${before_elem}
  elem
  ...${after_elem}
}

This code renders this image:
image

However, if I instead do this:
sub.d2

before_elem: {
  "before_elem"
}
after_elem: {
  "after_elem"
}

main.d2

x: {
  [email protected]_elem
  elem
  [email protected]_elem
}

I get this:
image

I am not sure if this is expected behaviour or just a missing case for handling variable imports to be in order.
Note that the same applies to connections also.

Activity

  1. alixander commented on Sep 25, 2024

    @alixander
    Collaborator

    another good find, thanks again @rutvora . variable substitution is indeed in the wrong order

  2. rutvora commented on Sep 25, 2024

    @rutvora
    Author

    Hi.
    At this point I feel like I am bothering you with daily issues :P

    I tried pulling your fix (I tried commits a3fd4ec and 643b670). The a3fd4ec commit fails some e2e tests, but otherwise compiles fine. The 643b670 commit compiles fine.

    However, neither of the commits fix the issue I mentioned, with the example I mentioned above.

    In addition, I tried a few more examples that are creating issues (not sure if the same code is causing this or not)

    main.d2 (v1)

    x: {
      grid-gap: 0
      grid-rows: 2
      grid-columns: 2
    
      # elem is required to be a var, else will be the first one to be imported instead of in-order
      vars: {
        before_1: {
          "before_1"
        }
        before_2: {
          "before_2"
        }
        elem: {
          "elem"
        }
        after_1: {
          "after_1"
        }
      }
      ...${before_1}
      ...${before_2}
      ...${elem}
      ...${after_1}
    }
    
    y
    
    # x.after_1 -> y
    

    Note: The last line, which contains the connetion is commented

    main.svg (v1)
    image

    main.d2 (v2)

    x: {
      grid-gap: 0
      grid-rows: 2
      grid-columns: 2
    
      # elem is required to be a var, else will be the first one to be imported instead of in-order
      vars: {
        before_1: {
          "before_1"
        }
        before_2: {
          "before_2"
        }
        elem: {
          "elem"
        }
        after_1: {
          "after_1"
        }
      }
      ...${before_1}
      ...${before_2}
      ...${elem}
      ...${after_1}
    }
    
    y
    
    x.after_1 -> y
    

    Note: Connection is uncommented

    main.svg (v2)
    image

    As you see, the element/connection declared before the variable substitution is always placed first.

  3. rutvora commented on Sep 26, 2024

    @rutvora
    Author

    Hi,
    You were right, I ran install.sh in a hurry, instead of using the ./ci/release/build.sh --install. Fixed that and the 2 examples above work fine.

    However, the order is still messed up.

    main.d2

    x: {
      grid-gap: 0
      grid-rows: 2
      grid-columns: 2
    
      # elem is required to be a var, else will be the first one to be imported instead of in-order
      vars: {
        before_1: {
          "before_1"
        }
        before_2: {
          "before_2"
        }
        elem: {
          "elem"
        }
        after_1: {
          "after_1"
        }
      }
      ...${before_1}
      ...${before_2}
      ...${elem}
      ...${after_1}
    }
    
    y
    
    x.before_1 -> y
    

    This is the same example as before, but the connection now originates from before_1 instead of after_1
    As you can see in the image below, before_1 has now been moved to be the last element imported.

    image

    If I comment out the last line (the connection), the order is as it should be.

  4. alixander commented on Sep 27, 2024

    @alixander
    Collaborator

    Darn, indeed. The solution was kind of a best-effort. I'll leave this open for a more robust solution. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions