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
27 changes: 17 additions & 10 deletions sqlmesh/core/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def _load(path: Path) -> t.List[Model]:
for row in YAML().load(file.read())
]
except Exception as ex:
raise ConfigError(self._failed_to_load_model_error(path, ex))
raise ConfigError(self._failed_to_load_model_error(path, ex), path)

for path in paths_to_load:
self._track_file(path)
Expand All @@ -363,7 +363,8 @@ def _load(path: Path) -> t.List[Model]:
raise ConfigError(
self._failed_to_load_model_error(
path, f"Duplicate external model name: '{model.name}'."
)
),
path,
)
models[model.fqn] = model

Expand All @@ -375,7 +376,8 @@ def _load(path: Path) -> t.List[Model]:
raise ConfigError(
self._failed_to_load_model_error(
path, f"Duplicate external model name: '{model.name}'."
)
),
path,
)
models.update({model.fqn: model})

Expand All @@ -402,13 +404,15 @@ def _load_requirements(self) -> t.Tuple[t.Dict[str, str], t.Set[str]]:
args = [k.strip() for k in line.split("==")]
if len(args) != 2:
raise ConfigError(
f"Invalid lock file entry '{line.strip()}'. Only 'dep==ver' is supported"
f"Invalid lock file entry '{line.strip()}'. Only 'dep==ver' is supported",
requirements_path,
)
dep, ver = args
other_ver = requirements.get(dep, ver)
if ver != other_ver:
raise ConfigError(
f"Conflicting requirement {dep}: {ver} != {other_ver}. Fix your {c.REQUIREMENTS} file."
f"Conflicting requirement {dep}: {ver} != {other_ver}. Fix your {c.REQUIREMENTS} file.",
requirements_path,
)
requirements[dep] = ver

Expand Down Expand Up @@ -619,13 +623,14 @@ def _load_sql_models(
raise ConfigError(
self._failed_to_load_model_error(
path, f"Duplicate SQL model name: '{model.name}'."
)
),
path,
)
elif model.enabled:
model._path = path
models[model.fqn] = model
except Exception as ex:
raise ConfigError(self._failed_to_load_model_error(path, ex))
raise ConfigError(self._failed_to_load_model_error(path, ex), path)

return models

Expand Down Expand Up @@ -678,7 +683,7 @@ def _load_python_models(
if model.enabled:
models[model.fqn] = model
except Exception as ex:
raise ConfigError(self._failed_to_load_model_error(path, ex))
raise ConfigError(self._failed_to_load_model_error(path, ex), path)

finally:
model_registry._dialect = None
Expand Down Expand Up @@ -782,7 +787,9 @@ def _load_metrics(self) -> UniqueKeyDict[str, MetricMeta]:
metric = load_metric_ddl(expression, path=path, dialect=dialect)
metrics[metric.name] = metric
except SqlglotError as ex:
raise ConfigError(f"Failed to parse metric definitions at '{path}': {ex}.")
raise ConfigError(
f"Failed to parse metric definitions at '{path}': {ex}.", path
)

return metrics

Expand Down Expand Up @@ -1005,7 +1012,7 @@ def _load_scripts(self) -> t.Tuple[MacroRegistry, JinjaMacroRegistry]:
package=package,
)
except Exception as e:
raise ConfigError(f"Failed to load macro file: {path}", e)
raise ConfigError(f"Failed to load macro file: {e}", path)

self._macros_max_mtime = macros_max_mtime

