Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat: Update `marimo config show` to show configuration sources and improve config warnings (#11423)

Martha Cryan8f7ad79c4347
diff --git a/docs/guides/configuration/index.md b/docs/guides/configuration/index.md
index 6ea8f89e71..40d86d170a 100644
--- a/docs/guides/configuration/index.md
+++ b/docs/guides/configuration/index.md
@@ -60,12 +60,20 @@ marimo searches for the `.marimo.toml` file in the following order:
If no `.marimo.toml` file is found, marimo creates one for you in an XDG config
compliant way.
-To view your current configuration and locate the config file, run:
+To view the configuration marimo is using, run:
```bash
marimo config show
```
+Add `--show-sources` to include where each configuration value was determined.
+
+To include a notebook's script metadata, pass its path:
+
+```bash
+marimo config show notebook.py
+```
+
To describe the user configuration options, run:
```bash
@@ -141,6 +149,13 @@ You can also configure marimo settings directly in your notebook files using scr
and `cache.verification` come from user configuration whatever any other
layer says.
+### Dotenv files
+
+By default, marimo loads `.env` from the project directory, or the notebook's
+directory when there is no `pyproject.toml`. Override this location with
+`runtime.dotenv`; run `marimo config describe` for the option's path resolution
+and loading rules.
+
## Environment variables
marimo supports the following environment variables for advanced configuration:
diff --git a/marimo/_cli/config/commands.py b/marimo/_cli/config/commands.py
index 46f8878651..65108ce6a8 100644
--- a/marimo/_cli/config/commands.py
+++ b/marimo/_cli/config/commands.py
@@ -7,15 +7,12 @@ import click
from marimo._cli.config.utils import (
format_config_docs,
+ format_config_with_sources,
highlight_toml_headers,
)
from marimo._cli.help_formatter import ColoredCommand, ColoredGroup
-from marimo._cli.print import echo, green
-from marimo._config.manager import (
- UserConfigStore,
- get_default_config_manager,
-)
-from marimo._config.reader import find_nearest_pyproject_toml
+from marimo._cli.print import echo
+from marimo._config.manager import get_default_config_manager
@click.group(
@@ -26,35 +23,38 @@ def config() -> None:
@click.command(cls=ColoredCommand, help="""Show the marimo config.""")
-def show() -> None:
+ "notebook",
+ required=False,
+ type=click.Path(exists=True, dir_okay=False, resolve_path=True),
+)
+ "--show-sources",
+ is_flag=True,
+ help="Include source comments for configuration settings and tables.",
+)
+def show(notebook: str | None = None, show_sources: bool = False) -> None:
"""
Print out marimo config information.
Example usage:
marimo config show
"""
- import tomlkit
-
- current_path = os.getcwd()
-
- config_manager = get_default_config_manager(current_path=current_path)
- # Save config if doesn't exist
- UserConfigStore().save_config_if_missing()
-
- # Show project overrides if they exist
- project_config_path = find_nearest_pyproject_toml(current_path)
- overrides = config_manager.get_config_overrides()
- if overrides:
- echo(
- f"\n📁 Project overrides from {green(str(project_config_path))}\n"
+ config_manager = get_default_config_manager(
+ current_path=notebook or os.getcwd()
+ )
+ config_manager.user_config_mgr.save_config_if_missing()
+ if show_sources:
+ output = format_config_with_sources(
+ config_manager.get_config_with_sources()
)
- echo(highlight_toml_headers(tomlkit.dumps(overrides)))
+ else:
+ import tomlkit
- # Show user config
- user_config_path = config_manager.user_config_mgr.get_config_path()
- echo(f"\n🏠 User config from {green(user_config_path)}\n")
- user_config = config_manager.get_user_config()
- echo(highlight_toml_headers(tomlkit.dumps(user_config)))
+ output = highlight_toml_headers(
+ tomlkit.dumps(config_manager.get_config(), sort_keys=True)
+ )
+ echo(output)
@click.command(cls=ColoredCommand, help="""Describe the marimo config.""")
diff --git a/marimo/_cli/config/utils.py b/marimo/_cli/config/utils.py
index cfc84f4b6e..5e65d6df42 100644
--- a/marimo/_cli/config/utils.py
+++ b/marimo/_cli/config/utils.py
@@ -3,14 +3,28 @@ from __future__ import annotations
import inspect
import types
-from typing import Annotated, Any, Literal, Union, get_args, get_origin
+from typing import (
+ TYPE_CHECKING,
+ Annotated,
+ Any,
+ Literal,
+ Union,
+ get_args,
+ get_origin,
+)
from msgspec import NODEFAULT, UNSET, Meta, UnsetType
from msgspec.structs import fields
from marimo._cli.print import cyan, green, muted, orange, yellow
+from marimo._config.provenance import format_config_source
from marimo._config.schema_utils import Config
+if TYPE_CHECKING:
+ from tomlkit import TOMLDocument
+
+ from marimo._config.provenance import ConfigPath, ResolvedConfig
+
def highlight_toml_headers(toml_string: str) -> str:
lines = toml_string.splitlines()
@@ -19,13 +33,43 @@ def highlight_toml_headers(toml_string: str) -> str:
for line in lines:
stripped_line = line.strip()
if stripped_line.startswith("[") and stripped_line.endswith("]"):
- highlighted_lines.append(orange(line))
+ highlighted_lines.append(yellow(line))
else:
highlighted_lines.append(line)
return "\n".join(highlighted_lines)
+def format_config_with_sources(resolved: ResolvedConfig) -> str:
+ """Render effective settings as TOML with a source comment per value."""
+ import tomlkit
+ from tomlkit.items import AoT, Item, Table
+
+ document = tomlkit.parse(tomlkit.dumps(resolved.config, sort_keys=True))
+
+ def annotate(table: TOMLDocument | Table, path: ConfigPath) -> None:
+ for key in table:
+ field_path = (*path, key)
+ item = table.item(key)
+ if isinstance(item, Table) and item:
+ annotate(item, field_path)
+ continue
+ source = (
+ format_config_source(field_path, resolved.origins[field_path])
+ .replace("\n", "\\n")
+ .replace("\r", "\\r")
+ )
+ if isinstance(item, AoT):
+ for entry in item:
+ entry.comment(source)
+ else:
+ assert isinstance(item, Item)
+ item.comment(source)
+
+ annotate(document, ())
+ return highlight_toml_headers(tomlkit.dumps(document))
+
+
def _format_type(annotation: Any) -> str:
origin = get_origin(annotation)
args = get_args(annotation)
diff --git a/marimo/_config/config_schema.py b/marimo/_config/config_schema.py
index b147b4b323..26e4b4cd2d 100644
--- a/marimo/_config/config_schema.py
+++ b/marimo/_config/config_schema.py
@@ -6,7 +6,6 @@ The dictionary interfaces in `config.py` remain in use during migration.
from __future__ import annotations
-import os
from typing import Annotated, Any, Literal
from msgspec import UNSET, Meta, UnsetType, field
@@ -179,11 +178,7 @@ class RuntimeConfig(Config, kw_only=True):
Meta(
description="the maximum size in bytes of cell outputs; larger values may affect frontend performance"
),
- ] = field(
- default_factory=lambda: int(
- os.getenv("MARIMO_OUTPUT_MAX_BYTES", "8000000")
- )
- )
+ ] = 8_000_000
serve_cached_sessions_in_apps: Annotated[
bool | UnsetType,
Meta(
@@ -195,11 +190,7 @@ class RuntimeConfig(Config, kw_only=True):
Meta(
description="the maximum size in bytes of console outputs; larger values may affect frontend performance"
),
- ] = field(
- default_factory=lambda: int(
- os.getenv("MARIMO_STD_STREAM_MAX_BYTES", "1000000")
- )
- )
+ ] = 1_000_000
pythonpath: Annotated[
list[str] | UnsetType,
Meta(
@@ -209,7 +200,9 @@ class RuntimeConfig(Config, kw_only=True):
dotenv: Annotated[
list[str] | UnsetType,
Meta(
- description="a list of paths to `.env` files to load. If the file does not exist, it will be silently ignored. Relative paths resolve against the directory holding the `pyproject.toml`, or the notebook's directory when there is none.",
+ description=(
+ "Override .env discovery with a list of files to load. Use an empty list to skip .env loading entirely. In project settings and script metadata, paths are relative to the project folder, or the notebook's folder if there is no project. In user settings, paths must be absolute."
+ ),
extra={"sensitive": True},
),
] = UNSET
diff --git a/marimo/_config/manager.py b/marimo/_config/manager.py
index 7c54793bfa..a3ec228f68 100644
--- a/marimo/_config/manager.py
+++ b/marimo/_config/manager.py
@@ -2,9 +2,12 @@
from __future__ import annotations
import os
+import re
from functools import cached_property, lru_cache
from pathlib import Path
-from typing import Any, cast
+from typing import TYPE_CHECKING, Any, cast
+
+import msgspec
from marimo import _loggers
from marimo._config.config import (
@@ -23,7 +26,16 @@ from marimo._config.config import (
merge_config,
merge_default_config,
)
+from marimo._config.config_schema import MarimoConfig as MarimoConfigSchema
from marimo._config.packages import PackageManagerKind
+from marimo._config.provenance import (
+ ConfigLayer,
+ ConfigPath,
+ ConfigSource,
+ ResolvedConfig,
+ env_var_name,
+ format_config_source,
+)
from marimo._config.reader import (
ALLOWED_SCRIPT_CONFIG_TOP_KEYS,
ConfigReader,
@@ -35,7 +47,12 @@ from marimo._config.reader import (
sanitize_pyproject_dict,
strip_untrusted_config,
)
-from marimo._config.resolver import merge_config_patches, resolve_config
+from marimo._config.resolver import (
+ merge_config_patches,
+ merge_resolved_configs,
+ resolve_config,
+ resolve_config_with_sources,
+)
from marimo._config.secrets import (
redact_secrets,
redact_secrets_partial,
@@ -49,9 +66,51 @@ from marimo._config.utils import (
)
from marimo._utils.env import env_to_value
+if TYPE_CHECKING:
+ from collections.abc import Callable
+
LOGGER = _loggers.marimo_logger()
+def _validate_config(resolved: ResolvedConfig) -> None:
+ try:
+ MarimoConfigSchema.from_dict(cast(dict[str, object], resolved.config))
+ except (
+ msgspec.ValidationError,
+ TypeError,
+ ValueError,
+ AttributeError,
+ ) as error:
+ match = re.search(r" - at `\$(.*?)`$", str(error))
+ # msgspec hides dictionary keys as [...] and indexes into lists whose
+ # provenance is tracked as a single value.
+ path = (
+ [
+ field or None
+ for field, index in re.findall(
+ r"\.([^.[\]]+)|\[(\.\.\.|\d+)\]", match[1]
+ )
+ if not index.isdigit()
+ ]
+ if match
+ else []
+ )
+ sources = dict.fromkeys(
+ format_config_source(origin, source)
+ for origin, source in resolved.origins.items()
+ if all(
+ field is None or field == key
+ for field, key in zip(path, origin, strict=False)
+ )
+ )
+ LOGGER.error(
+ "Invalid configuration from %s: %s",
+ ", ".join(sources),
+ error,
+ extra={"omit_log_prefix": True},
+ )
+
+
def get_default_config_manager(
*, current_path: str | None
) -> MarimoConfigManager:
@@ -144,7 +203,9 @@ class MarimoConfigManager:
def get_user_config(self, *, hide_secrets: bool = True) -> MarimoConfig:
"""Get the user configuration"""
- config = resolve_config(DEFAULT_CONFIG, (self.user_config_mgr.read(),))
+ config = resolve_config(
+ DEFAULT_CONFIG, (self.user_config_mgr.read_layer().config,)
+ )
return redact_secrets(config) if hide_secrets else config
def get_config_overrides(
@@ -160,7 +221,7 @@ class MarimoConfigManager:
def _read_overrides(self) -> PartialMarimoConfig:
return merge_config_patches(
- reader.read()
+ reader.read_layer().config
for reader in (*self.partials, *self.security_partials)
)
@@ -179,15 +240,51 @@ class MarimoConfigManager:
def get_config(self, *, hide_secrets: bool = True) -> MarimoConfig:
"""Get the configuration, by merging the user configuration and the configuration overrides"""
- # NB. Defaults go under the user configuration, overrides over it. A
- # default a partial computes from the notebook location is not
- # something anyone wrote, so it must lose to a value the user set.
- config = resolve_config(
- DEFAULT_CONFIG,
- (self.user_config_mgr.read(), self._read_overrides()),
- fallback=self._read_defaults(),
+ return self.get_config_with_sources(hide_secrets=hide_secrets).config
+
+ def get_config_with_sources(
+ self, *, hide_secrets: bool = True
+ ) -> ResolvedConfig:
+ """Resolve settings and their winning sources without collapsing layers."""
+ readers = (*self.partials, *self.security_partials)
+ layers = [
+ ConfigLayer(
+ _contextual_defaults(reader),
+ "inferred",
+ )
+ for reader in readers
+ ]
+ layers.append(
+ ConfigLayer(
+ cast(PartialMarimoConfig, DEFAULT_CONFIG),
+ "default",
+ )
)
- return redact_secrets(config) if hide_secrets else config
+ layers.append(
+ ConfigLayer(
+ {
+ "package_management": {
+ "manager": DEFAULT_CONFIG["package_management"][
+ "manager"
+ ]
+ }
+ },
+ "inferred",
+ )
+ )
+ layers.append(self.user_config_mgr.read_layer())
+ overrides = resolve_config_with_sources(
+ reader.read_layer() for reader in readers
+ )
+ # Record-replacement merges are not associative. Preserve the existing
+ # grouping of overrides before merging them with user settings.
+ resolved = merge_resolved_configs(
+ resolve_config_with_sources(layers), overrides
+ )
+ _validate_config(resolved)
+ if hide_secrets:
+ resolved.config = redact_secrets(resolved.config)
+ return resolved
def save_config(
self, config: MarimoConfig | PartialMarimoConfig
@@ -226,6 +323,14 @@ class PyprojectConfigReader(ConfigReader):
self.start_path = start_path
self.pyproject_path = find_nearest_pyproject_toml(start_path)
+ @property
+ def source(self) -> ConfigSource:
+ return (
+ self.pyproject_path
+ if self.pyproject_path is not None
+ else "default"
+ )
+
@property
def _dotenv_root(self) -> Path:
"""Directory that relative `dotenv` paths resolve against.
@@ -262,7 +367,13 @@ class PyprojectConfigReader(ConfigReader):
# project, so an allowlist here adds little protection.
project_config = strip_untrusted_config(project_config)
except Exception as e:
- LOGGER.warning("Failed to read project config: %s", e)
+ LOGGER.error(
+ 'Could not load project configuration from %s: "%s"\n'
+ "Settings from this file were skipped; other configuration sources still apply.",
+ self.source,
+ e,
+ extra={"omit_log_prefix": True},
+ )
return {}
return project_config
@@ -322,7 +433,7 @@ class PyprojectConfigReader(ConfigReader):
return config
display = config["display"]
- custom_css = display.get("custom_css", [])
+ custom_css = display.get("custom_css")
if not isinstance(custom_css, list):
return config
@@ -367,17 +478,35 @@ class PyprojectConfigReader(ConfigReader):
class EnvConfigReader(ConfigReader):
+ @property
+ def source(self) -> ConfigSource:
+ return "env_var"
+
def _maybe_override_from_env(
self,
- key: str,
- path: list[str],
+ path: ConfigPath,
config: PartialMarimoConfig,
allowed_values: tuple[Any, ...] | None = None,
+ *,
+ parse: Callable[[str], object] | None = None,
) -> None:
- loaded_value = env_to_value(key)
- if not isinstance(loaded_value, tuple):
- return
- value = loaded_value[0]
+ key = env_var_name(path)
+ if parse is not None:
+ raw = os.environ.get(key)
+ if raw is None:
+ return
+ try:
+ value = parse(raw)
+ except ValueError as error:
+ LOGGER.warning(
+ "Ignoring invalid value %r for %s: %s", raw, key, error
+ )
+ return
+ else:
+ loaded_value = env_to_value(key)
+ if not isinstance(loaded_value, tuple):
+ return
+ value = loaded_value[0]
if allowed_values is not None and value not in allowed_values:
LOGGER.warning(
"Ignoring invalid value %r for %s; expected one of %s",
@@ -401,22 +530,23 @@ class EnvConfigReader(ConfigReader):
# We could do this dynamically, but list explicitly for now to reduce
# surface area
self._maybe_override_from_env(
- "_MARIMO_CONFIG_OVERLOAD_RUNTIME_AUTO_INSTANTIATE",
- ["runtime", "auto_instantiate"],
+ ("runtime", "auto_instantiate"),
project_config,
)
self._maybe_override_from_env(
- "MARIMO_SERVER_TRANSPORT",
- ["server", "transport"],
+ ("server", "transport"),
project_config,
allowed_values=("websocket", "sse"),
)
self._maybe_override_from_env(
- "MARIMO_RUNTIME_ON_CELL_CHANGE",
- ["runtime", "on_cell_change"],
+ ("runtime", "on_cell_change"),
project_config,
allowed_values=("lazy", "autorun"),
)
+ for name in ("output_max_bytes", "std_stream_max_bytes"):
+ self._maybe_override_from_env(
+ ("runtime", name), project_config, parse=int
+ )
return project_config
@@ -433,6 +563,10 @@ class SecurityConfigReader(ConfigReader):
handles `MARIMO_RESTRICT_SHARING`.
"""
+ @property
+ def source(self) -> ConfigSource:
+ return "env_var"
+
def read(self) -> PartialMarimoConfig:
config: PartialMarimoConfig = {}
if GLOBAL_SETTINGS.RESTRICT_SHARING:
@@ -459,6 +593,10 @@ class ScriptConfigReader(ConfigReader):
def __init__(self, filename: str | None) -> None:
self.filename = filename
+ @property
+ def source(self) -> ConfigSource:
+ return Path(self.filename) if self.filename is not None else "default"
+
def read(self) -> PartialMarimoConfig:
return cast(PartialMarimoConfig, deep_copy(self._read_config()))
@@ -492,7 +630,7 @@ class ScriptConfigReader(ConfigReader):
)
marimo_config = get_marimo_config_from_pyproject_dict(
- script_config
+ script_config, location=self.filename
)
if marimo_config is None:
return {}
@@ -505,7 +643,13 @@ class ScriptConfigReader(ConfigReader):
marimo_config = strip_untrusted_config(marimo_config)
except Exception as e:
- LOGGER.warning("Failed to read script config: %s", e)
+ LOGGER.error(
+ 'Could not load script configuration from %s: "%s"\n'
+ "Settings from this file were skipped; other configuration sources still apply.",
+ self.source,
+ e,
+ extra={"omit_log_prefix": True},
+ )
return {}
return marimo_config
@@ -514,6 +658,13 @@ class ScriptConfigReader(ConfigReader):
class UserConfigStore(ConfigReader):
"""Read and write the user configuration"""
+ @property
+ def source(self) -> ConfigSource:
+ try:
+ return Path(self.get_config_path())
+ except OSError:
+ return "default"
+
def save_config(
self, config: MarimoConfig | PartialMarimoConfig
) -> MarimoConfig:
@@ -569,8 +720,13 @@ class UserConfigStore(ConfigReader):
try:
user_config = read_marimo_config(path)
except Exception as e:
- LOGGER.error("Failed to read user config at %s", path)
- LOGGER.error(str(e))
+ LOGGER.error(
+ 'Could not load configuration from %s: "%s"\n'
+ "Settings from this file were skipped; other configuration sources still apply.",
+ path,
+ e,
+ extra={"omit_log_prefix": True},
+ )
return {}
if not is_trusted_user_config_path(path):
# A `.marimo.toml` discovered by walking up from the cwd is
@@ -579,7 +735,7 @@ class UserConfigStore(ConfigReader):
user_config = strip_untrusted_config(
user_config, is_user_layer=True
)
- return _drop_hollow_dotenv(user_config)
+ return user_config
else:
LOGGER.debug("No config found; loading default settings.")
return {}
@@ -596,7 +752,7 @@ class OverrideConfigReader(ConfigReader):
def _contextual_defaults(reader: ConfigReader) -> PartialMarimoConfig:
- """Infer dotenv paths separately from settings supplied by a source."""
+ """Infer fallback dotenv paths; the runtime loads the selected files later."""
if isinstance(reader, ScriptConfigReader):
if reader.filename is None:
return {}
@@ -623,19 +779,6 @@ def _is_home_directory(path: Path) -> bool:
return os.path.realpath(path) == os.path.realpath(home)
-def _drop_hollow_dotenv(config: PartialMarimoConfig) -> PartialMarimoConfig:
- """Drop an empty `runtime.dotenv`, which is a masked value and not a choice."""
- # NB. Reading the configuration blanks runtime.dotenv by emptying the list,
- # and marimo 0.18 and earlier saved that masked copy straight back to disk,
- # so an empty list there means "hidden", not "load nothing". Keeping it
- # would let a stale file suppress the .env next to the notebook.
- runtime = config.get("runtime")
- if runtime is None or runtime.get("dotenv") != []:
- return config
- without_dotenv = {k: v for k, v in runtime.items() if k != "dotenv"}
- return {**config, "runtime": cast(RuntimeConfig, without_dotenv)}
-
-
def _drop_none_values(d: dict[str, Any]) -> None:
"""Recursively remove keys whose value is None, in place."""
for key in list(d):
diff --git a/marimo/_config/provenance.py b/marimo/_config/provenance.py
new file mode 100644
index 0000000000..d6bcf54066
--- /dev/null
+++ b/marimo/_config/provenance.py
@@ -0,0 +1,53 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+from dataclasses import dataclass
+from pathlib import Path
+from typing import TYPE_CHECKING, Literal, TypeAlias
+
+if TYPE_CHECKING:
+ from marimo._config.config import MarimoConfig, PartialMarimoConfig
+
+ConfigPath = tuple[str, ...]
+ConfigSource: TypeAlias = (
+ Path | Literal["env_var", "cli", "default", "inferred", "override"]
+)
+
+
+def env_var_name(path: ConfigPath) -> str:
+ """Map supported settings to their existing environment variable names."""
+ # Temporary until environment names are derived from schema paths.
+ names: dict[ConfigPath, str] = {
+ (
+ "runtime",
+ "auto_instantiate",
+ ): "_MARIMO_CONFIG_OVERLOAD_RUNTIME_AUTO_INSTANTIATE",
+ ("runtime", "on_cell_change"): "MARIMO_RUNTIME_ON_CELL_CHANGE",
+ ("runtime", "output_max_bytes"): "MARIMO_OUTPUT_MAX_BYTES",
+ ("runtime", "std_stream_max_bytes"): "MARIMO_STD_STREAM_MAX_BYTES",
+ ("server", "transport"): "MARIMO_SERVER_TRANSPORT",
+ ("sharing", "html"): "MARIMO_RESTRICT_SHARING",
+ ("sharing", "wasm"): "MARIMO_RESTRICT_SHARING",
+ ("sharing", "molab"): "MARIMO_RESTRICT_SHARING",
+ }
+ return names[path]
+
+
+def format_config_source(path: ConfigPath, source: ConfigSource) -> str:
+ return env_var_name(path) if source == "env_var" else str(source)
+
+
+@dataclass
+class ConfigLayer:
+ """Sparse settings from a single source."""
+
+ config: PartialMarimoConfig
+ source: ConfigSource
+
+
+@dataclass
+class ResolvedConfig:
+ """Effective settings with provenance kept separate from their values."""
+
+ config: MarimoConfig
+ origins: dict[ConfigPath, ConfigSource]
diff --git a/marimo/_config/reader.py b/marimo/_config/reader.py
index 5b25f24bd0..73aabddefc 100644
--- a/marimo/_config/reader.py
+++ b/marimo/_config/reader.py
@@ -7,6 +7,7 @@ from typing import Any, cast
from marimo import _loggers
from marimo._config.config import PartialMarimoConfig
+from marimo._config.provenance import ConfigLayer, ConfigSource
from marimo._utils.toml import toml_reader
LOGGER = _loggers.marimo_logger()
@@ -23,6 +24,14 @@ class ConfigReader(ABC):
def read(self) -> PartialMarimoConfig:
"""Read a sparse, unmasked configuration from this source."""
+ @property
+ def source(self) -> ConfigSource:
+ return "override"
+
+ def read_layer(self) -> ConfigLayer:
+ """Read sparse settings and their sources, without validating inherited fields."""
+ return ConfigLayer(self.read(), self.source)
+
def read_marimo_config(path: str) -> PartialMarimoConfig:
"""Read the marimo.toml configuration."""
@@ -35,7 +44,7 @@ def read_pyproject_marimo_config(
"""Read the marimo tool config from a pyproject.toml file."""
pyproject_config = toml_reader.read(pyproject_path)
marimo_tool_config = get_marimo_config_from_pyproject_dict(
- pyproject_config
+ pyproject_config, location=str(pyproject_path)
)
if marimo_tool_config is None:
return None
@@ -195,15 +204,19 @@ def strip_untrusted_config(
def get_marimo_config_from_pyproject_dict(
pyproject_dict: dict[str, Any],
+ *,
+ location: str | None = None,
) -> PartialMarimoConfig | None:
"""Get the marimo config from a pyproject.toml dictionary."""
marimo_tool_config = _get_tool_dict(pyproject_dict).get("marimo", None)
if marimo_tool_config is None:
return None
if not isinstance(marimo_tool_config, dict):
- LOGGER.warning(
- "pyproject.toml contains invalid marimo config: %s",
- marimo_tool_config,
+ LOGGER.error(
+ "Ignoring marimo configuration in %s: 'tool.marimo' must be a table, got %s.",
+ location or "pyproject.toml/script metadata",
+ type(marimo_tool_config).__name__,
+ extra={"omit_log_prefix": True},
)
return None
return cast(PartialMarimoConfig, marimo_tool_config)
diff --git a/marimo/_config/resolver.py b/marimo/_config/resolver.py
index 0c72f0bc86..8ebf90be0e 100644
--- a/marimo/_config/resolver.py
+++ b/marimo/_config/resolver.py
@@ -1,13 +1,18 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
-from typing import TYPE_CHECKING, cast
+from typing import TYPE_CHECKING, Any, cast
from marimo._config.config import (
MarimoConfig,
PartialMarimoConfig,
merge_config,
)
+from marimo._config.provenance import (
+ ConfigLayer,
+ ConfigPath,
+ ResolvedConfig,
+)
from marimo._config.utils import deep_copy
if TYPE_CHECKING:
@@ -41,3 +46,46 @@ def resolve_config(
for patch in patches:
config = merge_config(config, deep_copy(patch))
return config
+
+
+def config_paths(
+ config: dict[str, Any], path: ConfigPath = ()
+) -> Iterable[ConfigPath]:
+ """Yield leaf paths, treating lists and empty tables as single values."""
+ for key, value in config.items():
+ field_path = (*path, key)
+ if isinstance(value, dict) and value:
+ yield from config_paths(value, field_path)
+ else:
+ yield field_path
+
+
+def resolve_config_with_sources(
+ layers: Iterable[ConfigLayer],
+) -> ResolvedConfig:
+ """Track explicit writes while applying the normal configuration merge rules."""
+ resolved = ResolvedConfig(cast(MarimoConfig, {}), {})
+ for layer in layers:
+ supplied = dict.fromkeys(
+ config_paths(cast(dict[str, Any], layer.config)), layer.source
+ )
+ resolved = merge_resolved_configs(
+ resolved,
+ ResolvedConfig(cast(MarimoConfig, layer.config), supplied),
+ )
+ return resolved
+
+
+def merge_resolved_configs(
+ base: ResolvedConfig, overrides: ResolvedConfig
+) -> ResolvedConfig:
+ """Merge resolved groups without losing their original field sources."""
+ config = merge_config(base.config, deep_copy(overrides.config))
+ # Keep only surviving fields after record replacements and deletions.
+ origins = {
+ path: overrides.origins[path]
+ if path in overrides.origins
+ else base.origins[path]
+ for path in config_paths(cast(dict[str, Any], config))
+ }
+ return ResolvedConfig(config, origins)
diff --git a/marimo/_config/secrets.py b/marimo/_config/secrets.py
index 8910cf32a9..37186fbf89 100644
--- a/marimo/_config/secrets.py
+++ b/marimo/_config/secrets.py
@@ -8,8 +8,6 @@ from marimo._config.utils import deep_copy
SECRET_PLACEHOLDER = "********"
-# TODO: mypy doesn't like using @overload here
-
def redact_secrets_partial(config: PartialMarimoConfig) -> PartialMarimoConfig:
"""Return a sparse configuration copy with sensitive values redacted."""
@@ -44,7 +42,8 @@ def redact_secrets(config: MarimoConfig) -> MarimoConfig:
return
if len(path) == 1:
if isinstance(obj[key], list):
- obj[key] = []
+ if obj[key]:
+ obj.pop(key)
elif obj[key]:
obj[key] = SECRET_PLACEHOLDER
else:
diff --git a/marimo/_loggers.py b/marimo/_loggers.py
index fffc9906c1..bbddffac66 100644
--- a/marimo/_loggers.py
+++ b/marimo/_loggers.py
@@ -34,8 +34,9 @@ if TYPE_CHECKING:
# Global log level for loggers
_LOG_LEVEL: int = logging.WARNING
-# Custom log formatter
-_LOG_FORMATTER = LogFormatter()
+_LOG_FORMATTER = LogFormatter(
+ without_log_prefix_fmt=LogFormatter.WITHOUT_LOG_PREFIX_FORMAT
+)
# Cache of initialized loggers
_LOGGERS: dict[str, logging.Logger] = {}
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 2a29c3d5fc..57991b0eb7 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -15,7 +15,6 @@ import time
import traceback
from copy import copy
from multiprocessing import connection
-from pathlib import Path
from typing import (
TYPE_CHECKING,
Any,
@@ -160,9 +159,7 @@ from marimo._runtime.win32_interrupt_handler import (
Win32InterruptHandler,
ignore_console_ctrl_c,
)
-from marimo._secrets.load_dotenv import (
- load_dotenv_with_fallback,
-)
+from marimo._secrets.load_dotenv import DotEnvReader
from marimo._session.model import SessionMode
from marimo._session.queue import QueueType
from marimo._sql.bindings import (
@@ -2775,13 +2772,10 @@ class Kernel:
return
for env in dotenvs:
- if Path(env).exists():
- try:
- load_dotenv_with_fallback(env)
- except Exception as e:
- LOGGER.error(
- "Failed to load dotenv file %s", env, exc_info=e
- )
+ try:
+ DotEnvReader(env).load()
+ except Exception as e:
+ LOGGER.error("Failed to load dotenv file %s", env, exc_info=e)
async def handle_message(self, request: CommandMessage) -> None:
"""Handle a message from the client.
diff --git a/marimo/_secrets/load_dotenv.py b/marimo/_secrets/load_dotenv.py
index e094be7a67..7ba2ed9219 100644
--- a/marimo/_secrets/load_dotenv.py
+++ b/marimo/_secrets/load_dotenv.py
@@ -4,8 +4,10 @@ from __future__ import annotations
import os
import re
from functools import partial
+from pathlib import Path
from typing import TYPE_CHECKING
+from marimo import _loggers
from marimo._dependencies.dependencies import DependencyManager
if TYPE_CHECKING:
@@ -16,12 +18,42 @@ _POSIX_VARIABLE = re.compile(
r"\$\{(?P<name>[^}:]*)(?::-(?P<default>[^}]*))?\}"
)
+LOGGER = _loggers.marimo_logger()
+
+
+class DotEnvReader:
+ """Read environment variables from a selected dotenv file."""
+
+ def __init__(self, file: str) -> None:
+ self.file = file
+
+ def read(
+ self, *, environment: Mapping[str, str | None] | None = None
+ ) -> dict[str, str | None]:
+ """Parse and interpolate values without modifying the environment."""
+ if not Path(self.file).exists():
+ LOGGER.warning(
+ "Dotenv file not found: %s",
+ self.file,
+ extra={"omit_log_prefix": True},
+ )
+ return {}
+ return _interpolate_dotenv_values(
+ _read_dotenv_bindings(self.file),
+ os.environ if environment is None else environment,
+ environment_overrides=environment is not None,
+ )
+
+ def load(self) -> None:
+ """Load values without replacing existing environment variables."""
+ load_to_environ(self.read(environment=os.environ))
+
def load_dotenv_with_fallback(file: str) -> None:
"""Load a .env file using the dotenv library, falling to our custom
implementation if the dotenv library is not installed.
"""
- load_to_environ(read_dotenv_with_fallback(file, environment=os.environ))
+ DotEnvReader(file).load()
def read_dotenv_with_fallback(
@@ -32,12 +64,7 @@ def read_dotenv_with_fallback(
"""Read a .env file using the dotenv library, falling to our custom
implementation if the dotenv library is not installed.
"""
- environment_overrides = environment is not None
- return _interpolate_dotenv_values(
- _read_dotenv_bindings(file),
- os.environ if environment is None else environment,
- environment_overrides=environment_overrides,
- )
+ return DotEnvReader(file).read(environment=environment)
def resolve_dotenv_value(
diff --git a/marimo/_server/start.py b/marimo/_server/start.py
index c3ccbb6b1d..b1ef410c60 100644
--- a/marimo/_server/start.py
+++ b/marimo/_server/start.py
@@ -24,7 +24,6 @@ from marimo._server.config import (
)
from marimo._server.host.lifespan import host as host_lifespan
from marimo._server.lsp import CompositeLspServer, NoopLspServer
-from marimo._server.main import create_starlette_app
from marimo._server.registry import LIFESPAN_REGISTRY
from marimo._server.session_manager import SessionManager
from marimo._server.tokens import AuthToken
@@ -355,6 +354,8 @@ def start(
(external_port, external_host) = _resolve_proxy(port, host, proxy)
enable_auth = not AuthToken.is_empty(session_manager.auth_token)
+ from marimo._server.main import create_starlette_app
+
app = create_starlette_app(
base_url=base_url,
host=external_host,
diff --git a/marimo/_utils/log_formatter.py b/marimo/_utils/log_formatter.py
index 9207946958..5518388cb9 100644
--- a/marimo/_utils/log_formatter.py
+++ b/marimo/_utils/log_formatter.py
@@ -48,6 +48,9 @@ class LogFormatter(logging.Formatter):
"""
DEFAULT_FORMAT = "%(color)s[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d]%(end_color)s %(message)s"
+ WITHOUT_LOG_PREFIX_FORMAT = (
+ "%(color)s%(level_label)s: %(message)s%(end_color)s\n"
+ )
DEFAULT_DATE_FORMAT = "%y%m%d %H:%M:%S"
DEFAULT_COLORS = {
logging.DEBUG: 4, # Blue
@@ -64,6 +67,8 @@ class LogFormatter(logging.Formatter):
style: str = "%",
color: bool = True,
colors: dict[int, int] = DEFAULT_COLORS,
+ *,
+ without_log_prefix_fmt: str | None = None,
) -> None:
r"""
:arg bool color: Enables color support.
@@ -75,6 +80,8 @@ class LogFormatter(logging.Formatter):
code
:arg str datefmt: Datetime format.
Used for formatting `(asctime)` placeholder in `prefix_fmt`.
+ :arg str without_log_prefix_fmt: Optional format for records marked
+ `omit_log_prefix`. Multiline messages retain their original indentation.
.. versionchanged:: 3.2
@@ -83,6 +90,7 @@ class LogFormatter(logging.Formatter):
del style
logging.Formatter.__init__(self, datefmt=datefmt)
self._fmt = fmt
+ self._without_log_prefix_fmt = without_log_prefix_fmt
self._colors: dict[int, str] = {}
if color and _stderr_supports_color():
@@ -129,7 +137,15 @@ class LogFormatter(logging.Formatter):
else:
record.color = record.end_color = ""
- formatted = self._fmt % record.__dict__
+ fmt = self._fmt
+ omit_log_prefix = False
+ if self._without_log_prefix_fmt is not None and getattr(
+ record, "omit_log_prefix", False
+ ):
+ fmt = self._without_log_prefix_fmt
+ omit_log_prefix = True
+ record.level_label = record.levelname.capitalize()
+ formatted = fmt % record.__dict__
if record.exc_info and not record.exc_text:
record.exc_text = self.formatException(record.exc_info)
@@ -140,4 +156,6 @@ class LogFormatter(logging.Formatter):
lines = [formatted.rstrip()]
lines.extend(str(ln) for ln in record.exc_text.split("\n"))
formatted = "\n".join(lines)
- return formatted.replace("\n", "\n ")
+ return (
+ formatted if omit_log_prefix else formatted.replace("\n", "\n ")
+ )
diff --git a/tests/_cli/test_cli_config.py b/tests/_cli/test_cli_config.py
index f6eba09410..c73747efa9 100644
--- a/tests/_cli/test_cli_config.py
+++ b/tests/_cli/test_cli_config.py
@@ -2,16 +2,29 @@
from __future__ import annotations
import subprocess
+from typing import TYPE_CHECKING, cast
import pytest
from click.testing import CliRunner
from inline_snapshot import snapshot
+from marimo import _loggers
+from marimo._cli.config import commands
from marimo._cli.config.commands import config
-from marimo._cli.config.utils import format_config_docs
+from marimo._cli.config.utils import (
+ format_config_docs,
+ format_config_with_sources,
+)
+from marimo._config.config import PartialMarimoConfig
from marimo._config.config_schema import MCPServerStdioConfig, SaveConfig
+from marimo._config.manager import MarimoConfigManager, UserConfigStore
+from marimo._config.provenance import ConfigLayer
+from marimo._config.resolver import resolve_config_with_sources
from marimo._utils.platform import is_windows
+if TYPE_CHECKING:
+ from pathlib import Path
+
@pytest.mark.xfail(condition=is_windows(), reason="flaky on Windows")
def test_config_show() -> None:
@@ -21,7 +34,8 @@ def test_config_show() -> None:
)
assert p.returncode == 0, p.stderr.decode()
output = p.stdout.decode()
- assert "User config from" in output
+ assert output.startswith("[ai]")
+ assert "sources shown in comments" not in output
assert "[formatting]" in output
@@ -33,6 +47,182 @@ def test_config_describe() -> None:
assert p.returncode == 0, p.stderr.decode()
+def test_config_show_environment_source_names() -> None:
+ resolved = resolve_config_with_sources(
+ [
+ ConfigLayer(
+ {
+ "runtime": {"output_max_bytes": 1000},
+ "server": {"transport": "sse"},
+ },
+ "env_var",
+ )
+ ]
+ )
+ assert format_config_with_sources(resolved) == snapshot("""\
+[runtime]
+output_max_bytes = 1000 # MARIMO_OUTPUT_MAX_BYTES
+
+[server]
+transport = "sse" # MARIMO_SERVER_TRANSPORT""")
+
+
+def test_config_show_source_comments_are_valid_toml(tmp_path: Path) -> None:
+ import tomlkit
+
+ values = cast(
+ PartialMarimoConfig,
+ {
+ "display": {"theme": "dark", "custom_css": ["styles.css"]},
+ "keymap": {"overrides": {}},
+ "file_browser": {"folders": [{"path": "/data", "name": "Data"}]},
+ },
+ )
+ source = tmp_path / "pyproject.toml"
+ resolved = resolve_config_with_sources([ConfigLayer(values, source)])
+ output = format_config_with_sources(resolved)
+ assert output.replace(str(source), "/project/pyproject.toml") == snapshot(
+ """\
+[display]
+custom_css = ["styles.css"] # /project/pyproject.toml
+theme = "dark" # /project/pyproject.toml
+
+[[file_browser.folders]] # /project/pyproject.toml
+name = "Data"
+path = "/data"
+
+[keymap.overrides] # /project/pyproject.toml"""
+ )
+ assert tomlkit.parse(output).unwrap() == values
+
+
[email protected]("show_sources", [False, True])
+def test_config_show_includes_notebook_sources_and_does_not_save(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ show_sources: bool,
+) -> None:
+ user_path = tmp_path / "marimo.toml"
+ contents = (
+ '[display]\ntheme = "dark"\n[ai.open_ai]\napi_key = "private-key"\n'
+ )
+ user_path.write_text(contents)
+ notebook = tmp_path / "app.py"
+ notebook.write_text(
+ '# /// script\n# [tool.marimo.runtime]\n# on_cell_change = "lazy"\n# ///\n'
+ )
+ user = UserConfigStore()
+ monkeypatch.setattr(user, "get_config_path", lambda: str(user_path))
+ from marimo._config.manager import ScriptConfigReader
+
+ def manager(*, current_path: str) -> MarimoConfigManager:
+ assert current_path == str(notebook)
+ return MarimoConfigManager(user, ScriptConfigReader(current_path))
+
+ monkeypatch.setattr(commands, "get_default_config_manager", manager)
+ args = ["show", str(notebook)]
+ if show_sources:
+ args.append("--show-sources")
+ result = CliRunner().invoke(commands.config, args)
+ assert result.exit_code == 0, result.output
+ if show_sources:
+ assert f'theme = "dark" # {user_path}' in result.output
+ assert f'on_cell_change = "lazy" # {notebook}' in result.output
+ else:
+ assert 'theme = "dark"\n' in result.output
+ assert 'on_cell_change = "lazy"\n' in result.output
+ assert "#" not in result.output
+ assert 'api_key = "********"' in result.output
+ assert "private-key" not in result.output
+ assert user_path.read_text() == contents
+
+
+def test_config_show_relative_notebook_finds_parent_project(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ import tomlkit
+
+ user_path = tmp_path / "marimo.toml"
+ user_path.write_text("")
+ monkeypatch.setattr(
+ UserConfigStore, "get_config_path", lambda _self: str(user_path)
+ )
+ project = tmp_path / "pyproject.toml"
+ project.write_text('[tool.marimo.display]\ntheme = "dark"\n')
+ notebooks = tmp_path / "notebooks"
+ notebooks.mkdir()
+ notebook = notebooks / "app.py"
+ notebook.write_text(
+ '# /// script\n# [tool.marimo.runtime]\n# on_cell_change = "lazy"\n# ///\n'
+ )
+ monkeypatch.chdir(notebooks)
+ runner = CliRunner()
+ relative = runner.invoke(config, ["show", "app.py", "--show-sources"])
+ absolute = runner.invoke(config, ["show", str(notebook), "--show-sources"])
+ assert relative.exit_code == absolute.exit_code == 0, relative.output
+ assert relative.output == absolute.output
+ values = tomlkit.parse(relative.output).unwrap()
+ assert values["display"]["theme"] == "dark"
+ assert values["runtime"]["on_cell_change"] == "lazy"
+ assert f'theme = "dark" # {project}' in relative.output
+ assert f'on_cell_change = "lazy" # {notebook}' in relative.output
+
+
+def test_config_show_reports_malformed_project_file(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ user_path = tmp_path / "marimo.toml"
+ user_path.write_text("")
+ project = tmp_path / "pyproject.toml"
+ project.write_text("[tool.marimo.display\n")
+ user = UserConfigStore()
+ monkeypatch.setattr(user, "get_config_path", lambda: str(user_path))
+ from marimo._config.manager import PyprojectConfigReader
+
+ monkeypatch.setattr(
+ commands,
+ "get_default_config_manager",
+ lambda **_: MarimoConfigManager(
+ user, PyprojectConfigReader(str(tmp_path))
+ ),
+ )
+ with _loggers.capture_output() as (_, _, records):
+ result = CliRunner().invoke(commands.config, ["show"])
+ assert result.exit_code == 0, result.output
+ assert len(records) == 1
+ assert str(project) in records[0].getMessage()
+ assert "Settings from this file were skipped" in records[0].getMessage()
+ assert 'theme = "light"\n' in result.output
+ assert "# default" not in result.output
+
+
[email protected]("show_sources", [False, True])
+def test_config_show_parse_error_is_readable_and_printed_once(
+ tmp_path: Path, show_sources: bool
+) -> None:
+ config_path = tmp_path / ".marimo.toml"
+ config_path.write_text('[display]\ntheme = "light\n')
+ args = ["marimo", "config", "show"]
+ if show_sources:
+ args.append("--show-sources")
+ result = subprocess.run(args, cwd=tmp_path, capture_output=True, text=True)
+ assert result.returncode == 0, result.stderr
+ assert result.stderr.count("Error:") == 1
+ error_line, explanation, blank_line = result.stderr.splitlines()
+ assert blank_line == ""
+ assert error_line.startswith(
+ f'Error: Could not load configuration from {config_path}: "'
+ )
+ assert error_line.endswith('"')
+ assert "line 2" in error_line
+ assert "Settings from this file were skipped" in explanation
+ assert "other configuration sources still apply" in result.stderr
+ assert "[E " not in result.stderr
+ assert result.stdout.startswith("[ai]")
+
+
def test_config_describe_field_metadata() -> None:
assert format_config_docs(SaveConfig) == snapshot(
"""\
diff --git a/tests/_config/test_config_schema.py b/tests/_config/test_config_schema.py
index 0657dc2b38..6f20c24100 100644
--- a/tests/_config/test_config_schema.py
+++ b/tests/_config/test_config_schema.py
@@ -62,15 +62,15 @@ def test_schema_defaults_preserve_dictionary_api() -> None:
}
-def test_runtime_defaults_read_environment(
+def test_runtime_defaults_ignore_environment(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("MARIMO_OUTPUT_MAX_BYTES", "1234")
monkeypatch.setenv("MARIMO_STD_STREAM_MAX_BYTES", "5678")
assert config_schema.RuntimeConfig().to_dict() == {
**DEFAULT_CONFIG["runtime"],
- "output_max_bytes": 1234,
- "std_stream_max_bytes": 5678,
+ "output_max_bytes": 8_000_000,
+ "std_stream_max_bytes": 1_000_000,
}
diff --git a/tests/_config/test_config_snapshots.py b/tests/_config/test_config_snapshots.py
index ed15d38349..bc801b65cf 100644
--- a/tests/_config/test_config_snapshots.py
+++ b/tests/_config/test_config_snapshots.py
@@ -258,7 +258,6 @@ def test_layered_configuration_snapshot(
"signing": {"private_key_path": "********"},
"mcp": {"mcpServers": {}, "presets": []},
"runtime": {
- "dotenv": [],
"watcher_on_save": "lazy",
"auto_reload": "off",
"default_csv_encoding": "utf-8",
@@ -442,7 +441,6 @@ def test_layered_configuration_snapshot(
"output_max_bytes": 8000000,
"auto_reload": "off",
"default_csv_encoding": "utf-8",
- "dotenv": [],
"reactive_tests": True,
"auto_instantiate": False,
"default_sql_output": "auto",
@@ -535,7 +533,7 @@ def test_dotenv_fallbacks_snapshot(config_tree: Path) -> None:
"runtime": {"dotenv": ["<root>/home/project/.env"]}
},
"dotenv": ["<root>/home/project/.env"],
- "masked_dotenv": [],
+ "masked_dotenv": None,
"overrides": {},
},
"standalone_notebook": {
@@ -545,7 +543,7 @@ def test_dotenv_fallbacks_snapshot(config_tree: Path) -> None:
}
},
"dotenv": ["<root>/home/project/notebooks/.env"],
- "masked_dotenv": [],
+ "masked_dotenv": None,
"overrides": {"formatting": {"line_length": 100}},
},
"home": {
@@ -560,7 +558,7 @@ def test_dotenv_fallbacks_snapshot(config_tree: Path) -> None:
}
},
"explicit_user": {"dotenv": ["chosen.env"]},
- "masked_empty_user": {"dotenv": ["<root>/home/project/.env"]},
+ "masked_empty_user": {"dotenv": []},
}
)
@@ -817,7 +815,7 @@ def test_trust_origins_snapshot(config_tree: Path) -> None:
},
"workspace": {"signing": None, "cache": {}},
"pyproject": {
- "display": {"theme": "dark", "custom_css": []},
+ "display": {"theme": "dark"},
"cache": {"store": "disk"},
},
}
@@ -861,7 +859,6 @@ def test_secret_roundtrip_snapshot(config_tree: Path) -> None:
"models": {"displayed_models": [], "custom_models": []},
},
"runtime": {
- "dotenv": [],
"reactive_tests": True,
"auto_instantiate": False,
"default_sql_output": "auto",
@@ -898,6 +895,7 @@ def test_secret_roundtrip_snapshot(config_tree: Path) -> None:
"reactive_tests": True,
"show_tracebacks": False,
"output_max_bytes": 8000000,
+ "dotenv": ["secrets.env"],
"auto_reload": "off",
"on_cell_change": "autorun",
},
@@ -1006,7 +1004,7 @@ def test_dotenv_explicit_precedence_snapshot(config_tree: Path) -> None:
{
"user_only": {
"resolved": ["user.env"],
- "masked": [],
+ "masked": None,
"overrides": {},
},
"project_replaces_user": {
@@ -1014,7 +1012,7 @@ def test_dotenv_explicit_precedence_snapshot(config_tree: Path) -> None:
"<root>/home/project/first.env",
"<root>/home/project/../shared.env",
],
- "masked": [],
+ "masked": None,
"overrides": {
"dotenv": [
"<root>/home/project/first.env",
@@ -1029,7 +1027,7 @@ def test_dotenv_explicit_precedence_snapshot(config_tree: Path) -> None:
},
"script_replaces_project": {
"resolved": ["<root>/home/project/script.env"],
- "masked": [],
+ "masked": None,
"overrides": {"dotenv": ["<root>/home/project/script.env"]},
},
"script_empty": {
@@ -1044,12 +1042,12 @@ def test_dotenv_explicit_precedence_snapshot(config_tree: Path) -> None:
},
"memory_replaces_script": {
"resolved": ["memory.env"],
- "masked": [],
+ "masked": None,
"overrides": {"dotenv": ["memory.env"]},
},
"absolute_path": {
"resolved": ["<root>/home/project/absolute.env"],
- "masked": [],
+ "masked": None,
"overrides": {"dotenv": ["<root>/home/project/absolute.env"]},
},
}
@@ -1089,19 +1087,19 @@ def test_dotenv_workspace_session_snapshot(config_tree: Path) -> None:
{
"standalone_session": {
"workspace": ["<root>/home/project/.env"],
- "masked_session": [],
+ "masked_session": None,
"session": ["<root>/home/project/notebooks/.env"],
"overrides": {},
},
"unsaved_session": {
"workspace": ["<root>/home/project/.env"],
- "masked_session": [],
+ "masked_session": None,
"session": ["<root>/home/project/.env"],
"overrides": {},
},
"project_session": {
"workspace": ["<root>/home/project/.env"],
- "masked_session": [],
+ "masked_session": None,
"session": ["<root>/home/project/.env"],
"overrides": {},
},
diff --git a/tests/_config/test_manager.py b/tests/_config/test_manager.py
index 9d7b6236ae..523e2fc64c 100644
--- a/tests/_config/test_manager.py
+++ b/tests/_config/test_manager.py
@@ -214,15 +214,43 @@ def test_save_config_with_none_does_not_raise(tmp_path: Path) -> None:
assert "be terse" in contents
-def test_user_config_drops_hollow_dotenv(tmp_path: Path) -> None:
- """An empty runtime.dotenv on disk is a masked value, so it is ignored."""
+def test_user_config_keeps_empty_dotenv(tmp_path: Path) -> None:
+ """An explicit empty list disables loading in user configuration too."""
config_path = tmp_path / "marimo.toml"
config_path.write_text("[runtime]\ndotenv = []\n")
manager = UserConfigStore()
manager.get_config_path = lambda: str(config_path) # type: ignore[method-assign]
config = MarimoConfigManager(manager).get_user_config(hide_secrets=False)
- assert "dotenv" not in config["runtime"]
+ assert config["runtime"]["dotenv"] == []
+
+
+ ("contents", "expected"),
+ [
+ ("", None),
+ ("[runtime]\ndotenv = []\n", []),
+ ('[runtime]\ndotenv = ["secret.env"]\n', ["secret.env"]),
+ ],
+)
+def test_saving_masked_config_preserves_dotenv_choice(
+ tmp_path: Path,
+ contents: str,
+ expected: list[str] | None,
+) -> None:
+ path = tmp_path / "marimo.toml"
+ path.write_text(contents)
+ store = UserConfigStore()
+ store.get_config_path = lambda: str(path) # type: ignore[method-assign]
+ manager = MarimoConfigManager(store)
+ masked = manager.get_user_config()
+ masked["display"]["theme"] = "dark"
+ manager.save_config(masked)
+ assert (
+ manager.get_user_config(hide_secrets=False)["runtime"].get("dotenv")
+ == expected
+ )
+ assert manager.get_user_config()["display"]["theme"] == "dark"
def test_user_config_keeps_populated_dotenv(tmp_path: Path) -> None:
@@ -412,15 +440,13 @@ def test_default_dotenv_is_masked_with_the_other_secrets(
notebook_path.write_text("import marimo as mo")
manager = get_default_config_manager(current_path=str(notebook_path))
- assert manager.get_config()["runtime"]["dotenv"] == []
+ assert "dotenv" not in manager.get_config()["runtime"]
assert manager.get_config(hide_secrets=False)["runtime"]["dotenv"] == [
str(tmp_path / ".env")
]
def test_project_config_empty_dotenv_opts_out(tmp_path: Path) -> None:
- # A user-level [] is dropped as a masked leftover, but a pyproject.toml is
- # never written back masked, so its [] is a deliberate "load nothing".
notebook_path = _write_dotenv_project(tmp_path, "")
(notebook_path.parent / ".env").write_text("KEY=value")
@@ -658,10 +684,9 @@ def test_default_dotenv_applies_with_an_empty_user_config(
assert config["runtime"]["dotenv"] == [str(notebook_path.parent / ".env")]
-def test_default_dotenv_applies_over_a_hollow_user_dotenv(
+def test_user_config_empty_dotenv_disables_default(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
- # marimo 0.18 and earlier wrote the masked (emptied) dotenv back to disk.
_isolate_user_config(
monkeypatch,
tmp_path,
@@ -674,7 +699,7 @@ def test_default_dotenv_applies_over_a_hollow_user_dotenv(
manager = get_default_config_manager(current_path=str(notebook_path))
config = manager.get_config(hide_secrets=False)
- assert config["runtime"]["dotenv"] == [str(notebook_path.parent / ".env")]
+ assert config["runtime"]["dotenv"] == []
def test_project_dotenv_beats_user_config(
diff --git a/tests/_config/test_provenance.py b/tests/_config/test_provenance.py
new file mode 100644
index 0000000000..ab881abf35
--- /dev/null
+++ b/tests/_config/test_provenance.py
@@ -0,0 +1,572 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+import logging
+import sys
+from pathlib import Path
+from typing import Any, cast
+
+import pytest
+
+from marimo import _loggers
+from marimo._config.config import (
+ DEFAULT_CONFIG,
+ PartialMarimoConfig,
+ merge_default_config,
+)
+from marimo._config.manager import (
+ EnvConfigReader,
+ MarimoConfigManager,
+ OverrideConfigReader,
+ PyprojectConfigReader,
+ ScriptConfigReader,
+ SecurityConfigReader,
+ UserConfigStore,
+)
+from marimo._config.provenance import (
+ ConfigLayer,
+ ConfigSource,
+ format_config_source,
+)
+from marimo._config.resolver import resolve_config_with_sources
+from marimo._config.settings import GLOBAL_SETTINGS
+
+
+def test_leaf_sources_follow_merge_rules() -> None:
+ default: ConfigSource = "default"
+ project = Path("/project/pyproject.toml")
+ resolved = resolve_config_with_sources(
+ [
+ ConfigLayer(
+ cast(
+ PartialMarimoConfig,
+ {
+ "display": {
+ "theme": "light",
+ "custom_css": ["old.css"],
+ },
+ "keymap": {"overrides": {"run": "Ctrl-Enter"}},
+ "ai": {
+ "custom_providers": {
+ "removed": {"api_key": "old"},
+ "gateway.example": {
+ "api_key": "kept",
+ "base_url": "old",
+ },
+ }
+ },
+ "future": {"value": 1},
+ },
+ ),
+ default,
+ ),
+ ConfigLayer(
+ cast(
+ PartialMarimoConfig,
+ {
+ "display": {"theme": "light", "custom_css": []},
+ "keymap": {"overrides": {}},
+ "ai": {
+ "custom_providers": {
+ "gateway.example": {"base_url": "new"}
+ }
+ },
+ "future": None,
+ },
+ ),
+ project,
+ ),
+ ]
+ )
+ assert resolved.config == {
+ "display": {"theme": "light", "custom_css": []},
+ "keymap": {"overrides": {}},
+ "ai": {
+ "custom_providers": {
+ "gateway.example": {"api_key": "kept", "base_url": "new"}
+ }
+ },
+ "future": None,
+ }
+ assert resolved.origins == {
+ ("display", "theme"): project,
+ ("display", "custom_css"): project,
+ ("keymap", "overrides"): project,
+ ("ai", "custom_providers", "gateway.example", "api_key"): default,
+ ("ai", "custom_providers", "gateway.example", "base_url"): project,
+ ("future",): project,
+ }
+
+
+def make_user(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, contents: str = ""
+) -> UserConfigStore:
+ path = tmp_path / "marimo.toml"
+ path.write_text(contents)
+ user = UserConfigStore()
+ monkeypatch.setattr(user, "get_config_path", lambda: str(path))
+ return user
+
+
+def test_environment_output_limits_override_files(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ user = make_user(
+ tmp_path,
+ monkeypatch,
+ "[runtime]\noutput_max_bytes = 777\nstd_stream_max_bytes = 888\n",
+ )
+ (tmp_path / "pyproject.toml").write_text(
+ "[tool.marimo.runtime]\noutput_max_bytes = 999\nstd_stream_max_bytes = 1000\n"
+ )
+ notebook = tmp_path / "app.py"
+ notebook.write_text(
+ "# /// script\n# [tool.marimo.runtime]\n# output_max_bytes = 1111\n"
+ "# std_stream_max_bytes = 2222\n# ///\n"
+ )
+ monkeypatch.setenv("MARIMO_OUTPUT_MAX_BYTES", "1234")
+ monkeypatch.setenv("MARIMO_STD_STREAM_MAX_BYTES", "5678")
+ resolved = MarimoConfigManager(
+ user,
+ PyprojectConfigReader(str(notebook)),
+ ScriptConfigReader(str(notebook)),
+ EnvConfigReader(),
+ ).get_config_with_sources()
+ assert resolved.config["runtime"]["output_max_bytes"] == 1234
+ assert resolved.config["runtime"]["std_stream_max_bytes"] == 5678
+ assert resolved.origins[("runtime", "output_max_bytes")] == "env_var"
+ assert resolved.origins[("runtime", "std_stream_max_bytes")] == "env_var"
+ assert resolved.origins[("package_management", "manager")] == "inferred"
+
+
+ ("field", "variable"),
+ [
+ ("output_max_bytes", "MARIMO_OUTPUT_MAX_BYTES"),
+ ("std_stream_max_bytes", "MARIMO_STD_STREAM_MAX_BYTES"),
+ ],
+)
+def test_invalid_environment_output_limit_keeps_file_value(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ field: str,
+ variable: str,
+) -> None:
+ user = make_user(tmp_path, monkeypatch, f"[runtime]\n{field} = 777\n")
+ monkeypatch.setenv(variable, "invalid")
+ with _loggers.capture_output(log_level=logging.WARNING) as (_, _, records):
+ resolved = MarimoConfigManager(
+ user, EnvConfigReader()
+ ).get_config_with_sources()
+ assert resolved.config["runtime"][field] == 777
+ assert resolved.origins[("runtime", field)] == user.source
+ assert len(records) == 1
+ assert (
+ records[0]
+ .getMessage()
+ .startswith(f"Ignoring invalid value 'invalid' for {variable}:")
+ )
+
+
+def test_environment_validation_error_uses_variable_name(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ user = make_user(tmp_path, monkeypatch)
+ monkeypatch.setenv(
+ "_MARIMO_CONFIG_OVERLOAD_RUNTIME_AUTO_INSTANTIATE", "invalid"
+ )
+ with _loggers.capture_output(log_level=logging.WARNING) as (_, _, records):
+ MarimoConfigManager(user, EnvConfigReader()).get_config()
+ assert [record.getMessage() for record in records] == [
+ "Invalid configuration from _MARIMO_CONFIG_OVERLOAD_RUNTIME_AUTO_INSTANTIATE: Expected `bool`, got `str` - at `$.runtime.auto_instantiate`"
+ ]
+
+
+def test_environment_sources_use_variable_names(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setenv(
+ "_MARIMO_CONFIG_OVERLOAD_RUNTIME_AUTO_INSTANTIATE", "true"
+ )
+ monkeypatch.setenv("MARIMO_RUNTIME_ON_CELL_CHANGE", "lazy")
+ monkeypatch.setenv("MARIMO_SERVER_TRANSPORT", "sse")
+ layer = EnvConfigReader().read_layer()
+ assert layer == ConfigLayer(
+ {
+ "runtime": {"auto_instantiate": True, "on_cell_change": "lazy"},
+ "server": {"transport": "sse"},
+ },
+ "env_var",
+ )
+ resolved = resolve_config_with_sources([layer])
+ assert {
+ path: format_config_source(path, source)
+ for path, source in resolved.origins.items()
+ } == {
+ (
+ "runtime",
+ "auto_instantiate",
+ ): "_MARIMO_CONFIG_OVERLOAD_RUNTIME_AUTO_INSTANTIATE",
+ ("runtime", "on_cell_change"): "MARIMO_RUNTIME_ON_CELL_CHANGE",
+ ("server", "transport"): "MARIMO_SERVER_TRANSPORT",
+ }
+
+
[email protected]("clear_css", [False, True])
+def test_project_display_preserves_user_css_unless_explicitly_cleared(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ clear_css: bool,
+) -> None:
+ user = make_user(
+ tmp_path, monkeypatch, '[display]\ncustom_css = ["user.css"]\n'
+ )
+ project = tmp_path / "pyproject.toml"
+ project.write_text(
+ '[tool.marimo.display]\ntheme = "dark"\n'
+ + ("custom_css = []\n" if clear_css else "")
+ )
+ reader = PyprojectConfigReader(str(tmp_path))
+ resolved = MarimoConfigManager(user, reader).get_config_with_sources()
+ assert resolved.config["display"]["custom_css"] == (
+ [] if clear_css else ["user.css"]
+ )
+ assert resolved.origins[("display", "custom_css")] == (
+ project if clear_css else user.source
+ )
+
+
[email protected]("source", ["user", "project", "script", "override"])
+def test_empty_dotenv_disables_inferred_paths(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ source: str,
+) -> None:
+ user = make_user(
+ tmp_path,
+ monkeypatch,
+ "[runtime]\ndotenv = []\n" if source == "user" else "",
+ )
+ (tmp_path / "pyproject.toml").write_text(
+ "[tool.marimo.runtime]\ndotenv = []\n" if source == "project" else ""
+ )
+ notebook = tmp_path / "app.py"
+ notebook.write_text(
+ "# /// script\n# [tool.marimo.runtime]\n# dotenv = []\n# ///\n"
+ if source == "script"
+ else ""
+ )
+ manager = MarimoConfigManager(
+ user,
+ PyprojectConfigReader(str(notebook)),
+ ScriptConfigReader(str(notebook)),
+ OverrideConfigReader(
+ {"runtime": {"dotenv": []}} if source == "override" else {}
+ ),
+ )
+ resolved = manager.get_config_with_sources(hide_secrets=False)
+ assert resolved.config["runtime"]["dotenv"] == []
+ sources = {
+ "user": user.source,
+ "project": tmp_path / "pyproject.toml",
+ "script": notebook,
+ "override": "override",
+ }
+ assert resolved.origins[("runtime", "dotenv")] == sources[source]
+
+
+def test_empty_dotenv_overrides_inherited_paths(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ user = make_user(
+ tmp_path, monkeypatch, '[runtime]\ndotenv = ["custom.env"]\n'
+ )
+ (tmp_path / "pyproject.toml").write_text(
+ "[tool.marimo.runtime]\ndotenv = []\n"
+ )
+ notebook = tmp_path / "app.py"
+ notebook.write_text(
+ "# /// script\n# [tool.marimo.runtime]\n# dotenv = []\n# ///\n"
+ )
+ override = OverrideConfigReader({"runtime": {"dotenv": []}})
+ manager = MarimoConfigManager(
+ user,
+ PyprojectConfigReader(str(notebook)),
+ ScriptConfigReader(str(notebook)),
+ override,
+ )
+ resolved = manager.get_config_with_sources(hide_secrets=False)
+ assert resolved.config["runtime"]["dotenv"] == []
+ assert resolved.origins[("runtime", "dotenv")] == "override"
+ assert (
+ manager.get_config_overrides(hide_secrets=False)["runtime"]["dotenv"]
+ == []
+ )
+ assert override.read() == {"runtime": {"dotenv": []}}
+
+
+def test_manager_tracks_file_environment_default_and_security_sources(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ user = make_user(
+ tmp_path,
+ monkeypatch,
+ '[display]\ntheme = "dark"\n[ai.open_ai]\napi_key = "private-key"\n',
+ )
+ project_path = tmp_path / "pyproject.toml"
+ project_path.write_text('[tool.marimo.display]\ntheme = "light"\n')
+ notebook = tmp_path / "app.py"
+ notebook.write_text(
+ '# /// script\n# [tool.marimo.runtime]\n# on_cell_change = "lazy"\n# ///\n'
+ )
+ monkeypatch.setenv("MARIMO_SERVER_TRANSPORT", "sse")
+ monkeypatch.setattr(GLOBAL_SETTINGS, "RESTRICT_SHARING", True)
+ manager = MarimoConfigManager(
+ user,
+ PyprojectConfigReader(str(tmp_path)),
+ ScriptConfigReader(str(notebook)),
+ EnvConfigReader(),
+ SecurityConfigReader(),
+ )
+ resolved = manager.get_config_with_sources()
+ paths = [
+ ("display", "theme"),
+ ("save", "autosave_delay"),
+ ("runtime", "on_cell_change"),
+ ("runtime", "dotenv"),
+ ("server", "transport"),
+ ("sharing", "html"),
+ ("ai", "open_ai", "api_key"),
+ ]
+ assert {path: resolved.origins[path] for path in paths} == {
+ ("display", "theme"): project_path,
+ ("save", "autosave_delay"): "default",
+ ("runtime", "on_cell_change"): notebook,
+ ("runtime", "dotenv"): "inferred",
+ ("server", "transport"): "env_var",
+ ("sharing", "html"): "env_var",
+ ("ai", "open_ai", "api_key"): tmp_path / "marimo.toml",
+ }
+ assert resolved.config == merge_default_config(
+ {
+ "display": {"theme": "light"},
+ "runtime": {"on_cell_change": "lazy"},
+ "server": {"transport": "sse"},
+ "sharing": {"html": False, "wasm": False, "molab": False},
+ "ai": {"open_ai": {"api_key": "********"}},
+ }
+ )
+ assert (
+ manager.get_config(hide_secrets=False)["ai"]["open_ai"]["api_key"]
+ == "private-key"
+ )
+
+
+def test_manager_preserves_record_merge_grouping(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ user = make_user(
+ tmp_path,
+ monkeypatch,
+ '[ai.custom_providers.gateway]\napi_key = "user-key"\n',
+ )
+ first = OverrideConfigReader(
+ {"ai": {"custom_providers": {"other": {"base_url": "https://other"}}}}
+ )
+ last = OverrideConfigReader(
+ {
+ "ai": {
+ "custom_providers": {
+ "gateway": {"base_url": "https://gateway"}
+ }
+ }
+ }
+ )
+ resolved = MarimoConfigManager(user, first, last).get_config_with_sources(
+ hide_secrets=False
+ )
+ assert resolved.config["ai"]["custom_providers"] == {
+ "gateway": {"api_key": "user-key", "base_url": "https://gateway"}
+ }
+ assert (
+ resolved.origins[("ai", "custom_providers", "gateway", "api_key")]
+ == user.source
+ )
+ assert (
+ resolved.origins[("ai", "custom_providers", "gateway", "base_url")]
+ == last.source
+ )
+ assert not any("other" in path for path in resolved.origins)
+
+
[email protected]("kind", ["user", "project", "script"])
+def test_parse_errors_identify_the_file(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ kind: str,
+) -> None:
+ user = make_user(tmp_path, monkeypatch)
+ readers: list[Any] = []
+ if kind == "user":
+ path = tmp_path / "marimo.toml"
+ contents = "[display\n"
+ elif kind == "project":
+ path = tmp_path / "pyproject.toml"
+ contents = "[tool.marimo.display\n"
+ path.write_text(contents)
+ readers.append(PyprojectConfigReader(str(tmp_path)))
+ else:
+ path = tmp_path / "app.py"
+ contents = "# /// script\n# [tool.marimo.display\n# ///\n"
+ readers.append(ScriptConfigReader(str(path)))
+ path.write_text(contents)
+ with _loggers.capture_output(log_level=logging.WARNING) as (_, _, records):
+ resolved = MarimoConfigManager(
+ user, *readers
+ ).get_config_with_sources()
+ messages = [record.getMessage() for record in records]
+ assert len(messages) == 1
+ assert str(path) in messages[0]
+ assert (
+ "Expected ']'"
+ if sys.version_info >= (3, 11)
+ else "Unexpected character: '\\n'"
+ ) in messages[0]
+ assert "Settings from this file were skipped" in messages[0]
+ assert (
+ resolved.config["display"]["theme"]
+ == DEFAULT_CONFIG["display"]["theme"]
+ )
+ assert resolved.origins[("display", "theme")] == "default"
+
+
+def test_validation_error_identifies_source_and_field(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ user = make_user(
+ tmp_path, monkeypatch, '[save]\nautosave_delay = "fast"\n'
+ )
+ project = tmp_path / "pyproject.toml"
+ project.write_text('[tool.marimo.display]\ntheme = "dark"\n')
+ with _loggers.capture_output(log_level=logging.WARNING) as (_, _, records):
+ resolved = MarimoConfigManager(
+ user, PyprojectConfigReader(str(tmp_path))
+ ).get_config_with_sources()
+ assert [record.getMessage() for record in records] == [
+ f"Invalid configuration from {tmp_path / 'marimo.toml'}: Expected `int`, got `str` - at `$.save.autosave_delay`"
+ ]
+ assert resolved.origins[("save", "autosave_delay")] == user.source
+
+
+ ("overlay", "expected"),
+ [
+ ('args = ["--verbose"]', {"args": ["--verbose"]}),
+ ("disabled = true", {"disabled": True}),
+ ],
+)
+def test_sparse_mcp_overlay_inherits_transport_without_validation_errors(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ overlay: str,
+ expected: dict[str, Any],
+) -> None:
+ user = make_user(
+ tmp_path, monkeypatch, '[mcp.mcpServers.example]\ncommand = "uvx"\n'
+ )
+ project = tmp_path / "pyproject.toml"
+ project.write_text(f"[tool.marimo.mcp.mcpServers.example]\n{overlay}\n")
+ reader = PyprojectConfigReader(str(tmp_path))
+ manager = MarimoConfigManager(user, reader)
+ with _loggers.capture_output(log_level=logging.WARNING) as (_, _, records):
+ overrides = manager.get_config_overrides()
+ resolved = manager.get_config_with_sources()
+ assert manager.get_config() == resolved.config
+ assert records == []
+ assert overrides["mcp"]["mcpServers"] == {"example": expected}
+ assert resolved.config["mcp"]["mcpServers"] == {
+ "example": {"command": "uvx", **expected}
+ }
+ assert {
+ path: source
+ for path, source in resolved.origins.items()
+ if path[:3] == ("mcp", "mcpServers", "example")
+ } == {
+ ("mcp", "mcpServers", "example", "command"): user.source,
+ (
+ "mcp",
+ "mcpServers",
+ "example",
+ overlay.split(" = ", maxsplit=1)[0],
+ ): reader.source,
+ }
+
+
+ ("overlay", "error"),
+ [
+ (
+ "args = 123",
+ "Expected `array | null`, got `int` - at `$.mcp.mcpServers[...].args`",
+ ),
+ (
+ "args = [123]",
+ "Expected `str`, got `int` - at `$.mcp.mcpServers[...].args[0]`",
+ ),
+ (
+ 'disabled = "no"',
+ "Expected `bool | null`, got `str` - at `$.mcp.mcpServers[...].disabled`",
+ ),
+ ],
+)
+def test_invalid_mcp_overlay_reports_project_source(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ overlay: str,
+ error: str,
+) -> None:
+ user = make_user(
+ tmp_path, monkeypatch, '[mcp.mcpServers.example]\ncommand = "uvx"\n'
+ )
+ project = tmp_path / "pyproject.toml"
+ project.write_text(f"[tool.marimo.mcp.mcpServers.example]\n{overlay}\n")
+ with _loggers.capture_output(log_level=logging.WARNING) as (_, _, records):
+ MarimoConfigManager(
+ user, PyprojectConfigReader(str(tmp_path))
+ ).get_config_with_sources()
+ assert [record.getMessage() for record in records] == [
+ f"Invalid configuration from {project}: {error}"
+ ]
+
+
+def test_overridden_invalid_value_does_not_report_validation_error(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ user = make_user(
+ tmp_path, monkeypatch, '[save]\nautosave_delay = "fast"\n'
+ )
+ with _loggers.capture_output(log_level=logging.WARNING) as (_, _, records):
+ resolved = MarimoConfigManager(
+ user, OverrideConfigReader({"save": {"autosave_delay": 1000}})
+ ).get_config_with_sources()
+ assert records == []
+ assert resolved.config["save"]["autosave_delay"] == 1000
+
+
+def test_legacy_reload_values_keep_their_origin(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ user = make_user(tmp_path, monkeypatch, "[runtime]\nauto_reload = true\n")
+ with _loggers.capture_output(log_level=logging.WARNING) as (_, _, records):
+ resolved = MarimoConfigManager(user).get_config_with_sources()
+ assert records == []
+ assert resolved.config["runtime"]["auto_reload"] == "lazy"
+ assert resolved.origins[("runtime", "auto_reload")] == user.source
diff --git a/tests/_config/test_secrets_config.py b/tests/_config/test_secrets_config.py
index 386cf58fe2..4dd5da3eb3 100644
--- a/tests/_config/test_secrets_config.py
+++ b/tests/_config/test_secrets_config.py
@@ -39,6 +39,8 @@ def test_mask_secrets() -> None:
)
new_config = redact_secrets(config)
+ assert "dotenv" not in new_config["runtime"]
+ assert config["runtime"]["dotenv"] == [".env"]
assert new_config["ai"]["open_ai"]["api_key"] == SECRET_PLACEHOLDER
assert new_config["ai"]["anthropic"]["api_key"] == SECRET_PLACEHOLDER
assert new_config["ai"]["google"]["api_key"] == SECRET_PLACEHOLDER
diff --git a/tests/_runtime/test_dotenv.py b/tests/_runtime/test_dotenv.py
index 530713622f..0f3a2e8aa7 100644
--- a/tests/_runtime/test_dotenv.py
+++ b/tests/_runtime/test_dotenv.py
@@ -2,10 +2,13 @@ from __future__ import annotations
import os
import tempfile
+from typing import TYPE_CHECKING
import pytest
+from marimo import _loggers
from marimo._config.config import merge_default_config
+from marimo._config.manager import UserConfigStore, get_default_config_manager
from marimo._runtime.commands import (
CreateNotebookCommand,
UpdateUIElementCommand,
@@ -13,6 +16,97 @@ from marimo._runtime.commands import (
from marimo._utils.platform import is_windows
from tests.conftest import MockedKernel
+if TYPE_CHECKING:
+ from pathlib import Path
+
+
+ ("source", "expected"),
+ [
+ ("inferred", "inferred"),
+ ("user", "custom"),
+ ("project", "custom"),
+ ("script", "custom"),
+ ("multiple", "custom"),
+ ("empty_user", None),
+ ("empty_project", None),
+ ("empty_script", None),
+ ],
+)
+def test_load_resolved_dotenv_paths(
+ mocked_kernel: MockedKernel,
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ source: str,
+ expected: str | None,
+) -> None:
+ for key in (
+ "DOTENV_READER_VALUE",
+ "DOTENV_READER_DEFAULT_ONLY",
+ "DOTENV_READER_SECONDARY",
+ ):
+ monkeypatch.setenv(key, "before")
+ monkeypatch.delenv(key)
+ monkeypatch.setenv("DOTENV_READER_EXISTING", "shell")
+ (tmp_path / ".env").write_text(
+ "DOTENV_READER_VALUE=inferred\nDOTENV_READER_DEFAULT_ONLY=present\n"
+ )
+ custom = tmp_path / "custom.env"
+ custom.write_text(
+ "DOTENV_READER_VALUE=custom\nDOTENV_READER_EXISTING=file\n"
+ )
+ (tmp_path / "shared.env").write_text(
+ "DOTENV_READER_VALUE=second\nDOTENV_READER_SECONDARY=second\n"
+ )
+ user = tmp_path / "marimo.toml"
+ user.write_text(
+ f'[runtime]\ndotenv = ["{custom.as_posix()}"]\n'
+ if source == "user"
+ else "[runtime]\ndotenv = []\n"
+ if source == "empty_user"
+ else ""
+ )
+ monkeypatch.setattr(
+ UserConfigStore, "get_config_path", lambda _self: str(user)
+ )
+ project = tmp_path / "pyproject.toml"
+ project_settings = {
+ "project": '[tool.marimo.runtime]\ndotenv = ["custom.env"]\n',
+ "empty_project": "[tool.marimo.runtime]\ndotenv = []\n",
+ "multiple": '[tool.marimo.runtime]\ndotenv = ["custom.env", "shared.env"]\n',
+ }
+ project.write_text(project_settings.get(source, ""))
+ notebooks = tmp_path / "notebooks"
+ notebooks.mkdir()
+ notebook = notebooks / "app.py"
+ notebook.write_text(
+ '# /// script\n# [tool.marimo.runtime]\n# dotenv = ["custom.env"]\n# ///\n'
+ if source == "script"
+ else "# /// script\n# [tool.marimo.runtime]\n# dotenv = []\n# ///\n"
+ if source == "empty_script"
+ else ""
+ )
+ manager = get_default_config_manager(current_path=str(notebook))
+ mocked_kernel.k.user_config = manager.get_config(hide_secrets=False)
+ mocked_kernel.k.load_dotenv()
+ assert {
+ key: os.environ.get(key)
+ for key in (
+ "DOTENV_READER_VALUE",
+ "DOTENV_READER_DEFAULT_ONLY",
+ "DOTENV_READER_EXISTING",
+ "DOTENV_READER_SECONDARY",
+ )
+ } == {
+ "DOTENV_READER_VALUE": expected,
+ "DOTENV_READER_DEFAULT_ONLY": "present"
+ if source == "inferred"
+ else None,
+ "DOTENV_READER_EXISTING": "shell",
+ "DOTENV_READER_SECONDARY": "second" if source == "multiple" else None,
+ }
+
+
# These tests work with and without python-dotenv installed.
@@ -45,20 +139,33 @@ def test_load_dotenv_success(mocked_kernel: MockedKernel):
assert os.environ.get("ANOTHER_VAR") == "another_value"
-def test_load_dotenv_nonexistent_file(mocked_kernel: MockedKernel):
- # Create a config with a nonexistent .env file
+def test_load_dotenv_nonexistent_file(
+ mocked_kernel: MockedKernel,
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ missing = tmp_path / "missing.env"
+ other_missing = tmp_path / "also-missing.env"
+ valid = tmp_path / "valid.env"
+ valid.write_text("DOTENV_AFTER_MISSING=loaded\n")
+ monkeypatch.setenv("DOTENV_AFTER_MISSING", "before")
+ monkeypatch.delenv("DOTENV_AFTER_MISSING")
custom_config = merge_default_config(
{
"runtime": {
- "dotenv": ["nonexistent.env"],
+ "dotenv": [str(missing), str(valid), str(other_missing)],
},
}
)
mocked_kernel.k.user_config = custom_config
- # Create a kernel with our custom config
- # This should not raise an exception
- mocked_kernel.k.load_dotenv()
+ with _loggers.capture_output() as (_, _, records):
+ mocked_kernel.k.load_dotenv()
+ assert [(record.levelname, record.getMessage()) for record in records] == [
+ ("WARNING", f"Dotenv file not found: {missing}"),
+ ("WARNING", f"Dotenv file not found: {other_missing}"),
+ ]
+ assert os.environ["DOTENV_AFTER_MISSING"] == "loaded"
@pytest.mark.skipif(
diff --git a/tests/_secrets/test_load_dotenv.py b/tests/_secrets/test_load_dotenv.py
index 4f8436a639..7073495085 100644
--- a/tests/_secrets/test_load_dotenv.py
+++ b/tests/_secrets/test_load_dotenv.py
@@ -5,6 +5,7 @@ from typing import TYPE_CHECKING
import pytest
+from marimo import _loggers
from marimo._dependencies.dependencies import DependencyManager
from marimo._secrets.load_dotenv import (
_drop_quotes,
@@ -75,6 +76,16 @@ def test_parse_dotenv_nonexistent():
assert env_dict == {}
+def test_read_dotenv_nonexistent_warns(tmp_path: Path) -> None:
+ path = tmp_path / "missing.env"
+ with _loggers.capture_output() as (_, _, records):
+ values = read_dotenv_with_fallback(str(path))
+ assert values == {}
+ assert [(record.levelname, record.getMessage()) for record in records] == [
+ ("WARNING", f"Dotenv file not found: {path}")
+ ]
+
+
def test_load_to_environ():
env_dict = {"TEST_KEY": "test_value"}
load_to_environ(env_dict)
diff --git a/tests/_server/test_sessions.py b/tests/_server/test_sessions.py
index a8e6450f61..75ae99729c 100644
--- a/tests/_server/test_sessions.py
+++ b/tests/_server/test_sessions.py
@@ -1419,10 +1419,8 @@ async def test_sandbox_auto_picks_backend_from_manifest(
async def test_session_script_dotenv_reaches_the_kernel_config(
tmp_path: Path,
) -> None:
- # runtime.dotenv is masked to [] when read with secrets hidden. If the
- # session layered that masked snapshot on as an override, the empty list
- # would win the unmasked merge the kernel reads, so a script-level dotenv
- # would load nothing.
+ # The kernel needs the configured dotenv paths even though they are
+ # omitted from the configuration exposed to the frontend.
session_consumer = MagicMock()
session_consumer.connection_state.return_value = ConnectionState.OPEN
tmp_file = tmp_path / "nb.py"
@@ -1458,7 +1456,7 @@ async def test_session_script_dotenv_reaches_the_kernel_config(
str(tmp_path / ".env.local"),
]
masked = session.config_manager.get_config()
- assert masked["runtime"]["dotenv"] == []
+ assert "dotenv" not in masked["runtime"]
finally:
session.close()
diff --git a/tests/test_loggers.py b/tests/test_loggers.py
index 1429542263..81234553cb 100644
--- a/tests/test_loggers.py
+++ b/tests/test_loggers.py
@@ -18,6 +18,35 @@ from marimo._loggers import (
marimo_logger,
set_level,
)
+from marimo._utils.log_formatter import LogFormatter
+
+
+def test_error_format_without_log_prefix():
+ record = logging.LogRecord(
+ "marimo",
+ logging.ERROR,
+ __file__,
+ 1,
+ "Invalid config: %s\nDetails",
+ ("test",),
+ None,
+ )
+ record.omit_log_prefix = True
+ formatter = LogFormatter(
+ color=False,
+ without_log_prefix_fmt=LogFormatter.WITHOUT_LOG_PREFIX_FORMAT,
+ )
+ assert formatter.format(record) == (
+ "Error: Invalid config: test\nDetails\n"
+ )
+ formatter._colors = {logging.ERROR: "\033[31m"}
+ formatter._normal = "\033[0m"
+ assert formatter.format(record) == (
+ "\033[31mError: Invalid config: test\nDetails\033[0m\n"
+ )
+ assert "[E " in LogFormatter(color=False).format(record)
+ record.omit_log_prefix = False
+ assert "[E " in formatter.format(record)
def test_set_level():