Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Unreleased

* Handle blank `HTTP_PROXY`/`HTTPS_PROXY` environment variables as if they were not set

## v0.10.1 (2026-10-19)

* Don't try to compute the target if a `:path` is configured
Expand Down
17 changes: 13 additions & 4 deletions lib/esbuild/npm_registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,21 @@ defmodule Esbuild.NpmRegistry do
)
end

defp proxy_for_scheme("http") do
System.get_env("HTTP_PROXY") || System.get_env("http_proxy")
# Same handling as in phoenixframework/tailwind: blank variables are treated as unset.
@doc false
def proxy_for_scheme("http") do
get_and_sanitize_env_var("HTTP_PROXY") || get_and_sanitize_env_var("http_proxy")
end

defp proxy_for_scheme("https") do
System.get_env("HTTPS_PROXY") || System.get_env("https_proxy")
def proxy_for_scheme("https") do
get_and_sanitize_env_var("HTTPS_PROXY") || get_and_sanitize_env_var("https_proxy")
end

defp get_and_sanitize_env_var(env_var) do
case String.trim(System.get_env(env_var, "")) do
"" -> nil
trimmed -> trimmed
end
end

defp maybe_add_proxy_auth(http_options, scheme) do
Expand Down
46 changes: 46 additions & 0 deletions test/npm_registry_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
defmodule Esbuild.NpmRegistryTest do
# Changes the OS environment, so it cannot run concurrently with other tests.
use ExUnit.Case, async: false

@vars ["HTTP_PROXY", "http_proxy", "HTTPS_PROXY", "https_proxy"]

setup context do
envs = Map.get(context, :envs, %{})
previous = Map.new(@vars ++ Map.keys(envs), &{&1, System.get_env(&1)})

Enum.each(@vars, &System.delete_env/1)
Enum.each(envs, fn {name, value} -> System.put_env(name, value) end)

on_exit(fn ->
Enum.each(previous, fn
{name, nil} -> System.delete_env(name)
{name, value} -> System.put_env(name, value)
end)
end)
end

@tag envs: %{
"HTTP_PROXY" => "http://proxy.local:3128",
"https_proxy" => "http://secure.local:3128"
}
test "proxy_for_scheme/1 returns the configured proxy" do
assert Esbuild.NpmRegistry.proxy_for_scheme("http") == "http://proxy.local:3128"
assert Esbuild.NpmRegistry.proxy_for_scheme("https") == "http://secure.local:3128"
end

@tag envs: %{"HTTP_PROXY" => "", "HTTPS_PROXY" => " "}
test "proxy_for_scheme/1 handles blank variables as if they were not set" do
assert Esbuild.NpmRegistry.proxy_for_scheme("http") == nil
assert Esbuild.NpmRegistry.proxy_for_scheme("https") == nil
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a tag for tests:

@tag envs: %{"HTTP_PROXY" => ""}

This way setup knows exactly which tags to set and which ones to reset, instead of traversing the whole environment. It should be faster, simpler, and fewer LOCs!

@tag envs: %{"HTTP_PROXY" => "", "http_proxy" => "http://proxy.local:3128"}
test "proxy_for_scheme/1 falls back to the lowercase variable when the uppercase one is blank" do
assert Esbuild.NpmRegistry.proxy_for_scheme("http") == "http://proxy.local:3128"
end

@tag envs: %{"HTTPS_PROXY" => " http://secure.local:3128 "}
test "proxy_for_scheme/1 trims surrounding whitespace" do
assert Esbuild.NpmRegistry.proxy_for_scheme("https") == "http://secure.local:3128"
end
end
Loading