From 5de88fdf2c3f38cd31a39caf5535562ed7192c7b Mon Sep 17 00:00:00 2001 From: sabiwara Date: Tue, 10 Mar 2026 19:18:54 +0900 Subject: [PATCH] Add support for [:raw] opts in File.read --- lib/elixir/lib/file.ex | 28 ++++++++++++++++++++++------ lib/elixir/test/elixir/file_test.exs | 4 ++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/elixir/lib/file.ex b/lib/elixir/lib/file.ex index d6d7388142b..c62d9cf0b53 100644 --- a/lib/elixir/lib/file.ex +++ b/lib/elixir/lib/file.ex @@ -400,6 +400,13 @@ defmodule File do You can use `:file.format_error/1` to get a descriptive string of the error. + ## Options (since v1.20) + + The supported options are: + + * `:raw` - a single atom to bypass the file server and only check + for the file locally + ## Examples File.read("hello.txt") @@ -408,15 +415,24 @@ defmodule File do File.read("non_existing.txt") #=> {:error, :enoent} """ - @spec read(Path.t()) :: {:ok, binary} | {:error, posix | :badarg | :terminated | :system_limit} - def read(path) do - :file.read_file(IO.chardata_to_string(path)) + @spec read(Path.t(), [exists_option]) :: + {:ok, binary} | {:error, posix | :badarg | :terminated | :system_limit} + when exists_option: :raw + def read(path, opts \\ []) do + :file.read_file(IO.chardata_to_string(path), opts) end @doc """ Returns a binary with the contents of the given filename, or raises a `File.Error` exception if an error occurs. + ## Options (since v1.20) + + The supported options are: + + * `:raw` - a single atom to bypass the file server and only check + for the file locally + ## Examples File.read!("hello.txt") @@ -425,9 +441,9 @@ defmodule File do File.read!("non_existing.txt") ** (File.Error) could not read file "non_existing.txt": no such file or directory """ - @spec read!(Path.t()) :: binary - def read!(path) do - case read(path) do + @spec read!(Path.t(), [exists_option]) :: binary when exists_option: :raw + def read!(path, opts \\ []) do + case read(path, opts) do {:ok, binary} -> binary diff --git a/lib/elixir/test/elixir/file_test.exs b/lib/elixir/test/elixir/file_test.exs index 846789db58e..30ae98b9ab4 100644 --- a/lib/elixir/test/elixir/file_test.exs +++ b/lib/elixir/test/elixir/file_test.exs @@ -983,6 +983,10 @@ defmodule FileTest do assert {:ok, "Русский\n日\n"} = File.read(Path.expand(~c"fixtures/utf8.txt", __DIR__)) end + test "read with :raw options" do + assert {:ok, "FOO\n"} = File.read(fixture_path("file.txt"), [:raw]) + end + test "read!" do assert File.read!(fixture_path("file.txt")) == "FOO\n" expected_message = "could not read file \"fixtures/missing.txt\": no such file or directory"