gulpjs / vinyl

Virtual file format.
MIT License
1.28k stars 105 forks source link

file.relative for base='/' returns non-leading slash prefix #42

Closed pgilad closed 9 years ago

pgilad commented 9 years ago

Currently file.relative returns path.relative(file.base, file.path).

This works great, but when you want a plugin to use file.relative and supply base: '/', you get file paths that are non-absolute, and are incorrect (but are correct relative to root):

So you would get:

file.path === '/Users/pgilad/repos/personal/gulp-todo/lib/reporter.js'
file.relative === 'Users/pgilad/repos/personal/gulp-todo/lib/reporter.js'

This problem occurs when you use file.relative in your plugin to get file information, and expect users to set the base option in order to control if the path will be absolute or relative.

Any ideas on how to work around this without adding an option to the plugin?

yocontra commented 9 years ago

@pgilad That is correct though, /Users/pgilad/repos/personal/gulp-todo/lib/reporter.js relative to / is Users/pgilad/repos/personal/gulp-todo/lib/reporter.js - I don't understand the problem. Why would a plugin use the relative path?

pgilad commented 9 years ago

Because if you do any kind of action of collecting filenames (and possibly outputting them somehow) the user may want to control if the output is either relative (usually lib/reporter.js) or absolute (usually /Users/pgilad...). Each have their own usage, and a user of my plugin recently requested absolute paths in order to use with a CI tool (jenkins).

Point being - when writing a plugin, I would like it if users can control the path output of files by themselves. I realize that may be difficult and probably impossible to achieve via the base option.

Does that make sense, @contra?

yocontra commented 9 years ago

@pgilad Can you link the github issue?

pgilad commented 9 years ago

sure. I already closed it by adding the absolute option thought. :sob: https://github.com/pgilad/gulp-todo/issues/27

yocontra commented 9 years ago

@pgilad I think the way you solved it works fine, I'm not sure what you're proposing we add to gulp (or vinyl) to help you out

pgilad commented 9 years ago

@contra I'm not sure either, but I found path.relative works fine in almost all cases except when user supplies base path of /. I do realize it makes sense that Users/pgilad... is relative to / but then again - if someone supplies base path / they probably want absolute paths.

I guess this can be closed, I thought there might be a hidden solution I wasn't aware of