d2 icon indicating copy to clipboard operation
d2 copied to clipboard

Proposal: Add --if-changed flag to speed up compilation of large files and folders in CI

Open carsonjan opened this issue 3 months ago • 4 comments

Proposal: Add --if-changed flag to speed up compilation of large files and folders in CI

                ┌─────────┐                               
                │input.d2 │                               
                │         │                               
                └─────────┘                               
                     │                                    
                     ▼                                    
                 **********                               
       **********          **********                     
 ******  output.svg exits, has rev and id  ******                
      **********          **********                      
                **********                                
                                                          
        │                         │                       
        │                       true                      
        │                         │                       
        │                         ▼                       
        │                        ***                      
        │                      **   **                    
        │                    **       **                  
        │                  **           **                
        │                **┌───────────────┐              
        │              **  │get rev and id │**            
        │            **    │               │  **          
        │          **      └───────────────┘    **        
        │        **               │               **      
        │      **                 │                 **    
        │    **                   │                   **  
      false**                     │                     **
        │    **                equals?                **  
        │      **                 │                 **    
        │        **               │               **      
        │          **             │             **        
        │            ┌───────────────────────────────┐    
        │            │get current rev and compute id │    
        │            │   **               **         │    
        │            └───────────────────────────────┘    
        │                    **       **                  
        │                      **   **                    
        │                        ***                      
        │                     │     │                     
        │                   false   │                     
        │                     │     │                     
        └───────┐  ┌──────────┘     │                     
                │  │              true                    
                ▼  ▼                │                     
            ┌─────────┐             │                     
            │ render  │             │                     
            │         │             │                     
            └─────────┘             │                     
                 │                  │                     
                 │                  ▼                     
                 │               ┌───────┐                
           add metadata          │ skip  │                
                 │               │       │                
                 │               └───────┘                
                 │                  │                     
                 └──────┐   ┌───────┘                     
                        │   │                             
                        ▼   ▼                             
                   ┌───────────┐                          
                   │output.svg │                          
                   │           │                          
                   └───────────┘                          
                          │                               
                          ▼                               
               ┌───────────────────────┐                  
               │downstream (if needed) │                  
               │                       │                  
               └───────────────────────┘                  

Expected usage

original

command line

$ d2 file.d2
success: successfully compiled file.d2 to file.svg in 317.16995ms

file.svg

<?xml version="1.0" encoding="utf-8"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" data-d2-version="v0.7.1" preserveAspectRatio="xMinYMin meet" viewBox="0 0 255 434">
...more

.pre-commit-hooks.yaml

- id: d2
  name: d2
  description: Compiles and renders d2 files
  entry: sh -ec 'for f in "${@}"; do d2 --omit-version "${f}"; done' --
  language: golang
  files: \.d2$

.pre-commit-config.yaml

repos:
  - repo: https://github.com/terrastruct/d2
    rev: v0.7.1
    hooks:
      - id: d2

new

command line

$ d2 file.d2 --if-changed
skipped: no changes found for file.d2 to file.svg. skipped in 17.16995ms

file.svg

<?xml version="1.0" encoding="utf-8"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" data-d2-version="v0.7.1" preserveAspectRatio="xMinYMin meet" viewBox="0 0 255 434" data-d2-source-id-rev="1" data-d2-source-id="some-very-unique-id">
...more

.pre-commit-hooks.yaml

(unchanged)

.pre-commit-config.yaml

repos:
  - repo: https://github.com/terrastruct/d2
    rev: v0.7.1
    hooks:
      - id: d2
        args: ["--if-changed"]

Possible implementation

  • Modify d2cli/main.go's _render
  • Collect all args that affect the output file and hash it.
  • Similar to d2target/d2target.go's func (diagram Diagram) HashID(salt *string) (string, error)

Backwards compatibility / robustness

  • Optional flag
  • Silently re-compiles if data-d2-source-id-rev or data-d2-source-id is missing

carsonjan avatar Aug 15 '26 02:08 carsonjan

Hi @carsonjan! Could I implement this, if its not done yet?

KaustAbhinand avatar Aug 31 '26 10:08 KaustAbhinand

Hi @KaustAbhinand! I haven't implemented it yet. Please feel free to : )

carsonjan avatar Aug 31 '26 19:08 carsonjan

Hi @carsonjan, I've kept at this, but I have a major doubt:

While building - ./make.sh never completed. Specifically - it got hung here:

Submodule path 'ci/sub': checked out 'a91fb1731094f9408a5543ea09040dfd69dbe2ad'

  • set +x GIT_BASE=927dbc657 fmt^: ./ci/sub/bin/fmt.sh fmt: changed: d2layouts/d2grid/grid_integral_test.go fmt: changed: d2layouts/d2grid/grid_search.go fmt: changed: d2layouts/d2grid/grid_search_test.go fmt: go.mod^: gomodtidy fmt: gofmt^: gofmt fmt: go.mod: exec: go mod tidy fmt: go.mod: go: downloading github.com/d2lang/dagro v0.2.1 fmt: go.mod$: success (0h0m13s) fmt: gofmt: exec: hide xargsd '.go$' gofmt -s -w fmt: gofmt: exec: hide xargsd '.go$' go run golang.org/x/tools/cmd/[email protected] -w -local=oss.terrastruct.com,github.com/terrastruct,github.com/d2lang fmt: gofmt$: success (0h0m15s)

And it stayed thus for almost the next half-hour. Upon querying with the DeepWiki agent, it said the build involved a 30-minute timeout, but even post that, it never progressed.

Is this normal? Or is there anything I have missed?

I have forked, cloned, and ran ./make.sh.

Are there any prior installations to be done (if any)? Or is this the expected time to build? Please help me :)

KaustAbhinand avatar Sep 11 '26 17:09 KaustAbhinand

Hello @KaustAbhinand, Thanks for the update! Unfortunately, I'm not very familiar with the building process. Maybe you could create a separate issue to get some help. Sorry about that.

carsonjan avatar Sep 12 '26 22:09 carsonjan