Expand Down
9 changes: 6 additions & 3 deletions sqlmesh/core/model/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def make_python_env(
macros: MacroRegistry,
variables: t.Optional[t.Dict[str, t.Any]] = None,
used_variables: t.Optional[t.Set[str]] = None,
path: t.Optional[str | Path] = None,
path: t.Optional[Path] = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

why did you change this here? it seems a bit dangerous

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It makes downstream much simpler, and nothing seems to be passing in a string?

python_env: t.Optional[t.Dict[str, Executable]] = None,
strict_resolution: bool = True,
blueprint_variables: t.Optional[t.Dict[str, t.Any]] = None,
Expand Down Expand Up @@ -265,12 +265,14 @@ def validate_extra_and_required_fields(
klass: t.Type[PydanticModel],
provided_fields: t.Set[str],
entity_name: str,
path: t.Optional[Path] = None,
) -> None:
missing_required_fields = klass.missing_required_fields(provided_fields)
if missing_required_fields:
field_names = "'" + "', '".join(missing_required_fields) + "'"
raise_config_error(
f"Please add required field{'s' if len(missing_required_fields) > 1 else ''} {field_names} to the {entity_name}."
f"Please add required field{'s' if len(missing_required_fields) > 1 else ''} {field_names} to the {entity_name}.",
path,
)

extra_fields = klass.extra_fields(provided_fields)
Expand All @@ -293,7 +295,8 @@ def validate_extra_and_required_fields(
similar_msg = "\n\n " + "\n ".join(similar) if similar else ""

raise_config_error(
f"Invalid field name{'s' if len(extra_fields) > 1 else ''} present in the {entity_name}: {extra_field_names}{similar_msg}"
f"Invalid field name{'s' if len(extra_fields) > 1 else ''} present in the {entity_name}: {extra_field_names}{similar_msg}",
path,
)


Expand Down
5 changes: 4 additions & 1 deletion sqlmesh/core/model/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -2434,7 +2434,10 @@ def _create_model(
**kwargs: t.Any,
) -> Model:
validate_extra_and_required_fields(
klass, {"name", *kwargs} - {"grain", "table_properties"}, "MODEL block"
klass,
{"name", *kwargs} - {"grain", "table_properties"},
"MODEL block",
path,
)

for prop in PROPERTIES:
Expand Down
12 changes: 0 additions & 12 deletions sqlmesh/lsp/context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from dataclasses import dataclass
from pathlib import Path
import uuid
from sqlmesh.core.context import Context
import typing as t

Expand Down Expand Up @@ -35,14 +34,8 @@ class LSPContext:
map: t.Dict[Path, t.Union[ModelTarget, AuditTarget]]
_render_cache: t.Dict[Path, t.List[RenderModelEntry]]
_lint_cache: t.Dict[Path, t.List[AnnotatedRuleViolation]]
_version_id: str
"""
This is a version ID for the context. It is used to track changes to the context. It can be used to
return a version number to the LSP client.
"""

def __init__(self, context: Context) -> None:
self._version_id = str(uuid.uuid4())
self.context = context
self._render_cache = {}
self._lint_cache = {}
Expand Down Expand Up @@ -70,11 +63,6 @@ def __init__(self, context: Context) -> None:
**audit_map,
}

@property
def version_id(self) -> str:
"""Get the version ID for the context."""
return self._version_id

def render_model(self, uri: URI) -> t.List[RenderModelEntry]:
"""Get rendered models for a file, using cache when available.

Expand Down
51 changes: 51 additions & 0 deletions sqlmesh/lsp/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from lsprotocol.types import Diagnostic, DiagnosticSeverity, Range, Position

from sqlmesh.lsp.uri import URI
from sqlmesh.utils.errors import (
ConfigError,
)
import typing as t

ContextFailedError = t.Union[str, ConfigError, Exception]


def context_error_to_diagnostic(
error: t.Union[Exception, ContextFailedError],
uri_filter: t.Optional[URI] = None,
) -> t.Tuple[t.Optional[t.Tuple[str, Diagnostic]], ContextFailedError]:
"""
Convert an error to a diagnostic message.
If the error is a ConfigError, it will be converted to a diagnostic message.

uri_filter is used to filter diagnostics by URI. If present, only diagnostics
with a matching URI will be returned.
"""
if isinstance(error, ConfigError):
return config_error_to_diagnostic(error), error
return None, str(error)


def config_error_to_diagnostic(
error: ConfigError,
uri_filter: t.Optional[URI] = None,
) -> t.Optional[t.Tuple[str, Diagnostic]]:
if error.location is None:
return None
uri = URI.from_path(error.location).value
if uri_filter and uri != uri_filter.value:
return None
return uri, Diagnostic(
range=Range(
start=Position(
line=0,
character=0,
),
end=Position(
line=0,
character=0,
),
),
message=str(error),
severity=DiagnosticSeverity.Error,
source="SQLMesh",
)
Loading