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

path: tilde expansion #684

Closed
silverwind opened this issue Jan 31, 2015 · 14 comments
Closed

path: tilde expansion #684

silverwind opened this issue Jan 31, 2015 · 14 comments

Comments

@silverwind
Copy link
Contributor

Carrying over this often-requested feature from nodejs/node-v0.x-archive#2857. There's two mechanisms in Unix concering the tilde in paths:

  1. expand ~ at the beginning of a path string to the current user's home directory.
  2. expand ~user at the beginning of a path string to the given user's home directory.

The first part should be quite trivial to add if #682 get implemented. The second part is far more uncommon and would likely require passwd parsing (for which consensus seems to be that it's libuv territory - libuv/libuv#11), and god-knows-what on Windows.

Would we be satisified if only the first part gets added to all relevant path functions? I assume this could be added in a non-breaking way, which wouldn't require a semver-major bump.

@brendanashworth
Copy link
Contributor

> var fs = require('fs');
undefined
> fs.mkdirSync('~test');
undefined
> fs.writeFileSync('~test/~file', 'myData');
undefined
> fs.readFileSync('~test/~file', 'utf8');
'myData'

Seeing as right now one can viably and reliably write to / read from / create directories with a tilde as a prefix, I'd say that this would cause a breaking change and require semver-major. However, I would favor this feature and wouldn't cry if I 2 wasn't implemented.

@mscdex
Copy link
Contributor

mscdex commented Feb 1, 2015

I'm -1 on this. On the one hand yes, many developers may expect ~ on *nix to be (internally) replaced with the path to a user's home directory, but I'm not sure Windows users would expect that since Windows does not support ~ in the command prompt and other places?

There's also other issues:

  • The tilde expansion is really a bash-ism (yes, bash is very common, but still...) and not a more general *nix shell feature so the feature is "limited" more than just to a particular platform.
  • How do you escape the tilde (in a sane way) to keep expansion from happening?
  • If we add this, where does the support for bash-isms end? Do we also support the other ~ expansions (e.g. ~+, ~-, ~1, ~2, etc)? Do we add support for inline replacing of environment variables in strings (e.g. fs.mkdirSync('$HOME/foo'))? Do we keep parity when bash adds other new features? What about other shells' features?

@rvagg
Copy link
Member

rvagg commented Feb 1, 2015

-1, similar reasons to @mscdex, mainly because this is a shell construct and we are not building a shell.

@silverwind
Copy link
Contributor Author

How do you escape the tilde (in a sane way) to keep expansion from happening?

Good point. While shells allow a simple \~, the only way I can think of in JS would be '\\~' which is beyond ugly. Along with the (very slight) possibility of a breaking change, I'm almost convinced to leave this out of core. I'll close this shortly if no serious support is obtained.

@silverwind
Copy link
Contributor Author

I think it's decided then.

For anyone needing tilde expansion, have a look at untildify. I'm planning to add support for the more exotic tilde expansions of bash to it too.

@rhinoceraptor
Copy link

This functionality could be added to the posix._makeLong() function (lib/path.js), and that avoids the problem of what to do on Windows. win32._makeLong() has it's own logic for resolving path names, for POSIX there is no 'smart' logic.

@khanhankkhan
Copy link

has this been fixed?

it's causing aws-sdk to break, which uses ~/.aws/credentials

CredentialsError: Missing credentials in config

@rhinoceraptor
Copy link

rhinoceraptor commented Aug 18, 2016

I just tested node 6.3.1 and it's not fixed. You can test it by doing something like require('fs').readFileSync('~/.bash_history').toString() in the REPL.

@cjihrig
Copy link
Contributor

cjihrig commented Aug 18, 2016

No. You can use os.homedir() to expand the ~ yourself, or use a third party module like untildify.

@pmuens
Copy link

pmuens commented May 18, 2017

For everyone who stumbles upon this. Here's what I came up with (if you're working with a file in a directory / directories):

let credentials = '~/root/foo/bar/key.json';
const credParts = credentials.split(path.sep);

if (credParts[0] === '~') {
  credParts[0] = os.homedir();
  credentials = credParts.reduce((memo, part) => path.join(memo, part), '');
}

@refack
Copy link
Contributor

refack commented May 18, 2017

For everyone who stumbles upon this. Here's what I came up with:

Two other variations:
If your original path is OS appropriate

let credentials = '~/root/foo/bar/key.json';
credentials = credentials.replace(/^~/, os.homedir());

If you use node >= v6

let credentials = '~/root/foo/bar/key.json';
const credParts = credentials.split(path.sep);

if (credParts[0] === '~') {
  credParts[0] = os.homedir();
}
credentials = path.join(...credParts);

@silverwind
Copy link
Contributor Author

Your examples will fail on files named ~file. I'd suggest you use untildify.

@refack
Copy link
Contributor

refack commented May 19, 2017

Your examples will fail on files named ~file

That's valid (asking as an occasional non-expert *inx user)?

@addaleax
Copy link
Member

addaleax commented May 19, 2017

Your examples will fail on files named ~file

That's valid (asking as an occasional non-expert *inx user)?

Sure, filenames can start with a literal ~ – tilde expansion is purely a shell thing, the OS or the filesystem doesn’t care. (Also fyi, in shells ~foo typically expands to the home directory of the user named foo).

csordasmarton added a commit to csordasmarton/CodecheckerVSCodePlugin that referenced this issue Feb 23, 2022
We are using `spawn` method to execute a CodeChecker command.
If the executable path starts with a `~` (e.g.: `~/CodeChecker/bin/CodeChecker`),
the `spawn` method doesn't work properly and will give an exception.

For more information see: nodejs/node#684

To solve this problem we will expand the `~` in the file path before
executing the command.
csordasmarton added a commit to csordasmarton/CodecheckerVSCodePlugin that referenced this issue Feb 28, 2022
We are using `spawn` method to execute a CodeChecker command.
If the executable path starts with a `~` (e.g.: `~/CodeChecker/bin/CodeChecker`),
the `spawn` method doesn't work properly and will give an exception.

For more information see: nodejs/node#684

To solve this problem we will expand the `~` in the file path before
executing the command.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants