Open Function to open a file is not taking relative paths

const data = new SharedArray('some name', function () {
  // All heavy work (opening and processing big files for example) should be done inside here.
  // This way it will happen only once and the result will be shared between all VUs, saving time and memory.
  const f = JSON.parse(open('../sample.json'));
  return f; // f must be an array
});

The above code sample as per the sample.
per the same code in document the “Open” function should allow absolute or relative paths. but my observation is that the relative paths do not work. The Open function in the code above is used only in ‘init’ context. If i provide absolute path like c:\a\b\c’ then the Open function works
but when used relative paths its throwing error

ERRO[0000] GoError: CreateFile inputs\sample.json: The system cannot find the path specified.
at go.k6.io/k6/js.(*Bundle).setInitGlobals.func3 (native)
at file:///-:28:28(5)
at file:///-:27:12(46) hint=“script exception”

Hi @bhagatabhijeet ,

Looking at the exception it seems like you are running the test with cat scripts.js | k6 run - or similar way where the script is directly sent over stdin instead of k6 run script.js.

In this case k6 “thinks” it is running from the root directory (which on windows I guess will be C:\). This is reported in k6 path resolution doesn't work from CWD when running from stdin · Issue #1462 · grafana/k6 · GitHub.

I did try my hand at making a fairly simple fix for it in Set working dir to the current one if test is read from stdin by mstoykov · Pull Request #3676 · grafana/k6 · GitHub and will be glad if you can test it.

But as pointed out there as well if you do cat path/to/script.js | k6 run - - k6 still has no way of knowing that the file isn’t in the current directory. It just gets a bunch of bytes on it’s stdin.

Hope this helps you

1 Like

Hi @mstoykov Thank you for the reply.

actually
I am not at all doing anything like “cat script | k6 run.”

I am simply running the script as provided in the sample( k6 documentation). The JSON file is in the same dir as my Script is.
see the attached image in case it helps

If i execute the script from the dir where my script is then it works.but this is weird coz I want to run the scripts from root of my project and not switch directories for running the script

Hi @bhagatabhijeet

  • If the sample.json is in the same directory as the script, then you should use simply open('sample.json') or open('./sample.json') .
  • open('../sample.json') means, that the json file is in the parent directory.

I hope this helps

1 Like

@bandorko is right you should use ./sample.json if it is in the same folder.

But your exception stack clearly shows file:///- which only happens if you give the input on stdin. Or at least it should be.

If you were on linux I was going to ask you if you do not use snaps for k6 … But I have no idea how you can experience this.

Can you tell me how you got this k6 ? I guess you used xk6 as you have a bunch of extensions?

1 Like

@bandorko and @mstoykov
lemme mark the issue as resolved. I think i got what you’re saying. I am now importing all my local modules relative to root of project and this seems to resolve the issue.
Yes @mstoykov to answer your question. " How i got this k6" i followed the extension build process document on k6 and added a couple of extensions as i am planning to use InfluxDB, prometheus, and plan to test SQL etc
really appreciate your and @bandorko responses. I get a feel of a strong community. Thanks a million :handshake::handshake:
slowly i want to be contributor to the k6 OSS Project and I am happy to interact with you and community