A jsonnet package manager.
Find a file
Matthew Huxtable fe0eca6e7a Correct path resolution to nested local dependencies
The tool failed to properly resolve nested local dependencies to jsonnet
bundles in different directory trees. This arises because the
installation command resolves and installs nested jsonnet local
dependencies relative to the root jsonnetfile, rather than track and
evaluate the installation path relative to the nested library's
jsonnetfile.

Consider a repository with multiple local jsonnet bundles in various
directory trees, organised as follows (lockfiles elided for brevity):

  /top/of/tree
   |- lib/module_A
     |- jsonnetfile.json
   |- lib/module_B
     |- jsonnetfile.json
   |- src/root_module
     |- jsonnetfile.json

The modules depend on each other as follows:

  ┌───────────────────┐   ┌───────────────────┐   ┌───────────────────┐
  │                   │   │                   │   │                   │
  │  src/root_module  │──>│   lib/module_A    │──>│   lib/module_B    │
  │                   │   │                   │   │                   │
  └───────────────────┘   └───────────────────┘   └───────────────────┘

where X ──> Y indicates bundle X depends on bundle Y, expressed by
adding a dependency of type local in bundle X's jsonnetfile.json, whose
path is the relative path from bundle X to bundle Y in the directory
structure. For example, src/root_module will express a local dependency
on path ../lib/module_A to depend on library module A.

Invoking jb install in src/root_module will result in an error:

  jb: error: failed to install packages: downloading: symlink destination path does not exist: %w:
    stat /top/of/tree/src/module_B: no such file or directory

This occurs because jsonnet-bundler improperly attempts to resolve the
nested dependency on library module B relative to the root module path,
i.e. src/root_module.  The correct behaviour is to perform such
resolution relative to the depending module's jsonnetfile.json, i.e.
relative to lib/module_A.
2021-12-28 19:38:39 +00:00
cmd/jb Add --quiet option to suppress git progress output 2020-07-07 04:19:56 +00:00
pkg Correct path resolution to nested local dependencies 2021-12-28 19:38:39 +00:00
scripts fix: generate-help-script 2020-03-02 16:43:11 +01:00
spec fix: allow dots in a repository path's "user" section (#106) 2020-05-19 22:55:33 +02:00
tool/rewrite feat(spec): version field (#85) 2020-02-28 17:41:49 +01:00
vendor chore(vendor): github.com/stretchr/testify/require 2019-10-29 22:10:16 +01:00
.drone.jsonnet feat(spec): version field (#85) 2020-02-28 17:41:49 +01:00
.drone.yml feat(spec): version field (#85) 2020-02-28 17:41:49 +01:00
.gitignore Initial commit 2018-04-24 16:01:37 +01:00
.header Initial commit 2018-04-24 16:01:37 +01:00
CHANGELOG.md doc(release): CHANGELOG for v0.4.0 (#107) 2020-05-15 12:47:07 +02:00
Dockerfile Bump busybox to 1.33.0 in Dockerfile 2021-02-08 17:01:01 -05:00
go.mod chore(vendor): embedmd 2019-10-29 22:18:22 +01:00
go.sum chore(vendor): embedmd 2019-10-29 22:18:22 +01:00
LICENSE Initial commit 2018-04-24 16:01:37 +01:00
Makefile fix: windows enhancements (#110) 2020-05-20 15:51:58 +02:00
README.md update readme so install method works for Go 1.17+ too 2021-11-19 09:53:31 +01:00
VERSION doc(release): CHANGELOG for v0.4.0 (#107) 2020-05-15 12:47:07 +02:00

jsonnet-bundler

NOTE: This project is alpha stage. Flags, configuration, behavior and design may change significantly in following releases.

The jsonnet-bundler is a package manager for Jsonnet.

Install

go install -a github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb@latest

NOTE: please use a recent Go version to do this, ideally Go 1.13 or greater.

This will put jb in $(go env GOPATH)/bin. If you encounter the error jb: command not found after installation then you may need to add that directory to your $PATH as shown in their docs.

Package Install

  • Arch Linux AUR
  • Mac OS X via Homebrew: brew install jsonnet-bundler
  • Fedora (>= 32): sudo dnf install golang-github-jsonnet-bundler

Features

  • Fetches transitive dependencies
  • Can vendor subtrees, as opposed to whole repositories

Current Limitations

  • Always downloads entire dependent repositories, even when updating
  • If two dependencies depend on the same package (diamond problem), they must require the same version

Example Usage

Initialize your project:

mkdir myproject
cd myproject
jb init

The existence of the jsonnetfile.json file means your directory is now a jsonnet-bundler package that can define dependencies.

To depend on another package (another Github repository): Note that your dependency need not be initialized with a jsonnetfile.json. If it is not, it is assumed it has no transitive dependencies.

jb install https://github.com/anguslees/kustomize-libsonnet

Now write myconfig.jsonnet, which can import a file from that package. Remember to use -J vendor when running Jsonnet to include the vendor tree.

local kustomize = import 'kustomize-libsonnet/kustomize.libsonnet';

local my_resource = {
  metadata: {
    name: 'my-resource',
  },
};

kustomize.namePrefix('staging-')(my_resource)

To depend on a package that is in a subtree of a Github repo (this package also happens to bring in a transitive dependency):

jb install https://github.com/prometheus-operator/prometheus-operator/jsonnet/prometheus-operator

Note that if you are copy pasting from the Github website's address bar, remove the tree/master from the path.

If pushed to Github, your project can now be referenced from other packages in the same way, with its dependencies fetched automatically.

All command line flags

$ jb -h
usage: jb [<flags>] <command> [<args> ...]

A jsonnet package manager

Flags:
  -h, --help     Show context-sensitive help (also try --help-long and
                 --help-man).
      --version  Show application version.
      --jsonnetpkg-home="vendor"  
                 The directory used to cache packages in.
  -q, --quiet    Suppress any output from git command.

Commands:
  help [<command>...]
    Show help.

  init
    Initialize a new empty jsonnetfile

  install [<flags>] [<uris>...]
    Install new dependencies. Existing ones are silently skipped

  update [<uris>...]
    Update all or specific dependencies.

  rewrite
    Automatically rewrite legacy imports to absolute ones


Design

This is an implemention of the design specified in this document: https://docs.google.com/document/d/1czRScSvvOiAJaIjwf3CogOULgQxhY9MkiBKOQI1yR14/edit#heading=h.upn4d5pcxy4c