Skip to content

Commit 069d2bb

Browse files
committed
Default src.root to ['.', '<project_name>'] if the directory exists
1 parent 0adbb3d commit 069d2bb

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

crates/ty/docs/configuration.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ typeshed = "/path/to/custom/typeshed"
164164

165165
The root of the project, used for finding first-party modules.
166166

167-
**Default value**: `[".", "./src"]`
167+
If left unspecified, ty will default to the current working directory and:
168+
* `./src` if it exists
169+
* `./<project-name>` if the folder `./<project-name>/<project-name>` exists
170+
171+
**Default value**: `null`
168172

169173
**Type**: `str`
170174

crates/ty_project/src/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl ProjectMetadata {
244244
}
245245

246246
pub fn to_program_settings(&self, system: &dyn System) -> ProgramSettings {
247-
self.options.to_program_settings(self.root(), system)
247+
self.options.to_program_settings(self.root(), self.name(), system)
248248
}
249249

250250
/// Combine the project options with the CLI options where the CLI options take precedence.

crates/ty_project/src/metadata/options.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl Options {
8787
pub(crate) fn to_program_settings(
8888
&self,
8989
project_root: &SystemPath,
90+
project_name: &str,
9091
system: &dyn System,
9192
) -> ProgramSettings {
9293
let python_version = self
@@ -106,13 +107,14 @@ impl Options {
106107
ProgramSettings {
107108
python_version,
108109
python_platform,
109-
search_paths: self.to_search_path_settings(project_root, system),
110+
search_paths: self.to_search_path_settings(project_root, project_name, system),
110111
}
111112
}
112113

113114
fn to_search_path_settings(
114115
&self,
115116
project_root: &SystemPath,
117+
project_name: &str,
116118
system: &dyn System,
117119
) -> SearchPathSettings {
118120
let src_roots = if let Some(src_root) = self.src.as_ref().and_then(|src| src.root.as_ref())
@@ -124,6 +126,8 @@ impl Options {
124126
// Default to `src` and the project root if `src` exists and the root hasn't been specified.
125127
if system.is_directory(&src) {
126128
vec![project_root.to_path_buf(), src]
129+
} else if system.is_directory(&project_root.join(project_name).join(project_name)) {
130+
vec![project_root.to_path_buf(), project_root.join(project_name)]
127131
} else {
128132
vec![project_root.to_path_buf()]
129133
}
@@ -353,9 +357,13 @@ pub struct EnvironmentOptions {
353357
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
354358
pub struct SrcOptions {
355359
/// The root of the project, used for finding first-party modules.
360+
///
361+
/// If left unspecified, ty will default to the current working directory and:
362+
/// * `./src` if it exists
363+
/// * `./<project-name>` if the folder `./<project-name>/<project-name>` exists
356364
#[serde(skip_serializing_if = "Option::is_none")]
357365
#[option(
358-
default = r#"[".", "./src"]"#,
366+
default = r#"null"#,
359367
value_type = "str",
360368
example = r#"
361369
root = "./app"

ty.schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@
839839
"type": "object",
840840
"properties": {
841841
"root": {
842-
"description": "The root of the project, used for finding first-party modules.",
842+
"description": "The root of the project, used for finding first-party modules.\n\nIf left unspecified, ty will default to the current working directory and: * `./src` if it exists * `./<project-name>` if the folder `./<project-name>/<project-name>` exists",
843843
"type": [
844844
"string",
845845
"null"

0 commit comments

Comments
 (0)