Skip to content

TOML.print generates invalid TOML on stdout when the values include multiline strings #55083

@palday

Description

@palday
julia> TOML.print(Dict("a" => """x\nx"""))
a = "x\nx"

julia> TOML.parse("""a = "x\nx" """)
ERROR: TOML Parser error:
none:2:-1 error: newline character in single quoted string
  a = "x
       ^ 
Stacktrace:
 [1] parse
   @ ./toml_parser.jl:445 [inlined]
 [2] parse(str::String)
   @ TOML ~/.julia/juliaup/julia-1.10.4+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/TOML/src/TOML.jl:71
 [3] top-level scope
   @ REPL[99]:1

Interestingly, this seems to only be an issue with stdout and is handled correctly with other IO streams

julia> io = IOBuffer();  TOML.print(io, Dict("a" => """x\nx""")); TOML.parse(seekstart(io))
Dict{String, Any} with 1 entry:
  "a" => "x\nx"

I'm guessing this intersection of two different expectations -- we generally don't include triple quotes for show-type methods going to stdout, but in this particular case, it prevents a simple copy-paste of the generated TOML into other use cases.

julia> versioninfo()
Julia Version 1.10.4
Commit 48d4fd48430 (2024-06-04 10:41 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 8 × Apple M1
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 4 default, 0 interactive, 2 GC (on 4 virtual cores)

Metadata

Metadata

Assignees

No one assigned

    Labels

    TOMLbugIndicates an unexpected problem or unintended behaviorstdlibJulia's standard library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions