Splitting development files?

I understand why when I use various crates I end up with a lot of debug code on my system. No problem. I have the disk space.
However, I have my rust development work in my documents area. (This is all personal, not professional). For which I have limited backup space. I want that backup to include my own rust source code. 1.5 gig of debug (and it will be larger) from dependencies takes up a lot of that capacity. Is there a way to tell rust to store the dependencies somewhere else?
Thanks,
Joel

I'm unsure what you mean by splitting development files, but maybe the environment variable CARGO_TARGET_DIR is what you are looking for. It will use a common target folder for all builds, and it can be somewhere else.

1 Like

That seems to be what I am looking for. For some variables, the documentation notes how to set in the corgo.toml file. I presume it should be obvious how to do so for this variable, but I am missing it. Would it be
[build]
target_dir = "c:\users\jmh...."
?
Thanks,
Joel

From the docs it seems to use a hyphen instead of an underscore.

[build]
target-dir = "/some/path"

Be aware, that this will set the build dir per project, only. If you want to set it for all of your rust projects, you should set the env variable: CARGO_BUILD_TARGET_DIR or CARGO_TARGET_DIR.

Thanks. Yes, project specific setting is what I am looking for.
Joel

Tying to point it to the right place, and getting trouble.
I tried

[build]
tager-dir = "C:\Users\jmh\RustHolding\target"

and got the error:

error: too few unicode value digits, expected unicode hexadecimal value
  --> Cargo.toml:12:18
   |
12 | tager-dir = "C:\Users\jmh\RustHolding\target"
   |                  ^

I tried replacing the "\" with "/", and got an errors:

warning: unused manifest key: build
help: build is a valid .cargo/config.toml key

It is target-dir, not tager-dir.

You can also set the build dir for all builds on your system via adding

to $HOME/.cargo/config.toml. (The project Cargo.toml has precedence over the config.toml file)

Also, „\\“ for every „\“ could be sufficient

Yeah, fixing the typos should help. But, I still get the unicode errors if I use "\" . If I use "/" or "\\" as the seperator, I get told that [build] is unused.

Interesting…but does it still work?

When it tells me build is unused, it builds, in the default location. It does suggest I might want to put it in .cargo. I am wondering if target-dir only works in .cargo and not cargo.toml?
Yours,
Joel

Yes. I thought someone told you already?

On Linux I have something like (but currently commented out)

$ cat ~/.cargo/config.toml 
#[target.x86_64-unknown-linux-gnu]
#linker = "clang"
#rustflags = ["-C", "link-arg=-fuse-ld=mold", "-C", "target-cpu=native"]

#[build]
#rustflags = ["-C", "target-cpu=native"]


[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = [
    "-C", "link-arg=-fuse-ld=mold", 
    "-C", "target-cpu=native"
]


#[build]
# Use a single absolute path for all build artifacts
#target-dir = "/home/stefan/.cargo/target_shared"

[profile.dev]
# One debuginfo file per dependency, to reduce file size of tests/examples.
# Note that this value is not supported on Windows.
# See https://doc.rust-lang.org/cargo/reference/profiles.html#split-debuginfo
#split-debuginfo="unpacked"

But I still do not understand your initial post, I use cargo clean and and have no space problems until I continue with the project. The state is conserved in Cargo.lock, so you have to backup only your own source files.

I am working on the project. The 1.5 gig of target\debug is a problem for the limited backup facility I use. Since I don't need to backup the stuff that rust can recreate, I want it to store it somewhere that won't be backed up.

I had read the earlier comment as telling me I can move it to .cargo, not that I needed to. And warning me that if I only did it in cargo.toml, it would only apply to the one project. Which was fine with me.

It would be nice to know what syntax I need for the target-dir path in .cargo. Can I leave out the C:? Do I use "/" or "" or?

Not even sure what cargo clean does?
Yours,
Joel

There's some confusion here:

  • project/Cargo.toml: the wrong place for this
  • project/.cargo/config.toml: will set this for one project
  • /.cargo/config.toml: sets it for all projects, but doesn't transfer with the project when you e.g. host it on git
3 Likes

I backup typically only a project after a cargo clean, or use .gitignore for GitHub store.

Note, when you really intend to use target-dir you might read

Outsourcing the `target` directory for every project - #8 by HQ2000

there are a few important notes from smart people.

Thanks for the elaboration. I only knew about /.cargo/....
I will move a project.cargo\config.toml.
Do you know what syntax I need for windows?
Yours,
Joel

"/" did the job. Not sure why, but I don't really care.
Yours,
Joel

Rust automatically translates / to \ in paths on Windows. However, when you're writing it manually, you have to actually type \\ (such as C:\Users\...)

This is because \ is used as an escape character (think \n). \U in particular is used to write an arbitrary Unicode scalar value, in the form \Uxxxx where each x is a hexadecimal digit. That's why you got an error: it's reading \Users and seeing that s isn't a valid hexadecimal digit.

Thanks for explaining both why the final thing worked, and why I got such an odd (but now understandable) error message. I should have realized it was thinking "\u" as the unicode indicator.
Yours,
Joel