Repository navigation
fix: python imports #438
fix: python imports #438
Changes from 1 commit
7621ac2
b326053
8a98dce
ad4d517
5ba472a
ba1d3cd
a297647
6bcb5a3
c7f9fd9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # ------ path: foo/bar/module.py -----------# | ||
| # ------ global: ROOT_PATH=foo/bar -----------# | ||
|
|
||
| foo = 42 | ||
|
|
||
| # ------ path: foo/bar/baz/module.py -----------# | ||
| # ------ global: ROOT_PATH=foo/bar -----------# | ||
|
|
||
| bar = "hello" | ||
|
|
||
| # ------ path: foo/bar/main.py -------------# | ||
| # ------ global: ROOT_PATH=foo/bar -----------# | ||
|
|
||
| from module import foo | ||
| from baz.module import bar | ||
|
|
||
| print(foo) | ||
| # ^ defined: 4, 14 | ||
|
|
||
| print(bar) | ||
| # ^ defined: 9, 15 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -428,10 +428,19 @@ impl<'a> Indexer<'a> { | |
| cancellation_flag: &dyn CancellationFlag, | ||
| ) -> std::result::Result<(), BuildErrorWithSource<'b>> { | ||
| let relative_source_path = source_path.strip_prefix(source_root).unwrap(); | ||
| // here the file should also have stripped the source_root from its path | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we want to use the relative path for the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep 100% agreed, this is an old comment I forgot to remove |
||
| if let Some(lc) = lcs.primary { | ||
| let globals = Variables::new(); | ||
| lc.sgl | ||
| .build_stack_graph_into(graph, file, source, &globals, cancellation_flag) | ||
| .build_stack_graph_into( | ||
| graph, | ||
| file, | ||
| source, | ||
| source_path, | ||
| source_root, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of pushing source path and root path all the way down, I think it's better to set |
||
| &globals, | ||
| cancellation_flag, | ||
| ) | ||
| .map_err(|inner| BuildErrorWithSource { | ||
| inner, | ||
| source_path: source_path.to_path_buf(), | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -438,6 +438,7 @@ static PRECEDENCE_ATTR: &'static str = "precedence"; | |||||||||||||||||||||||||||||||||||
| static ROOT_NODE_VAR: &'static str = "ROOT_NODE"; | ||||||||||||||||||||||||||||||||||||
| static JUMP_TO_SCOPE_NODE_VAR: &'static str = "JUMP_TO_SCOPE_NODE"; | ||||||||||||||||||||||||||||||||||||
| static FILE_PATH_VAR: &'static str = "FILE_PATH"; | ||||||||||||||||||||||||||||||||||||
| static ROOT_PATH_VAR: &'static str = "ROOT_PATH"; | ||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made those consts |
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /// Holds information about how to construct stack graphs for a particular language. | ||||||||||||||||||||||||||||||||||||
| pub struct StackGraphLanguage { | ||||||||||||||||||||||||||||||||||||
|
|
@@ -557,10 +558,12 @@ impl StackGraphLanguage { | |||||||||||||||||||||||||||||||||||
| stack_graph: &'a mut StackGraph, | ||||||||||||||||||||||||||||||||||||
| file: Handle<File>, | ||||||||||||||||||||||||||||||||||||
| source: &'a str, | ||||||||||||||||||||||||||||||||||||
| source_path: &Path, | ||||||||||||||||||||||||||||||||||||
| source_root: &Path, | ||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These parameters won't be necessary anymore, and other places where they are propagated. |
||||||||||||||||||||||||||||||||||||
| globals: &'a Variables<'a>, | ||||||||||||||||||||||||||||||||||||
| cancellation_flag: &'a dyn CancellationFlag, | ||||||||||||||||||||||||||||||||||||
| ) -> Result<(), BuildError> { | ||||||||||||||||||||||||||||||||||||
| self.builder_into_stack_graph(stack_graph, file, source) | ||||||||||||||||||||||||||||||||||||
| self.builder_into_stack_graph(stack_graph, file, source, source_path, source_root) | ||||||||||||||||||||||||||||||||||||
| .build(globals, cancellation_flag) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -573,8 +576,10 @@ impl StackGraphLanguage { | |||||||||||||||||||||||||||||||||||
| stack_graph: &'a mut StackGraph, | ||||||||||||||||||||||||||||||||||||
| file: Handle<File>, | ||||||||||||||||||||||||||||||||||||
| source: &'a str, | ||||||||||||||||||||||||||||||||||||
| source_path: &'a Path, | ||||||||||||||||||||||||||||||||||||
| source_root: &'a Path, | ||||||||||||||||||||||||||||||||||||
| ) -> Builder<'a> { | ||||||||||||||||||||||||||||||||||||
| Builder::new(self, stack_graph, file, source) | ||||||||||||||||||||||||||||||||||||
| Builder::new(self, stack_graph, file, source, source_path, source_root) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -583,6 +588,8 @@ pub struct Builder<'a> { | |||||||||||||||||||||||||||||||||||
| stack_graph: &'a mut StackGraph, | ||||||||||||||||||||||||||||||||||||
| file: Handle<File>, | ||||||||||||||||||||||||||||||||||||
| source: &'a str, | ||||||||||||||||||||||||||||||||||||
| source_path: &'a Path, | ||||||||||||||||||||||||||||||||||||
| source_root: &'a Path, | ||||||||||||||||||||||||||||||||||||
| graph: Graph<'a>, | ||||||||||||||||||||||||||||||||||||
| remapped_nodes: HashMap<usize, NodeID>, | ||||||||||||||||||||||||||||||||||||
| injected_node_count: usize, | ||||||||||||||||||||||||||||||||||||
|
|
@@ -595,13 +602,17 @@ impl<'a> Builder<'a> { | |||||||||||||||||||||||||||||||||||
| stack_graph: &'a mut StackGraph, | ||||||||||||||||||||||||||||||||||||
| file: Handle<File>, | ||||||||||||||||||||||||||||||||||||
| source: &'a str, | ||||||||||||||||||||||||||||||||||||
| source_path: &'a Path, | ||||||||||||||||||||||||||||||||||||
| source_root: &'a Path, | ||||||||||||||||||||||||||||||||||||
| ) -> Self { | ||||||||||||||||||||||||||||||||||||
| let span_calculator = SpanCalculator::new(source); | ||||||||||||||||||||||||||||||||||||
| Builder { | ||||||||||||||||||||||||||||||||||||
| sgl, | ||||||||||||||||||||||||||||||||||||
| stack_graph, | ||||||||||||||||||||||||||||||||||||
| file, | ||||||||||||||||||||||||||||||||||||
| source, | ||||||||||||||||||||||||||||||||||||
| source_path, | ||||||||||||||||||||||||||||||||||||
| source_root, | ||||||||||||||||||||||||||||||||||||
| graph: Graph::new(), | ||||||||||||||||||||||||||||||||||||
| remapped_nodes: HashMap::new(), | ||||||||||||||||||||||||||||||||||||
| injected_node_count: 0, | ||||||||||||||||||||||||||||||||||||
|
|
@@ -635,23 +646,33 @@ impl<'a> Builder<'a> { | |||||||||||||||||||||||||||||||||||
| let tree = parse_errors.into_tree(); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| let mut globals = Variables::nested(globals); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if globals.get(&ROOT_NODE_VAR.into()).is_none() { | ||||||||||||||||||||||||||||||||||||
| let root_node = self.inject_node(NodeID::root()); | ||||||||||||||||||||||||||||||||||||
| globals | ||||||||||||||||||||||||||||||||||||
| .add(ROOT_NODE_VAR.into(), root_node.into()) | ||||||||||||||||||||||||||||||||||||
| .expect("Failed to set ROOT_NODE"); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| let jump_to_scope_node = self.inject_node(NodeID::jump_to()); | ||||||||||||||||||||||||||||||||||||
| globals | ||||||||||||||||||||||||||||||||||||
| .add(JUMP_TO_SCOPE_NODE_VAR.into(), jump_to_scope_node.into()) | ||||||||||||||||||||||||||||||||||||
| .expect("Failed to set JUMP_TO_SCOPE_NODE"); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if globals.get(&FILE_PATH_VAR.into()).is_none() { | ||||||||||||||||||||||||||||||||||||
| let file_name = self.stack_graph[self.file].to_string(); | ||||||||||||||||||||||||||||||||||||
| let file_name = self.source_path.to_str().unwrap().to_string(); | ||||||||||||||||||||||||||||||||||||
| globals | ||||||||||||||||||||||||||||||||||||
| .add(FILE_PATH_VAR.into(), file_name.into()) | ||||||||||||||||||||||||||||||||||||
| .expect("Failed to set FILE_PATH"); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if globals.get(&ROOT_PATH_VAR.into()).is_none() { | ||||||||||||||||||||||||||||||||||||
| let root_path = self.source_root.to_str().unwrap().to_string(); | ||||||||||||||||||||||||||||||||||||
| globals | ||||||||||||||||||||||||||||||||||||
| .add(ROOT_PATH_VAR.into(), root_path.into()) | ||||||||||||||||||||||||||||||||||||
| .expect("Failed to set ROOT_PATH"); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole block would be removed here, and move into the indexer. The conditionals won't be needed there anymore. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately the removal of the first of these blocks broke our usage of I agree that passing if globals.get(&FILE_PATH_VAR.into()).is_none() {
let file_name = self.stack_graph[self.file].to_string();
globals
.add(FILE_PATH_VAR.into(), file_name.into())
.expect("Failed to set FILE_PATH");
}If the user of this function has the somewhat special use case and of needing to set both a If, however, the user has the probably far more common use case of simply building a Stack Graph for the file passed to create the I’ve opened PR #466 to restore the first of these conditionals, which I think perfectly supports both use cases. |
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| let mut config = ExecutionConfig::new(&self.sgl.functions, &globals) | ||||||||||||||||||||||||||||||||||||
| .lazy(true) | ||||||||||||||||||||||||||||||||||||
| .debug_attributes( | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,10 +79,14 @@ impl LanguageConfiguration { | |
| Loader::load_globals_from_config_str(builtins_config, &mut builtins_globals)?; | ||
| } | ||
| let file = builtins.add_file("<builtins>").unwrap(); | ||
| let builtins_path_var = Path::new("<builtins>"); | ||
| let builtins_root = Path::new(""); | ||
| sgl.build_stack_graph_into( | ||
| &mut builtins, | ||
| file, | ||
| builtins_source, | ||
| builtins_path_var, | ||
| builtins_root, | ||
| &builtins_globals, | ||
| cancellation_flag, | ||
| ) | ||
|
|
@@ -325,17 +329,28 @@ impl Loader { | |
| graph: &mut StackGraph, | ||
| cancellation_flag: &dyn CancellationFlag, | ||
| ) -> Result<(), LoadError<'a>> { | ||
| let file = graph.add_file(&path.to_string_lossy()).unwrap(); | ||
| let file_name = path.to_string_lossy(); | ||
| let file: stack_graphs::arena::Handle<stack_graphs::graph::File> = | ||
| graph.add_file(&file_name).unwrap(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the type annotation is necessary here, that should be possible to infer.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes my bad, probably miss-clicked on the annotation in my IDE |
||
| let builtins_root = Path::new(""); | ||
| let mut globals = Variables::new(); | ||
| Self::load_globals_from_config_str(&config, &mut globals)?; | ||
|
Comment on lines
336
to
343
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could add to |
||
| sgl.build_stack_graph_into(graph, file, &source, &globals, cancellation_flag) | ||
| .map_err(|err| LoadError::Builtins { | ||
| inner: err, | ||
| source_path: path.to_path_buf(), | ||
| source, | ||
| tsg_path: sgl.tsg_path.to_path_buf(), | ||
| tsg: sgl.tsg_source.clone(), | ||
| })?; | ||
| sgl.build_stack_graph_into( | ||
| graph, | ||
| file, | ||
| &source, | ||
| path, | ||
| builtins_root, | ||
| &globals, | ||
| cancellation_flag, | ||
| ) | ||
| .map_err(|err| LoadError::Builtins { | ||
| inner: err, | ||
| source_path: path.to_path_buf(), | ||
| source, | ||
| tsg_path: sgl.tsg_path.to_path_buf(), | ||
| tsg: sgl.tsg_source.clone(), | ||
| })?; | ||
| return Ok(()); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ | |
| // Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details. | ||
| // ------------------------------------------------------------------------------------------------ | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep this. |
||
| use std::path::Path; | ||
|
|
||
| use stack_graphs::graph::StackGraph; | ||
| use tree_sitter_graph::Variables; | ||
| use tree_sitter_stack_graphs::NoCancellation; | ||
|
|
@@ -22,15 +24,27 @@ fn can_support_preexisting_nodes() { | |
| "#; | ||
| let python = "pass"; | ||
|
|
||
| let file_name = "test.py"; | ||
| let source_path = Path::new(file_name); | ||
| let source_root = Path::new(""); | ||
|
|
||
| let mut graph = StackGraph::new(); | ||
| let file = graph.get_or_create_file("test.py"); | ||
| let file = graph.get_or_create_file(file_name); | ||
| let node_id = graph.new_node_id(file); | ||
| let _preexisting_node = graph.add_scope_node(node_id, true).unwrap(); | ||
|
|
||
| let globals = Variables::new(); | ||
| let language = StackGraphLanguage::from_str(tree_sitter_python::language(), tsg).unwrap(); | ||
| language | ||
| .build_stack_graph_into(&mut graph, file, python, &globals, &NoCancellation) | ||
| .build_stack_graph_into( | ||
| &mut graph, | ||
| file, | ||
| python, | ||
| source_path, | ||
| source_root, | ||
| &globals, | ||
| &NoCancellation, | ||
| ) | ||
| .expect("Failed to build graph"); | ||
| } | ||
|
|
||
|
|
@@ -45,13 +59,18 @@ fn can_support_injected_nodes() { | |
| "#; | ||
| let python = "pass"; | ||
|
|
||
| let file_name = "test.py"; | ||
| let source_path = Path::new(file_name); | ||
| let source_root = Path::new(""); | ||
|
|
||
| let mut graph = StackGraph::new(); | ||
| let file = graph.get_or_create_file("test.py"); | ||
| let file = graph.get_or_create_file(file_name); | ||
| let node_id = graph.new_node_id(file); | ||
| let _preexisting_node = graph.add_scope_node(node_id, true).unwrap(); | ||
|
|
||
| let language = StackGraphLanguage::from_str(tree_sitter_python::language(), tsg).unwrap(); | ||
| let mut builder = language.builder_into_stack_graph(&mut graph, file, python); | ||
| let mut builder = | ||
| language.builder_into_stack_graph(&mut graph, file, python, source_path, source_root); | ||
|
|
||
| let mut globals = Variables::new(); | ||
| globals | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you set a default value here, it makes life a bit easier because you don't have to set
ROOT_PATHunless you need to.