diff --git a/src/poetry/console/commands/publish.py b/src/poetry/console/commands/publish.py
index 98c0bebe896..cd9ba7d5f4a 100644
--- a/src/poetry/console/commands/publish.py
+++ b/src/poetry/console/commands/publish.py
@@ -72,13 +72,21 @@ def handle(self) -> int:
# Building package first, if told
if self.option("build"):
- if publisher.files and not self.confirm(
- f"There are {len(publisher.files)} files ready for"
- " publishing. Build anyway?"
- ):
- self.line_error("Aborted!")
-
- return 1
+ if publisher.files:
+ if self.io.is_interactive():
+ if not self.confirm(
+ f"There are {len(publisher.files)} files ready for"
+ f" publishing in {dist_dir}. Build anyway?"
+ ):
+ self.line_error("Aborted!")
+
+ return 1
+
+ else:
+ self.line(
+ f"Warning: There are {len(publisher.files)} files "
+ f"ready for publishing in {dist_dir}. Build anyway!"
+ )
self.call("build", args=f"--output {dist_dir}")
diff --git a/tests/console/commands/test_publish.py b/tests/console/commands/test_publish.py
index 31723ac8ce3..1f6f44ce3dc 100644
--- a/tests/console/commands/test_publish.py
+++ b/tests/console/commands/test_publish.py
@@ -5,6 +5,7 @@
from pathlib import Path
from typing import TYPE_CHECKING
from typing import NoReturn
+from unittest.mock import PropertyMock
import pytest
import requests
@@ -215,3 +216,31 @@ def test_publish_dist_dir_and_build_options(
assert "Publishing simple-project (1.2.3) to PyPI" in output
assert "- Uploading simple_project-1.2.3.tar.gz" in error
assert "- Uploading simple_project-1.2.3-py2.py3-none-any.whl" in error
+
+
+def test_publish_build_no_interaction_skips_confirmation(
+ app_tester: ApplicationTester, mocker: MockerFixture
+) -> None:
+ mocker.patch(
+ "poetry.publishing.publisher.Publisher.files",
+ new_callable=PropertyMock,
+ return_value=[Path("dist/simple_project-1.2.3-py2.py3-none-any.whl")],
+ )
+ confirm = mocker.patch("poetry.console.commands.publish.PublishCommand.confirm")
+ command_call = mocker.patch("poetry.console.commands.publish.PublishCommand.call")
+ publisher_publish = mocker.patch("poetry.publishing.Publisher.publish")
+
+ exit_code = app_tester.execute("publish --build --no-interaction --dry-run")
+
+ assert exit_code == 0
+ output = app_tester.io.fetch_output()
+ error = app_tester.io.fetch_error()
+
+ confirm.assert_not_called()
+ assert "Build anyway?" not in output
+ assert "Build anyway?" not in error
+ assert (
+ "Warning: There are 1 files ready for publishing in dist. Build anyway!"
+ ) in output
+ command_call.assert_called_once_with("build", args="--output dist")
+ assert publisher_publish.call_count == 1