2222function format_project_toml! (path:: AbstractString )
2323 @assert isfile (path) " Expected a file path, got: $path "
2424 isprojecttoml (path) || return nothing
25- # This calls `strip_compat_trailing_zeros!(path)` internally.
2625 strip_compat_trailing_zeros! (path)
26+ # Always canonicalize ordering/formatting, even if compat did not change.
27+ sort_project_toml! (path)
2728 return nothing
2829end
2930
@@ -53,6 +54,9 @@ function sort_project_toml!(path::AbstractString)
5354 for k in scalar_keys
5455 TOML. print (io, Dict (k => data[k]))
5556 end
57+ sections = String[]
58+ scalar_out = strip (String (take! (io)), ' \n ' )
59+ ! isempty (scalar_out) && push! (sections, scalar_out)
5660 table_keys = String[]
5761 seen = Set {String} ()
5862 for k in table_order
@@ -65,10 +69,12 @@ function sort_project_toml!(path::AbstractString)
6569 is_table (data[k]) && ! (k in seen) && push! (table_keys, k)
6670 end
6771 for k in table_keys
68- println (io)
69- TOML. print (io, Dict (k => data[k]); sorted = true )
72+ table_io = IOBuffer ()
73+ TOML. print (table_io, Dict (k => data[k]); sorted = true )
74+ table_out = strip (String (take! (table_io)), ' \n ' )
75+ ! isempty (table_out) && push! (sections, table_out)
7076 end
71- out = String ( take! (io) )
77+ out = join (sections, " \n\n " )
7278 endswith (out, " \n " ) || (out *= " \n " )
7379 out == raw && return false
7480 write (path, out)
@@ -103,6 +109,5 @@ function strip_compat_trailing_zeros!(path::AbstractString)
103109 open (path, " w" ) do io
104110 return TOML. print (io, data)
105111 end
106- sort_project_toml! (path)
107112 return true
108113end
0 commit comments