Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expand ~ when opening files #137

Closed
haakon-e opened this issue Apr 11, 2024 · 4 comments
Closed

expand ~ when opening files #137

haakon-e opened this issue Apr 11, 2024 · 4 comments
Labels
upstream Things that are part of upstream dependencies wontfix

Comments

@haakon-e
Copy link

Would you be open to modifying

YAML.jl/src/YAML.jl

Lines 94 to 102 in 844ad27

load_file(filename::AbstractString, args...; kwargs...) =
open(filename, "r") do input
load(input, args...; kwargs...)
end
load_all_file(filename::AbstractString, args...; kwargs...) =
open(filename, "r") do input
load_all(input, args...; kwargs...)
end

to allow for data = YAML.load_file("~/test.yml") to "just work"?

I think it would simply be:

load_file(filename::AbstractString, args...; kwargs...) =
    open(expanduser(filename), "r") do input
        load(input, args...; kwargs...)
    end

load_all_file(filename::AbstractString, args...; kwargs...) =
    open(expanduser(filename), "r") do input
        load_all(input, args...; kwargs...)
    end

Current behavior:

julia> data = YAML.load_file("~/test.yml")
ERROR: SystemError: opening file "~/test.yml": No such file or directory
Stacktrace:
       internal @ Base, Unknown
  [9] load_file(::String)
    @ YAML ~/.julia/packages/YAML/U6OOW/src/YAML.jl:94
Use `err` to retrieve the full stack trace.

julia> data = YAML.load_file(expanduser("~/test.yml"))
Dict{Any, Any} with 7 entries:
  "receipt"         => "Oz-Ware Purchase Invoice"
  "items"           => Dict{Any, Any}[Dict("part_no"=>"A4786", "price"=>1.47, "descrip"=>"Water Bucket (Filled)", "quantity"
  "customer"        => Dict{Any, Any}("given"=>"Dorothy", "family"=>"Gale")
  "ship-to"         => Dict{Any, Any}("city"=>"East Centerville", "street"=>"123 Tornado Alley\nSuite 16\n", "state"=>"KS")
  "bill-to"         => Dict{Any, Any}("city"=>"East Centerville", "street"=>"123 Tornado Alley\nSuite 16\n", "state"=>"KS")
  "specialDelivery" => "Follow the Yellow Brick Road to the Emerald City. Pay no attention to the man behind the curtain."
  "date"            => Date("2012-08-06")
@kescobo
Copy link
Collaborator

kescobo commented Apr 12, 2024

This is true in most (all?) julia functions that take strings as paths. While I agree this is inconvenient, and it frequently bites me, I'd be reluctant to change something that's so common across the language in just this package.

@kescobo
Copy link
Collaborator

kescobo commented Apr 12, 2024

One thing that might be worth an issue or PR (though I think it would go into julia itself) would be to improve the error message to mention the use or normpath() and expanduser() if someone hits that system error

@haakon-e
Copy link
Author

Thanks. It seems like this is on the way to being addressed (at least through more helpful error messages) in newer versions of Julia. Looking through past issues in the Julia repo, there seems to be some justification for not "fixing" this.

@kescobo kescobo added wontfix upstream Things that are part of upstream dependencies labels Apr 16, 2024
@kescobo
Copy link
Collaborator

kescobo commented Apr 16, 2024

Great! Thanks for engaging and being part of the solution!

@kescobo kescobo closed this as completed Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream Things that are part of upstream dependencies wontfix
Projects
None yet
Development

No branches or pull requests

2 participants