jsonnet icon indicating copy to clipboard operation
jsonnet copied to clipboard

A tool to extract the dependencies (imports)

Open FranklinYu opened this issue 5 years ago • 4 comments

This is a feature request.

I use GNU Make and the jsonnet utility to generate a JSON from my .jsonnet files. It would generate a file suitable to consumed by GNU Make, similar to gcc -MD. Note that it should do so only when output file is specified with --output-file/-o (otherwise it's complicated to decide the make rule target).

For example, if the example mentioned on website is run with -o output.json, it would generate a file output.d with content

output.json: imports.jsonnet martinis.libsonnet garnish.txt

martinis.libsonnet:

garnish.txt:

FranklinYu avatar Feb 15 '20 00:02 FranklinYu

Let me rephrase:

You have a bunch of Jsonnet files, which are used to generate a bunch of JSON output files. Jsonnet files of course import other files. If one of the Jsonnet files changes, you want to regenerate the affected output files to keep everything in sync. You use make to run Jsonnet, so you would like to have the dependencies recorded in a file that make can use.

Is that correct?

I think it would be better to have a separate tool which generates the dependency graph through static analysis. A static analysis tool which generates the dependency graph could be implemented quite easily with go-jsonnet I think. It should require is <50 lines of the "real code" (not counting the command line interface and such). It would be enough to walk the AST, enter any encountered imports and report the paths.

Doing so at runtime also makes sense, but I think the static approach is probably a better fit for make.

sbarzowski avatar Feb 15 '20 08:02 sbarzowski

It should require is <50 lines of the "real code" (not counting the command line interface and such). It would be enough to walk the AST, enter any encountered imports and report the paths.

Yes, and the AST should be walked recursively. So it needs to import the AST of the imported .libjsonnet, which might also need the AST of another lib, etc.

I think it would be better to have a separate tool which generates the dependency graph through static analysis.

This works for me since I don’t have a very large Jsonnet structure. I’m not sure whether we should worry about performance (one-pass v.s. two-pass for lexer and parser).

FranklinYu avatar Feb 15 '20 10:02 FranklinYu

I've implemented a version of this tool that generates a sha256sum file for caching purposes:

  • https://gitlab.com/gitlab-com/gl-infra/jsonnet-tool/-/merge_requests/57

If there is any interest, I could put some work into upstreaming it.

@igorwwwwwwwwwwwwwwwwwwww

FWIW I'd be happy to include this upstream. Also, there already exists a tool jsonnet-deps which just prints out the deps. Maybe it could have an option to print checksums?

sbarzowski avatar Apr 05 '23 21:04 sbarzowski