"generate_parser_in_directory" generates spurious "grammar.js" and "package.json" files when "grammar_path" is specified but not equal to "<repo_path>/grammar.js".
Imisnew2 opened this issue · comments
Problem
Spurious files are created by generate::generate_parser_in_directory
under the following conditions:
grammar_path
isSome
, but not equal to"{repo_path}/grammar.js"
.repo_path
is a directory
It looks like this was introduced around 2 months ago by commit 9e5bf65. The fix seems to be: also check whether grammar_path
was specified before generating the grammar.js
and package.js
.
Also note, right below the added code, there's two paths for calling load_grammar_file
:
- If
grammar_path
is specified, it uses that file. - If
grammar_path
is not specified, it uses"{repo_path}/grammar.js"
.
This seems to suggest that the grammar path should be used if present, otherwise fall-back to "{repo_path}/grammar.js"
. So, one fix would be to first compute the grammar path to use at the top of the function, then checking it later. Something like:
// ...
let should_generate_grammar_json = grammar_path.is_none();
let grammar_path = match grammar_path {
Some(path) => PathBuf::from(path),
None => repo_path.join("grammar.js"),
}
if repo_path.is_dir() && !grammar_path.exists() && !path_in_ignore(&repo_path) {
// ...
}
let grammar_json = load_grammar_file(&grammar_path, js_runtime)?;
// ...
Steps to reproduce
Create the following directory structure
- top/
- input/
- grammar.js
- output/
- input/
Then invoke generate_parser_in_directory
:
// CWD: top
tree_sitter_cli::generate::generate_parser_in_directory(
Path::new("output"),
Some("input/grammar.js"),
14,
false,
None,
None,
);
Expected behavior
If grammar_path
is specified and exists, skeleton grammar.js
and package.json
are not generated under repo_path
.
Tree-sitter version (tree-sitter --version)
tree-sitter-cli 0.22.2
Operating system/version
All Versions
Do you have repro steps using the Tree-sitter CLI, or is this only a problem if you use tree_sitter_cli
as a library and call this function manually?
Do you have repro steps using the Tree-sitter CLI, or is this only a problem if you use
tree_sitter_cli
as a library and call this function manually?
Yes. Like before, create the following directory structure:
- top/
- input/
- grammar.js
- output/
- input/
Then change the CWD to any directory other than input
(e.g. cd top/output
).
Then run tree-sitter generate path/to/grammar.js
.
grammar.js
and package.json
will be generated in the current directory, alongside other artifacts.
Great thanks for the explanation.