Allow custom base path for asset resolution
Here's the file structure of a project:
```shell
$ pwd
~/my/project
$ ls
src/ Cargo.toml style.css default.nix
$ cat style.css
@import "tailwindcss";
```
The index.html file is generated with [nix](https://nixos.org), and is located outside the project at `/nix/store/9naz5isizpi8xyj55ba328pk3nxrfiaz-index.html`. Currently it looks like this:
```html
<!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:
```shell
$ 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:
```shell
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 https://github.com/trunk-rs/trunk/blob/cbaadce275064312dabb14b15cc98e0931149382/src/config/rt/build.rs#L145 to have it work something like this:
```shell
$ 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.
1 条评论