Write to file permissions

I have a mix task that outputs results to file as follows:

  defp write_to_file(contents) do
    app_dir = File.cwd!()
    new_file_path = Path.join([app_dir, "results.txt"])

    File.write(
      new_file_path,
      contents,
      [:write]
    )
  end

It seems like the file is missing. Are there special permissions that need to be set? It is running in ubuntu.

Is it supposed to error if it doesn’t have permissions?

Use File.write!/3 to get an exception, File.write/3 returns an error tuple.
Maybe check for the contents of new_file_path.
And unless you are doing this as a temporary thing, I would suggest not to write files in your application folder.

I’m getting {:error, :eacces}

Yes I’m just trying to write logs. Could put it in a better folder. If the folder doesn’t exist does it create it automatically?

I’m using ubuntu. How to give correct permissions to the mix task?

I presume you try to write logs to a file you don’t have permissions for writing. My advice is to use Logger instead of reinventing the wheel. It can be configured to write to a file but you can see all the options by clicking the link.