-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
tilde expansion in path names #1136
Comments
Just to make sure I understand, you're proposing that I kind of like it, it seems to offer control and flexibility. Especially since Windows temporary files seem to start with ~, I had to put an exception in |
Some interesting data points to consider about the shell's tilde expansion... It only applies at the beginning of words: bash-3.2$ echo ~
/Users/stefan
bash-3.2$ echo foo~
foo~
bash-3.2$ echo ~stefan
/Users/stefan
bash-3.2$ echo foo~stefan
foo~stefan If the username does not exist, no expansion occurs: bash-3.2$ echo ~foo
~foo If the user name is interpolated, no expansion occurs: bash-3.2$ var=stefan
bash-3.2$ echo ~$var
~stefan I'm not sure we should mimic all of that, but it's worth considering. |
Yep, that's exactly what I'm proposing. |
On windows |
PowerShell allows |
Oh, I've actually never noticed that. In that case we should definitely allow it on windows. |
Doing tilde expansion at compile time sounds like a terrible idea! To me that is absolutely wrong. |
The tilde expansion doesn't have to be done at compile time, but determining what is tilde expanded and what isn't would be done at compile time. So |
Oh, carry on then :) |
Stefan, I'm sure you already noticed this, but for the record: of your three points, the first matches the current behavior of But I am presuming that a new implementation would be needed with backticks, anyway. |
bump. now that the file API cleanup happened [#1782], what is the status of this? |
Close related to this: #2761 (comment) |
Bump. I'd really like to see ~ recognised within strings as the home directory; cd(expanduser("~")) just seems too clunky. |
When this was first proposed, I thought it sounded like a good idea (expanding tilde only in backticks, that is). But really, it is rather annoying that it doesn't work in most file contexts (or even in backticks yet). I also don't quite follow the original argument that it "violates the treatment of strings as just data". What's wrong with assigning meaning to data in proper context? As for filenames named or starting with So, to me, at least, the convenience of tilde expansion on normal strings (and backtick strings) in context outweighs any perceived disadvantages... |
File names that begin with
This approach changes running external programs from a brittle, janky, awkward hack that is almost always not-quite-correct, to a completely legitimate and resilient way to write software. In fact, if you don't care about the overhead, using an external program is a great way to perfectly isolate a subtask by letting the OS take care of it. I'm still ok with doing tilde expansion and glob expansion in backticks, because there it's syntactic, so you know it's intentional. To see the difference, |
One of the first things to do here would be to implement correct user home directory lookup – cross-platform, of course. Even for the current user, looking at |
since homedir support landed recently in libuv (libuv/libuv#350), we may want to consider switching over to that once #12266 is merged |
Oh nice! We should definitely do that. |
it looks like this proposal would provide a fairly accurate match of the sh semantics (for which tilde expansion precedes variable interpolation). The man page for bash seems to be the most detailed (and the one referenced in the testing above for #1136 (comment)). In particular, shells may differ in their handling of
My proposal then is to implement this only for a literal |
this hasn't had any work on it recently, moving to 1.0 |
Deprecations in #19786 pave the way for this change in 1.0. |
Seems like we should have this and globbing. Globbing should happen when a command is run, relative to command's working directory. This is actually a feature since you can't use these special characters in command syntax anymore. |
Hmmm, I hope this isn't unwanted, but as a relatively new user, I thought I'd chime in with my confusion that |
There is still the same ambiguity. You could make a directory called julia> cd("/tmp")
julia> mkpath(joinpath(pwd(), "~", "Documents"))
"/private/tmp/~/Documents"
julia> pwd()
"/private/tmp"
julia> cd("~/Documents")
julia> pwd()
"/private/tmp/~/Documents" And that's distinct from julia> cd(expanduser("~/Documents"))
julia> pwd()
"/Users/goretkin/Documents" |
Just an idea to help users, if the path doesn't exist and the first character in the string is |
That would be great. This does seem like one of a number of spots where the error message could be much more helpful. Rust would probably be a good inspiration in this regard. |
The suggestion has been added to the error message printing for |
@timholy implemented
tilde_expand
(see file.jl) some time ago and it is now used automatically by a few file-system commands, notablycd()
. This concerns me, however, since while it's convenient for some things, it's inconsistently used, and it violates the treatment of strings as just data. What if the name of a file is just~
? We're now in a position where that name needs to be escaped and handled specially in code. On the other hand, tilde expansion is awfully handy.So here's a possibility: bake tilde expansion into the backtick syntax instead. For example, we could make this work:
Currently this just looks for a file literally named
~/.julia_history
. Obviously, this would be handy for entering commands, but you could also use it in situations where you want shell-like treatment of any string. For examplecd(
~)
would expand tocd(ENV["HOME"])
whereascd("~")
would treat its argument literally. Very importantly, tilde expansion would be done at compile time, not based on the run-time data — this is essential to avoiding nasty surprises and corner cases. In particular, interpolated values are not tilde expanded:One corollary of this approach is that file and directory operations have to work with
Cmd
objects, which maybe ought to be renamed in light of their broadened use-cases under this proposal. Maybe they should be calledShellString
objects since backtick strings are basically strings with shell-like behavior. Except that they're also not strings, so maybe that's not a great name either.The text was updated successfully, but these errors were encountered: