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 docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Reads deployment settings from `[tool.posit.connect]` in `pyproject.toml` and generates it in quickstart projects. The legacy `[tool.rsconnect]` name remains supported with a deprecation warning.

### Fixed

- Follows symlinks when bundling content: linked files are archived with their target contents and linked directories are included, instead of archiving broken links or omitting the directories. [#955](https://github.com/posit-dev/rsconnect-python/pull/955)
Expand Down
9 changes: 5 additions & 4 deletions docs/deploying.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,14 @@ type and dependencies. The options are the same as for `deploy manifest`; see

### Deploying from a pyproject.toml

`rsconnect deploy pyproject` reads a `[tool.rsconnect]` table from a project's
`rsconnect deploy pyproject` reads a `[tool.posit.connect]` table from a project's
`pyproject.toml` instead of taking the app mode and entrypoint as CLI arguments.
It is designed as the deploy partner for projects scaffolded by
`rsconnect quickstart`, but works with any project whose `pyproject.toml`
contains the required keys.
contains the required keys. The deprecated `[tool.rsconnect]` table is also accepted;
rename it to `[tool.posit.connect]`. Use only one of the two tables.

The `[tool.rsconnect]` table has two required keys:
The `[tool.posit.connect]` table has two required keys:

- `app_mode` — the Connect app mode the deployment uses. Supported values
are `python-streamlit`, `python-shiny`, `python-fastapi`, `python-api`,
Expand All @@ -380,7 +381,7 @@ version = "0.1.0"
requires-python = ">=3.9"
dependencies = ["streamlit"]

[tool.rsconnect]
[tool.posit.connect]
app_mode = "python-streamlit"
entrypoint = "app.py"
```
Expand Down
14 changes: 7 additions & 7 deletions rsconnect/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2029,7 +2029,7 @@ def info(file: str):
"Create a new Posit Connect project of the given TYPE in ./<name>/. "
"Supported TYPE values: streamlit, shiny, fastapi, api, flask, "
"notebook, voila, quarto. Writes a pyproject.toml "
"with a [tool.rsconnect] section, creates a uv-managed virtualenv, "
"with a [tool.posit.connect] section, creates a uv-managed virtualenv, "
"and prints the local-run and deploy commands."
),
no_args_is_help=True,
Expand Down Expand Up @@ -2727,7 +2727,7 @@ def deploy_bundle(
short_help="Deploy content to Posit Connect, Posit Connect Cloud, or shinyapps.io by pyproject.",
help=(
"Deploy content described by a project's pyproject.toml. The given directory must contain "
"a pyproject.toml with a [tool.rsconnect] table specifying app_mode and entrypoint. "
"a pyproject.toml with a [tool.posit.connect] table specifying app_mode and entrypoint. "
"Designed as the deploy partner for projects scaffolded by 'rsconnect quickstart'."
),
no_args_is_help=True,
Expand All @@ -2744,7 +2744,7 @@ def deploy_bundle(
default=None,
help=(
"Path to the requirements source, relative to the project directory. "
"Overrides ``[tool.rsconnect].requirements_file`` in pyproject.toml; "
"Overrides ``[tool.posit.connect].requirements_file`` in pyproject.toml; "
"defaults to ``pyproject.toml`` (the project's declared dependencies). "
"Pass ``uv.lock`` for a fully resolved deploy, or any ``requirements.txt``-compatible file."
),
Expand Down Expand Up @@ -2903,7 +2903,7 @@ def quickstart_hint() -> str:
"r_environment": r_environment,
}
else:
raise RSConnectException(f"Unsupported app_mode '{target.configured_app_mode}' in [tool.rsconnect]")
raise RSConnectException(f"Unsupported app_mode '{target.configured_app_mode}' in {target.table}")

ce = RSConnectExecutor(
ctx=ctx,
Expand Down Expand Up @@ -4364,7 +4364,7 @@ def write_manifest_quarto(
short_help="Create a manifest.json file from a project's pyproject.toml.",
help=(
"Create a manifest.json file for later deployment, for content described by a project's "
"pyproject.toml. The given directory must contain a pyproject.toml with a [tool.rsconnect] "
"pyproject.toml. The given directory must contain a pyproject.toml with a [tool.posit.connect] "
"table specifying app_mode and entrypoint. This will also write the environment file the "
'manifest references (e.g. "requirements.txt"), regenerating it on each run unless it is '
"itself the requirements source. Designed as the write-manifest partner for projects "
Expand All @@ -4380,7 +4380,7 @@ def write_manifest_quarto(
default=None,
help=(
"Path to the requirements source, relative to the project directory. "
"Overrides ``[tool.rsconnect].requirements_file`` in pyproject.toml; "
"Overrides ``[tool.posit.connect].requirements_file`` in pyproject.toml; "
"defaults to ``pyproject.toml`` (the project's declared dependencies). "
"Pass ``uv.lock`` for a fully resolved manifest, or any ``requirements.txt``-compatible file."
),
Expand Down Expand Up @@ -4440,7 +4440,7 @@ def quickstart_hint() -> str:
AppModes.SHINY_QUARTO,
)
if app_mode not in api_modes and app_mode not in entrypoint_manifest_modes:
raise RSConnectException(f"Unsupported app_mode '{target.configured_app_mode}' in [tool.rsconnect]")
raise RSConnectException(f"Unsupported app_mode '{target.configured_app_mode}' in {target.table}")

with cli_feedback("Checking arguments"):
# The bundle.py writers put manifest.json in different places: API modes
Expand Down
90 changes: 55 additions & 35 deletions rsconnect/pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,71 +187,87 @@ class InvalidVersionConstraintError(ValueError):


class InvalidPyprojectConfigError(ValueError):
"""Raised when ``[tool.rsconnect]`` is missing or incomplete."""
"""Raised when the deployment table is missing or incomplete."""


class UnsupportedAppModeError(ValueError):
"""Raised when ``[tool.rsconnect].app_mode`` names an app mode rsconnect does not know.
"""Raised when the configured app mode is unknown.

Kept distinct from :class:`InvalidPyprojectConfigError` because the CLI does
not append the quickstart hint for this failure.
"""


_MINIMUM_VALID_TOOL_RSCONNECT_SNIPPET = """[tool.rsconnect]
_MINIMUM_VALID_TOOL_SNIPPET = """[tool.posit.connect]
# e.g. python-streamlit, python-shiny, python-fastapi, jupyter-static, quarto-shiny
app_mode = "<app_mode>"
entrypoint = "<entrypoint>" # e.g. app.py"""

_PYPROJECT_APP_MODES = (
AppModes.STREAMLIT_APP,
AppModes.PYTHON_SHINY,
AppModes.PYTHON_FASTAPI,
AppModes.PYTHON_API,
AppModes.JUPYTER_NOTEBOOK,
AppModes.JUPYTER_VOILA,
AppModes.STATIC_QUARTO,
AppModes.SHINY_QUARTO,
)


def read_tool_rsconnect(pyproject_file: pathlib.Path) -> typing.Mapping[str, typing.Any]:
"""Read the ``[tool.rsconnect]`` deployment config from pyproject.toml.
"""Read the deployment config from the supported pyproject.toml tables."""
return _read_deployment_config(pyproject_file)[1]

Returns the section mapping unchanged so forward-compatible fields pass
through. Raises ``InvalidPyprojectConfigError`` when the section is
missing or when required ``app_mode`` / ``entrypoint`` fields are absent or
not non-empty strings.
"""

def _read_deployment_config(pyproject_file: pathlib.Path) -> typing.Tuple[str, typing.Mapping[str, typing.Any]]:
content = pyproject_file.read_text(encoding="utf-8-sig")
pyproject = tomllib.loads(content)

tool = pyproject.get("tool")
if tool is None:
if tool is not None and not isinstance(tool, Mapping):
raise InvalidPyprojectConfigError(f"[tool] is not a TOML table. Add at least:\n\n{_MINIMUM_VALID_TOOL_SNIPPET}")
tool = typing.cast(typing.Mapping[str, typing.Any], tool or {})
posit = tool.get("posit", {})
if not isinstance(posit, Mapping):
raise InvalidPyprojectConfigError(
f"The [tool.rsconnect] section is missing. Add at least:\n\n{_MINIMUM_VALID_TOOL_RSCONNECT_SNIPPET}"
f"[tool.posit] is not a TOML table. Add at least:\n\n{_MINIMUM_VALID_TOOL_SNIPPET}"
)
if not isinstance(tool, Mapping):
posit = typing.cast(typing.Mapping[str, typing.Any], posit)
if "connect" in posit and "rsconnect" in tool:
raise InvalidPyprojectConfigError(
f"[tool.rsconnect] is not a TOML table. Add at least:\n\n{_MINIMUM_VALID_TOOL_RSCONNECT_SNIPPET}"
"Both [tool.posit.connect] and [tool.rsconnect] are present. Keep only one table."
)
tool = typing.cast(typing.Mapping[str, typing.Any], tool)

tool_rsconnect = tool.get("rsconnect")
if tool_rsconnect is None:
if "connect" in posit:
table = "[tool.posit.connect]"
config = posit["connect"]
elif "rsconnect" in tool:
table = "[tool.rsconnect]"
config = tool["rsconnect"]
else:
raise InvalidPyprojectConfigError(
f"The [tool.rsconnect] section is missing. Add at least:\n\n{_MINIMUM_VALID_TOOL_RSCONNECT_SNIPPET}"
f"The [tool.posit.connect] section is missing; [tool.rsconnect] is also accepted. "
f"Add at least:\n\n{_MINIMUM_VALID_TOOL_SNIPPET}"
)
if not isinstance(tool_rsconnect, Mapping):

if not isinstance(config, Mapping):
raise InvalidPyprojectConfigError(
f"[tool.rsconnect] is not a TOML table. Add at least:\n\n{_MINIMUM_VALID_TOOL_RSCONNECT_SNIPPET}"
f"{table} is not a TOML table. Add at least:\n\n{_MINIMUM_VALID_TOOL_SNIPPET}"
)
tool_rsconnect = typing.cast(typing.Mapping[str, typing.Any], tool_rsconnect)

config = typing.cast(typing.Mapping[str, typing.Any], config)
for field in ("app_mode", "entrypoint"):
value = tool_rsconnect.get(field)
value = config.get(field)
if not isinstance(value, str) or not value:
raise InvalidPyprojectConfigError(
f"The [tool.rsconnect] field {field} must be a non-empty string. Add at least:\n\n"
f"{_MINIMUM_VALID_TOOL_RSCONNECT_SNIPPET}"
f"The {table} field {field} must be a non-empty string. Add at least:\n\n{_MINIMUM_VALID_TOOL_SNIPPET}"
)

return tool_rsconnect
return table, config


@dataclasses.dataclass(frozen=True)
class PyprojectDeployTarget:
"""Deployment configuration resolved from ``[tool.rsconnect]`` in pyproject.toml."""
"""Deployment configuration resolved from pyproject.toml."""

table: str
app_mode: AppMode
# The app_mode string as written in pyproject.toml; may be an alias of
# app_mode.name() and is what error messages should quote back to the user.
Expand All @@ -266,32 +282,36 @@ def resolve_pyproject_deploy_target(
requirements_file: typing.Optional[str] = None,
title_override: typing.Optional[str] = None,
) -> PyprojectDeployTarget:
"""Resolve the deployment target described by ``[tool.rsconnect]`` in pyproject.toml.
"""Resolve the deployment target described in pyproject.toml.

Raises ``InvalidPyprojectConfigError`` when the config is missing or
incomplete, and ``UnsupportedAppModeError`` when ``app_mode`` does not name
a known app mode.

:param pathlib.Path pyproject_file: path to the project's pyproject.toml.
:param typing.Optional[str] requirements_file: caller override for the
requirements source; wins over ``[tool.rsconnect].requirements_file``.
requirements source; wins over the configured ``requirements_file``.
:param typing.Optional[str] title_override: fallback title used when the
config declares none.
"""
config = read_tool_rsconnect(pyproject_file)
table, config = _read_deployment_config(pyproject_file)

configured_app_mode = typing.cast(str, config["app_mode"])
app_mode = AppModes.get_by_name(configured_app_mode, return_unknown=True)
if app_mode == AppModes.UNKNOWN:
raise UnsupportedAppModeError(f"Unsupported app_mode '{configured_app_mode}' in [tool.rsconnect]")
if app_mode not in _PYPROJECT_APP_MODES:
raise UnsupportedAppModeError(f"Unsupported app_mode '{configured_app_mode}' in {table}")

if table == "[tool.rsconnect]":
logger.warning("[tool.rsconnect] is deprecated. Rename it to [tool.posit.connect].")

# Requirements source precedence: caller override (the ``-r`` flag) >
# ``[tool.rsconnect].requirements_file`` > built-in default ``pyproject.toml``
# configured ``requirements_file`` > built-in default ``pyproject.toml``
# (top-level deps; Connect resolves transitive). An explicit default keeps the
# inspector from falling back to a ``pip freeze`` of the caller's interpreter.
# Malformed TOML values (wrong type, missing file) are surfaced by the
# inspector / file existence check.
return PyprojectDeployTarget(
table=table,
app_mode=app_mode,
configured_app_mode=configured_app_mode,
entrypoint=typing.cast(str, config["entrypoint"]),
Expand Down
2 changes: 1 addition & 1 deletion rsconnect/quickstart/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def _require_cwd_writable(cwd: pathlib.Path) -> None:
#
# The registry is the single source of truth that ties together what each
# supported mode produces: the canonical Connect ``app_mode`` written to
# ``[tool.rsconnect]``, the entrypoint form, the local-run command
# ``[tool.posit.connect]``, the entrypoint form, the local-run command
# documented in the post-scaffold stdout and the README, the minimum
# dependencies for the hello-world, and the source files the per-mode
# template materializes.
Expand Down
2 changes: 1 addition & 1 deletion rsconnect/quickstart/templates/api/pyproject.toml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dependencies = [
"flask",
]

[tool.rsconnect]
[tool.posit.connect]
app_mode = "python-api"
entrypoint = "$name.__connect__:app"
title = "$name"
Expand Down
2 changes: 1 addition & 1 deletion rsconnect/quickstart/templates/fastapi/pyproject.toml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies = [
"uvicorn",
]

[tool.rsconnect]
[tool.posit.connect]
app_mode = "python-fastapi"
entrypoint = "$name.__connect__:app"
title = "$name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dependencies = [
"jupyter",
]

[tool.rsconnect]
[tool.posit.connect]
app_mode = "jupyter-static"
entrypoint = "notebook.ipynb"
title = "$name"
Expand Down
2 changes: 1 addition & 1 deletion rsconnect/quickstart/templates/quarto/pyproject.toml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.0.1"
requires-python = "$requires_python"
dependencies = []

[tool.rsconnect]
[tool.posit.connect]
app_mode = "quarto-static"
entrypoint = "report.qmd"
title = "$name"
Expand Down
2 changes: 1 addition & 1 deletion rsconnect/quickstart/templates/shiny/pyproject.toml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dependencies = [
"shiny",
]

[tool.rsconnect]
[tool.posit.connect]
app_mode = "python-shiny"
entrypoint = "app.py"
title = "$name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dependencies = [
"streamlit",
]

[tool.rsconnect]
[tool.posit.connect]
app_mode = "python-streamlit"
entrypoint = "app.py"
title = "$name"
Expand Down
2 changes: 1 addition & 1 deletion rsconnect/quickstart/templates/voila/pyproject.toml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies = [
"jupyter",
]

[tool.rsconnect]
[tool.posit.connect]
app_mode = "jupyter-voila"
entrypoint = "notebook.ipynb"
title = "$name"
Expand Down
Loading
Loading