Skip to content

Commit eeeec35

Browse files
committed
Downgrade to SemiStrict in production
1 parent ea7f568 commit eeeec35

File tree

6 files changed

+46
-7
lines changed

6 files changed

+46
-7
lines changed

crates/cli/src/commands/server.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,14 @@ impl Options {
160160
)?;
161161

162162
// Load and compile the templates
163-
let templates =
164-
templates_from_config(&config.templates, &site_config, &url_builder).await?;
163+
let templates = templates_from_config(
164+
&config.templates,
165+
&site_config,
166+
&url_builder,
167+
// Don't use strict mode in production yet
168+
false,
169+
)
170+
.await?;
165171
shutdown.register_reloadable(&templates);
166172

167173
let http_client = mas_http::reqwest_client();

crates/cli/src/commands/templates.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,14 @@ impl Options {
6565
&account_config,
6666
&captcha_config,
6767
)?;
68-
let templates =
69-
templates_from_config(&template_config, &site_config, &url_builder).await?;
68+
let templates = templates_from_config(
69+
&template_config,
70+
&site_config,
71+
&url_builder,
72+
// Use strict mode in template checks
73+
true,
74+
)
75+
.await?;
7076
templates.check_render(clock.now(), &mut rng)?;
7177

7278
Ok(ExitCode::SUCCESS)

crates/cli/src/commands/worker.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ impl Options {
5252
)?;
5353

5454
// Load and compile the templates
55-
let templates =
56-
templates_from_config(&config.templates, &site_config, &url_builder).await?;
55+
let templates = templates_from_config(
56+
&config.templates,
57+
&site_config,
58+
&url_builder,
59+
// Don't use strict mode on task workers for now
60+
false,
61+
)
62+
.await?;
5763

5864
let mailer = mailer_from_config(&config.email, &templates)?;
5965
test_mailer_in_background(&mailer, Duration::from_secs(30));

crates/cli/src/util.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ pub async fn templates_from_config(
232232
config: &TemplatesConfig,
233233
site_config: &SiteConfig,
234234
url_builder: &UrlBuilder,
235+
strict: bool,
235236
) -> Result<Templates, anyhow::Error> {
236237
Templates::load(
237238
config.path.clone(),
@@ -240,6 +241,7 @@ pub async fn templates_from_config(
240241
config.translations_path.clone(),
241242
site_config.templates_branding(),
242243
site_config.templates_features(),
244+
strict,
243245
)
244246
.await
245247
.with_context(|| format!("Failed to load the templates at {}", config.path))

crates/handlers/src/test_utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ impl TestState {
176176
workspace_root.join("translations"),
177177
site_config.templates_branding(),
178178
site_config.templates_features(),
179+
// Strict mode in testing
180+
true,
179181
)
180182
.await?;
181183

crates/templates/src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ pub struct Templates {
7171
vite_manifest_path: Utf8PathBuf,
7272
translations_path: Utf8PathBuf,
7373
path: Utf8PathBuf,
74+
/// Whether template rendering is in strict mode (for testing,
75+
/// until this can be rolled out in production.)
76+
strict: bool,
7477
}
7578

7679
/// There was an issue while loading the templates
@@ -151,6 +154,7 @@ impl Templates {
151154
translations_path: Utf8PathBuf,
152155
branding: SiteBranding,
153156
features: SiteFeatures,
157+
strict: bool,
154158
) -> Result<Self, TemplateLoadingError> {
155159
let (translator, environment) = Self::load_(
156160
&path,
@@ -159,6 +163,7 @@ impl Templates {
159163
&translations_path,
160164
branding.clone(),
161165
features,
166+
strict,
162167
)
163168
.await?;
164169
Ok(Self {
@@ -170,6 +175,7 @@ impl Templates {
170175
translations_path,
171176
branding,
172177
features,
178+
strict,
173179
})
174180
}
175181

@@ -180,6 +186,7 @@ impl Templates {
180186
translations_path: &Utf8Path,
181187
branding: SiteBranding,
182188
features: SiteFeatures,
189+
strict: bool,
183190
) -> Result<(Arc<Translator>, Arc<minijinja::Environment<'static>>), TemplateLoadingError> {
184191
let path = path.to_owned();
185192
let span = tracing::Span::current();
@@ -206,7 +213,14 @@ impl Templates {
206213
let mut loaded: HashSet<_> = HashSet::new();
207214
let mut env = minijinja::Environment::new();
208215
// Don't allow use of undefined variables
209-
env.set_undefined_behavior(UndefinedBehavior::Strict);
216+
env.set_undefined_behavior(if strict {
217+
UndefinedBehavior::Strict
218+
} else {
219+
// For now, allow semi-strict, because we don't have total test coverage of
220+
// tests and some tests rely on if conditions against sometimes-undefined
221+
// variables
222+
UndefinedBehavior::SemiStrict
223+
});
210224
let root = path.canonicalize_utf8()?;
211225
info!(%root, "Loading templates from filesystem");
212226
for entry in walkdir::WalkDir::new(&root)
@@ -277,6 +291,7 @@ impl Templates {
277291
&self.translations_path,
278292
self.branding.clone(),
279293
self.features,
294+
self.strict,
280295
)
281296
.await?;
282297

@@ -526,6 +541,8 @@ mod tests {
526541
translations_path,
527542
branding,
528543
features,
544+
// Use strict mode in tests
545+
true,
529546
)
530547
.await
531548
.unwrap();

0 commit comments

Comments
 (0)