Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"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". #3300

Open
Imisnew2 opened this issue Apr 15, 2024 · 3 comments

Comments

@Imisnew2
Copy link

Problem

Spurious files are created by generate::generate_parser_in_directory under the following conditions:

  • grammar_path is Some, 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/

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

@Imisnew2 Imisnew2 added the bug label Apr 15, 2024
@maxbrunsfeld
Copy link
Contributor

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?

@Imisnew2
Copy link
Author

Imisnew2 commented Apr 15, 2024

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/

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.

@maxbrunsfeld
Copy link
Contributor

Great thanks for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants