Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternating cargo build / trunk build leads to full build #156

Closed
iulianR opened this issue Mar 28, 2021 · 10 comments
Closed

Alternating cargo build / trunk build leads to full build #156

iulianR opened this issue Mar 28, 2021 · 10 comments
Labels
discussion This item needs some discussion disposition:close The tagged item can probably be closed

Comments

@iulianR
Copy link

iulianR commented Mar 28, 2021

Hello! Should a full build be done every time if I alternate between cargo build and trunk build? I can understand if they are incremental builds because I am basically building for different targets, but I don't see why these are always started from scratch. You can see the problem here, where I try to compile the seed example in this repo:
https://asciinema.org/a/wzaeItOsJBZZSlFw8xFHbYjVK

This is causing me a problem in a project where I have a workspace with two members, a server (that I compile with cargo -p server) and a Seed-based app (where I use trunk build). It basically makes incremental compilation impossible if I modify both the server and the Seed app.

I can provide more info, thanks!

@iulianR iulianR changed the title Alternating cargo build / trunk build leads to recompilation Alternating cargo build / trunk build leads to full build Mar 28, 2021
@thedodd
Copy link
Member

thedodd commented Mar 29, 2021

Hello @iulianR, the issue is probably simply that trunk forces the compilation target wasm32-unknown-unknown, whereas invoking cargo directly will default to using your system OS as the target. So, if you plan on building the project outside of trunk, then you should use the cargo config system to make things consistent, as described here: https://doc.rust-lang.org/cargo/reference/config.html

In short, create a dir next to your Cargo.toml for the wasm project called .cargo and place one file therein config.toml. The contents of that file should be:

[build]
target = "wasm32-unknown-unknown"

@thedodd thedodd added discussion This item needs some discussion disposition:close The tagged item can probably be closed labels Mar 29, 2021
@iulianR
Copy link
Author

iulianR commented Apr 1, 2021

Hmm, but this means that the server will be built for wasm32-unknown-unknown instead of being built for my system OS. I want to:

  • build the server member of my workspace for x86_64-unknown-linux-gnu
  • build the client member of my workspace for wasm32-unknown-unknown

I can maybe prepare an example, to make it more clear.

@thedodd
Copy link
Member

thedodd commented Apr 1, 2021

@iulianR I understand, which is why I said create a dir next to your Cargo.toml for the wasm project called. So in this case that would be your client project. Doing so will make cargo default to building that project (and only that project) for wasm without impacting your server project.

@iulianR
Copy link
Author

iulianR commented Apr 2, 2021

My apologies, I missed that detail. I went on and made the change, but the same thing is happening as far as I can see:
https://asciinema.org/a/u64DnuWgg7gVLT9eN9KvseTCE

I also don't have anything else running that would affect the build (such as rust-analyzer running cargo check).

@thedodd
Copy link
Member

thedodd commented Apr 2, 2021

So, in reality, regardless of whether the targets are different, the only thing that would actually cause a full build to be triggered again is if the cached build artifacts in target were being removed. On its own, alternating between build targets would impact things as their artifacts are stored in different locations on disk.

Is anything doing a cargo clean per chance?

@thedodd
Copy link
Member

thedodd commented Apr 6, 2021

Per my last comment here, it would seem that there was something else at play. Any updates on your end @iulianR?

@iulianR
Copy link
Author

iulianR commented Apr 6, 2021

There is no cargo clean taking place. I was wondering if it's caused by the fact that I have a common crate, which is a dependency of both ui and server, but I didn't get the chance to test it yet. I'll try to do it today or tomorrow. Sorry for the slow answers!

@iulianR
Copy link
Author

iulianR commented Apr 7, 2021

I think I might have found the culprit. It is caused by the fact that I am using using lld as a linker for x86_64-unknown-linux-gnu, which is done with the following lines in my ~/.cargo/config (https://stackoverflow.com/questions/57812916/how-do-i-change-the-default-rustc-cargo-linker):

[target.x86_64-unknown-linux-gnu]
rustflags = [
    "-C", "link-arg=-fuse-ld=lld",
]

Regardless, I prepared this example:
https://github.com/iulianR/trunk-test

And I was alternating between these commands (rebuild every time):

cd ../client && trunk build
cd ../server && cargo build

It just happens that I noticed that no rebuilds are triggered if I have trunk watch running in a terminal tab. That led me to think that nothing actually changes with the project/cached artifacts. The problem no longer happens if I comment those lines, letting rustc use its default linker. I can probably also fix this if I set custom flags for each crate separately.

I guess it is not really a trunk problem, but it's probably good to know about. Although maybe trunk watch should take rustflags changes into account? As far as I know (and see here), those will lead to rebuilds.

Thanks for your help! Feel free to close/rename/move this issue.

@thedodd
Copy link
Member

thedodd commented Apr 7, 2021

Glad you were able to resolve the issue.

@thedodd thedodd closed this as completed Apr 7, 2021
@jplatte
Copy link

jplatte commented Sep 24, 2022

Just ran into the same issue (because I have set mold as my linker via ~/.cargo/config.toml). I think the underlying issue is rust-lang/cargo#8716, I have worked around it with

[build]
target = "wasm32-unknown-unknown"

in the project's config.toml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion This item needs some discussion disposition:close The tagged item can probably be closed
Projects
None yet
Development

No branches or pull requests

3 participants