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

Add __filename/__dirname constants (like Node.js) #977

Closed
mstoykov opened this issue Mar 26, 2019 · 2 comments
Closed

Add __filename/__dirname constants (like Node.js) #977

mstoykov opened this issue Mar 26, 2019 · 2 comments
Labels
enhancement evaluation needed proposal needs to be validated or tested before fully implementing it in k6 js-compat ux

Comments

@mstoykov
Copy link
Contributor

It will be useful if we can have consts such as nodejs __filename and __dirname in order to be easier to open() files relative to the current file instead of the current working directory.

@na-- na-- added the ux label Mar 26, 2019
@na-- na-- changed the title Adding __filename/__dirname like nodejs Add __filename/__dirname constants (like Node.js) Mar 26, 2019
@na-- na-- added evaluation needed proposal needs to be validated or tested before fully implementing it in k6 and removed good first issue labels Jun 27, 2019
@na--
Copy link
Member

na-- commented Jan 26, 2021

We've discussed this elsewhere, but we don't want to add new magic constants to our runtime. Constants like __filename and __dirname that overlap with the NodeJS runtime are even less desirable, since they might cause some poor feature detection JS logic to mistake k6 with Node...

Instead, we should probably tackle this by adding functions in some internal k6 module (e.g. k6/execution) that return the current working directory and the script file name. So, this falls under #1320, and I'll make a comment there about it after closing this issue.

@sarasensible
Copy link

Workaround to obtain the absolute path to a file when you know what the relative path would be from the root but you don't know what directory level you'll be accessing it from:

const dirs = ['path','to','file'];
const current_dirs = __ENV.PWD.split("/")
const root_dir = current_dirs.filter(x => !dirs.includes(x));

const locationsFile = `${root_dir.join("/")}/${dirs.join("/")}/thefile.txt`;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement evaluation needed proposal needs to be validated or tested before fully implementing it in k6 js-compat ux
Projects
None yet
Development

No branches or pull requests

3 participants