Best practice for resolving relative paths to generic files
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from google/jsonnet
-
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
-
Difficulty 3/5 1-2 days Newbie friendliness 78/100
-
std.format: wrong digits for %f/%d on large values; malformed output for non-integer * precision Open
Difficulty 4/5 3-5 days Newbie friendliness 52/100
-
Difficulty 4/5 3-5 days Newbie friendliness 38/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
danielmiessler/LifeOS#2218 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
hemilabs/ui-monorepo#2332 ·
-
Help-Wanted Needs-Triage Package-Update
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
microsoft/winget-pkgs#438662 ·
-
priority: p3
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
googleapis/librarian#7636 ·