Sass @debug, @warn & @error
When you write functions and mixins, you need ways to see values and report problems. Sass has three at-rules that print messages while it compiles:
| Rule | Prints | Stops compilation? | Use it for |
|---|---|---|---|
@debug |
A value, with file and line | No | Inspecting values while developing |
@warn |
A warning (with a stack trace in the CLI) | No | Deprecated arguments, suspicious input |
@error |
An error message | Yes | Invalid input that can't produce correct CSS |
None of them add anything to the CSS. In this site's playground, messages appear after the compiled CSS.
@debug
@use "sass:math";
@use "sass:meta";
$columns: 12;
$gutter: 24px;
$col: math.div(100%, $columns);
@debug "one column = #{$col}";
@debug meta.type-of($gutter);
@debug math.unit($gutter);
.grid {
gap: $gutter;
}.grid {
gap: 24px;
}
Debug (line 8): one column = 8.3333333333%
Debug (line 9): number
Debug (line 10): px
The @debug lines don't appear in the CSS. meta.type-of() and meta.inspect() are useful companions when you're not sure what kind of value you have.
Note: Try changing
$coltomath.div(100% - $gutter, $columns). Percentages and pixels can't be combined at compile time, so Sass stops with an incompatible units error — use CSScalc()for mixed units.
@warn
@warn reports a problem without stopping the build — ideal for deprecating an argument while keeping old code working:
@mixin button($size: md, $rounded: null) {
@if $rounded != null {
@warn "$rounded is deprecated; use the .btn--pill modifier instead.";
}
padding: size-padding($size);
}
@function size-padding($size) {
@if $size == sm { @return 4px 8px; }
@return 8px 16px;
}
.legacy-btn {
@include button(sm, $rounded: true);
}.legacy-btn {
padding: 4px 8px;
}
Warning: $rounded is deprecated; use the .btn--pill modifier instead.
The CSS was still generated; the warning appears next to it.
@error
When input is simply wrong, stop with a clear message rather than producing broken CSS:
@use "sass:map";
$sizes: (sm: 0.875rem, md: 1rem, lg: 1.25rem);
@function font-size($key) {
@if not map.has-key($sizes, $key) {
@error "Unknown size `#{$key}`. Use one of: #{map.keys($sizes)}.";
}
@return map.get($sizes, $key);
}
.title {
font-size: font-size(xl);
}Error: "Unknown size `xl`. Use one of: sm, md, lg."
on line 13, column 14
No CSS was produced at all. The message names the problem and lists the valid choices — the best error messages tell the user how to fix it.
Validating types
meta.type-of() returns number, string, color, list, map, bool, null, function or mixin. Combine it with @error:
@use "sass:meta";
@use "sass:math";
@function rem($px) {
@if meta.type-of($px) != number {
@error "rem() expects a number, got #{meta.inspect($px)}.";
}
@if math.unit($px) != "px" {
@error "rem() expects pixels, got #{$px}.";
}
@return math.div($px, 16px) * 1rem;
}Built-in deprecation warnings
Sass itself uses warnings to tell you about outdated code — for example / division, @import, or global functions like darken(). Don't ignore them: each one will become an error in a future major version. You can silence specific ones temporarily with --silence-deprecation=<name>, but fixing them is better.
Tip: Remove
@debuglines before committing. Leave@warnand@errorin — they're part of your library's interface.
Test yourself
Which rule stops compilation?
- @debug
- @warn
- @error
- None of them
Do @debug messages appear in the compiled CSS?
- Yes, as comments
- No, they're printed by the compiler only
- Only in expanded mode
- Only in compressed mode
What's the best use of @warn?
- Printing every variable
- Flagging deprecated or suspicious usage without breaking the build
- Stopping invalid input
- Adding comments to the CSS