Skip to content

Commit 50279da

Browse files
simplify Python interpreter discovery and consolidate config (#272)
1 parent ade1c6b commit 50279da

File tree

12 files changed

+416
-797
lines changed

12 files changed

+416
-797
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/djls-conf/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub struct Settings {
3636
#[serde(default)]
3737
debug: bool,
3838
venv_path: Option<String>,
39+
django_settings_module: Option<String>,
3940
#[serde(default)]
4041
tagspecs: Vec<TagSpecDef>,
4142
}
@@ -101,6 +102,11 @@ impl Settings {
101102
self.venv_path.as_deref()
102103
}
103104

105+
#[must_use]
106+
pub fn django_settings_module(&self) -> Option<&str> {
107+
self.django_settings_module.as_deref()
108+
}
109+
104110
#[must_use]
105111
pub fn tagspecs(&self) -> &[TagSpecDef] {
106112
&self.tagspecs
@@ -128,6 +134,7 @@ mod tests {
128134
Settings {
129135
debug: false,
130136
venv_path: None,
137+
django_settings_module: None,
131138
tagspecs: vec![],
132139
}
133140
);

crates/djls-project/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ salsa = { workspace = true }
1414
serde = { workspace = true }
1515
serde_json = { workspace = true }
1616
tempfile = { workspace = true }
17+
tracing = { workspace = true }
1718
which = { workspace = true}
1819

1920
[build-dependencies]

crates/djls-project/src/django.rs

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use serde::Serialize;
66
use crate::db::Db as ProjectDb;
77
use crate::inspector;
88
use crate::inspector::InspectorRequest;
9-
use crate::python::python_environment;
109
use crate::Project;
1110

1211
#[derive(Serialize)]
@@ -20,70 +19,15 @@ impl InspectorRequest for DjangoInitRequest {
2019
type Response = DjangoInitResponse;
2120
}
2221

23-
/// Initialize Django for the current project.
22+
/// Check if Django is available for the current project.
2423
///
2524
/// This tracked function attempts to initialize Django via the inspector.
2625
/// Returns true if Django was successfully initialized, false otherwise.
2726
#[salsa::tracked]
28-
pub fn django_initialized(db: &dyn ProjectDb, _project: Project) -> bool {
27+
pub fn django_available(db: &dyn ProjectDb, _project: Project) -> bool {
2928
inspector::query(db, &DjangoInitRequest).is_some()
3029
}
3130

32-
/// Check if Django is available for the current project.
33-
///
34-
/// This determines if Django is installed and configured in the Python environment.
35-
/// First attempts to initialize Django, then falls back to environment detection.
36-
#[salsa::tracked]
37-
pub fn django_available(db: &dyn ProjectDb, project: Project) -> bool {
38-
// Try to initialize Django
39-
if django_initialized(db, project) {
40-
return true;
41-
}
42-
43-
// Fallback to environment detection
44-
python_environment(db, project).is_some()
45-
}
46-
47-
/// Get the Django settings module name for the current project.
48-
///
49-
/// Returns `DJANGO_SETTINGS_MODULE` env var, or attempts to detect it
50-
/// via common patterns.
51-
#[salsa::tracked]
52-
pub fn django_settings_module(db: &dyn ProjectDb, project: Project) -> Option<String> {
53-
// Note: The django_init query doesn't return the settings module,
54-
// it just initializes Django. So we detect it ourselves.
55-
56-
// Check environment override first
57-
if let Ok(env_value) = std::env::var("DJANGO_SETTINGS_MODULE") {
58-
if !env_value.is_empty() {
59-
return Some(env_value);
60-
}
61-
}
62-
63-
let project_path = project.root(db);
64-
65-
// Try to detect settings module
66-
if project_path.join("manage.py").exists() {
67-
// Look for common settings modules
68-
for candidate in &["settings", "config.settings", "project.settings"] {
69-
let parts: Vec<&str> = candidate.split('.').collect();
70-
let mut path = project_path.clone();
71-
for part in &parts[..parts.len() - 1] {
72-
path = path.join(part);
73-
}
74-
if let Some(last) = parts.last() {
75-
path = path.join(format!("{last}.py"));
76-
}
77-
78-
if path.exists() {
79-
return Some((*candidate).to_string());
80-
}
81-
}
82-
}
83-
84-
None
85-
}
86-
8731
#[derive(Serialize)]
8832
struct TemplatetagsRequest;
8933

@@ -103,6 +47,8 @@ impl InspectorRequest for TemplatetagsRequest {
10347
#[salsa::tracked]
10448
pub fn templatetags(db: &dyn ProjectDb, _project: Project) -> Option<TemplateTags> {
10549
let response = inspector::query(db, &TemplatetagsRequest)?;
50+
let tag_count = response.templatetags.len();
51+
tracing::debug!("Retrieved {} templatetags from inspector", tag_count);
10652
Some(TemplateTags(response.templatetags))
10753
}
10854

0 commit comments

Comments
 (0)