Best practice for resolving relative paths to generic files

Open
#1,330 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
42/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Domain
tooling

Research direction

Start by examining the existing import behavior and std.thisFile in the Jsonnet language, then reproduce the workaround while evaluating a file from different working directories. Done means there is a documented or built-in way to resolve generic files relative to the current Jsonnet file without duplicating the helper functions.

Written by the indexing model from the issue text.

Description

I know I can easily import other .jsonnet files using import (which natively resolves relative to the current file). However, I want to resolve the file path of a generic file (e.g., image.png) relative to the current Jsonnet file, rather than relative to the execution directory.

Right now, I am using the following workaround:

local getDirname(path) =
  local parts = std.split(path, '/');
  if std.length(parts) <= 1 then
    '.' // Returns '.' if the file is in the root/current execution directory
  else
    std.join('/', parts[:std.length(parts) - 1]);

local getRelativePath(path) =
    std.join('/', [getDirname(std.thisFile), path]);

{
  fileToLoad: getRelativePath("image.png")
}

Independently of the working directory from which I evaluate the Jsonnet command, this correctly gives me the path of the image.

While this works, my project has many sparse .jsonnet files that need this feature, and duplicating these helper functions in every file feels inefficient.

Is there a built-in or recommended way to do this?

Thanks
Francesco

Dominant language
Jsonnet
Stars
7.6k
Forks
475
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from google/jsonnet

All issues in google/jsonnet

Similar issues

More DevTools issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.