Best practice for resolving relative paths to generic files
- Dominant language
- Jsonnet
- Stars
- 7.6k
- Forks
- 475
- PR merge metrics
- No merged PRs in 30d
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
Contributor guide
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.
Assessment
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100