diff --git a/src/Preferences.jl b/src/Preferences.jl index 1065b71..3eb28b3 100644 --- a/src/Preferences.jl +++ b/src/Preferences.jl @@ -135,7 +135,8 @@ function set_preferences!(target_toml::String, pkg_name::String, pairs::Pair{Str d = Base.parsed_toml(target_toml) end prefs = d - if endswith(target_toml, "Project.toml") + is_project_toml = endswith(target_toml, "Project.toml") + if is_project_toml if !haskey(prefs, "preferences") prefs["preferences"] = Dict{String,Any}() end @@ -158,11 +159,34 @@ function set_preferences!(target_toml::String, pkg_name::String, pairs::Pair{Str prefs[pkg_name] = process_sentinel_values!(prefs[pkg_name]) end open(target_toml, "w") do io - TOML.print(io, d, sorted=true) + if is_project_toml + project_toml_print(io, d) + else + TOML.print(io, d; sorted=true) + end end return nothing end +# Taken from Pkg.jl v1.11.4 (but we don't want to depend on Pkg.jl directly just for this) +# https://github.com/JuliaLang/Pkg.jl/blob/v1.11.4/src/project.jl#L235 +# with `preferences` added before `deps` +const _project_key_order = ["name", "uuid", "keywords", "license", "desc", "preferences", "deps", "weakdeps", "sources", "extensions", "compat"] +project_key_order(key::String) = something(findfirst(x -> x == key, _project_key_order), length(_project_key_order) + 1) +function project_toml_print(io::IO, d::Dict) + @static if VERSION <= v"1.11-" + TOML.print(io, d; sorted=true, by=key -> (project_key_order(key), key)) + else + kw = (sorted=true, by=key -> (project_key_order(key), key)) + if haskey(d, "sources") + inline_tables = IdSet{Dict{String}}(v for v in values(d["sources"])) + kw = merge(kw, (; inline_tables)) + end + TOML.print(io, d; kw...) + end +end + + """ set_preferences!(uuid_or_module_or_name_or_tuple, prefs::Pair{String,Any}...; export_prefs=false, active_project_only=true, force=false) diff --git a/test/PkgV_1_11_plus/LocalPkg/Project.toml b/test/PkgV_1_11_plus/LocalPkg/Project.toml new file mode 100644 index 0000000..05cf645 --- /dev/null +++ b/test/PkgV_1_11_plus/LocalPkg/Project.toml @@ -0,0 +1,4 @@ +name = "LocalPkg" +uuid = "8fa365b9-38ac-4f5f-96f8-37668e8e369e" +authors = ["Preferences.jl contributors"] +version = "0.1.0" diff --git a/test/PkgV_1_11_plus/LocalPkg/src/LocalPkg.jl b/test/PkgV_1_11_plus/LocalPkg/src/LocalPkg.jl new file mode 100644 index 0000000..42feb00 --- /dev/null +++ b/test/PkgV_1_11_plus/LocalPkg/src/LocalPkg.jl @@ -0,0 +1,5 @@ +module LocalPkg + +greet() = print("Hello World!") + +end # module LocalPkg diff --git a/test/PkgV_1_11_plus/Project-reference-after.toml b/test/PkgV_1_11_plus/Project-reference-after.toml new file mode 100644 index 0000000..929b2b7 --- /dev/null +++ b/test/PkgV_1_11_plus/Project-reference-after.toml @@ -0,0 +1,26 @@ +name = "PkgV_1_11_plus" +uuid = "6fde81da-fe91-40db-985f-5f1fb79db309" +authors = ["Preferences.jl contributors"] +version = "0.1.0" + +[preferences.PkgV_1_11_plus] +TEST_PREF = true + +[deps] +Example = "7876af07-990d-54b4-ab0e-23690620f79a" +LocalPkg = "fcf55292-0d03-4e8a-9e0b-701580031fc3" + +[sources] +Example = {url = "https://github.com/JuliaLang/Example.jl"} +LocalPkg = {path = "LocalPkg"} + +[compat] +Example = "0.5" +Test = "1.11.0" +julia = "1.11" + +[extras] +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[targets] +test = ["Test"] diff --git a/test/PkgV_1_11_plus/Project-reference-before.toml b/test/PkgV_1_11_plus/Project-reference-before.toml new file mode 100644 index 0000000..e1bc92c --- /dev/null +++ b/test/PkgV_1_11_plus/Project-reference-before.toml @@ -0,0 +1,23 @@ +name = "PkgV_1_11_plus" +uuid = "6fde81da-fe91-40db-985f-5f1fb79db309" +authors = ["Preferences.jl contributors"] +version = "0.1.0" + +[deps] +Example = "7876af07-990d-54b4-ab0e-23690620f79a" +LocalPkg = "fcf55292-0d03-4e8a-9e0b-701580031fc3" + +[sources] +Example = {url = "https://github.com/JuliaLang/Example.jl"} +LocalPkg = {path = "LocalPkg"} + +[compat] +Example = "0.5" +Test = "1.11.0" +julia = "1.11" + +[extras] +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[targets] +test = ["Test"] diff --git a/test/PkgV_1_11_plus/Project.toml b/test/PkgV_1_11_plus/Project.toml new file mode 100644 index 0000000..e1bc92c --- /dev/null +++ b/test/PkgV_1_11_plus/Project.toml @@ -0,0 +1,23 @@ +name = "PkgV_1_11_plus" +uuid = "6fde81da-fe91-40db-985f-5f1fb79db309" +authors = ["Preferences.jl contributors"] +version = "0.1.0" + +[deps] +Example = "7876af07-990d-54b4-ab0e-23690620f79a" +LocalPkg = "fcf55292-0d03-4e8a-9e0b-701580031fc3" + +[sources] +Example = {url = "https://github.com/JuliaLang/Example.jl"} +LocalPkg = {path = "LocalPkg"} + +[compat] +Example = "0.5" +Test = "1.11.0" +julia = "1.11" + +[extras] +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[targets] +test = ["Test"] diff --git a/test/PkgV_1_11_plus/src/PkgV_1_11_plus.jl b/test/PkgV_1_11_plus/src/PkgV_1_11_plus.jl new file mode 100644 index 0000000..35a98b3 --- /dev/null +++ b/test/PkgV_1_11_plus/src/PkgV_1_11_plus.jl @@ -0,0 +1,5 @@ +module PkgV_1_11_plus + +greet() = print("Hello World!") + +end # module PkgV_1_11_plus diff --git a/test/runtests.jl b/test/runtests.jl index 631be05..ca1a43d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -444,3 +444,28 @@ end end end end + +# https://github.com/JuliaPackaging/Preferences.jl/issues/52 +# Need v1.11+ for Project.toml `[sources]` section to be supported. +if VERSION > v"1.11.0-" +@testset "Project.toml writing" begin + Pkg.activate("PkgV_1_11_plus") do + @eval using PkgV_1_11_plus + proj_path = joinpath("PkgV_1_11_plus", "Project.toml") + proj_before = read(proj_path, String) + ref_before = read(joinpath("PkgV_1_11_plus", "Project-reference-before.toml"), String) + @test proj_before == ref_before + try + set_preferences!(PkgV_1_11_plus, "TEST_PREF" => true; export_prefs=true) + proj_after = read(proj_path, String) + ref_after = read(joinpath("PkgV_1_11_plus", "Project-reference-after.toml"), String) + @assert ref_before != ref_after + @test proj_after == ref_after + finally + open(proj_path, "w") do io + write(io, proj_before) + end + end + end +end +end # if VERSION