Skip to content

Allow custom base path for asset resolution #1049

Open

Description

@deadbaed

Here's the file structure of a project:

$ pwd
~/my/project
$ ls
src/ Cargo.toml style.css default.nix
$ cat style.css
@import "tailwindcss";

The index.html file is generated with nix, and is located outside the project at /nix/store/9naz5isizpi8xyj55ba328pk3nxrfiaz-index.html. Currently it looks like this:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- Tailwind CSS -->
    <link data-trunk data-integrity="sha512" rel="tailwind-css" href="style.css">

    <!-- Leaflet -->
    <link data-trunk data-integrity="sha512" data-no-minify data-target-path="leaflet" rel="css" href="/nix/store/sbal4dnkz83iqaqb31iwifvjk7yq29bv-leaflet-1.9.4/lib/node_modules/leaflet/dist/leaflet.css" />
    <link data-trunk data-target-path="leaflet/images" rel="copy-dir" href="/nix/store/sbal4dnkz83iqaqb31iwifvjk7yq29bv-leaflet-1.9.4/lib/node_modules/leaflet/dist/images/" />
    <script data-trunk data-integrity="sha512" data-no-minify data-target-path="leaflet" src="/nix/store/sbal4dnkz83iqaqb31iwifvjk7yq29bv-leaflet-1.9.4/lib/node_modules/leaflet/dist/leaflet.js"></script>
  </head>
  <body>
  </body>
</html>

When I try to build the project I get this error:

$ pwd
~/my/project
$ trunk build /nix/store/9naz5isizpi8xyj55ba328pk3nxrfiaz-index.html
2026-02-24T22:52:28.983576Z  INFO 🚀 Starting trunk 0.21.14
2026-02-24T22:52:29.225321Z DEBUG spawning asset pipelines
2026-02-24T22:52:29.228025Z ERROR ❌ error
error from build pipeline

Caused by:
    0: error getting canonical path for "/nix/store/style.css"
    1: No such file or directory (os error 2)
2026-02-24T22:52:29.228089Z ERROR error from build pipeline
2026-02-24T22:52:29.228095Z  INFO   1: error getting canonical path for "/nix/store/style.css"
2026-02-24T22:52:29.228100Z  INFO   2: No such file or directory (os error 2)

When digging a bit in the code it confirms that relative paths are relative to the location of the index.html file provided for the build.

My build fails because my index.css file is in another folder. A workaround is to wrap the build in a script to copy the html file prior to the build, and gitignore it:

cp /nix/store/9naz5isizpi8xyj55ba328pk3nxrfiaz-index.html index.html
trunk build index.html

That works, but it pollutes the directory in my opinion, I would like the project directory to only contain source files.

I was thinking of introducing a configuration flag to override the pre_target in

let target_parent = target
to have it work something like this:

$ pwd
~/my/project
trunk build /nix/store/9naz5isizpi8xyj55ba328pk3nxrfiaz-index.html --pre_target $PWD

It could also be in the TOML file and as an env variable, of course.

Activity

  1. ctron commented on Feb 25, 2026

    @ctron
    Collaborator

    When digging a bit in the code it confirms that relative paths are relative to the location of the index.html file provided for the build.

    Yea, that is the current behavior. I guess it would be hard to change this. At least without breaking a lot.

    Just looking at the code: you can provide build.target, wouldn't that be close? If you provide a directory? Then maybe adding a --index <your-index.html> which would deviate from <build.target>/index.html?

  2. deadbaed commented on Feb 27, 2026

    @deadbaed
    ContributorAuthor

    I guess it would be hard to change this. At least without breaking a lot.

    I have a working patch which adds a new flag to use the working directory for assets resolution. It turns out the patch was not that hard, because the working directory is already known and is available to be used.

    I'll make a PR to get the discussion going.

    Just looking at the code: you can provide build.target, wouldn't that be close? If you provide a directory? Then maybe adding a --index <your-index.html> which would deviate from <build.target>/index.html?

    I have not thought of that, thank you for the suggestion! I'll experiment .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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