Skip to content

Best practice for resolving relative paths to generic files #1330

Description

@ceccocats

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions